search_api.install 29 KB

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