Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

engine/classes/Elgg/BootData.php (2 issues)

1
<?php
2
3
namespace Elgg;
4
5
use Elgg\Database;
6
use Elgg\Database\EntityTable;
7
use Elgg\Database\Plugins;
8
use Elgg\Database\SiteSecret;
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|false
20
	 */
21
	private $site = false;
22
23
	/**
24
	 * @var \ElggPlugin[]
25
	 */
26
	private $active_plugins;
27
28
	/**
29
	 * @var array
30
	 */
31
	private $plugin_settings = [];
32
33
	/**
34
	 * @var array
35
	 */
36
	private $plugin_metadata = [];
37
38
	/**
39
	 * Populate the boot data
40
	 *
41
	 * @param Database    $db        Elgg database
42
	 * @param EntityTable $entities  Entities service
43
	 * @param Plugins     $plugins   Plugins service
44
	 * @param bool        $installed Is the site installed?
45
	 *
46
	 * @return void
47
	 * @throws \InstallationException
48
	 * @throws \InvalidParameterException
49
	 * @throws \DatabaseException
50
	 * @throws \ClassException
51
	 */
52 4777
	public function populate(Database $db, EntityTable $entities, Plugins $plugins, $installed) {
53
54
		// get site entity
55 4777
		$this->site = $entities->get(1, 'site');
56 4777
		if (!$this->site && $installed) {
57
			throw new \InstallationException("Unable to handle this request. This site is not configured or the database is down.");
58
		}
59
60
		// get plugins
61 4777
		$this->active_plugins = $plugins->find('active');
62
63
		// get plugin settings
64 4777
		if (!$this->active_plugins) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->active_plugins of type ElggPlugin[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
65 4764
			return;
66
		}
67
68
		// find GUIDs with not too many private settings
69 13
		$guids = array_map(function (\ElggPlugin $plugin) {
70 13
			return $plugin->guid;
71 13
		}, $this->active_plugins);
72
73 13
		_elgg_services()->metadataCache->populateFromEntities($guids);
74
75 13
		foreach ($guids as $guid) {
76 13
			$this->plugin_metadata[$guid] = _elgg_services()->metadataCache->getEntityMetadata($guid);
77
		}
78
79
		// find plugin GUIDs with not too many settings
80 13
		$limit = 40;
81 13
		$set = implode(',', $guids);
82
		$sql = "
83
			SELECT entity_guid
84 13
			FROM {$db->prefix}private_settings
85 13
			WHERE entity_guid IN ($set)
86
			  AND name NOT LIKE 'plugin:user_setting:%'
87
			GROUP BY entity_guid
88 13
			HAVING COUNT(*) > $limit
89
		";
90 13
		$unsuitable_guids = $db->getData($sql, function ($row) {
91
			return (int) $row->entity_guid;
92 13
		});
93 13
		$guids = array_values($guids);
94 13
		$guids = array_diff($guids, $unsuitable_guids);
95
96 13
		if ($guids) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $guids of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
97
			// get the settings
98 13
			$set = implode(',', $guids);
99 13
			$rows = $db->getData("
100
				SELECT entity_guid, `name`, `value`
101 13
				FROM {$db->prefix}private_settings
102 13
				WHERE entity_guid IN ($set)
103
				  AND name NOT LIKE 'plugin:user_setting:%'
104
				ORDER BY entity_guid
105
			");
106
			// make sure we show all entities as loaded
107 13
			$this->plugin_settings = array_fill_keys($guids, []);
108 13
			foreach ($rows as $i => $row) {
109 13
				$this->plugin_settings[$row->entity_guid][$row->name] = $row->value;
110
			}
111
		}
112 13
	}
113
114
	/**
115
	 * Get the site entity
116
	 *
117
	 * @return \ElggSite|false False if not installed
118
	 */
119 4777
	public function getSite() {
120 4777
		return $this->site;
121
	}
122
123
	/**
124
	 * Get active plugins
125
	 *
126
	 * @return \ElggPlugin[]
127
	 */
128 4777
	public function getActivePlugins() {
129 4777
		return $this->active_plugins;
130
	}
131
132
	/**
133
	 * Get the plugin settings (may not include all active plugins)
134
	 *
135
	 * @return array
136
	 */
137 4777
	public function getPluginSettings() {
138 4777
		return $this->plugin_settings;
139
	}
140
141
	/**
142
	 * Get plugin metadata
143
	 *
144
	 * @return array
145
	 */
146 4777
	public function getPluginMetadata() {
147 4777
		return $this->plugin_metadata;
148
	}
149
}
150