search_api.install 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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('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. $table = array(
  996. 'description' => 'Stores the items which should be indexed for each index, and their status. Used only for items with string IDs.',
  997. 'fields' => array(
  998. 'item_id' => array(
  999. 'description' => "The item's ID.",
  1000. 'type' => 'varchar',
  1001. 'length' => 64,
  1002. 'not null' => TRUE,
  1003. ),
  1004. 'index_id' => array(
  1005. 'description' => 'The {search_api_index}.id this item belongs to.',
  1006. 'type' => 'int',
  1007. 'unsigned' => TRUE,
  1008. 'not null' => TRUE,
  1009. ),
  1010. 'changed' => array(
  1011. 'description' => 'Either a flag or a timestamp to indicate if or when the item was changed since it was last indexed.',
  1012. 'type' => 'int',
  1013. 'size' => 'big',
  1014. 'not null' => TRUE,
  1015. 'default' => 1,
  1016. ),
  1017. ),
  1018. 'indexes' => array(
  1019. 'indexing' => array('index_id', 'changed'),
  1020. ),
  1021. 'primary key' => array('item_id', 'index_id'),
  1022. );
  1023. db_create_table('search_api_item_string_id', $table);
  1024. }