search_api.install 32 KB

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