AttributeLoader.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. namespace Elgg;
  3. /**
  4. * Loads \ElggEntity attributes from DB or validates those passed in via constructor
  5. *
  6. * @access private
  7. *
  8. * @package Elgg.Core
  9. * @subpackage DataModel
  10. */
  11. class AttributeLoader {
  12. /**
  13. * @var array names of attributes in all entities
  14. *
  15. * @todo require this to be injected and get it from \ElggEntity
  16. */
  17. protected static $primary_attr_names = array(
  18. 'guid',
  19. 'type',
  20. 'subtype',
  21. 'owner_guid',
  22. 'container_guid',
  23. 'site_guid',
  24. 'access_id',
  25. 'time_created',
  26. 'time_updated',
  27. 'last_action',
  28. 'enabled',
  29. );
  30. /**
  31. * @var array names of attributes in all entities that should be stored as integer values
  32. */
  33. protected static $integer_attr_names = array(
  34. 'guid',
  35. 'owner_guid',
  36. 'container_guid',
  37. 'site_guid',
  38. 'access_id',
  39. 'time_created',
  40. 'time_updated',
  41. 'last_action',
  42. // \ElggUser
  43. 'prev_last_action',
  44. 'last_login',
  45. 'prev_last_login'
  46. );
  47. /**
  48. * @var array names of attributes in all entities that should be stored as null if empty
  49. */
  50. protected static $null_attr_names = array(
  51. 'name',
  52. 'title',
  53. 'description',
  54. 'url',
  55. );
  56. /**
  57. * @var array names of secondary attributes required for the entity
  58. */
  59. protected $secondary_attr_names = array();
  60. /**
  61. * @var string entity type (not class) required for fetched primaries
  62. */
  63. protected $required_type;
  64. /**
  65. * @var array
  66. */
  67. protected $initialized_attributes;
  68. /**
  69. * @var string class of object being loaded
  70. */
  71. protected $class;
  72. /**
  73. * @var bool should access control be considered when fetching entity?
  74. */
  75. public $requires_access_control = true;
  76. /**
  77. * @var callable function used to load attributes from {prefix}entities table
  78. */
  79. public $primary_loader = 'get_entity_as_row';
  80. /**
  81. * @var callable function used to load attributes from secondary table
  82. */
  83. public $secondary_loader = '';
  84. /**
  85. * @var callable function used to load all necessary attributes
  86. */
  87. public $full_loader = '';
  88. /**
  89. * @var array retrieved values that are not attributes
  90. */
  91. protected $additional_select_values = array();
  92. /**
  93. * Constructor
  94. *
  95. * @param string $class class of object being loaded
  96. * @param string $required_type entity type this is being used to populate
  97. * @param array $initialized_attrs attributes after initializeAttributes() has been run
  98. * @throws \InvalidArgumentException
  99. */
  100. public function __construct($class, $required_type, array $initialized_attrs) {
  101. if (!is_string($class)) {
  102. throw new \InvalidArgumentException('$class must be a class name.');
  103. }
  104. $this->class = $class;
  105. if (!is_string($required_type)) {
  106. throw new \InvalidArgumentException('$requiredType must be a system entity type.');
  107. }
  108. $this->required_type = $required_type;
  109. $this->initialized_attributes = $initialized_attrs;
  110. $all_attr_names = array_keys($initialized_attrs);
  111. $this->secondary_attr_names = array_diff($all_attr_names, self::$primary_attr_names);
  112. }
  113. /**
  114. * Get primary attributes missing that are missing
  115. *
  116. * @param \stdClass $row Database row
  117. * @return array
  118. */
  119. protected function isMissingPrimaries($row) {
  120. return array_diff(self::$primary_attr_names, array_keys($row)) !== array();
  121. }
  122. /**
  123. * Get secondary attributes that are missing
  124. *
  125. * @param \stdClass $row Database row
  126. * @return array
  127. */
  128. protected function isMissingSecondaries($row) {
  129. return array_diff($this->secondary_attr_names, array_keys($row)) !== array();
  130. }
  131. /**
  132. * Check that the type is correct
  133. *
  134. * @param \stdClass $row Database row
  135. * @return void
  136. * @throws \InvalidClassException
  137. */
  138. protected function checkType($row) {
  139. if ($row['type'] !== $this->required_type) {
  140. $msg = "GUID:" . $row['guid'] . " is not a valid " . $this->class;
  141. throw new \InvalidClassException($msg);
  142. }
  143. }
  144. /**
  145. * Get values selected from the database that are not attributes
  146. *
  147. * @return array
  148. */
  149. public function getAdditionalSelectValues() {
  150. return $this->additional_select_values;
  151. }
  152. /**
  153. * Get all required attributes for the entity, validating any that are passed in. Returns empty array
  154. * if can't be loaded (Check $failure_reason).
  155. *
  156. * This function splits loading between "primary" attributes (those in {prefix}entities table) and
  157. * "secondary" attributes (e.g. those in {prefix}objects_entity), but can load all at once if a
  158. * combined loader is available.
  159. *
  160. * @param mixed $row a row loaded from DB (array or \stdClass) or a GUID
  161. * @return array will be empty if failed to load all attributes (access control or entity doesn't exist)
  162. *
  163. * @throws \InvalidArgumentException|\LogicException|\IncompleteEntityException
  164. */
  165. public function getRequiredAttributes($row) {
  166. if (!is_array($row) && !($row instanceof \stdClass)) {
  167. // assume row is the GUID
  168. $row = array('guid' => $row);
  169. }
  170. $row = (array) $row;
  171. if (empty($row['guid'])) {
  172. throw new \InvalidArgumentException('$row must be or contain a GUID');
  173. }
  174. $was_missing_primaries = $this->isMissingPrimaries($row);
  175. $was_missing_secondaries = $this->isMissingSecondaries($row);
  176. // some types have a function to load all attributes at once, it should be faster
  177. if (($was_missing_primaries || $was_missing_secondaries) && is_callable($this->full_loader)) {
  178. $fetched = (array) call_user_func($this->full_loader, $row['guid']);
  179. if (!$fetched) {
  180. return array();
  181. }
  182. $row = array_merge($row, $fetched);
  183. $this->checkType($row);
  184. } else {
  185. if ($was_missing_primaries) {
  186. if (!is_callable($this->primary_loader)) {
  187. throw new \LogicException('Primary attribute loader must be callable');
  188. }
  189. if ($this->requires_access_control) {
  190. $fetched = (array) call_user_func($this->primary_loader, $row['guid']);
  191. } else {
  192. $ignoring_access = elgg_set_ignore_access();
  193. $fetched = (array) call_user_func($this->primary_loader, $row['guid']);
  194. elgg_set_ignore_access($ignoring_access);
  195. }
  196. if (!$fetched) {
  197. return array();
  198. }
  199. $row = array_merge($row, $fetched);
  200. }
  201. // We must test type before trying to load the secondaries so that InvalidClassException
  202. // gets thrown. Otherwise the secondary loader will fail and return false.
  203. $this->checkType($row);
  204. if ($was_missing_secondaries) {
  205. if (!is_callable($this->secondary_loader)) {
  206. throw new \LogicException('Secondary attribute loader must be callable');
  207. }
  208. $fetched = (array) call_user_func($this->secondary_loader, $row['guid']);
  209. if (!$fetched) {
  210. throw new \IncompleteEntityException("Secondary loader failed to return row for {$row['guid']}");
  211. }
  212. $row = array_merge($row, $fetched);
  213. }
  214. }
  215. $row = $this->filterAddedColumns($row);
  216. $row['subtype'] = (int)$row['subtype'];
  217. // set to null when reading empty value, to match default empty value; See #5456
  218. foreach (self::$null_attr_names as $key) {
  219. if (isset($row[$key]) && !$row[$key]) {
  220. $row[$key] = null;
  221. }
  222. }
  223. // Note: If there are still missing attributes, we're running on a 1.7 or earlier schema. We let
  224. // this pass so the upgrades can run.
  225. // guid needs to be an int https://github.com/elgg/elgg/issues/4111
  226. foreach (self::$integer_attr_names as $key) {
  227. if (isset($row[$key])) {
  228. $row[$key] = (int) $row[$key];
  229. }
  230. }
  231. return $row;
  232. }
  233. /**
  234. * Filter non-attribute keys into $this->additional_select_values
  235. *
  236. * @param array $row All columns from the query
  237. * @return array Columns acceptable for the entity's attributes
  238. */
  239. protected function filterAddedColumns($row) {
  240. // make an array with keys as acceptable attribute names
  241. $acceptable_attrs = self::$primary_attr_names;
  242. array_splice($acceptable_attrs, count($acceptable_attrs), 0, $this->secondary_attr_names);
  243. $acceptable_attrs = array_combine($acceptable_attrs, $acceptable_attrs);
  244. foreach ($row as $key => $val) {
  245. if (!isset($acceptable_attrs[$key])) {
  246. $this->additional_select_values[$key] = $val;
  247. unset($row[$key]);
  248. }
  249. }
  250. return $row;
  251. }
  252. }