datasource_entity.inc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * @file
  4. * Contains the SearchApiEntityDataSourceController class.
  5. */
  6. /**
  7. * Data source for all entities known to the Entity API.
  8. */
  9. class SearchApiEntityDataSourceController extends SearchApiAbstractDataSourceController {
  10. /**
  11. * Return information on the ID field for this controller's type.
  12. *
  13. * @return array
  14. * An associative array containing the following keys:
  15. * - key: The property key for the ID field, as used in the item wrapper.
  16. * - type: The type of the ID field. Has to be one of the types from
  17. * search_api_field_types(). List types ("list<*>") are not allowed.
  18. */
  19. public function getIdFieldInfo() {
  20. $info = entity_get_info($this->type);
  21. $properties = entity_get_property_info($this->type);
  22. if (empty($info['entity keys']['id'])) {
  23. throw new SearchApiDataSourceException(t("Entity type @type doesn't specify an ID key.", array('@type' => $info['label'])));
  24. }
  25. $field = $info['entity keys']['id'];
  26. if (empty($properties['properties'][$field]['type'])) {
  27. throw new SearchApiDataSourceException(t("Entity type @type doesn't specify a type for the @prop property.", array('@type' => $info['label'], '@prop' => $field)));
  28. }
  29. $type = $properties['properties'][$field]['type'];
  30. if (search_api_is_list_type($type)) {
  31. throw new SearchApiDataSourceException(t("Entity type @type uses list field @prop as its ID.", array('@type' => $info['label'], '@prop' => $field)));
  32. }
  33. if ($type == 'token') {
  34. $type = 'string';
  35. }
  36. return array(
  37. 'key' => $field,
  38. 'type' => $type,
  39. );
  40. }
  41. /**
  42. * Load items of the type of this data source controller.
  43. *
  44. * @param array $ids
  45. * The IDs of the items to laod.
  46. *
  47. * @return array
  48. * The loaded items, keyed by ID.
  49. */
  50. public function loadItems(array $ids) {
  51. $items = entity_load($this->type, $ids);
  52. // If some items couldn't be loaded, remove them from tracking.
  53. if (count($items) != count($ids)) {
  54. $ids = array_flip($ids);
  55. $unknown = array_keys(array_diff_key($ids, $items));
  56. if ($unknown) {
  57. search_api_track_item_delete($this->type, $unknown);
  58. }
  59. }
  60. return $items;
  61. }
  62. /**
  63. * Get a metadata wrapper for the item type of this data source controller.
  64. *
  65. * @param $item
  66. * Unless NULL, an item of the item type for this controller to be wrapped.
  67. * @param array $info
  68. * Optionally, additional information that should be used for creating the
  69. * wrapper. Uses the same format as entity_metadata_wrapper().
  70. *
  71. * @return EntityMetadataWrapper
  72. * A wrapper for the item type of this data source controller, according to
  73. * the info array, and optionally loaded with the given data.
  74. *
  75. * @see entity_metadata_wrapper()
  76. */
  77. public function getMetadataWrapper($item = NULL, array $info = array()) {
  78. return entity_metadata_wrapper($this->type, $item, $info);
  79. }
  80. /**
  81. * Get the unique ID of an item.
  82. *
  83. * @param $item
  84. * An item of this controller's type.
  85. *
  86. * @return
  87. * Either the unique ID of the item, or NULL if none is available.
  88. */
  89. public function getItemId($item) {
  90. $id = entity_id($this->type, $item);
  91. return $id ? $id : NULL;
  92. }
  93. /**
  94. * Get a human-readable label for an item.
  95. *
  96. * @param $item
  97. * An item of this controller's type.
  98. *
  99. * @return
  100. * Either a human-readable label for the item, or NULL if none is available.
  101. */
  102. public function getItemLabel($item) {
  103. $label = entity_label($this->type, $item);
  104. return $label ? $label : NULL;
  105. }
  106. /**
  107. * Get a URL at which the item can be viewed on the web.
  108. *
  109. * @param $item
  110. * An item of this controller's type.
  111. *
  112. * @return
  113. * Either an array containing the 'path' and 'options' keys used to build
  114. * the URL of the item, and matching the signature of url(), or NULL if the
  115. * item has no URL of its own.
  116. */
  117. public function getItemUrl($item) {
  118. if ($this->type == 'file') {
  119. return array(
  120. 'path' => file_create_url($item->uri),
  121. 'options' => array(
  122. 'entity_type' => 'file',
  123. 'entity' => $item,
  124. ),
  125. );
  126. }
  127. $url = entity_uri($this->type, $item);
  128. return $url ? $url : NULL;
  129. }
  130. /**
  131. * Initialize tracking of the index status of items for the given indexes.
  132. *
  133. * All currently known items of this data source's type should be inserted
  134. * into the tracking table for the given indexes, with status "changed". If
  135. * items were already present, these should also be set to "changed" and not
  136. * be inserted again.
  137. *
  138. * @param array $indexes
  139. * The SearchApiIndex objects for which item tracking should be initialized.
  140. *
  141. * @throws SearchApiDataSourceException
  142. * If any of the indexes doesn't use the same item type as this controller.
  143. */
  144. public function startTracking(array $indexes) {
  145. if (!$this->table) {
  146. return;
  147. }
  148. // We first clear the tracking table for all indexes, so we can just insert
  149. // all items again without any key conflicts.
  150. $this->stopTracking($indexes);
  151. $entity_info = entity_get_info($this->type);
  152. if (!empty($entity_info['base table'])) {
  153. // Use a subselect, which will probably be much faster than entity_load().
  154. // Assumes that all entities use the "base table" property and the
  155. // "entity keys[id]" in the same way as the default controller.
  156. $id_field = $entity_info['entity keys']['id'];
  157. $table = $entity_info['base table'];
  158. // We could also use a single insert (with a JOIN in the nested query),
  159. // but this method will be mostly called with a single index, anyways.
  160. foreach ($indexes as $index) {
  161. // Select all entity ids.
  162. $query = db_select($table, 't');
  163. $query->addField('t', $id_field, 'item_id');
  164. $query->addExpression(':index_id', 'index_id', array(':index_id' => $index->id));
  165. $query->addExpression('1', 'changed');
  166. // INSERT ... SELECT ...
  167. db_insert($this->table)
  168. ->from($query)
  169. ->execute();
  170. }
  171. }
  172. else {
  173. // In the absence of a 'base table', use the slow entity_load().
  174. parent::startTracking($indexes);
  175. }
  176. }
  177. /**
  178. * Helper method that can be used by subclasses instead of implementing startTracking().
  179. *
  180. * Returns the IDs of all items that are known for this controller's type.
  181. *
  182. * Will be used when the entity type doesn't specify a "base table".
  183. *
  184. * @return array
  185. * An array containing all item IDs for this type.
  186. */
  187. protected function getAllItemIds() {
  188. return array_keys(entity_load($this->type));
  189. }
  190. }