search_api.install 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the Search API module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function search_api_schema() {
  10. $schema['search_api_server'] = array(
  11. 'description' => 'Stores all search servers created through the Search API.',
  12. 'fields' => array(
  13. 'id' => array(
  14. 'description' => 'The primary identifier for a server.',
  15. 'type' => 'serial',
  16. 'unsigned' => TRUE,
  17. 'not null' => TRUE,
  18. ),
  19. 'name' => array(
  20. 'description' => 'The displayed name for a server.',
  21. 'type' => 'varchar',
  22. 'length' => 50,
  23. 'not null' => TRUE,
  24. ),
  25. 'machine_name' => array(
  26. 'description' => 'The machine name for a server.',
  27. 'type' => 'varchar',
  28. 'length' => 50,
  29. 'not null' => TRUE,
  30. ),
  31. 'description' => array(
  32. 'description' => 'The displayed description for a server.',
  33. 'type' => 'text',
  34. 'not null' => FALSE,
  35. ),
  36. 'class' => array(
  37. 'description' => 'The id of the service class to use for this server.',
  38. 'type' => 'varchar',
  39. 'length' => 50,
  40. 'not null' => TRUE,
  41. ),
  42. 'options' => array(
  43. 'description' => 'The options used to configure the service object.',
  44. 'type' => 'text',
  45. 'size' => 'medium',
  46. 'serialize' => TRUE,
  47. 'not null' => TRUE,
  48. ),
  49. 'enabled' => array(
  50. 'description' => 'A flag indicating whether the server is enabled.',
  51. 'type' => 'int',
  52. 'size' => 'tiny',
  53. 'not null' => TRUE,
  54. 'default' => 1,
  55. ),
  56. 'status' => array(
  57. 'description' => 'The exportable status of the entity.',
  58. 'type' => 'int',
  59. 'not null' => TRUE,
  60. 'default' => 0x01,
  61. 'size' => 'tiny',
  62. ),
  63. 'module' => array(
  64. 'description' => 'The name of the providing module if the entity has been defined in code.',
  65. 'type' => 'varchar',
  66. 'length' => 255,
  67. 'not null' => FALSE,
  68. ),
  69. ),
  70. 'indexes' => array(
  71. 'enabled' => array('enabled'),
  72. ),
  73. 'unique keys' => array(
  74. 'machine_name' => array('machine_name'),
  75. ),
  76. 'primary key' => array('id'),
  77. );
  78. $schema['search_api_index'] = array(
  79. 'description' => 'Stores all search indexes on a {search_api_server}.',
  80. 'fields' => array(
  81. 'id' => array(
  82. 'description' => 'An integer identifying the index.',
  83. 'type' => 'serial',
  84. 'unsigned' => TRUE,
  85. 'not null' => TRUE,
  86. ),
  87. 'name' => array(
  88. 'description' => 'A name to be displayed for the index.',
  89. 'type' => 'varchar',
  90. 'length' => 50,
  91. 'not null' => TRUE,
  92. ),
  93. 'machine_name' => array(
  94. 'description' => 'The machine name of the index.',
  95. 'type' => 'varchar',
  96. 'length' => 50,
  97. 'not null' => TRUE,
  98. ),
  99. 'description' => array(
  100. 'description' => "A string describing the index' use to users.",
  101. 'type' => 'text',
  102. 'not null' => FALSE,
  103. ),
  104. 'server' => array(
  105. 'description' => 'The {search_api_server}.machine_name with which data should be indexed.',
  106. 'type' => 'varchar',
  107. 'length' => 50,
  108. 'not null' => FALSE,
  109. ),
  110. 'item_type' => array(
  111. 'description' => 'The type of items stored in this index.',
  112. 'type' => 'varchar',
  113. 'length' => 50,
  114. 'not null' => TRUE,
  115. ),
  116. 'options' => array(
  117. 'description' => 'An array of additional arguments configuring this index.',
  118. 'type' => 'text',
  119. 'size' => 'medium',
  120. 'serialize' => TRUE,
  121. 'not null' => TRUE,
  122. ),
  123. 'enabled' => array(
  124. 'description' => 'A flag indicating whether this index is enabled.',
  125. 'type' => 'int',
  126. 'size' => 'tiny',
  127. 'not null' => TRUE,
  128. 'default' => 1,
  129. ),
  130. 'read_only' => array(
  131. 'description' => 'A flag indicating whether to write to this index.',
  132. 'type' => 'int',
  133. 'size' => 'tiny',
  134. 'not null' => TRUE,
  135. 'default' => 0,
  136. ),
  137. 'status' => array(
  138. 'description' => 'The exportable status of the entity.',
  139. 'type' => 'int',
  140. 'not null' => TRUE,
  141. 'default' => 0x01,
  142. 'size' => 'tiny',
  143. ),
  144. 'module' => array(
  145. 'description' => 'The name of the providing module if the entity has been defined in code.',
  146. 'type' => 'varchar',
  147. 'length' => 255,
  148. 'not null' => FALSE,
  149. ),
  150. ),
  151. 'indexes' => array(
  152. 'item_type' => array('item_type'),
  153. 'server' => array('server'),
  154. 'enabled' => array('enabled'),
  155. ),
  156. 'unique keys' => array(
  157. 'machine_name' => array('machine_name'),
  158. ),
  159. 'primary key' => array('id'),
  160. );
  161. $schema['search_api_item'] = array(
  162. 'description' => 'Stores the items which should be indexed for each index, and their status.',
  163. 'fields' => array(
  164. 'item_id' => array(
  165. 'description' => "The item's entity id (e.g. {node}.nid for nodes).",
  166. 'type' => 'int',
  167. 'unsigned' => TRUE,
  168. 'not null' => TRUE,
  169. ),
  170. 'index_id' => array(
  171. 'description' => 'The {search_api_index}.id this item belongs to.',
  172. 'type' => 'int',
  173. 'unsigned' => TRUE,
  174. 'not null' => TRUE,
  175. ),
  176. 'changed' => array(
  177. 'description' => 'Either a flag or a timestamp to indicate if or when the item was changed since it was last indexed.',
  178. 'type' => 'int',
  179. 'size' => 'big',
  180. 'not null' => TRUE,
  181. 'default' => 1,
  182. ),
  183. ),
  184. 'indexes' => array(
  185. 'indexing' => array('index_id', 'changed'),
  186. ),
  187. 'primary key' => array('item_id', 'index_id'),
  188. );
  189. $schema['search_api_item_string_id'] = array(
  190. 'description' => 'Stores the items which should be indexed for each index, and their status. Used only for items with string IDs.',
  191. 'fields' => array(
  192. 'item_id' => array(
  193. 'description' => "The item's ID.",
  194. 'type' => 'varchar',
  195. 'length' => 64,
  196. 'not null' => TRUE,
  197. ),
  198. 'index_id' => array(
  199. 'description' => 'The {search_api_index}.id this item belongs to.',
  200. 'type' => 'int',
  201. 'unsigned' => TRUE,
  202. 'not null' => TRUE,
  203. ),
  204. 'changed' => array(
  205. 'description' => 'Either a flag or a timestamp to indicate if or when the item was changed since it was last indexed.',
  206. 'type' => 'int',
  207. 'size' => 'big',
  208. 'not null' => TRUE,
  209. 'default' => 1,
  210. ),
  211. ),
  212. 'indexes' => array(
  213. 'indexing' => array('index_id', 'changed'),
  214. ),
  215. 'primary key' => array('item_id', 'index_id'),
  216. );
  217. $schema['search_api_task'] = array(
  218. 'description' => 'Stores pending tasks for servers.',
  219. 'fields' => array(
  220. 'id' => array(
  221. 'description' => 'An integer identifying this task.',
  222. 'type' => 'serial',
  223. 'unsigned' => TRUE,
  224. 'not null' => TRUE,
  225. ),
  226. 'server_id' => array(
  227. 'description' => 'The {search_api_server}.machine_name for which this task should be executed.',
  228. 'type' => 'varchar',
  229. 'length' => 50,
  230. 'not null' => TRUE,
  231. ),
  232. 'type' => array(
  233. 'description' => 'A keyword identifying the type of task that should be executed.',
  234. 'type' => 'varchar',
  235. 'length' => 50,
  236. 'not null' => TRUE,
  237. ),
  238. 'index_id' => array(
  239. 'description' => 'The {search_api_index}.machine_name to which this task pertains, if applicable for this type.',
  240. 'type' => 'varchar',
  241. 'length' => 50,
  242. 'not null' => FALSE,
  243. ),
  244. 'data' => array(
  245. 'description' => 'Some data needed for the task, might be optional depending on the type.',
  246. 'type' => 'text',
  247. 'size' => 'medium',
  248. 'serialize' => TRUE,
  249. 'not null' => FALSE,
  250. ),
  251. ),
  252. 'indexes' => array(
  253. 'server' => array('server_id'),
  254. ),
  255. 'primary key' => array('id'),
  256. );
  257. return $schema;
  258. }
  259. /**
  260. * Implements hook_requirements().
  261. */
  262. function search_api_requirements($phase) {
  263. $requirements = array();
  264. if ($phase == 'runtime') {
  265. // Check whether at least one server has pending tasks.
  266. if (search_api_server_tasks_count()) {
  267. $items = array();
  268. $conditions = array('enabled' => TRUE);
  269. foreach (search_api_server_load_multiple(FALSE, $conditions) as $server) {
  270. $count = search_api_server_tasks_count($server);
  271. if ($count) {
  272. $args = array(
  273. '@name' => $server->name,
  274. );
  275. $text = format_plural($count, '@name has @count pending task.', '@name has @count pending tasks.', $args);
  276. $items[] = l($text, "admin/config/search/search_api/server/{$server->machine_name}/execute-tasks");
  277. }
  278. }
  279. if ($items) {
  280. $text = t('There are pending tasks for the following servers:');
  281. $text .= theme('item_list', array(
  282. 'type' => 'ul',
  283. 'items' => $items,
  284. ));
  285. if (count($items) > 1) {
  286. $label = t('Execute pending tasks on all servers');
  287. $text .= l($label, 'admin/config/search/search_api/execute-tasks');
  288. }
  289. $requirements['search_api_pending_tasks'] = array(
  290. 'title' => t('Search API'),
  291. 'value' => $text,
  292. 'severity' => REQUIREMENT_WARNING,
  293. );
  294. }
  295. }
  296. }
  297. return $requirements;
  298. }
  299. /**
  300. * Implements hook_install().
  301. *
  302. * Creates a default node index if the module is installed manually.
  303. */
  304. function search_api_install() {
  305. // In case the module is installed via an installation profile, a batch is
  306. // active and we skip that.
  307. if (batch_get()) {
  308. return;
  309. }
  310. $name = t('Default node index');
  311. $values = array(
  312. 'name' => $name,
  313. 'machine_name' => preg_replace('/[^a-z0-9]+/', '_', drupal_strtolower($name)),
  314. 'description' => t('An automatically created search index for indexing node data. Might be configured to specific needs.'),
  315. 'server' => NULL,
  316. 'item_type' => 'node',
  317. 'options' => array(
  318. 'index_directly' => 1,
  319. 'cron_limit' => '50',
  320. 'data_alter_callbacks' => array(
  321. 'search_api_alter_node_access' => array(
  322. 'status' => 1,
  323. 'weight' => '0',
  324. 'settings' => array(),
  325. ),
  326. ),
  327. 'processors' => array(
  328. 'search_api_case_ignore' => array(
  329. 'status' => 1,
  330. 'weight' => '0',
  331. 'settings' => array(
  332. 'strings' => 0,
  333. ),
  334. ),
  335. 'search_api_html_filter' => array(
  336. 'status' => 1,
  337. 'weight' => '10',
  338. 'settings' => array(
  339. 'title' => 0,
  340. 'alt' => 1,
  341. 'tags' => "h1 = 5\n" .
  342. "h2 = 3\n" .
  343. "h3 = 2\n" .
  344. "strong = 2\n" .
  345. "b = 2\n" .
  346. "em = 1.5\n" .
  347. "u = 1.5",
  348. ),
  349. ),
  350. 'search_api_tokenizer' => array(
  351. 'status' => 1,
  352. 'weight' => '20',
  353. 'settings' => array(
  354. 'spaces' => '[^\\p{L}\\p{N}]',
  355. 'ignorable' => '[-]',
  356. ),
  357. ),
  358. ),
  359. 'fields' => array(
  360. 'type' => array(
  361. 'type' => 'string',
  362. ),
  363. 'title' => array(
  364. 'type' => 'text',
  365. 'boost' => '5.0',
  366. ),
  367. 'promote' => array(
  368. 'type' => 'boolean',
  369. ),
  370. 'sticky' => array(
  371. 'type' => 'boolean',
  372. ),
  373. 'created' => array(
  374. 'type' => 'date',
  375. ),
  376. 'changed' => array(
  377. 'type' => 'date',
  378. ),
  379. 'author' => array(
  380. 'type' => 'integer',
  381. 'entity_type' => 'user',
  382. ),
  383. 'comment_count' => array(
  384. 'type' => 'integer',
  385. ),
  386. 'search_api_language' => array(
  387. 'type' => 'string',
  388. ),
  389. 'body:value' => array(
  390. 'type' => 'text',
  391. ),
  392. ),
  393. ),
  394. );
  395. search_api_index_insert($values);
  396. drupal_set_message(t('The Search API module was installed. A new default node index was created.'));
  397. }
  398. /**
  399. * Implements hook_enable().
  400. *
  401. * Mark all items as "dirty", since we can't know whether they are.
  402. */
  403. function search_api_enable() {
  404. $types = array();
  405. foreach (search_api_index_load_multiple(FALSE) as $index) {
  406. if ($index->enabled) {
  407. $types[$index->item_type][] = $index;
  408. }
  409. }
  410. foreach ($types as $type => $indexes) {
  411. try {
  412. $controller = search_api_get_datasource_controller($type);
  413. $controller->startTracking($indexes);
  414. }
  415. catch (SearchApiException $e) {
  416. watchdog_exception('search_api', $e);
  417. }
  418. }
  419. }
  420. /**
  421. * Implements hook_disable().
  422. */
  423. function search_api_disable() {
  424. $types = array();
  425. foreach (search_api_index_load_multiple(FALSE) as $index) {
  426. $types[$index->item_type][] = $index;
  427. }
  428. foreach ($types as $type => $indexes) {
  429. try {
  430. $controller = search_api_get_datasource_controller($type);
  431. $controller->stopTracking($indexes);
  432. }
  433. catch (SearchApiException $e) {
  434. // Modules defining entity or item types might have been disabled. Ignore.
  435. }
  436. }
  437. }
  438. /**
  439. * Implements hook_uninstall().
  440. */
  441. function search_api_uninstall() {
  442. variable_del('search_api_index_worker_callback_runtime');
  443. }
  444. /**
  445. * Update function that adds the machine names for servers and indexes.
  446. */
  447. function search_api_update_7101() {
  448. $tx = db_transaction();
  449. try {
  450. // Servers
  451. $spec = array(
  452. 'description' => 'The machine name for a server.',
  453. 'type' => 'varchar',
  454. 'length' => 50,
  455. 'not null' => TRUE,
  456. 'default' => '',
  457. );
  458. db_add_field('search_api_server', 'machine_name', $spec);
  459. $names = array();
  460. $servers = db_select('search_api_server', 's')
  461. ->fields('s')
  462. ->execute();
  463. foreach ($servers as $server) {
  464. $base = $name = drupal_strtolower(preg_replace('/[^a-z0-9]+/i', '_', $server->name));
  465. $i = 0;
  466. while (isset($names[$name])) {
  467. $name = $base . '_' . ++$i;
  468. }
  469. $names[$name] = TRUE;
  470. db_update('search_api_server')
  471. ->fields(array('machine_name' => $name))
  472. ->condition('id', $server->id)
  473. ->execute();
  474. }
  475. db_add_unique_key('search_api_server', 'machine_name', array('machine_name'));
  476. //Indexes
  477. $spec = array(
  478. 'description' => 'The machine name of the index.',
  479. 'type' => 'varchar',
  480. 'length' => 50,
  481. 'not null' => TRUE,
  482. 'default' => '',
  483. );
  484. db_add_field('search_api_index', 'machine_name', $spec);
  485. $names = array();
  486. $indexes = db_select('search_api_index', 'i')
  487. ->fields('i')
  488. ->execute();
  489. foreach ($indexes as $index) {
  490. $base = $name = drupal_strtolower(preg_replace('/[^a-z0-9]+/i', '_', $index->name));
  491. $i = 0;
  492. while (isset($names[$name])) {
  493. $name = $base . '_' . ++$i;
  494. }
  495. $names[$name] = TRUE;
  496. db_update('search_api_index')
  497. ->fields(array('machine_name' => $name))
  498. ->condition('id', $index->id)
  499. ->execute();
  500. }
  501. db_add_unique_key('search_api_index', 'machine_name', array('machine_name'));
  502. }
  503. catch (Exception $e) {
  504. $tx->rollback();
  505. try {
  506. db_drop_field('search_api_server', 'machine_name');
  507. db_drop_field('search_api_index', 'machine_name');
  508. }
  509. catch (Exception $e1) {
  510. // Ignore.
  511. }
  512. throw new DrupalUpdateException(t('An exception occurred during the update: @msg.', array('@msg' => $e->getMessage())));
  513. }
  514. }
  515. /**
  516. * Update replacing IDs with machine names for foreign keys.
  517. * {search_api_index}.server and {search_api_item}.index_id are altered.
  518. */
  519. function search_api_update_7102() {
  520. // Update of search_api_index:
  521. $indexes = array();
  522. $select = db_select('search_api_index', 'i')->fields('i');
  523. foreach ($select->execute() as $index) {
  524. $indexes[$index->id] = $index;
  525. }
  526. $servers = db_select('search_api_server', 's')->fields('s', array('id', 'machine_name'))->execute()->fetchAllKeyed();
  527. db_drop_index('search_api_index', 'server');
  528. db_drop_field('search_api_index', 'server');
  529. $spec = array(
  530. 'description' => 'The {search_api_server}.machine_name with which data should be indexed.',
  531. 'type' => 'varchar',
  532. 'length' => 50,
  533. 'not null' => FALSE,
  534. );
  535. db_add_field('search_api_index', 'server', $spec);
  536. foreach ($indexes as $index) {
  537. db_update('search_api_index')
  538. ->fields(array('server' => $servers[$index->server]))
  539. ->condition('id', $index->id)
  540. ->execute();
  541. }
  542. db_add_index('search_api_index', 'server', array('server'));
  543. // Update of search_api_item:
  544. db_drop_index('search_api_item', 'indexing');
  545. db_drop_primary_key('search_api_item');
  546. $spec = array(
  547. 'description' => 'The {search_api_index}.machine_name this item belongs to.',
  548. 'type' => 'varchar',
  549. 'length' => 50,
  550. 'not null' => TRUE,
  551. );
  552. $keys_new = array(
  553. 'indexes' => array(
  554. 'indexing' => array('index_id', 'changed'),
  555. ),
  556. 'primary key' => array('item_id', 'index_id'),
  557. );
  558. db_change_field('search_api_item', 'index_id', 'index_id', $spec, $keys_new);
  559. foreach ($indexes as $index) {
  560. // We explicitly forbid numeric machine names, therefore we don't have to
  561. // worry about conflicts here.
  562. db_update('search_api_item')
  563. ->fields(array(
  564. 'index_id' => $index->machine_name,
  565. ))
  566. ->condition('index_id', $index->id)
  567. ->execute();
  568. }
  569. }
  570. /**
  571. * Add the database fields newly required for entities by the Entity API.
  572. */
  573. function search_api_update_7103() {
  574. if (!function_exists('entity_exportable_schema_fields')) {
  575. throw new DrupalUpdateException(t('Please update the Entity API module first.'));
  576. }
  577. foreach (array('search_api_server', 'search_api_index') as $table) {
  578. foreach (entity_exportable_schema_fields() as $field => $spec) {
  579. db_add_field($table, $field, $spec);
  580. }
  581. }
  582. }
  583. /**
  584. * Initialize the "Fields to run on" settings for processors.
  585. */
  586. function search_api_update_7107() {
  587. $rows = db_select('search_api_index', 'i')
  588. ->fields('i', array('id', 'options'))
  589. ->execute()
  590. ->fetchAllKeyed();
  591. foreach ($rows as $id => $options) {
  592. $opt = unserialize($options);
  593. $processors = &$opt['processors'];
  594. // Only update our own processors, don't mess with others.
  595. $check_processors = array(
  596. 'search_api_case_ignore' => 1,
  597. 'search_api_html_filter' => 1,
  598. 'search_api_tokenizer' => 1,
  599. );
  600. foreach (array_intersect_key($processors, $check_processors) as $name => $info) {
  601. $types = array('text');
  602. if (!empty($info['settings']['strings'])) {
  603. $types[] = 'string';
  604. unset($processors[$name]['settings']['strings']);
  605. }
  606. foreach ($opt['fields'] as $field => $info) {
  607. if ($info['indexed'] && search_api_is_text_type($info['type'], $types)) {
  608. $processors[$name]['settings']['fields'][$field] = $field;
  609. }
  610. }
  611. }
  612. $opt = serialize($opt);
  613. if ($opt != $options) {
  614. db_update('search_api_index')
  615. ->fields(array(
  616. 'options' => $opt,
  617. ))
  618. ->condition('id', $id)
  619. ->execute();
  620. }
  621. }
  622. }
  623. /**
  624. * Change {search_api_item}.index_id back to the index' numeric ID.
  625. */
  626. function search_api_update_7104() {
  627. $select = db_select('search_api_index', 'i')->fields('i');
  628. foreach ($select->execute() as $index) {
  629. // We explicitly forbid numeric machine names, therefore we don't have to
  630. // worry about conflicts here.
  631. db_update('search_api_item')
  632. ->fields(array(
  633. 'index_id' => $index->id,
  634. ))
  635. ->condition('index_id', $index->machine_name)
  636. ->execute();
  637. }
  638. // Update primary key and index.
  639. db_drop_index('search_api_item', 'indexing');
  640. db_drop_primary_key('search_api_item');
  641. $spec = array(
  642. 'description' => 'The {search_api_index}.id this item belongs to.',
  643. 'type' => 'int',
  644. 'unsigned' => TRUE,
  645. 'not null' => TRUE,
  646. );
  647. $keys_new = array(
  648. 'indexes' => array(
  649. 'indexing' => array('index_id', 'changed'),
  650. ),
  651. 'primary key' => array('item_id', 'index_id'),
  652. );
  653. db_change_field('search_api_item', 'index_id', 'index_id', $spec, $keys_new);
  654. }
  655. /**
  656. * Remove all empty aggregated fields for the search_api_alter_add_fulltext data
  657. * alterations.
  658. */
  659. function search_api_update_7105() {
  660. $rows = db_select('search_api_index', 'i')
  661. ->fields('i', array('id', 'options'))
  662. ->execute()
  663. ->fetchAllKeyed();
  664. foreach ($rows as $id => $options) {
  665. $opt = unserialize($options);
  666. if (isset($opt['data_alter_callbacks']['search_api_alter_add_fulltext']['settings']['fields'])) {
  667. foreach ($opt['data_alter_callbacks']['search_api_alter_add_fulltext']['settings']['fields'] as $name => $field) {
  668. if (empty($field['name']) || empty($field['fields'])) {
  669. unset($opt['data_alter_callbacks']['search_api_alter_add_fulltext']['settings']['fields'][$name]);
  670. }
  671. }
  672. }
  673. $opt = serialize($opt);
  674. if ($opt != $options) {
  675. db_update('search_api_index')
  676. ->fields(array(
  677. 'options' => $opt,
  678. ))
  679. ->condition('id', $id)
  680. ->execute();
  681. }
  682. }
  683. }
  684. /**
  685. * Update the settings for the "Aggregated fields" data alteration.
  686. */
  687. function search_api_update_7106() {
  688. $rows = db_select('search_api_index', 'i')
  689. ->fields('i')
  690. ->execute()
  691. ->fetchAll();
  692. foreach ($rows as $row) {
  693. $opt = unserialize($row->options);
  694. $callbacks = &$opt['data_alter_callbacks'];
  695. if (isset($callbacks['search_api_alter_add_fulltext'])) {
  696. $callbacks['search_api_alter_add_aggregation'] = $callbacks['search_api_alter_add_fulltext'];
  697. unset($callbacks['search_api_alter_add_fulltext']);
  698. if (!empty($callbacks['search_api_alter_add_aggregation']['settings']['fields'])) {
  699. foreach ($callbacks['search_api_alter_add_aggregation']['settings']['fields'] as &$info) {
  700. if (!isset($info['type'])) {
  701. $info['type'] = 'fulltext';
  702. }
  703. }
  704. }
  705. }
  706. $opt = serialize($opt);
  707. if ($opt != $row->options) {
  708. // Mark the entity as overridden, in case it has been defined in code
  709. // only.
  710. $row->status |= 0x01;
  711. db_update('search_api_index')
  712. ->fields(array(
  713. 'options' => $opt,
  714. 'status' => $row->status,
  715. ))
  716. ->condition('id', $row->id)
  717. ->execute();
  718. }
  719. }
  720. }
  721. /**
  722. * Add "read only" property to Search API index entities.
  723. */
  724. function search_api_update_7108() {
  725. $db_field = array(
  726. 'description' => 'A flag indicating whether to write to this index.',
  727. 'type' => 'int',
  728. 'size' => 'tiny',
  729. 'not null' => TRUE,
  730. 'default' => 0,
  731. );
  732. db_add_field('search_api_index', 'read_only', $db_field);
  733. return t('Added a "read only" property to index entities.');
  734. }
  735. /**
  736. * Clear entity info cache, as entity controller classes hae changed.
  737. */
  738. function search_api_update_7109() {
  739. cache_clear_all('entity_info:', 'cache', TRUE);
  740. }
  741. /**
  742. * Rename the "entity_type" field to "item_type" in the {search_api_index} table.
  743. */
  744. function search_api_update_7110() {
  745. $table = 'search_api_index';
  746. // This index isn't used anymore.
  747. db_drop_index($table, 'entity_type');
  748. // Rename the "item_type" field (and change the description).
  749. $item_type = array(
  750. 'description' => 'The type of items stored in this index.',
  751. 'type' => 'varchar',
  752. 'length' => 50,
  753. 'not null' => TRUE,
  754. );
  755. // Also add the new "item_type" index, while we're at it.
  756. $keys_new['indexes']['item_type'] = array('item_type');
  757. db_change_field($table, 'entity_type', 'item_type', $item_type, $keys_new);
  758. // Mark all indexes in code as "OVERRIDDEN".
  759. db_update($table)
  760. ->fields(array(
  761. 'status' => 0x03,
  762. ))
  763. ->condition('status', 0x02)
  764. ->execute();
  765. // Clear entity info caches.
  766. cache_clear_all('*', 'cache', TRUE);
  767. }
  768. /**
  769. * Change the definition of the {search_api_item}.changed field.
  770. */
  771. function search_api_update_7111() {
  772. $spec = array(
  773. 'description' => 'Either a flag or a timestamp to indicate if or when the item was changed since it was last indexed.',
  774. 'type' => 'int',
  775. 'size' => 'big',
  776. 'not null' => TRUE,
  777. 'default' => 1,
  778. );
  779. db_change_field('search_api_item', 'changed', 'changed', $spec);
  780. }
  781. /**
  782. * Changes the size of the {search_api_index}.options and {search_api_server}.options fields to "medium".
  783. */
  784. function search_api_update_7112() {
  785. $spec = array(
  786. 'description' => 'The options used to configure the service object.',
  787. 'type' => 'text',
  788. 'size' => 'medium',
  789. 'serialize' => TRUE,
  790. 'not null' => TRUE,
  791. );
  792. db_change_field('search_api_server', 'options', 'options', $spec);
  793. $spec = array(
  794. 'description' => 'An array of additional arguments configuring this index.',
  795. 'type' => 'text',
  796. 'size' => 'medium',
  797. 'serialize' => TRUE,
  798. 'not null' => TRUE,
  799. );
  800. db_change_field('search_api_index', 'options', 'options', $spec);
  801. }
  802. /**
  803. * Removes superfluous data from the stored index options.
  804. */
  805. function search_api_update_7113() {
  806. $indexes = db_select('search_api_index', 'i')
  807. ->fields('i')
  808. ->execute();
  809. foreach ($indexes as $index) {
  810. $options = unserialize($index->options);
  811. // Weed out fields settings.
  812. if (!empty($options['fields'])) {
  813. foreach ($options['fields'] as $key => $field) {
  814. if (isset($field['indexed']) && !$field['indexed']) {
  815. unset($options['fields'][$key]);
  816. continue;
  817. }
  818. unset($options['fields'][$key]['name'], $options['fields'][$key]['indexed']);
  819. if (isset($field['boost']) && $field['boost'] == '1.0') {
  820. unset($options['fields'][$key]['boost']);
  821. }
  822. }
  823. }
  824. // Weed out processor settings.
  825. if (!empty($options['processors'])) {
  826. // Only weed out settings for our own processors.
  827. $processors = array('search_api_case_ignore', 'search_api_html_filter', 'search_api_tokenizer', 'search_api_stopwords');
  828. foreach ($processors as $key) {
  829. if (empty($options['processors'][$key])) {
  830. continue;
  831. }
  832. $processor = $options['processors'][$key];
  833. if (empty($processor['settings']['fields'])) {
  834. continue;
  835. }
  836. $fields = array_filter($processor['settings']['fields']);
  837. if ($fields) {
  838. $fields = array_combine($fields, array_fill(0, count($fields), TRUE));
  839. }
  840. $options['processors'][$key]['settings']['fields'] = $fields;
  841. }
  842. }
  843. // Weed out settings for the „Aggregated fields“ data alteration.
  844. if (!empty($options['data_alter_callbacks']['search_api_alter_add_aggregation']['settings']['fields'])) {
  845. unset($options['data_alter_callbacks']['search_api_alter_add_aggregation']['settings']['actions']);
  846. $aggregated_fields = &$options['data_alter_callbacks']['search_api_alter_add_aggregation']['settings']['fields'];
  847. foreach ($aggregated_fields as $key => $field) {
  848. unset($aggregated_fields[$key]['actions']);
  849. if (!empty($field['fields'])) {
  850. $aggregated_fields[$key]['fields'] = array_values(array_filter($field['fields']));
  851. }
  852. }
  853. }
  854. $options = serialize($options);
  855. if ($options != $index->options) {
  856. // Mark the entity as overridden, in case it has been defined in code
  857. // only.
  858. $index->status |= 0x01;
  859. db_update('search_api_index')
  860. ->fields(array(
  861. 'options' => $options,
  862. 'status' => $index->status,
  863. ))
  864. ->condition('id', $index->id)
  865. ->execute();
  866. }
  867. }
  868. }
  869. /**
  870. * Sanitize watchdog messages.
  871. */
  872. function search_api_update_7114() {
  873. if (db_table_exists('watchdog')) {
  874. try {
  875. $entries = db_select('watchdog', 'w')
  876. ->fields('w', array('wid', 'message'))
  877. ->condition('type', 'search_api')
  878. ->execute();
  879. foreach ($entries as $entry) {
  880. db_update('watchdog')
  881. ->fields(array(
  882. 'message' => check_plain($entry->message),
  883. ))
  884. ->condition('wid', $entry->wid)
  885. ->execute();
  886. }
  887. }
  888. catch (Exception $e) {
  889. throw new DrupalUpdateException(t('An exception occurred during the update: @msg.', array('@msg' => $e->getMessage())));
  890. }
  891. }
  892. }
  893. /**
  894. * Switch to indexing without the use of a cron queue.
  895. */
  896. function search_api_update_7115() {
  897. variable_del('search_api_batch_per_cron');
  898. DrupalQueue::get('search_api_indexing_queue')->deleteQueue();
  899. db_update('search_api_item')
  900. ->fields(array(
  901. 'changed' => 1,
  902. ))
  903. ->condition('changed', 0, '<')
  904. ->execute();
  905. }
  906. /**
  907. * Transfers the tasks for disabled servers to a separate database table.
  908. */
  909. function search_api_update_7116() {
  910. // Create table.
  911. $table = array(
  912. 'description' => 'Stores pending tasks for servers.',
  913. 'fields' => array(
  914. 'id' => array(
  915. 'description' => 'An integer identifying this task.',
  916. 'type' => 'serial',
  917. 'unsigned' => TRUE,
  918. 'not null' => TRUE,
  919. ),
  920. 'server_id' => array(
  921. 'description' => 'The {search_api_server}.machine_name for which this task should be executed.',
  922. 'type' => 'varchar',
  923. 'length' => 50,
  924. 'not null' => TRUE,
  925. ),
  926. 'type' => array(
  927. 'description' => 'A keyword identifying the type of task that should be executed.',
  928. 'type' => 'varchar',
  929. 'length' => 50,
  930. 'not null' => TRUE,
  931. ),
  932. 'index_id' => array(
  933. 'description' => 'The {search_api_index}.machine_name to which this task pertains, if applicable for this type.',
  934. 'type' => 'varchar',
  935. 'length' => 50,
  936. 'not null' => FALSE,
  937. ),
  938. 'data' => array(
  939. 'description' => 'Some data needed for the task, might be optional depending on the type.',
  940. 'type' => 'text',
  941. 'size' => 'medium',
  942. 'serialize' => TRUE,
  943. 'not null' => FALSE,
  944. ),
  945. ),
  946. 'indexes' => array(
  947. 'server' => array('server_id'),
  948. ),
  949. 'primary key' => array('id'),
  950. );
  951. db_create_table('search_api_task', $table);
  952. // Collect old tasks.
  953. $tasks = array();
  954. foreach (variable_get('search_api_tasks', array()) as $server => $indexes) {
  955. foreach ($indexes as $index => $old_tasks) {
  956. if (in_array('clear all', $old_tasks)) {
  957. $tasks[] = array(
  958. 'server_id' => $server,
  959. 'type' => 'deleteItems',
  960. );
  961. }
  962. if (in_array('remove', $old_tasks)) {
  963. $tasks[] = array(
  964. 'server_id' => $server,
  965. 'type' => 'removeIndex',
  966. 'index_id' => $index,
  967. );
  968. }
  969. }
  970. }
  971. variable_del('search_api_tasks');
  972. $select = db_select('search_api_index', 'i')
  973. ->fields('i', array('machine_name', 'server'));
  974. $select->innerJoin('search_api_server', 's', 'i.server = s.machine_name AND s.enabled = 0');
  975. $index_ids = array();
  976. foreach ($select->execute() as $index) {
  977. $index_ids[] = $index->machine_name;
  978. $tasks[] = array(
  979. 'server_id' => $index->server,
  980. 'type' => 'removeIndex',
  981. 'index_id' => $index->machine_name,
  982. );
  983. }
  984. if ($index_ids) {
  985. db_update('search_api_index')
  986. ->fields(array(
  987. 'enabled' => 0,
  988. 'server' => NULL,
  989. ))
  990. ->condition('machine_name', $index_ids)
  991. ->execute();
  992. }
  993. if ($tasks) {
  994. $insert = db_insert('search_api_task')
  995. ->fields(array('server_id', 'type', 'index_id', 'data'));
  996. foreach ($tasks as $task) {
  997. $task += array(
  998. 'index_id' => NULL,
  999. 'data' => NULL,
  1000. );
  1001. $insert->values($task);
  1002. }
  1003. $insert->execute();
  1004. }
  1005. }
  1006. /**
  1007. * Checks the database for illegal {search_api_index}.server values.
  1008. */
  1009. function search_api_update_7117() {
  1010. $servers = db_select('search_api_server', 's')
  1011. ->fields('s', array('machine_name'))
  1012. ->condition('enabled', 1);
  1013. $indexes = db_select('search_api_index', 'i')
  1014. ->fields('i', array('id'))
  1015. ->condition('server', $servers, 'NOT IN')
  1016. ->execute()
  1017. ->fetchCol();
  1018. if ($indexes) {
  1019. db_delete('search_api_item')
  1020. ->condition('index_id', $indexes)
  1021. ->execute();
  1022. db_update('search_api_index')
  1023. ->fields(array(
  1024. 'server' => NULL,
  1025. 'enabled' => 0,
  1026. ))
  1027. ->condition('id', $indexes)
  1028. ->execute();
  1029. }
  1030. }
  1031. /**
  1032. * Adds the {search_api_item_string_id} table for items with string IDs.
  1033. */
  1034. function search_api_update_7118() {
  1035. // Some users have reported that the table already existed for them, for
  1036. // whatever reason. Therefore, just bail if the table already exists, assuming
  1037. // it already looks as expected.
  1038. if (db_table_exists('search_api_item_string_id')) {
  1039. return;
  1040. }
  1041. $table = array(
  1042. 'description' => 'Stores the items which should be indexed for each index, and their status. Used only for items with string IDs.',
  1043. 'fields' => array(
  1044. 'item_id' => array(
  1045. 'description' => "The item's ID.",
  1046. 'type' => 'varchar',
  1047. 'length' => 64,
  1048. 'not null' => TRUE,
  1049. ),
  1050. 'index_id' => array(
  1051. 'description' => 'The {search_api_index}.id this item belongs to.',
  1052. 'type' => 'int',
  1053. 'unsigned' => TRUE,
  1054. 'not null' => TRUE,
  1055. ),
  1056. 'changed' => array(
  1057. 'description' => 'Either a flag or a timestamp to indicate if or when the item was changed since it was last indexed.',
  1058. 'type' => 'int',
  1059. 'size' => 'big',
  1060. 'not null' => TRUE,
  1061. 'default' => 1,
  1062. ),
  1063. ),
  1064. 'indexes' => array(
  1065. 'indexing' => array('index_id', 'changed'),
  1066. ),
  1067. 'primary key' => array('item_id', 'index_id'),
  1068. );
  1069. db_create_table('search_api_item_string_id', $table);
  1070. }