search_api_test.module 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /**
  3. * Implements hook_menu().
  4. */
  5. function search_api_test_menu() {
  6. return array(
  7. 'search_api_test/insert' => array(
  8. 'title' => 'Insert item',
  9. 'page callback' => 'drupal_get_form',
  10. 'page arguments' => array('search_api_test_insert_item'),
  11. 'access callback' => TRUE,
  12. ),
  13. 'search_api_test/%search_api_test' => array(
  14. 'title' => 'View item',
  15. 'page callback' => 'search_api_test_view',
  16. 'page arguments' => array(1),
  17. 'access callback' => TRUE,
  18. ),
  19. 'search_api_test/query/%search_api_index' => array(
  20. 'title' => 'Search query',
  21. 'page callback' => 'search_api_test_query',
  22. 'page arguments' => array(2),
  23. 'access callback' => TRUE,
  24. ),
  25. );
  26. }
  27. /**
  28. * Form callback for inserting an item.
  29. */
  30. function search_api_test_insert_item(array $form, array &$form_state) {
  31. return array(
  32. 'id' => array(
  33. '#type' => 'textfield',
  34. ),
  35. 'title' => array(
  36. '#type' => 'textfield',
  37. ),
  38. 'body' => array(
  39. '#type' => 'textarea',
  40. ),
  41. 'type' => array(
  42. '#type' => 'textfield',
  43. ),
  44. 'submit' => array(
  45. '#type' => 'submit',
  46. '#value' => t('Save'),
  47. ),
  48. );
  49. }
  50. /**
  51. * Submit callback for search_api_test_insert_item().
  52. */
  53. function search_api_test_insert_item_submit(array $form, array &$form_state) {
  54. form_state_values_clean($form_state);
  55. db_insert('search_api_test')->fields($form_state['values'])->execute();
  56. module_invoke_all('entity_insert', search_api_test_load($form_state['values']['id']), 'search_api_test');
  57. }
  58. /**
  59. * Load handler for search_api_test entities.
  60. */
  61. function search_api_test_load($id) {
  62. $ret = entity_load('search_api_test', array($id));
  63. return $ret ? array_shift($ret) : NULL;
  64. }
  65. /**
  66. * Menu callback for displaying search_api_test entities.
  67. */
  68. function search_api_test_view($entity) {
  69. return array('text' => nl2br(check_plain(print_r($entity, TRUE))));
  70. }
  71. /**
  72. * Menu callback for executing a search.
  73. */
  74. function search_api_test_query(SearchApiIndex $index, $keys = 'foo bar', $offset = 0, $limit = 10, $fields = NULL, $sort = NULL, $filters = NULL) {
  75. // Slight "hack" for testing complex queries.
  76. if ($keys == '|COMPLEX|') {
  77. $keys = array(
  78. '#conjunction' => 'AND',
  79. 'test',
  80. array(
  81. '#conjunction' => 'OR',
  82. 'baz',
  83. 'foobar',
  84. ),
  85. array(
  86. '#conjunction' => 'AND',
  87. '#negation' => TRUE,
  88. 'bar',
  89. ),
  90. );
  91. }
  92. $query = $index->query()
  93. ->keys($keys)
  94. ->range($offset, $limit);
  95. if ($fields) {
  96. $query->fields(explode(',', $fields));
  97. }
  98. if ($sort) {
  99. $sort = explode(',', $sort);
  100. $query->sort($sort[0], $sort[1]);
  101. }
  102. else {
  103. $query->sort('search_api_id', 'ASC');
  104. }
  105. if ($filters) {
  106. $filters = explode(',', $filters);
  107. foreach ($filters as $filter) {
  108. $filter = explode('=', $filter);
  109. $query->condition($filter[0], $filter[1]);
  110. }
  111. }
  112. $result = $query->execute();
  113. $ret = '';
  114. $ret .= 'result count = ' . (int) $result['result count'] . '<br/>';
  115. $ret .= 'results = (' . (empty($result['results']) ? '' : implode(', ', array_keys($result['results']))) . ')<br/>';
  116. $ret .= 'warnings = (' . (empty($result['warnings']) ? '' : '"' . implode('", "', $result['warnings']) . '"') . ')<br/>';
  117. $ret .= 'ignored = (' . (empty($result['ignored']) ? '' : implode(', ', $result['ignored'])) . ')<br/>';
  118. $ret .= nl2br(check_plain(print_r($result['performance'], TRUE)));
  119. return $ret;
  120. }
  121. /**
  122. * Implements hook_entity_info().
  123. */
  124. function search_api_test_entity_info() {
  125. return array(
  126. 'search_api_test' => array(
  127. 'label' => 'Search API test entity',
  128. 'base table' => 'search_api_test',
  129. 'uri callback' => 'search_api_test_uri',
  130. 'entity keys' => array(
  131. 'id' => 'id',
  132. ),
  133. ),
  134. );
  135. }
  136. /**
  137. * Implements hook_entity_property_info().
  138. */
  139. function search_api_test_entity_property_info() {
  140. $info['search_api_test']['properties'] = array(
  141. 'id' => array(
  142. 'label' => 'ID',
  143. 'type' => 'integer',
  144. 'description' => 'The primary identifier for a server.',
  145. ),
  146. 'title' => array(
  147. 'label' => 'Title',
  148. 'type' => 'text',
  149. 'description' => 'The title of the item.',
  150. 'required' => TRUE,
  151. ),
  152. 'body' => array(
  153. 'label' => 'Body',
  154. 'type' => 'text',
  155. 'description' => 'A text belonging to the item.',
  156. 'sanitize' => 'filter_xss',
  157. 'required' => TRUE,
  158. ),
  159. 'type' => array(
  160. 'label' => 'Type',
  161. 'type' => 'text',
  162. 'description' => 'A string identifying the type of item.',
  163. 'required' => TRUE,
  164. ),
  165. 'parent' => array(
  166. 'label' => 'Parent',
  167. 'type' => 'search_api_test',
  168. 'description' => "The item's parent.",
  169. 'getter callback' => 'search_api_test_parent',
  170. ),
  171. );
  172. return $info;
  173. }
  174. /**
  175. * URI callback for test entity.
  176. */
  177. function search_api_test_uri($entity) {
  178. return array(
  179. 'path' => 'search_api_test/' . $entity->id,
  180. );
  181. }
  182. /**
  183. * Parent callback.
  184. */
  185. function search_api_test_parent($entity) {
  186. return search_api_test_load($entity->id - 1);
  187. }
  188. /**
  189. * Implements hook_search_api_service_info().
  190. */
  191. function search_api_test_search_api_service_info() {
  192. $services['search_api_test_service'] = array(
  193. 'name' => 'search_api_test_service',
  194. 'description' => 'search_api_test_service description',
  195. 'class' => 'SearchApiTestService',
  196. );
  197. return $services;
  198. }
  199. /**
  200. * Test service class.
  201. */
  202. class SearchApiTestService extends SearchApiAbstractService {
  203. public function configurationForm(array $form, array &$form_state) {
  204. $form = array(
  205. 'test' => array(
  206. '#type' => 'textfield',
  207. '#title' => 'Test option',
  208. ),
  209. );
  210. if (!empty($this->options)) {
  211. $form['test']['#default_value'] = $this->options['test'];
  212. }
  213. return $form;
  214. }
  215. public function indexItems(SearchApiIndex $index, array $items) {
  216. // Refuse to index items with IDs that are multiples of 8 unless the
  217. // "search_api_test_index_all" variable is set.
  218. if (variable_get('search_api_test_index_all', FALSE)) {
  219. return $this->index($index, array_keys($items));
  220. }
  221. $ret = array();
  222. foreach ($items as $id => $item) {
  223. if ($id % 8) {
  224. $ret[] = $id;
  225. }
  226. }
  227. return $this->index($index, $ret);
  228. }
  229. protected function index(SearchApiIndex $index, array $ids) {
  230. $this->options += array('indexes' => array());
  231. $this->options['indexes'] += array($index->machine_name => array());
  232. $this->options['indexes'][$index->machine_name] += drupal_map_assoc($ids);
  233. sort($this->options['indexes'][$index->machine_name]);
  234. $this->server->save();
  235. return $ids;
  236. }
  237. /**
  238. * Override so deleteItems() isn't called which would otherwise lead to the
  239. * server being updated and, eventually, to a notice because there is no
  240. * server to be updated anymore.
  241. */
  242. public function preDelete() {}
  243. public function deleteItems($ids = 'all', SearchApiIndex $index = NULL) {
  244. if ($ids == 'all') {
  245. if ($index) {
  246. $this->options['indexes'][$index->machine_name] = array();
  247. }
  248. else {
  249. $this->options['indexes'] = array();
  250. }
  251. }
  252. else {
  253. foreach ($ids as $id) {
  254. unset($this->options['indexes'][$index->machine_name][$id]);
  255. }
  256. }
  257. $this->server->save();
  258. }
  259. public function search(SearchApiQueryInterface $query) {
  260. $options = $query->getOptions();
  261. $ret = array();
  262. $index_id = $query->getIndex()->machine_name;
  263. if (empty($this->options['indexes'][$index_id])) {
  264. return array(
  265. 'result count' => 0,
  266. 'results' => array(),
  267. );
  268. }
  269. $items = $this->options['indexes'][$index_id];
  270. $min = isset($options['offset']) ? $options['offset'] : 0;
  271. $max = $min + (isset($options['limit']) ? $options['limit'] : count($items));
  272. $i = 0;
  273. $ret['result count'] = count($items);
  274. $ret['results'] = array();
  275. foreach ($items as $id) {
  276. ++$i;
  277. if ($i > $max) {
  278. break;
  279. }
  280. if ($i > $min) {
  281. $ret['results'][$id] = array(
  282. 'id' => $id,
  283. 'score' => 1,
  284. );
  285. }
  286. }
  287. return $ret;
  288. }
  289. public function fieldsUpdated(SearchApiIndex $index) {
  290. return db_query('SELECT COUNT(*) FROM {search_api_test}')->fetchField() > 0;
  291. }
  292. }