Test Failed
Push — master ( 8c47c2...3acf9f )
by Steve
12:37
created

engine/classes/Elgg/BootData.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Elgg;
4
5
use Elgg\Database;
6
use Elgg\Database\EntityTable;
7
use Elgg\Database\Plugins;
8
use Elgg\Cache\Pool\InMemory;
9
10
/**
11
 * Serializable collection of data used to boot Elgg
12
 *
13
 * @access private
14
 * @since 2.1
15
 */
16
class BootData {
17
18
	/**
19
	 * @var \ElggSite
20
	 */
21
	private $site;
22
23
	/**
24
	 * @var array
25
	 */
26
	private $config_values = [];
27
28
	/**
29
	 * @var \stdClass[]
30
	 */
31
	private $subtype_data = [];
32
33
	/**
34
	 * @var \ElggPlugin[]
35
	 */
36
	private $active_plugins = [];
37
38
	/**
39
	 * @var array
40
	 */
41
	private $plugin_settings = [];
42
43
	/**
44
	 * Populate the boot data
45
	 *
46
	 * @param \stdClass      $config   Elgg CONFIG object
47
	 * @param \Elgg\Database $db       Elgg database
48
	 * @param EntityTable    $entities Entities service
49
	 * @param Plugins        $plugins  Plugins service
50
	 *
51
	 * @return void
52
	 * @throws \InstallationException
53
	 */
54
	public function populate(\stdClass $config, Database $db, EntityTable $entities, Plugins $plugins) {
0 ignored issues
show
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
		// get subtypes
56
		$rows = $db->getData("
57
			SELECT *
58
			FROM {$db->prefix}entity_subtypes
59
		");
60
		foreach ($rows as $row) {
61
			$this->subtype_data[$row->id] = $row;
62
		}
63
64
		// get config
65
		$rows = $db->getData("
66
			SELECT *
67
			FROM {$db->prefix}config
68
		");
69
		foreach ($rows as $row) {
70
			$this->config_values[$row->name] = unserialize($row->value);
71
		}
72
		
73
		if (!array_key_exists('installed', $this->config_values)) {
74
			// try to fetch from old pre 3.0 datalists table
75
			// need to do this to be able to perform an upgrade from 2.x to 3.0
76
			try {
77
				$rows = $db->getData("
78
					SELECT *
79
					FROM {$db->prefix}datalists
80
				");
81
				foreach ($rows as $row) {
82
					$value = $row->value;
83
					if ($row->name == 'processed_upgrades') {
84
						// config table already serializes data so no need to double serialize
85
						$value = unserialize($value);
86
					}
87
					$this->config_values[$row->name] = $value;
88
				}
89
			} catch (\Exception $e) {
90
			}
91
		}
92
93
		// don't pull in old config values
94
		/**
95
		 * @see \Elgg\Config::__construct sets this
96
		 */
97
		unset($this->config_values['path']);
98
		unset($this->config_values['dataroot']);
99
		unset($this->config_values['default_site']);
100
		
101
		// get site entity
102
		$this->site = $entities->get(1, 'site');
103
		if (!$this->site) {
104
			throw new \InstallationException("Unable to handle this request. This site is not configured or the database is down.");
105
		}
106
107
		// get plugins
108
		$this->active_plugins = $plugins->find('active');
109
110
		// get plugin settings
111
		if (!$this->active_plugins) {
112
			return;
113
		}
114
115
		// find GUIDs with not too many private settings
116
		$guids = array_map(function (\ElggPlugin $plugin) {
117
			return $plugin->guid;
118
		}, $this->active_plugins);
119
120
		// find plugin GUIDs with not too many settings
121
		$limit = 40;
122
		$set = implode(',', $guids);
123
		$sql = "
124
			SELECT entity_guid
125
			FROM {$db->prefix}private_settings
126
			WHERE entity_guid IN ($set)
127
			  AND name NOT LIKE 'plugin:user_setting:%'
128
			GROUP BY entity_guid
129
			HAVING COUNT(*) > $limit
130
		";
131
		$unsuitable_guids = $db->getData($sql, function ($row) {
132
			return (int) $row->entity_guid;
133
		});
134
		$guids = array_values($guids);
135
		$guids = array_diff($guids, $unsuitable_guids);
136
137
		if ($guids) {
138
			// get the settings
139
			$set = implode(',', $guids);
140
			$rows = $db->getData("
141
				SELECT entity_guid, `name`, `value`
142
				FROM {$db->prefix}private_settings
143
				WHERE entity_guid IN ($set)
144
				  AND name NOT LIKE 'plugin:user_setting:%'
145
				ORDER BY entity_guid
146
			");
147
			// make sure we show all entities as loaded
148
			$this->plugin_settings = array_fill_keys($guids, []);
149
			foreach ($rows as $i => $row) {
150
				$this->plugin_settings[$row->entity_guid][$row->name] = $row->value;
151
			}
152
		}
153
	}
154
155
	/**
156
	 * Get the site entity
157
	 *
158
	 * @return \ElggSite
159
	 */
160
	public function getSite() {
161
		return $this->site;
162
	}
163
164
	/**
165
	 * Get config values to merge into $CONFIG
166
	 *
167
	 * @return array
168
	 */
169
	public function getConfigValues() {
170
		return $this->config_values;
171
	}
172
173
	/**
174
	 * Get the subtype data
175
	 *
176
	 * @return \stdClass[]
177
	 */
178
	public function getSubtypeData() {
179
		return $this->subtype_data;
180
	}
181
182
	/**
183
	 * Get active plugins
184
	 *
185
	 * @return \ElggPlugin[]
186
	 */
187
	public function getActivePlugins() {
188
		return $this->active_plugins;
189
	}
190
191
	/**
192
	 * Get the plugin settings (may not include all active plugins)
193
	 *
194
	 * @return array
195
	 */
196
	public function getPluginSettings() {
197
		return $this->plugin_settings;
198
	}
199
}
200