search_api.install 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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. return $schema;
  190. }
  191. /**
  192. * Implements hook_install().
  193. *
  194. * Creates a default node index if the module is installed manually.
  195. */
  196. function search_api_install() {
  197. // In case the module is installed via an installation profile, a batch is
  198. // active and we skip that.
  199. if (batch_get()) {
  200. return;
  201. }
  202. $name = t('Default node index');
  203. $values = array(
  204. 'name' => $name,
  205. 'machine_name' => preg_replace('/[^a-z0-9]+/', '_', drupal_strtolower($name)),
  206. 'description' => t('An automatically created search index for indexing node data. Might be configured to specific needs.'),
  207. 'server' => NULL,
  208. 'item_type' => 'node',
  209. 'options' => array(
  210. 'index_directly' => 1,
  211. 'cron_limit' => '50',
  212. 'data_alter_callbacks' => array(
  213. 'search_api_alter_node_access' => array(
  214. 'status' => 1,
  215. 'weight' => '0',
  216. 'settings' => array(),
  217. ),
  218. ),
  219. 'processors' => array(
  220. 'search_api_case_ignore' => array(
  221. 'status' => 1,
  222. 'weight' => '0',
  223. 'settings' => array(
  224. 'strings' => 0,
  225. ),
  226. ),
  227. 'search_api_html_filter' => array(
  228. 'status' => 1,
  229. 'weight' => '10',
  230. 'settings' => array(
  231. 'title' => 0,
  232. 'alt' => 1,
  233. 'tags' => "h1 = 5\n" .
  234. "h2 = 3\n" .
  235. "h3 = 2\n" .
  236. "strong = 2\n" .
  237. "b = 2\n" .
  238. "em = 1.5\n" .
  239. "u = 1.5",
  240. ),
  241. ),
  242. 'search_api_tokenizer' => array(
  243. 'status' => 1,
  244. 'weight' => '20',
  245. 'settings' => array(
  246. 'spaces' => '[^\\p{L}\\p{N}]',
  247. 'ignorable' => '[-]',
  248. ),
  249. ),
  250. ),
  251. 'fields' => array(
  252. 'type' => array(
  253. 'type' => 'string',
  254. ),
  255. 'title' => array(
  256. 'type' => 'text',
  257. 'boost' => '5.0',
  258. ),
  259. 'promote' => array(
  260. 'type' => 'boolean',
  261. ),
  262. 'sticky' => array(
  263. 'type' => 'boolean',
  264. ),
  265. 'created' => array(
  266. 'type' => 'date',
  267. ),
  268. 'changed' => array(
  269. 'type' => 'date',
  270. ),
  271. 'author' => array(
  272. 'type' => 'integer',
  273. 'entity_type' => 'user',
  274. ),
  275. 'comment_count' => array(
  276. 'type' => 'integer',
  277. ),
  278. 'search_api_language' => array(
  279. 'type' => 'string',
  280. ),
  281. 'body:value' => array(
  282. 'type' => 'text',
  283. ),
  284. ),
  285. ),
  286. );
  287. search_api_index_insert($values);
  288. drupal_set_message('The Search API module was installed. A new default node index was created.');
  289. }
  290. /**
  291. * Implements hook_enable().
  292. *
  293. * Mark all items as "dirty", since we can't know whether they are.
  294. */
  295. function search_api_enable() {
  296. $types = array();
  297. foreach (search_api_index_load_multiple(FALSE) as $index) {
  298. if ($index->enabled) {
  299. $types[$index->item_type][] = $index;
  300. }
  301. }
  302. foreach ($types as $type => $indexes) {
  303. $controller = search_api_get_datasource_controller($type);
  304. $controller->startTracking($indexes);
  305. }
  306. }
  307. /**
  308. * Implements hook_disable().
  309. */
  310. function search_api_disable() {
  311. $types = array();
  312. foreach (search_api_index_load_multiple(FALSE) as $index) {
  313. $types[$index->item_type][] = $index;
  314. }
  315. foreach ($types as $type => $indexes) {
  316. try {
  317. $controller = search_api_get_datasource_controller($type);
  318. $controller->stopTracking($indexes);
  319. }
  320. catch (SearchApiException $e) {
  321. // Modules defining entity or item types might have been disabled. Ignore.
  322. }
  323. }
  324. DrupalQueue::get('search_api_indexing_queue')->deleteQueue();
  325. }
  326. /**
  327. * Implements hook_uninstall().
  328. */
  329. function search_api_uninstall() {
  330. variable_del('search_api_tasks');
  331. variable_del('search_api_index_worker_callback_runtime');
  332. }
  333. /**
  334. * Update function that adds the machine names for servers and indexes.
  335. */
  336. function search_api_update_7101() {
  337. $tx = db_transaction();
  338. try {
  339. // Servers
  340. $spec = array(
  341. 'description' => 'The machine name for a server.',
  342. 'type' => 'varchar',
  343. 'length' => 50,
  344. 'not null' => TRUE,
  345. 'default' => '',
  346. );
  347. db_add_field('search_api_server', 'machine_name', $spec);
  348. $names = array();
  349. $servers = db_select('search_api_server', 's')
  350. ->fields('s')
  351. ->execute();
  352. foreach ($servers as $server) {
  353. $base = $name = drupal_strtolower(preg_replace('/[^a-z0-9]+/i', '_', $server->name));
  354. $i = 0;
  355. while (isset($names[$name])) {
  356. $name = $base . '_' . ++$i;
  357. }
  358. $names[$name] = TRUE;
  359. db_update('search_api_server')
  360. ->fields(array('machine_name' => $name))
  361. ->condition('id', $server->id)
  362. ->execute();
  363. }
  364. db_add_unique_key('search_api_server', 'machine_name', array('machine_name'));
  365. //Indexes
  366. $spec = array(
  367. 'description' => 'The machine name of the index.',
  368. 'type' => 'varchar',
  369. 'length' => 50,
  370. 'not null' => TRUE,
  371. 'default' => '',
  372. );
  373. db_add_field('search_api_index', 'machine_name', $spec);
  374. $names = array();
  375. $indexes = db_select('search_api_index', 'i')
  376. ->fields('i')
  377. ->execute();
  378. foreach ($indexes as $index) {
  379. $base = $name = drupal_strtolower(preg_replace('/[^a-z0-9]+/i', '_', $index->name));
  380. $i = 0;
  381. while (isset($names[$name])) {
  382. $name = $base . '_' . ++$i;
  383. }
  384. $names[$name] = TRUE;
  385. db_update('search_api_index')
  386. ->fields(array('machine_name' => $name))
  387. ->condition('id', $index->id)
  388. ->execute();
  389. }
  390. db_add_unique_key('search_api_index', 'machine_name', array('machine_name'));
  391. }
  392. catch (Exception $e) {
  393. $tx->rollback();
  394. try {
  395. db_drop_field('search_api_server', 'machine_name');
  396. db_drop_field('search_api_index', 'machine_name');
  397. }
  398. catch (Exception $e1) {
  399. // Ignore.
  400. }
  401. throw new DrupalUpdateException(t('An exception occurred during the update: @msg.', array('@msg' => $e->getMessage())));
  402. }
  403. }
  404. /**
  405. * Update replacing IDs with machine names for foreign keys.
  406. * {search_api_index}.server and {search_api_item}.index_id are altered.
  407. */
  408. function search_api_update_7102() {
  409. // Update of search_api_index:
  410. $indexes = array();
  411. $select = db_select('search_api_index', 'i')->fields('i');
  412. foreach ($select->execute() as $index) {
  413. $indexes[$index->id] = $index;
  414. }
  415. $servers = db_select('search_api_server', 's')->fields('s', array('id', 'machine_name'))->execute()->fetchAllKeyed();
  416. db_drop_index('search_api_index', 'server');
  417. db_drop_field('search_api_index', 'server');
  418. $spec = array(
  419. 'description' => 'The {search_api_server}.machine_name with which data should be indexed.',
  420. 'type' => 'varchar',
  421. 'length' => 50,
  422. 'not null' => FALSE,
  423. );
  424. db_add_field('search_api_index', 'server', $spec);
  425. foreach ($indexes as $index) {
  426. db_update('search_api_index')
  427. ->fields(array('server' => $servers[$index->server]))
  428. ->condition('id', $index->id)
  429. ->execute();
  430. }
  431. db_add_index('search_api_index', 'server', array('server'));
  432. // Update of search_api_item:
  433. db_drop_index('search_api_item', 'indexing');
  434. db_drop_primary_key('search_api_item');
  435. $spec = array(
  436. 'description' => 'The {search_api_index}.machine_name this item belongs to.',
  437. 'type' => 'varchar',
  438. 'length' => 50,
  439. 'not null' => TRUE,
  440. );
  441. $keys_new = array(
  442. 'indexes' => array(
  443. 'indexing' => array('index_id', 'changed'),
  444. ),
  445. 'primary key' => array('item_id', 'index_id'),
  446. );
  447. db_change_field('search_api_item', 'index_id', 'index_id', $spec, $keys_new);
  448. foreach ($indexes as $index) {
  449. // We explicitly forbid numeric machine names, therefore we don't have to
  450. // worry about conflicts here.
  451. db_update('search_api_item')
  452. ->fields(array(
  453. 'index_id' => $index->machine_name,
  454. ))
  455. ->condition('index_id', $index->id)
  456. ->execute();
  457. }
  458. }
  459. /**
  460. * Add the database fields newly required for entities by the Entity API.
  461. */
  462. function search_api_update_7103() {
  463. if (!function_exists('entity_exportable_schema_fields')) {
  464. throw new DrupalUpdateException(t('Please update the Entity API module first.'));
  465. }
  466. foreach (array('search_api_server', 'search_api_index') as $table) {
  467. foreach (entity_exportable_schema_fields() as $field => $spec) {
  468. db_add_field($table, $field, $spec);
  469. }
  470. }
  471. }
  472. /**
  473. * Initialize the "Fields to run on" settings for processors.
  474. */
  475. function search_api_update_7107() {
  476. $rows = db_select('search_api_index', 'i')
  477. ->fields('i', array('id', 'options'))
  478. ->execute()
  479. ->fetchAllKeyed();
  480. foreach ($rows as $id => $options) {
  481. $opt = unserialize($options);
  482. $processors = &$opt['processors'];
  483. // Only update our own processors, don't mess with others.
  484. $check_processors = array(
  485. 'search_api_case_ignore' => 1,
  486. 'search_api_html_filter' => 1,
  487. 'search_api_tokenizer' => 1,
  488. );
  489. foreach (array_intersect_key($processors, $check_processors) as $name => $info) {
  490. $types = array('text');
  491. if (!empty($info['settings']['strings'])) {
  492. $types[] = 'string';
  493. unset($processors[$name]['settings']['strings']);
  494. }
  495. foreach ($opt['fields'] as $field => $info) {
  496. if ($info['indexed'] && search_api_is_text_type($info['type'], $types)) {
  497. $processors[$name]['settings']['fields'][$field] = $field;
  498. }
  499. }
  500. }
  501. $opt = serialize($opt);
  502. if ($opt != $options) {
  503. db_update('search_api_index')
  504. ->fields(array(
  505. 'options' => $opt,
  506. ))
  507. ->condition('id', $id)
  508. ->execute();
  509. }
  510. }
  511. }
  512. /**
  513. * Change {search_api_item}.index_id back to the index' numeric ID.
  514. */
  515. function search_api_update_7104() {
  516. $select = db_select('search_api_index', 'i')->fields('i');
  517. foreach ($select->execute() as $index) {
  518. // We explicitly forbid numeric machine names, therefore we don't have to
  519. // worry about conflicts here.
  520. db_update('search_api_item')
  521. ->fields(array(
  522. 'index_id' => $index->id,
  523. ))
  524. ->condition('index_id', $index->machine_name)
  525. ->execute();
  526. }
  527. // Update primary key and index.
  528. db_drop_index('search_api_item', 'indexing');
  529. db_drop_primary_key('search_api_item');
  530. $spec = array(
  531. 'description' => 'The {search_api_index}.id this item belongs to.',
  532. 'type' => 'int',
  533. 'unsigned' => TRUE,
  534. 'not null' => TRUE,
  535. );
  536. $keys_new = array(
  537. 'indexes' => array(
  538. 'indexing' => array('index_id', 'changed'),
  539. ),
  540. 'primary key' => array('item_id', 'index_id'),
  541. );
  542. db_change_field('search_api_item', 'index_id', 'index_id', $spec, $keys_new);
  543. }
  544. /**
  545. * Remove all empty aggregated fields for the search_api_alter_add_fulltext data
  546. * alterations.
  547. */
  548. function search_api_update_7105() {
  549. $rows = db_select('search_api_index', 'i')
  550. ->fields('i', array('id', 'options'))
  551. ->execute()
  552. ->fetchAllKeyed();
  553. foreach ($rows as $id => $options) {
  554. $opt = unserialize($options);
  555. if (isset($opt['data_alter_callbacks']['search_api_alter_add_fulltext']['settings']['fields'])) {
  556. foreach ($opt['data_alter_callbacks']['search_api_alter_add_fulltext']['settings']['fields'] as $name => $field) {
  557. if (empty($field['name']) || empty($field['fields'])) {
  558. unset($opt['data_alter_callbacks']['search_api_alter_add_fulltext']['settings']['fields'][$name]);
  559. }
  560. }
  561. }
  562. $opt = serialize($opt);
  563. if ($opt != $options) {
  564. db_update('search_api_index')
  565. ->fields(array(
  566. 'options' => $opt,
  567. ))
  568. ->condition('id', $id)
  569. ->execute();
  570. }
  571. }
  572. }
  573. /**
  574. * Update the settings for the "Aggregated fields" data alteration.
  575. */
  576. function search_api_update_7106() {
  577. $rows = db_select('search_api_index', 'i')
  578. ->fields('i')
  579. ->execute()
  580. ->fetchAll();
  581. foreach ($rows as $row) {
  582. $opt = unserialize($row->options);
  583. $callbacks = &$opt['data_alter_callbacks'];
  584. if (isset($callbacks['search_api_alter_add_fulltext'])) {
  585. $callbacks['search_api_alter_add_aggregation'] = $callbacks['search_api_alter_add_fulltext'];
  586. unset($callbacks['search_api_alter_add_fulltext']);
  587. if (!empty($callbacks['search_api_alter_add_aggregation']['settings']['fields'])) {
  588. foreach ($callbacks['search_api_alter_add_aggregation']['settings']['fields'] as $field => &$info) {
  589. if (!isset($info['type'])) {
  590. $info['type'] = 'fulltext';
  591. }
  592. }
  593. }
  594. }
  595. $opt = serialize($opt);
  596. if ($opt != $row->options) {
  597. // Mark the entity as overridden, in case it has been defined in code
  598. // only.
  599. $row->status |= 0x01;
  600. db_update('search_api_index')
  601. ->fields(array(
  602. 'options' => $opt,
  603. 'status' => $row->status,
  604. ))
  605. ->condition('id', $row->id)
  606. ->execute();
  607. }
  608. }
  609. }
  610. /**
  611. * Add "read only" property to Search API index entities.
  612. */
  613. function search_api_update_7108() {
  614. $db_field = array(
  615. 'description' => 'A flag indicating whether to write to this index.',
  616. 'type' => 'int',
  617. 'size' => 'tiny',
  618. 'not null' => TRUE,
  619. 'default' => 0,
  620. );
  621. db_add_field('search_api_index', 'read_only', $db_field);
  622. return t('Added a "read only" property to index entities.');
  623. }
  624. /**
  625. * Clear entity info cache, as entity controller classes hae changed.
  626. */
  627. function search_api_update_7109() {
  628. cache_clear_all('entity_info:', 'cache', TRUE);
  629. }
  630. /**
  631. * Rename the "entity_type" field to "item_type" in the {search_api_index} table.
  632. */
  633. function search_api_update_7110() {
  634. $table = 'search_api_index';
  635. // This index isn't used anymore.
  636. db_drop_index($table, 'entity_type');
  637. // Rename the "item_type" field (and change the description).
  638. $item_type = array(
  639. 'description' => 'The type of items stored in this index.',
  640. 'type' => 'varchar',
  641. 'length' => 50,
  642. 'not null' => TRUE,
  643. );
  644. // Also add the new "item_type" index, while we're at it.
  645. $keys_new['indexes']['item_type'] = array('item_type');
  646. db_change_field($table, 'entity_type', 'item_type', $item_type, $keys_new);
  647. // Mark all indexes in code as "OVERRIDDEN".
  648. db_update($table)
  649. ->fields(array(
  650. 'status' => 0x03,
  651. ))
  652. ->condition('status', 0x02)
  653. ->execute();
  654. // Clear entity info caches.
  655. cache_clear_all('*', 'cache', TRUE);
  656. }
  657. /**
  658. * Change the definition of the {search_api_item}.changed field.
  659. */
  660. function search_api_update_7111() {
  661. $spec = array(
  662. 'description' => 'Either a flag or a timestamp to indicate if or when the item was changed since it was last indexed.',
  663. 'type' => 'int',
  664. 'size' => 'big',
  665. 'not null' => TRUE,
  666. 'default' => 1,
  667. );
  668. db_change_field('search_api_item', 'changed', 'changed', $spec);
  669. }
  670. /**
  671. * Changes the size of the {search_api_index}.options and {search_api_server}.options fields to "medium".
  672. */
  673. function search_api_update_7112() {
  674. $spec = array(
  675. 'description' => 'The options used to configure the service object.',
  676. 'type' => 'text',
  677. 'size' => 'medium',
  678. 'serialize' => TRUE,
  679. 'not null' => TRUE,
  680. );
  681. db_change_field('search_api_server', 'options', 'options', $spec);
  682. $spec = array(
  683. 'description' => 'An array of additional arguments configuring this index.',
  684. 'type' => 'text',
  685. 'size' => 'medium',
  686. 'serialize' => TRUE,
  687. 'not null' => TRUE,
  688. );
  689. db_change_field('search_api_index', 'options', 'options', $spec);
  690. }
  691. /**
  692. * Removes superfluous data from the stored index options.
  693. */
  694. function search_api_update_7113() {
  695. $indexes = db_select('search_api_index', 'i')
  696. ->fields('i')
  697. ->execute();
  698. foreach ($indexes as $index) {
  699. $options = unserialize($index->options);
  700. // Weed out fields settings.
  701. if (!empty($options['fields'])) {
  702. foreach ($options['fields'] as $key => $field) {
  703. if (isset($field['indexed']) && !$field['indexed']) {
  704. unset($options['fields'][$key]);
  705. continue;
  706. }
  707. unset($options['fields'][$key]['name'], $options['fields'][$key]['indexed']);
  708. if (isset($field['boost']) && $field['boost'] == '1.0') {
  709. unset($options['fields'][$key]['boost']);
  710. }
  711. }
  712. }
  713. // Weed out processor settings.
  714. if (!empty($options['processors'])) {
  715. // Only weed out settings for our own processors.
  716. $processors = array('search_api_case_ignore', 'search_api_html_filter', 'search_api_tokenizer', 'search_api_stopwords');
  717. foreach ($processors as $key) {
  718. if (empty($options['processors'][$key])) {
  719. continue;
  720. }
  721. $processor = $options['processors'][$key];
  722. if (empty($processor['settings']['fields'])) {
  723. continue;
  724. }
  725. $fields = array_filter($processor['settings']['fields']);
  726. if ($fields) {
  727. $fields = array_combine($fields, array_fill(0, count($fields), TRUE));
  728. }
  729. $options['processors'][$key]['settings']['fields'] = $fields;
  730. }
  731. }
  732. // Weed out settings for the „Aggregated fields“ data alteration.
  733. if (!empty($options['data_alter_callbacks']['search_api_alter_add_aggregation']['settings']['fields'])) {
  734. unset($options['data_alter_callbacks']['search_api_alter_add_aggregation']['settings']['actions']);
  735. $aggregated_fields = &$options['data_alter_callbacks']['search_api_alter_add_aggregation']['settings']['fields'];
  736. foreach ($aggregated_fields as $key => $field) {
  737. unset($aggregated_fields[$key]['actions']);
  738. if (!empty($field['fields'])) {
  739. $aggregated_fields[$key]['fields'] = array_values(array_filter($field['fields']));
  740. }
  741. }
  742. }
  743. $options = serialize($options);
  744. if ($options != $index->options) {
  745. // Mark the entity as overridden, in case it has been defined in code
  746. // only.
  747. $index->status |= 0x01;
  748. db_update('search_api_index')
  749. ->fields(array(
  750. 'options' => $options,
  751. 'status' => $index->status,
  752. ))
  753. ->condition('id', $index->id)
  754. ->execute();
  755. }
  756. }
  757. }
  758. /**
  759. * Sanitize watchdog messages.
  760. */
  761. function search_api_update_7114() {
  762. if (db_table_exists('watchdog')) {
  763. try {
  764. $entries = db_select('watchdog', 'w')
  765. ->fields('w', array('wid', 'message'))
  766. ->condition('type', 'search_api')
  767. ->execute();
  768. foreach ($entries as $entry) {
  769. db_update('watchdog')
  770. ->fields(array(
  771. 'message' => check_plain($entry->message),
  772. ))
  773. ->condition('wid', $entry->wid)
  774. ->execute();
  775. }
  776. }
  777. catch (Exception $e) {
  778. throw new DrupalUpdateException(t('An exception occurred during the update: @msg.', array('@msg' => $e->getMessage())));
  779. }
  780. }
  781. }