search_api.drush.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. <?php
  2. /**
  3. * @file
  4. * Drush commands for SearchAPI.
  5. *
  6. * Original file by agentrickard for Palantir.net
  7. */
  8. /**
  9. * Implements hook_drush_command().
  10. */
  11. function search_api_drush_command() {
  12. $items = array();
  13. $items['search-api-list'] = array(
  14. 'description' => 'List all search indexes.',
  15. 'examples' => array(
  16. 'drush searchapi-list' => dt('List all search indexes.'),
  17. 'drush sapi-l' => dt('Alias to list all search indexes.'),
  18. ),
  19. 'aliases' => array('sapi-l'),
  20. );
  21. $items['search-api-enable'] = array(
  22. 'description' => 'Enable one or all disabled search_api indexes.',
  23. 'examples' => array(
  24. 'drush searchapi-enable' => dt('Enable all disabled indexes.'),
  25. 'drush sapi-en' => dt('Alias to enable all disabled indexes.'),
  26. 'drush sapi-en 1' => dt('Enable index with the ID !id.', array('!id' => 1)),
  27. ),
  28. 'arguments' => array(
  29. 'index_id' => dt('The numeric ID or machine name of an index to enable.'),
  30. ),
  31. 'aliases' => array('sapi-en'),
  32. );
  33. $items['search-api-disable'] = array(
  34. 'description' => 'Disable one or all enabled search_api indexes.',
  35. 'examples' => array(
  36. 'drush searchapi-disable' => dt('Disable all enabled indexes.'),
  37. 'drush sapi-dis' => dt('Alias to disable all enabled indexes.'),
  38. 'drush sapi-dis 1' => dt('Disable index with the ID !id.', array('!id' => 1)),
  39. ),
  40. 'arguments' => array(
  41. 'index_id' => dt('The numeric ID or machine name of an index to disable.'),
  42. ),
  43. 'aliases' => array('sapi-dis'),
  44. );
  45. $items['search-api-status'] = array(
  46. 'description' => 'Show the status of one or all search indexes.',
  47. 'examples' => array(
  48. 'drush searchapi-status' => dt('Show the status of all search indexes.'),
  49. 'drush sapi-s' => dt('Alias to show the status of all search indexes.'),
  50. 'drush sapi-s 1' => dt('Show the status of the search index with the ID !id.', array('!id' => 1)),
  51. 'drush sapi-s default_node_index' => dt('Show the status of the search index with the machine name !name.', array('!name' => 'default_node_index')),
  52. ),
  53. 'arguments' => array(
  54. 'index_id' => dt('The numeric ID or machine name of an index.'),
  55. ),
  56. 'aliases' => array('sapi-s'),
  57. );
  58. $items['search-api-index'] = array(
  59. 'description' => 'Index items for one or all enabled search_api indexes.',
  60. 'examples' => array(
  61. 'drush searchapi-index' => dt('Index items for all enabled indexes.'),
  62. 'drush sapi-i' => dt('Alias to index items for all enabled indexes.'),
  63. 'drush sapi-i 1' => dt('Index items for the index with the ID !id.', array('!id' => 1)),
  64. 'drush sapi-i default_node_index' => dt('Index items for the index with the machine name !name.', array('!name' => 'default_node_index')),
  65. 'drush sapi-i 1 100' => dt("Index a maximum number of !limit items (index's cron batch size items per batch run) for the index with the ID !id.", array('!limit' => 100, '!id' => 1)),
  66. 'drush sapi-i 1 100 10' => dt("Index a maximum number of !limit items (!batch_size items per batch run) for the index with the ID !id.", array('!limit' => 100, '!batch_size' => 10, '!id' => 1)),
  67. ),
  68. 'arguments' => array(
  69. 'index_id' => dt('The numeric ID or machine name of an index.'),
  70. 'limit' => dt("The number of items to index (index's cron batch size items per run). Set to 0 to index all items. Defaults to 0 (index all)."),
  71. 'batch_size' => dt("The number of items to index per batch run. Set to 0 to index all items at once. Defaults to the index's cron batch size."),
  72. ),
  73. 'aliases' => array('sapi-i'),
  74. );
  75. $items['search-api-reindex'] = array(
  76. 'description' => 'Force reindexing of one or all search indexes, without clearing existing index data.',
  77. 'examples' => array(
  78. 'drush searchapi-reindex' => dt('Schedule all search indexes for reindexing.'),
  79. 'drush sapi-r' => dt('Alias to schedule all search indexes for reindexing .'),
  80. 'drush sapi-r 1' => dt('Schedule the search index with the ID !id for re-indexing.', array('!id' => 1)),
  81. 'drush sapi-r default_node_index' => dt('Schedule the search index with the machine name !name for re-indexing.', array('!name' => 'default_node_index')),
  82. ),
  83. 'arguments' => array(
  84. 'index_id' => dt('The numeric ID or machine name of an index.'),
  85. ),
  86. 'aliases' => array('sapi-r'),
  87. );
  88. $items['search-api-clear'] = array(
  89. 'description' => 'Clear one or all search indexes and mark them for re-indexing.',
  90. 'examples' => array(
  91. 'drush searchapi-clear' => dt('Clear all search indexes.'),
  92. 'drush sapi-c' => dt('Alias to clear all search indexes.'),
  93. 'drush sapi-c 1' => dt('Clear the search index with the ID !id.', array('!id' => 1)),
  94. 'drush sapi-c default_node_index' => dt('Clear the search index with the machine name !name.', array('!name' => 'default_node_index')),
  95. ),
  96. 'arguments' => array(
  97. 'index_id' => dt('The numeric ID or machine name of an index.'),
  98. ),
  99. 'aliases' => array('sapi-c'),
  100. );
  101. $items['search-api-set-index-server'] = array(
  102. 'description' => 'Set the search server used by a given index.',
  103. 'examples' => array(
  104. 'drush search-api-set-index-server default_node_index my_solr_server' => dt('Set the !index index to use the !server server.', array('!index' => 'default_node_index', '!server' => 'my_solr_server')),
  105. 'drush sapi-sis default_node_index my_solr_server' => dt('Alias to set the !index index to use the !server server.', array('!index' => 'default_node_index', '!server' => 'my_solr_server')),
  106. ),
  107. 'arguments' => array(
  108. 'index_id' => dt('The numeric ID or machine name of an index.'),
  109. 'server_id' => dt('The numeric ID or machine name of a server to set on the index.'),
  110. ),
  111. 'aliases' => array('sapi-sis'),
  112. );
  113. return $items;
  114. }
  115. /**
  116. * List all search indexes.
  117. */
  118. function drush_search_api_list() {
  119. if (search_api_drush_static(__FUNCTION__)) {
  120. return;
  121. }
  122. // See search_api_list_indexes()
  123. $indexes = search_api_index_load_multiple(FALSE);
  124. if (empty($indexes)) {
  125. drush_print(dt('There are no indexes present.'));
  126. return;
  127. }
  128. $rows[] = array(
  129. dt('Id'),
  130. dt('Name'),
  131. dt('Index'),
  132. dt('Server'),
  133. dt('Type'),
  134. dt('Status'),
  135. dt('Limit'),
  136. );
  137. foreach ($indexes as $index) {
  138. $type = search_api_get_item_type_info($index->item_type);
  139. $type = isset($type['name']) ? $type['name'] : $index->item_type;
  140. try {
  141. $server = $index->server();
  142. $server = $server ? $server->name : '(' . dt('none') . ')';
  143. }
  144. catch (SearchApiException $e) {
  145. watchdog_exception('search_api', $e);
  146. $server = '(' . dt('unknown: !server', array('server' => $index->server)) . ')';
  147. }
  148. $row = array(
  149. $index->id,
  150. $index->name,
  151. $index->machine_name,
  152. $server,
  153. $type,
  154. $index->enabled ? dt('enabled') : dt('disabled'),
  155. $index->options['cron_limit'],
  156. );
  157. $rows[] = $row;
  158. }
  159. drush_print_table($rows);
  160. }
  161. /**
  162. * Enable index(es).
  163. *
  164. * @param string|integer $index_id
  165. * The index name or id which should be enabled.
  166. */
  167. function drush_search_api_enable($index_id = NULL) {
  168. if (search_api_drush_static(__FUNCTION__)) {
  169. return;
  170. }
  171. $indexes = search_api_drush_get_index($index_id);
  172. if (empty($indexes)) {
  173. return;
  174. }
  175. foreach ($indexes as $index) {
  176. $vars = array('!index' => $index->name);
  177. if (!$index->enabled) {
  178. drush_log(dt("Enabling index !index and queueing items for indexing.", $vars), 'notice');
  179. $success = FALSE;
  180. try {
  181. if ($success = search_api_index_enable($index->id)) {
  182. drush_log(dt("The index !index was successfully enabled.", $vars), 'ok');
  183. }
  184. }
  185. catch (SearchApiException $e) {
  186. drush_log($e->getMessage(), 'error');
  187. }
  188. if (!$success) {
  189. drush_log(dt("Error enabling index !index.", $vars), 'error');
  190. }
  191. }
  192. else {
  193. drush_log(dt("The index !index is already enabled.", $vars), 'error');
  194. }
  195. }
  196. }
  197. /**
  198. * Disable index(es).
  199. *
  200. * @param string|integer $index_id
  201. * The index name or id which should be disabled.
  202. */
  203. function drush_search_api_disable($index_id = NULL) {
  204. if (search_api_drush_static(__FUNCTION__)) {
  205. return;
  206. }
  207. $indexes = search_api_drush_get_index($index_id);
  208. if (empty($indexes)) {
  209. return;
  210. }
  211. foreach ($indexes as $index) {
  212. $vars = array('!index' => $index->name);
  213. if ($index->enabled) {
  214. $success = FALSE;
  215. try {
  216. if ($success = search_api_index_disable($index->id)) {
  217. drush_log(dt("The index !index was successfully disabled.", $vars), 'ok');
  218. }
  219. }
  220. catch (SearchApiException $e) {
  221. drush_log($e->getMessage(), 'error');
  222. }
  223. if (!$success) {
  224. drush_log(dt("Error disabling index !index.", $vars), 'error');
  225. }
  226. }
  227. else {
  228. drush_log(dt("The index !index is already disabled.", $vars), 'error');
  229. }
  230. }
  231. }
  232. /**
  233. * Display index status.
  234. */
  235. function drush_search_api_status($index_id = NULL) {
  236. if (search_api_drush_static(__FUNCTION__)) {
  237. return;
  238. }
  239. $indexes = search_api_drush_get_index($index_id);
  240. if (empty($indexes)) {
  241. return;
  242. }
  243. // See search_api_index_status()
  244. $rows = array(array(
  245. dt('Id'),
  246. dt('Index'),
  247. dt('% Complete'),
  248. dt('Indexed'),
  249. dt('Total'),
  250. ));
  251. foreach ($indexes as $index) {
  252. $status = search_api_index_status($index);
  253. $complete = ($status['total'] > 0) ? 100 * round($status['indexed'] / $status['total'], 3) . '%' : '-';
  254. $row = array(
  255. $index->id,
  256. $index->name,
  257. $complete,
  258. $status['indexed'],
  259. $status['total'],
  260. );
  261. $rows[] = $row;
  262. }
  263. drush_print_table($rows);
  264. }
  265. /**
  266. * Index items.
  267. *
  268. * @param string|integer $index_id
  269. * The index name or id for which items should be indexed.
  270. * @param integer $limit
  271. * Maximum number of items to index.
  272. * @param integer $batch_size
  273. * Number of items to index per batch.
  274. */
  275. function drush_search_api_index($index_id = NULL, $limit = NULL, $batch_size = NULL) {
  276. if (search_api_drush_static(__FUNCTION__)) {
  277. return;
  278. }
  279. $indexes = search_api_drush_get_index($index_id);
  280. if (empty($indexes)) {
  281. return;
  282. }
  283. $process_batch = FALSE;
  284. foreach ($indexes as $index) {
  285. if (_drush_search_api_batch_indexing_create($index, $limit, $batch_size)) {
  286. $process_batch = TRUE;
  287. }
  288. }
  289. if ($process_batch) {
  290. drush_backend_batch_process();
  291. }
  292. }
  293. /**
  294. * Creates and sets a batch for indexing items for a particular index.
  295. *
  296. * @param SearchApiIndex $index
  297. * The index for which items should be indexed.
  298. * @param int $limit
  299. * (optional) The maximum number of items to index, or NULL to index all
  300. * items.
  301. * @param int $batch_size
  302. * (optional) The number of items to index per batch, or NULL to index all
  303. * items at once.
  304. *
  305. * @return bool
  306. * TRUE if batch was created, FALSE otherwise.
  307. */
  308. function _drush_search_api_batch_indexing_create(SearchApiIndex $index, $limit = NULL, $batch_size = NULL) {
  309. // Get the number of remaining items to index.
  310. try {
  311. $datasource = $index->datasource();
  312. }
  313. catch (SearchApiException $e) {
  314. drush_log($e->getMessage(), 'error');
  315. return FALSE;
  316. }
  317. $index_status = $datasource->getIndexStatus($index);
  318. $remaining = $index_status['total'] - $index_status['indexed'];
  319. if ($remaining <= 0) {
  320. drush_log(dt("The index !index is up to date.", array('!index' => $index->name)), 'ok');
  321. return FALSE;
  322. }
  323. // Get the number of items to index per batch run.
  324. if (!isset($batch_size)) {
  325. $batch_size = empty($index->options['cron_limit']) ? SEARCH_API_DEFAULT_CRON_LIMIT : $index->options['cron_limit'];
  326. }
  327. elseif ($batch_size <= 0) {
  328. $batch_size = $remaining;
  329. }
  330. // Get the total number of items to index.
  331. if (!isset($limit) || !is_int($limit += 0) || $limit <= 0) {
  332. $limit = $remaining;
  333. }
  334. drush_log(dt("Indexing a maximum number of !limit items (!batch_size items per batch run) for the index !index.", array('!index' => $index->name, '!limit' => $limit, '!batch_size' => $batch_size)), 'ok');
  335. // Create the batch.
  336. if (!_search_api_batch_indexing_create($index, $batch_size, $limit, $remaining, TRUE)) {
  337. drush_log(dt("Couldn't create a batch, please check the batch size and limit parameters."), 'error');
  338. return FALSE;
  339. }
  340. return TRUE;
  341. }
  342. /**
  343. * Copy of formal_plural that works with drush as 't' may not be available.
  344. */
  345. function _search_api_drush_format_plural($count, $singular, $plural, array $args = array(), array $options = array()) {
  346. $args['@count'] = $count;
  347. if ($count == 1) {
  348. return dt($singular, $args, $options);
  349. }
  350. // Get the plural index through the gettext formula.
  351. $index = (function_exists('locale_get_plural')) ? locale_get_plural($count, isset($options['langcode']) ? $options['langcode'] : NULL) : -1;
  352. // If the index cannot be computed, use the plural as a fallback (which
  353. // allows for most flexiblity with the replaceable @count value).
  354. if ($index < 0) {
  355. return dt($plural, $args, $options);
  356. }
  357. else {
  358. switch ($index) {
  359. case "0":
  360. return dt($singular, $args, $options);
  361. case "1":
  362. return dt($plural, $args, $options);
  363. default:
  364. unset($args['@count']);
  365. $args['@count[' . $index . ']'] = $count;
  366. return dt(strtr($plural, array('@count' => '@count[' . $index . ']')), $args, $options);
  367. }
  368. }
  369. }
  370. /**
  371. * Mark for re-indexing.
  372. */
  373. function drush_search_api_reindex($index_id = NULL) {
  374. if (search_api_drush_static(__FUNCTION__)) {
  375. return;
  376. }
  377. $indexes = search_api_drush_get_index($index_id);
  378. if (empty($indexes)) {
  379. return;
  380. }
  381. // See search_api_index_reindex()
  382. foreach ($indexes as $index) {
  383. $index->reindex();
  384. drush_log(dt('!index was successfully marked for re-indexing.', array('!index' => $index->machine_name)), 'ok');
  385. }
  386. }
  387. /**
  388. * Clear an index.
  389. */
  390. function drush_search_api_clear($index_id = NULL) {
  391. if (search_api_drush_static(__FUNCTION__)) {
  392. return;
  393. }
  394. $indexes = search_api_drush_get_index($index_id);
  395. if (empty($indexes)) {
  396. return;
  397. }
  398. // See search_api_index_reindex()
  399. foreach ($indexes as $index) {
  400. $index->clear();
  401. drush_log(dt('!index was successfully cleared.', array('!index' => $index->machine_name)), 'ok');
  402. }
  403. }
  404. /**
  405. * Set the server for a given index.
  406. */
  407. function drush_search_api_set_index_server($index_id = NULL, $server_id = NULL) {
  408. if (search_api_drush_static(__FUNCTION__)) {
  409. return;
  410. }
  411. // Make sure we have parameters to work with.
  412. if (empty($index_id) || empty($server_id)) {
  413. drush_log(dt('You must specify both an index and server.'), 'error');
  414. return;
  415. }
  416. // Fetch current index and server data.
  417. $indexes = search_api_drush_get_index($index_id);
  418. $servers = search_api_drush_get_server($server_id);
  419. if (empty($indexes) || empty($servers)) {
  420. // If the specified index or server can't be found, just return. An
  421. // appropriate error message should have been printed already.
  422. return;
  423. }
  424. // Set the new server on the index.
  425. $success = FALSE;
  426. $index = reset($indexes);
  427. $server = reset($servers);
  428. try {
  429. $success = $index->update(array('server' => $server->machine_name));
  430. }
  431. catch (SearchApiException $e) {
  432. drush_log($e->getMessage(), 'error');
  433. }
  434. if ($success === FALSE) {
  435. drush_log(dt('There was an error setting index !index to use server !server.', array('!index' => $index->name, '!server' => $server->name)), 'error');
  436. }
  437. elseif (!$success) {
  438. drush_log(dt('Index !index was already using server !server.', array('!index' => $index->name, '!server' => $server->name)), 'ok');
  439. }
  440. else {
  441. drush_log(dt('Index !index has been set to use server !server and items have been queued for indexing.', array('!index' => $index->name, '!server' => $server->name)), 'ok');
  442. }
  443. }
  444. /**
  445. * Returns an index or all indexes as an array.
  446. *
  447. * @param string|int|null $index_id
  448. * (optional) The ID or machine name of the index to load. Defaults to
  449. * loading all available indexes.
  450. *
  451. * @return SearchApiIndex[]
  452. * An array of indexes.
  453. */
  454. function search_api_drush_get_index($index_id = NULL) {
  455. $ids = isset($index_id) ? array($index_id) : FALSE;
  456. $indexes = search_api_index_load_multiple($ids);
  457. if (empty($indexes)) {
  458. drush_set_error(dt('Invalid index_id or no indexes present. Listing all indexes:'));
  459. drush_print();
  460. drush_search_api_list();
  461. }
  462. return $indexes;
  463. }
  464. /**
  465. * Returns a server or all servers as an array.
  466. *
  467. * @param string|int|null $server_id
  468. * (optional) The ID or machine name of the server to load. Defaults to
  469. * loading all available servers.
  470. *
  471. * @return SearchApiServer[]
  472. * An array of servers.
  473. */
  474. function search_api_drush_get_server($server_id = NULL) {
  475. $ids = isset($server_id) ? array($server_id) : FALSE;
  476. $servers = search_api_server_load_multiple($ids);
  477. if (empty($servers)) {
  478. drush_set_error(dt('Invalid server_id or no servers present.'));
  479. // @todo: Maybe add logic to print table of all servers.
  480. }
  481. return $servers;
  482. }
  483. /**
  484. * Does a static lookup to prevent Drush 4 from running twice.
  485. *
  486. * @param string $function
  487. * The Drush function being called.
  488. *
  489. * @return bool
  490. * TRUE if the function was already called in this Drush execution, FALSE
  491. * otherwise.
  492. *
  493. * @see http://drupal.org/node/704848
  494. */
  495. function search_api_drush_static($function) {
  496. static $index = array();
  497. if (isset($index[$function])) {
  498. return TRUE;
  499. }
  500. $index[$function] = TRUE;
  501. return FALSE;
  502. }