search_api.drush.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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. 'drush sapi-i 0 0 100' => dt("Index all items of all indexes with !batch_size items per batch run.", array('!batch_size' => 100)),
  68. ),
  69. 'arguments' => array(
  70. 'index_id' => dt('The numeric ID or machine name of an index. Set to 0 to index all indexes. Defaults to 0 (index all).'),
  71. '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)."),
  72. '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."),
  73. ),
  74. 'aliases' => array('sapi-i'),
  75. );
  76. $items['search-api-reindex'] = array(
  77. 'description' => 'Force reindexing of one or all search indexes, without clearing existing index data.',
  78. 'examples' => array(
  79. 'drush searchapi-reindex' => dt('Schedule all search indexes for reindexing.'),
  80. 'drush sapi-r' => dt('Alias to schedule all search indexes for reindexing .'),
  81. 'drush sapi-r 1' => dt('Schedule the search index with the ID !id for re-indexing.', array('!id' => 1)),
  82. 'drush sapi-r default_node_index' => dt('Schedule the search index with the machine name !name for re-indexing.', array('!name' => 'default_node_index')),
  83. ),
  84. 'arguments' => array(
  85. 'index_id' => dt('The numeric ID or machine name of an index.'),
  86. ),
  87. 'aliases' => array('sapi-r'),
  88. );
  89. $items['search-api-reindex-items'] = array(
  90. 'description' => 'Force re-indexing of one or more specific items.',
  91. 'examples' => array(
  92. 'drush search-api-reindex-items node 12,34,56' => dt('Schedule the nodes with ID 12, 34 and 56 for re-indexing.'),
  93. ),
  94. 'arguments' => array(
  95. 'entity_type' => dt('The entity type whose items should be re-indexed.'),
  96. 'entities' => dt('The entities of the given entity type to be re-indexed.'),
  97. ),
  98. 'aliases' => array('sapi-ri'),
  99. );
  100. $items['search-api-clear'] = array(
  101. 'description' => 'Clear one or all search indexes and mark them for re-indexing.',
  102. 'examples' => array(
  103. 'drush searchapi-clear' => dt('Clear all search indexes.'),
  104. 'drush sapi-c' => dt('Alias to clear all search indexes.'),
  105. 'drush sapi-c 1' => dt('Clear the search index with the ID !id.', array('!id' => 1)),
  106. 'drush sapi-c default_node_index' => dt('Clear the search index with the machine name !name.', array('!name' => 'default_node_index')),
  107. ),
  108. 'arguments' => array(
  109. 'index_id' => dt('The numeric ID or machine name of an index.'),
  110. ),
  111. 'aliases' => array('sapi-c'),
  112. );
  113. $items['search-api-execute-tasks'] = array(
  114. 'description' => 'Execute all pending tasks or all for a given server.',
  115. 'examples' => array(
  116. 'drush search-api-execute-tasks my_solr_server' => dt('Execute all pending tasks on !server', array('!server' => 'my_solr_server')),
  117. 'drush sapi-et my_solr_server' => dt('Execute all pending tasks on !server', array('!server' => 'my_solr_server')),
  118. 'drush sapi-et' => dt('Execute all pending tasks on all servers.')
  119. ),
  120. 'arguments' => array(
  121. 'server_id' => dt('The numeric ID or machine name of a server to execute tasks on.'),
  122. ),
  123. 'aliases' => array('sapi-et')
  124. );
  125. $items['search-api-set-index-server'] = array(
  126. 'description' => 'Set the search server used by a given index.',
  127. 'examples' => array(
  128. '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')),
  129. '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')),
  130. ),
  131. 'arguments' => array(
  132. 'index_id' => dt('The numeric ID or machine name of an index.'),
  133. 'server_id' => dt('The numeric ID or machine name of a server to set on the index.'),
  134. ),
  135. 'aliases' => array('sapi-sis'),
  136. );
  137. $items['search-api-server-list'] = array(
  138. 'description' => 'List all search servers.',
  139. 'examples' => array(
  140. 'drush search-api-server-list' => dt('List all search servers.'),
  141. 'drush sapi-sl' => dt('Alias to list all search servers.'),
  142. ),
  143. 'aliases' => array('sapi-sl'),
  144. );
  145. $items['search-api-server-enable'] = array(
  146. 'description' => 'Enable a search server.',
  147. 'examples' => array(
  148. 'drush search-api-server-e my_solr_server' => dt('Enable the !server search server.', array('!server' => 'my_solr_server')),
  149. 'drush sapi-se my_solr_server' => dt('Alias to enable the !server search server.', array('!server' => 'my_solr_server')),
  150. ),
  151. 'arguments' => array(
  152. 'server_id' => dt('The numeric ID or machine name of a search server to enable.'),
  153. ),
  154. 'aliases' => array('sapi-se'),
  155. );
  156. $items['search-api-server-disable'] = array(
  157. 'description' => 'Disable a search server.',
  158. 'examples' => array(
  159. 'drush search-api-server-disable' => dt('Disable the !server search server.', array('!server' => 'my_solr_server')),
  160. 'drush sapi-sd' => dt('Alias to disable the !server search server.', array('!server' => 'my_solr_server')),
  161. ),
  162. 'arguments' => array(
  163. 'server_id' => dt('The numeric ID or machine name of a search server to disable.'),
  164. ),
  165. 'aliases' => array('sapi-sd'),
  166. );
  167. return $items;
  168. }
  169. /**
  170. * List all search indexes.
  171. */
  172. function drush_search_api_list() {
  173. if (search_api_drush_static(__FUNCTION__)) {
  174. return;
  175. }
  176. // See search_api_list_indexes()
  177. $indexes = search_api_index_load_multiple(FALSE);
  178. if (empty($indexes)) {
  179. drush_print(dt('There are no indexes present.'));
  180. return;
  181. }
  182. $rows[] = array(
  183. dt('Id'),
  184. dt('Name'),
  185. dt('Index'),
  186. dt('Server'),
  187. dt('Type'),
  188. dt('Status'),
  189. dt('Limit'),
  190. );
  191. foreach ($indexes as $index) {
  192. $type = search_api_get_item_type_info($index->item_type);
  193. $type = isset($type['name']) ? $type['name'] : $index->item_type;
  194. try {
  195. $server = $index->server();
  196. $server = $server ? $server->name : '(' . dt('none') . ')';
  197. }
  198. catch (SearchApiException $e) {
  199. watchdog_exception('search_api', $e);
  200. $server = '(' . dt('unknown: !server', array('server' => $index->server)) . ')';
  201. }
  202. $row = array(
  203. $index->id,
  204. $index->name,
  205. $index->machine_name,
  206. $server,
  207. $type,
  208. $index->enabled ? dt('enabled') : dt('disabled'),
  209. $index->options['cron_limit'],
  210. );
  211. $rows[] = $row;
  212. }
  213. drush_print_table($rows);
  214. }
  215. /**
  216. * Enable index(es).
  217. *
  218. * @param string|integer $index_id
  219. * The index name or id which should be enabled.
  220. */
  221. function drush_search_api_enable($index_id = NULL) {
  222. if (search_api_drush_static(__FUNCTION__)) {
  223. return;
  224. }
  225. $indexes = search_api_drush_get_index($index_id);
  226. if (empty($indexes)) {
  227. return;
  228. }
  229. foreach ($indexes as $index) {
  230. $vars = array('!index' => $index->name);
  231. if (!$index->enabled) {
  232. drush_log(dt("Enabling index !index and queueing items for indexing.", $vars), 'notice');
  233. $success = FALSE;
  234. try {
  235. if ($success = search_api_index_enable($index->id)) {
  236. drush_log(dt("The index !index was successfully enabled.", $vars), 'ok');
  237. }
  238. }
  239. catch (SearchApiException $e) {
  240. drush_log($e->getMessage(), 'error');
  241. }
  242. if (!$success) {
  243. drush_log(dt("Error enabling index !index.", $vars), 'error');
  244. }
  245. }
  246. else {
  247. drush_log(dt("The index !index is already enabled.", $vars), 'error');
  248. }
  249. }
  250. }
  251. /**
  252. * Disable index(es).
  253. *
  254. * @param string|integer $index_id
  255. * The index name or id which should be disabled.
  256. */
  257. function drush_search_api_disable($index_id = NULL) {
  258. if (search_api_drush_static(__FUNCTION__)) {
  259. return;
  260. }
  261. $indexes = search_api_drush_get_index($index_id);
  262. if (empty($indexes)) {
  263. return;
  264. }
  265. foreach ($indexes as $index) {
  266. $vars = array('!index' => $index->name);
  267. if ($index->enabled) {
  268. $success = FALSE;
  269. try {
  270. if ($success = search_api_index_disable($index->id)) {
  271. drush_log(dt("The index !index was successfully disabled.", $vars), 'ok');
  272. }
  273. }
  274. catch (SearchApiException $e) {
  275. drush_log($e->getMessage(), 'error');
  276. }
  277. if (!$success) {
  278. drush_log(dt("Error disabling index !index.", $vars), 'error');
  279. }
  280. }
  281. else {
  282. drush_log(dt("The index !index is already disabled.", $vars), 'error');
  283. }
  284. }
  285. }
  286. /**
  287. * Display index status.
  288. */
  289. function drush_search_api_status($index_id = NULL) {
  290. if (search_api_drush_static(__FUNCTION__)) {
  291. return;
  292. }
  293. $indexes = search_api_drush_get_index($index_id);
  294. if (empty($indexes)) {
  295. return;
  296. }
  297. // See search_api_index_status()
  298. $rows = array(array(
  299. dt('Id'),
  300. dt('Index'),
  301. dt('% Complete'),
  302. dt('Indexed'),
  303. dt('Total'),
  304. ));
  305. foreach ($indexes as $index) {
  306. $status = search_api_index_status($index);
  307. $complete = ($status['total'] > 0) ? 100 * round($status['indexed'] / $status['total'], 3) . '%' : '-';
  308. $row = array(
  309. $index->id,
  310. $index->name,
  311. $complete,
  312. $status['indexed'],
  313. $status['total'],
  314. );
  315. $rows[] = $row;
  316. }
  317. drush_print_table($rows);
  318. }
  319. /**
  320. * Index items.
  321. *
  322. * @param string|integer $index_id
  323. * The index name or id for which items should be indexed.
  324. * @param integer $limit
  325. * Maximum number of items to index.
  326. * @param integer $batch_size
  327. * Number of items to index per batch.
  328. */
  329. function drush_search_api_index($index_id = NULL, $limit = NULL, $batch_size = NULL) {
  330. if (search_api_drush_static(__FUNCTION__)) {
  331. return;
  332. }
  333. $index_id = !empty($index_id) ? $index_id : NULL;
  334. $indexes = search_api_drush_get_index($index_id);
  335. if (empty($indexes)) {
  336. return;
  337. }
  338. $process_batch = FALSE;
  339. foreach ($indexes as $index) {
  340. if (_drush_search_api_batch_indexing_create($index, $limit, $batch_size)) {
  341. $process_batch = TRUE;
  342. }
  343. }
  344. if ($process_batch) {
  345. drush_backend_batch_process();
  346. }
  347. }
  348. /**
  349. * Creates and sets a batch for indexing items for a particular index.
  350. *
  351. * @param SearchApiIndex $index
  352. * The index for which items should be indexed.
  353. * @param int $limit
  354. * (optional) The maximum number of items to index, or NULL to index all
  355. * items.
  356. * @param int $batch_size
  357. * (optional) The number of items to index per batch, or NULL to index all
  358. * items at once.
  359. *
  360. * @return bool
  361. * TRUE if batch was created, FALSE otherwise.
  362. */
  363. function _drush_search_api_batch_indexing_create(SearchApiIndex $index, $limit = NULL, $batch_size = NULL) {
  364. // Get the number of remaining items to index.
  365. try {
  366. $datasource = $index->datasource();
  367. }
  368. catch (SearchApiException $e) {
  369. drush_log($e->getMessage(), 'error');
  370. return FALSE;
  371. }
  372. $index_status = $datasource->getIndexStatus($index);
  373. $remaining = $index_status['total'] - $index_status['indexed'];
  374. if ($remaining <= 0) {
  375. drush_log(dt("The index !index is up to date.", array('!index' => $index->name)), 'ok');
  376. return FALSE;
  377. }
  378. // Get the number of items to index per batch run.
  379. if (!isset($batch_size)) {
  380. $batch_size = empty($index->options['cron_limit']) ? SEARCH_API_DEFAULT_CRON_LIMIT : $index->options['cron_limit'];
  381. }
  382. elseif ($batch_size <= 0) {
  383. $batch_size = $remaining;
  384. }
  385. // Get the total number of items to index.
  386. if (!isset($limit) || !is_int($limit += 0) || $limit <= 0) {
  387. $limit = $remaining;
  388. }
  389. 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');
  390. // Create the batch.
  391. if (!_search_api_batch_indexing_create($index, $batch_size, $limit, $remaining, TRUE)) {
  392. drush_log(dt("Couldn't create a batch, please check the batch size and limit parameters."), 'error');
  393. return FALSE;
  394. }
  395. return TRUE;
  396. }
  397. /**
  398. * Copy of formal_plural that works with drush as 't' may not be available.
  399. */
  400. function _search_api_drush_format_plural($count, $singular, $plural, array $args = array(), array $options = array()) {
  401. $args['@count'] = $count;
  402. if ($count == 1) {
  403. return dt($singular, $args, $options);
  404. }
  405. // Get the plural index through the gettext formula.
  406. $index = (function_exists('locale_get_plural')) ? locale_get_plural($count, isset($options['langcode']) ? $options['langcode'] : NULL) : -1;
  407. // If the index cannot be computed, use the plural as a fallback (which
  408. // allows for most flexiblity with the replaceable @count value).
  409. if ($index < 0) {
  410. return dt($plural, $args, $options);
  411. }
  412. else {
  413. switch ($index) {
  414. case "0":
  415. return dt($singular, $args, $options);
  416. case "1":
  417. return dt($plural, $args, $options);
  418. default:
  419. unset($args['@count']);
  420. $args['@count[' . $index . ']'] = $count;
  421. return dt(strtr($plural, array('@count' => '@count[' . $index . ']')), $args, $options);
  422. }
  423. }
  424. }
  425. /**
  426. * Mark for re-indexing.
  427. */
  428. function drush_search_api_reindex($index_id = NULL) {
  429. if (search_api_drush_static(__FUNCTION__)) {
  430. return;
  431. }
  432. $indexes = search_api_drush_get_index($index_id);
  433. if (empty($indexes)) {
  434. return;
  435. }
  436. // See search_api_index_reindex()
  437. foreach ($indexes as $index) {
  438. $index->reindex();
  439. drush_log(dt('!index was successfully marked for re-indexing.', array('!index' => $index->machine_name)), 'ok');
  440. }
  441. }
  442. /**
  443. * Marks the given entities as needing to be re-indexed.
  444. */
  445. function drush_search_api_reindex_items($entity_type, $entities) {
  446. if (search_api_drush_static(__FUNCTION__)) {
  447. return;
  448. }
  449. // Validate list of entity ids.
  450. if (!empty($entities) && !preg_match('#^[0-9]*(,[0-9]*)*$#', $entities)) {
  451. drush_log(dt('Entities should be a single numeric entity ID or a list with the numeric entity IDs separated by comma.'), 'error');
  452. return;
  453. }
  454. $ids = explode(',', $entities);
  455. if (!empty($ids)) {
  456. search_api_track_item_change($entity_type, $ids);
  457. $combined_ids = array();
  458. foreach ($ids as $id) {
  459. $combined_ids[] = $entity_type . '/' . $id;
  460. }
  461. search_api_track_item_change('multiple', $combined_ids);
  462. }
  463. }
  464. /**
  465. * Clear an index.
  466. */
  467. function drush_search_api_clear($index_id = NULL) {
  468. if (search_api_drush_static(__FUNCTION__)) {
  469. return;
  470. }
  471. $indexes = search_api_drush_get_index($index_id);
  472. if (empty($indexes)) {
  473. return;
  474. }
  475. // See search_api_index_reindex()
  476. foreach ($indexes as $index) {
  477. $index->clear();
  478. drush_log(dt('!index was successfully cleared.', array('!index' => $index->machine_name)), 'ok');
  479. }
  480. }
  481. /**
  482. * Execute all pending tasks or all for a given server.
  483. */
  484. function drush_search_api_execute_tasks($server_id = NULL) {
  485. if (search_api_drush_static(__FUNCTION__)) {
  486. return;
  487. }
  488. // Attempt to load the associated server.
  489. $server = NULL;
  490. if ($server_id) {
  491. $servers = search_api_drush_get_server($server_id);
  492. if (!$servers) {
  493. return;
  494. }
  495. $server = reset($servers);
  496. }
  497. // Process batch op with drush.
  498. try {
  499. search_api_execute_pending_tasks($server);
  500. drush_log(dt('!server tasks have been successfully executed.', array('!server' => $server->machine_name ? $server->machine_name : 'All')), 'ok');
  501. }
  502. catch (SearchApiException $e) {
  503. drush_log($e->getMessage(), 'error');
  504. }
  505. }
  506. /**
  507. * Set the server for a given index.
  508. */
  509. function drush_search_api_set_index_server($index_id = NULL, $server_id = NULL) {
  510. if (search_api_drush_static(__FUNCTION__)) {
  511. return;
  512. }
  513. // Make sure we have parameters to work with.
  514. if (empty($index_id) || empty($server_id)) {
  515. drush_log(dt('You must specify both an index and server.'), 'error');
  516. return;
  517. }
  518. // Fetch current index and server data.
  519. $indexes = search_api_drush_get_index($index_id);
  520. $servers = search_api_drush_get_server($server_id);
  521. if (empty($indexes) || empty($servers)) {
  522. // If the specified index or server can't be found, just return. An
  523. // appropriate error message should have been printed already.
  524. return;
  525. }
  526. // Set the new server on the index.
  527. $success = FALSE;
  528. $index = reset($indexes);
  529. $server = reset($servers);
  530. try {
  531. $success = $index->update(array('server' => $server->machine_name));
  532. }
  533. catch (SearchApiException $e) {
  534. drush_log($e->getMessage(), 'error');
  535. }
  536. if ($success === FALSE) {
  537. drush_log(dt('There was an error setting index !index to use server !server.', array('!index' => $index->name, '!server' => $server->name)), 'error');
  538. }
  539. elseif (!$success) {
  540. drush_log(dt('Index !index was already using server !server.', array('!index' => $index->name, '!server' => $server->name)), 'ok');
  541. }
  542. else {
  543. 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');
  544. }
  545. }
  546. /**
  547. * Returns an index or all indexes as an array.
  548. *
  549. * @param string|int|null $index_id
  550. * (optional) The ID or machine name of the index to load. Defaults to
  551. * loading all available indexes.
  552. *
  553. * @return SearchApiIndex[]
  554. * An array of indexes.
  555. */
  556. function search_api_drush_get_index($index_id = NULL) {
  557. $ids = isset($index_id) ? array($index_id) : FALSE;
  558. $indexes = search_api_index_load_multiple($ids);
  559. if (empty($indexes)) {
  560. drush_set_error(dt('Invalid index_id or no indexes present. Listing all indexes:'));
  561. drush_print();
  562. drush_search_api_list();
  563. }
  564. return $indexes;
  565. }
  566. /**
  567. * Returns a server or all servers as an array.
  568. *
  569. * @param string|int|null $server_id
  570. * (optional) The ID or machine name of the server to load. Defaults to
  571. * loading all available servers.
  572. *
  573. * @return SearchApiServer[]
  574. * An array of servers.
  575. */
  576. function search_api_drush_get_server($server_id = NULL) {
  577. $ids = isset($server_id) ? array($server_id) : FALSE;
  578. $servers = search_api_server_load_multiple($ids);
  579. if (empty($servers)) {
  580. drush_set_error(dt('Invalid server_id or no servers present.'));
  581. drush_print();
  582. drush_search_api_server_list();
  583. }
  584. return $servers;
  585. }
  586. /**
  587. * Does a static lookup to prevent Drush 4 from running twice.
  588. *
  589. * @param string $function
  590. * The Drush function being called.
  591. *
  592. * @return bool
  593. * TRUE if the function was already called in this Drush execution, FALSE
  594. * otherwise.
  595. *
  596. * @see http://drupal.org/node/704848
  597. */
  598. function search_api_drush_static($function) {
  599. static $index = array();
  600. if (isset($index[$function])) {
  601. return TRUE;
  602. }
  603. $index[$function] = TRUE;
  604. return FALSE;
  605. }
  606. /**
  607. * Lists all search servers.
  608. */
  609. function drush_search_api_server_list() {
  610. if (search_api_drush_static(__FUNCTION__)) {
  611. return;
  612. }
  613. $servers = search_api_server_load_multiple(FALSE);
  614. if (empty($servers)) {
  615. drush_print(dt('There are no servers present.'));
  616. return;
  617. }
  618. $rows[] = array(
  619. dt('Machine name'),
  620. dt('Name'),
  621. dt('Status'),
  622. );
  623. foreach ($servers as $server) {
  624. $row = array(
  625. $server->machine_name,
  626. $server->name,
  627. $server->enabled ? dt('enabled') : dt('disabled'),
  628. );
  629. $rows[] = $row;
  630. }
  631. drush_print_table($rows);
  632. }
  633. /**
  634. * Enables a search server.
  635. *
  636. * @param int|string $server_id
  637. * The numeric ID or machine name of the server to enable.
  638. */
  639. function drush_search_api_server_enable($server_id = NULL) {
  640. if (!isset($server_id)) {
  641. drush_print(dt('Please provide a valid server id.'));
  642. return;
  643. }
  644. $server = search_api_server_load($server_id);
  645. if (empty($server)) {
  646. drush_print(dt('The server was not able to load.'));
  647. return;
  648. }
  649. else {
  650. $server->update(array('enabled' => 1));
  651. drush_print(dt('The server was enabled successfully.'));
  652. }
  653. }
  654. /**
  655. * Disables a search server.
  656. *
  657. * @param int|string $server_id
  658. * The numeric ID or machine name of the server to disable.
  659. */
  660. function drush_search_api_server_disable($server_id = NULL) {
  661. if (!isset($server_id)) {
  662. drush_print(dt('Please provide a valid server id.'));
  663. return;
  664. }
  665. $server = search_api_server_load($server_id);
  666. if (empty($server)) {
  667. drush_print(dt('The server was not able to load.'));
  668. return;
  669. }
  670. else {
  671. $server->update(array('enabled' => 0));
  672. drush_print(dt('The server was disabled successfully.'));
  673. }
  674. }