datasource_entity.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * @file
  4. * Contains the SearchApiEntityDataSourceController class.
  5. */
  6. /**
  7. * Represents a datasource for all entities known to the Entity API.
  8. */
  9. class SearchApiEntityDataSourceController extends SearchApiAbstractDataSourceController {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public function getIdFieldInfo() {
  14. $info = entity_get_info($this->entityType);
  15. $properties = entity_get_property_info($this->entityType);
  16. if (empty($info['entity keys']['id'])) {
  17. throw new SearchApiDataSourceException(t("Entity type @type doesn't specify an ID key.", array('@type' => $info['label'])));
  18. }
  19. $field = $info['entity keys']['id'];
  20. if (empty($properties['properties'][$field]['type'])) {
  21. throw new SearchApiDataSourceException(t("Entity type @type doesn't specify a type for the @prop property.", array('@type' => $info['label'], '@prop' => $field)));
  22. }
  23. $type = $properties['properties'][$field]['type'];
  24. if (search_api_is_list_type($type)) {
  25. throw new SearchApiDataSourceException(t("Entity type @type uses list field @prop as its ID.", array('@type' => $info['label'], '@prop' => $field)));
  26. }
  27. if ($type == 'token') {
  28. $type = 'string';
  29. }
  30. return array(
  31. 'key' => $field,
  32. 'type' => $type,
  33. );
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function loadItems(array $ids) {
  39. $items = entity_load($this->entityType, $ids);
  40. // If some items couldn't be loaded, remove them from tracking.
  41. if (count($items) != count($ids)) {
  42. $ids = array_flip($ids);
  43. $unknown = array_keys(array_diff_key($ids, $items));
  44. if ($unknown) {
  45. search_api_track_item_delete($this->type, $unknown);
  46. }
  47. }
  48. return $items;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function getMetadataWrapper($item = NULL, array $info = array()) {
  54. return entity_metadata_wrapper($this->entityType, $item, $info);
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function getItemId($item) {
  60. $id = entity_id($this->entityType, $item);
  61. return $id ? $id : NULL;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function getItemLabel($item) {
  67. $label = entity_label($this->entityType, $item);
  68. return $label ? $label : NULL;
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function getItemUrl($item) {
  74. if ($this->entityType == 'file') {
  75. return array(
  76. 'path' => file_create_url($item->uri),
  77. 'options' => array(
  78. 'entity_type' => 'file',
  79. 'entity' => $item,
  80. ),
  81. );
  82. }
  83. $url = entity_uri($this->entityType, $item);
  84. return $url ? $url : NULL;
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function startTracking(array $indexes) {
  90. if (!$this->table) {
  91. return;
  92. }
  93. // We first clear the tracking table for all indexes, so we can just insert
  94. // all items again without any key conflicts.
  95. $this->stopTracking($indexes);
  96. $entity_info = entity_get_info($this->entityType);
  97. if (!empty($entity_info['base table'])) {
  98. // Use a subselect, which will probably be much faster than entity_load().
  99. // Assumes that all entities use the "base table" property and the
  100. // "entity keys[id]" in the same way as the default controller.
  101. $id_field = $entity_info['entity keys']['id'];
  102. $table = $entity_info['base table'];
  103. // We could also use a single insert (with a JOIN in the nested query),
  104. // but this method will be mostly called with a single index, anyways.
  105. foreach ($indexes as $index) {
  106. // Select all entity ids.
  107. $query = db_select($table, 't');
  108. $query->addField('t', $id_field, 'item_id');
  109. $query->addExpression(':index_id', 'index_id', array(':index_id' => $index->id));
  110. $query->addExpression('1', 'changed');
  111. // INSERT ... SELECT ...
  112. db_insert($this->table)
  113. ->from($query)
  114. ->execute();
  115. }
  116. }
  117. else {
  118. // In the absence of a 'base table', use the slow entity_load().
  119. parent::startTracking($indexes);
  120. }
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. protected function getAllItemIds() {
  126. return array_keys(entity_load($this->entityType));
  127. }
  128. }