search_api.drush.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <?php
  2. /**
  3. * @file
  4. * Drush commands for Search API.
  5. */
  6. use Drupal\search_api\ConsoleException;
  7. use Drupal\search_api\Utility\CommandHelper;
  8. /**
  9. * Implements hook_drush_command().
  10. */
  11. function search_api_drush_command() {
  12. $items = [];
  13. $index['index_id'] = dt('The machine name of an index');
  14. $server['server_id'] = dt('The machine name of a server');
  15. $items['search-api-list'] = [
  16. 'description' => 'List all search indexes.',
  17. 'examples' => [
  18. 'drush search-api-list' => dt('List all search indexes.'),
  19. 'drush sapi-l' => dt('Alias to list all search indexes.'),
  20. ],
  21. 'aliases' => ['sapi-l'],
  22. ];
  23. $items['search-api-enable'] = [
  24. 'description' => 'Enable one or more disabled search indexes.',
  25. 'examples' => [
  26. 'drush search-api-enable node_index' => dt('Enable the search index with the ID @name.', ['@name' => 'node_index']),
  27. 'drush sapi-en node_index' => dt('Alias to enable the search index with the ID @name.', ['@name' => 'node_index']),
  28. ],
  29. 'arguments' => $index,
  30. 'aliases' => ['sapi-en'],
  31. ];
  32. $items['search-api-enable-all'] = [
  33. 'description' => 'Enable all disabled search indexes.',
  34. 'examples' => [
  35. 'drush search-api-enable-all' => dt('Enable all disabled indexes.'),
  36. 'drush sapi-ena' => dt('Alias to enable all disabled indexes.'),
  37. ],
  38. 'arguments' => [],
  39. 'aliases' => ['sapi-ena'],
  40. ];
  41. $items['search-api-disable'] = [
  42. 'description' => 'Disable one or more enabled search indexes.',
  43. 'examples' => [
  44. 'drush search-api-disable node_index' => dt('Disable the search index with the ID @name.', ['@name' => 'node_index']),
  45. 'drush sapi-dis node_index' => dt('Alias to disable the search index with the ID @name.', ['@name' => 'node_index']),
  46. ],
  47. 'arguments' => $index,
  48. 'aliases' => ['sapi-dis'],
  49. ];
  50. $items['search-api-disable-all'] = [
  51. 'description' => 'Disable all enabled search indexes.',
  52. 'examples' => [
  53. 'drush search-api-disable-all' => dt('Disable all enabled indexes.'),
  54. 'drush sapi-disa' => dt('Alias to disable all enabled indexes.'),
  55. ],
  56. 'arguments' => [],
  57. 'aliases' => ['sapi-disa'],
  58. ];
  59. $items['search-api-status'] = [
  60. 'description' => 'Show the status of one or all search indexes.',
  61. 'examples' => [
  62. 'drush search-api-status' => dt('Show the status of all search indexes.'),
  63. 'drush sapi-s' => dt('Alias to show the status of all search indexes.'),
  64. 'drush sapi-s node_index' => dt('Show the status of the search index with the ID @name.', ['@name' => 'node_index']),
  65. ],
  66. 'arguments' => $index,
  67. 'aliases' => ['sapi-s'],
  68. ];
  69. $items['search-api-index'] = [
  70. 'description' => 'Index items for one or all enabled search indexes.',
  71. 'examples' => [
  72. 'drush search-api-index' => dt('Index all items for all enabled indexes.'),
  73. 'drush sapi-i' => dt('Alias to index all items for all enabled indexes.'),
  74. 'drush sapi-i node_index' => dt('Index all items for the index with the ID @name.', ['@name' => 'node_index']),
  75. 'drush sapi-i node_index 100' => dt('Index a maximum number of @limit items for the index with the ID @name.', ['@limit' => 100, '@name' => 'node_index']),
  76. 'drush sapi-i node_index 100 10' => dt('Index a maximum number of @limit items (@batch_size items per batch run) for the index with the ID @name.', ['@limit' => 100, '@batch_size' => 10, '@name' => 'node_index']),
  77. ],
  78. 'options' => [
  79. 'limit' => dt('The number of items to index. Set to 0 to index all items. Defaults to 0 (index all).'),
  80. 'batch-size' => dt('The number of items to index per batch run. Set to 0 to index all items at once. Defaults to the "!batch_size_label" setting of the index.', ['!batch_size_label' => dt('Cron batch size')]),
  81. ],
  82. 'arguments' => $index,
  83. 'aliases' => ['sapi-i'],
  84. ];
  85. $items['search-api-reset-tracker'] = [
  86. 'description' => 'Force reindexing of one or all search indexes, without deleting existing index data.',
  87. 'examples' => [
  88. 'drush search-api-reindex' => dt('Schedule all search indexes for reindexing.'),
  89. 'drush sapi-r' => dt('Alias to schedule all search indexes for reindexing .'),
  90. 'drush sapi-r node_index' => dt('Schedule the search index with the ID @name for reindexing.', ['@name' => 'node_index']),
  91. ],
  92. 'options' => [
  93. 'entity-types' => [
  94. 'description' => dt('List of entity type ids to reset tracker for.'),
  95. 'example_value' => 'user,node',
  96. ],
  97. ],
  98. 'arguments' => $index,
  99. 'aliases' => [
  100. 'search-api-mark-all',
  101. 'search-api-reindex',
  102. 'sapi-r',
  103. ],
  104. ];
  105. $items['search-api-clear'] = [
  106. 'description' => 'Clear one or all search indexes and mark them for reindexing.',
  107. 'examples' => [
  108. 'drush search-api-clear' => dt('Clear all search indexes.'),
  109. 'drush sapi-c' => dt('Alias to clear all search indexes.'),
  110. 'drush sapi-c node_index' => dt('Clear the search index with the ID @name.', ['@name' => 'node_index']),
  111. ],
  112. 'arguments' => $index,
  113. 'aliases' => ['sapi-c'],
  114. ];
  115. $items['search-api-search'] = [
  116. 'description' => 'Search for a keyword or phrase in a given index.',
  117. 'examples' => [
  118. 'drush search-api-search node_index title' => dt('Search for "title" inside the "node_index" index.'),
  119. 'drush sapi-search node_index title' => dt('Alias to search for "title" inside the "node_index" index.'),
  120. ],
  121. 'arguments' => $index + [
  122. 'keyword' => dt('The keyword to look for.'),
  123. ],
  124. 'aliases' => ['sapi-search'],
  125. ];
  126. $items['search-api-server-list'] = [
  127. 'description' => 'List all search servers.',
  128. 'examples' => [
  129. 'drush search-api-server-list' => dt('List all search servers.'),
  130. 'drush sapi-sl' => dt('Alias to list all search servers.'),
  131. ],
  132. 'aliases' => ['sapi-sl'],
  133. ];
  134. $items['search-api-server-enable'] = [
  135. 'description' => 'Enable a search server.',
  136. 'examples' => [
  137. 'drush search-api-server-e my_solr_server' => dt('Enable the @server search server.', ['@server' => 'my_solr_server']),
  138. 'drush sapi-se my_solr_server' => dt('Alias to enable the @server search server.', ['@server' => 'my_solr_server']),
  139. ],
  140. 'arguments' => $server,
  141. 'aliases' => ['sapi-se'],
  142. ];
  143. $items['search-api-server-disable'] = [
  144. 'description' => 'Disable a search server.',
  145. 'examples' => [
  146. 'drush search-api-server-disable' => dt('Disable the @server search server.', ['@server' => 'my_solr_server']),
  147. 'drush sapi-sd' => dt('Alias to disable the @server search server.', ['@server' => 'my_solr_server']),
  148. ],
  149. 'arguments' => $server,
  150. 'aliases' => ['sapi-sd'],
  151. ];
  152. $items['search-api-server-clear'] = [
  153. 'description' => 'Clear all search indexes on the search server and mark them for reindexing.',
  154. 'examples' => [
  155. 'drush search-api-server-clear' => dt('Clear all search indexes on the search server @server.', ['@server' => 'my_solr_server']),
  156. 'drush sapi-sc' => dt('Alias to clear all search indexes on the search server @server.', ['@server' => 'my_solr_server']),
  157. ],
  158. 'arguments' => $server,
  159. 'aliases' => ['sapi-sc'],
  160. ];
  161. $items['search-api-set-index-server'] = [
  162. 'description' => 'Set the search server used by a given index.',
  163. 'examples' => [
  164. 'drush search-api-set-index-server default_node_index my_solr_server' => dt('Set the @index index to used the @server server.', ['@index' => 'default_node_index', '@server' => 'my_solr_server']),
  165. 'drush sapi-sis default_node_index my_solr_server' => dt('Alias to set the @index index to used the @server server.', ['@index' => 'default_node_index', '@server' => 'my_solr_server']),
  166. ],
  167. 'arguments' => $index + $server,
  168. 'aliases' => ['sapi-sis'],
  169. ];
  170. return $items;
  171. }
  172. /**
  173. * Prints a list of all search indexes.
  174. */
  175. function drush_search_api_list() {
  176. $command_helper = _search_api_drush_command_helper();
  177. $rows[] = [
  178. dt('ID'),
  179. dt('Name'),
  180. dt('Server'),
  181. dt('Type'),
  182. dt('Status'),
  183. dt('Limit'),
  184. ];
  185. $rows += $command_helper->indexListCommand();
  186. foreach ($rows as &$row) {
  187. $row['types'] = is_array($row['types']) ? implode(', ', $row['types']) : $row['types'];
  188. $row['typeNames'] = is_array($row['types']) ? implode(', ', $row['typeNames']) : $row['types'];
  189. }
  190. drush_print_table($rows);
  191. }
  192. /**
  193. * Enables one or more search indexes.
  194. *
  195. * @param string|null $index_id
  196. * The ID of a search index to enable. Or NULL (only used internally) to
  197. * enable all disabled indexes.
  198. */
  199. function drush_search_api_enable($index_id = NULL) {
  200. $command_helper = _search_api_drush_command_helper();
  201. try {
  202. $command_helper->enableIndexCommand([$index_id]);
  203. }
  204. catch (ConsoleException $exception) {
  205. drush_set_error($exception->getMessage());
  206. }
  207. }
  208. /**
  209. * Enables all search indexes.
  210. */
  211. function drush_search_api_enable_all() {
  212. $command_helper = _search_api_drush_command_helper();
  213. try {
  214. $command_helper->enableIndexCommand();
  215. }
  216. catch (ConsoleException $exception) {
  217. drush_set_error($exception->getMessage());
  218. }
  219. }
  220. /**
  221. * Disables one or more search indexes.
  222. *
  223. * @param string|null $index_id
  224. * The ID of a search index to disable. Or NULL (only used internally) to
  225. * disable all enabled indexes.
  226. */
  227. function drush_search_api_disable($index_id = NULL) {
  228. $command_helper = _search_api_drush_command_helper();
  229. try {
  230. $command_helper->disableIndexCommand([$index_id]);
  231. }
  232. catch (ConsoleException $exception) {
  233. drush_set_error($exception->getMessage());
  234. }
  235. }
  236. /**
  237. * Disables all search indexes.
  238. */
  239. function drush_search_api_disable_all() {
  240. $command_helper = _search_api_drush_command_helper();
  241. try {
  242. $command_helper->disableIndexCommand();
  243. }
  244. catch (ConsoleException $exception) {
  245. drush_set_error($exception->getMessage());
  246. }
  247. }
  248. /**
  249. * Displays the status of one or all search indexes.
  250. *
  251. * @param string|null $index_id
  252. * (optional) The ID of the search index whose status should be displayed, or
  253. * NULL to display the status of all search indexes.
  254. */
  255. function drush_search_api_status($index_id = NULL) {
  256. $command_helper = _search_api_drush_command_helper();
  257. $rows[] = [
  258. dt('ID'),
  259. dt('Name'),
  260. dt('% Complete'),
  261. dt('Indexed'),
  262. dt('Total'),
  263. ];
  264. $rows += $command_helper->indexStatusCommand([$index_id]);
  265. drush_print_table($rows);
  266. }
  267. /**
  268. * Indexes items.
  269. *
  270. * @param string|null $index_id
  271. * (optional) The index ID for which items should be indexed, or NULL to index
  272. * items on all indexes.
  273. */
  274. function drush_search_api_index($index_id = NULL) {
  275. $command_helper = _search_api_drush_command_helper();
  276. $limit = drush_get_option('limit');
  277. $batch_size = drush_get_option('batch-size');
  278. $batch_set = $command_helper->indexItemsToIndexCommand([$index_id], $limit, $batch_size);
  279. if ($batch_set) {
  280. drush_backend_batch_process();
  281. }
  282. }
  283. /**
  284. * Schedules a search index for reindexing.
  285. *
  286. * @param string|null $index_id
  287. * (optional) The index ID for which items should be reindexed, or NULL to
  288. * reindex all search indexes.
  289. */
  290. function drush_search_api_reset_tracker($index_id = NULL) {
  291. $command_helper = _search_api_drush_command_helper();
  292. $entity_types = drush_get_option_list('entity-types');
  293. $command_helper->resetTrackerCommand([$index_id], $entity_types);
  294. }
  295. /**
  296. * Clears a search index.
  297. *
  298. * @param string|null $index_id
  299. * (optional) The ID of the search index which should be cleared, or NULL to
  300. * clear all search indexes.
  301. */
  302. function drush_search_api_clear($index_id = NULL) {
  303. $command_helper = _search_api_drush_command_helper();
  304. $command_helper->clearIndexCommand([$index_id]);
  305. }
  306. /**
  307. * Executes a simple keyword search and displays the results in a table.
  308. *
  309. * @param string $index_id
  310. * The ID of the index being searched.
  311. * @param string $keyword
  312. * The search keyword.
  313. */
  314. function drush_search_api_search($index_id, $keyword) {
  315. $command_helper = _search_api_drush_command_helper();
  316. $rows = $command_helper->searchIndexCommand($index_id, $keyword);
  317. drush_print_table($rows);
  318. }
  319. /**
  320. * Lists all available search servers.
  321. */
  322. function drush_search_api_server_list() {
  323. $command_helper = _search_api_drush_command_helper();
  324. $rows[] = [
  325. dt('ID'),
  326. dt('Name'),
  327. dt('Status'),
  328. ];
  329. try {
  330. $rows += $command_helper->serverListCommand();
  331. }
  332. catch (ConsoleException $exception) {
  333. drush_print($exception->getMessage());
  334. }
  335. drush_print_table($rows);
  336. }
  337. /**
  338. * Enables a search server.
  339. *
  340. * @param string $server_id
  341. * The ID of the server to enable.
  342. */
  343. function drush_search_api_server_enable($server_id = NULL) {
  344. $command_helper = _search_api_drush_command_helper();
  345. try {
  346. $command_helper->enableServerCommand($server_id);
  347. }
  348. catch (ConsoleException $exception) {
  349. drush_print($exception->getMessage());
  350. }
  351. }
  352. /**
  353. * Disables a search server.
  354. *
  355. * @param string $server_id
  356. * The ID of the server to disable.
  357. */
  358. function drush_search_api_server_disable($server_id = NULL) {
  359. $command_helper = _search_api_drush_command_helper();
  360. try {
  361. $command_helper->disableServerCommand($server_id);
  362. }
  363. catch (ConsoleException $exception) {
  364. drush_print($exception->getMessage());
  365. }
  366. }
  367. /**
  368. * Clears all search indexes on the server and marks them for reindexing.
  369. *
  370. * @param string $server_id
  371. * The ID of the server to clear all search indexes.
  372. */
  373. function drush_search_api_server_clear($server_id = NULL) {
  374. $command_helper = _search_api_drush_command_helper();
  375. try {
  376. $command_helper->clearServerCommand($server_id);
  377. }
  378. catch (ConsoleException $exception) {
  379. drush_print($exception->getMessage());
  380. }
  381. }
  382. /**
  383. * Sets the server for a given index.
  384. *
  385. * @param string $index_id
  386. * The ID of the index whose server should be changed.
  387. * @param string $server_id
  388. * The ID of the new server for the index.
  389. */
  390. function drush_search_api_set_index_server($index_id = NULL, $server_id = NULL) {
  391. $command_helper = _search_api_drush_command_helper();
  392. try {
  393. $command_helper->setIndexServerCommand($index_id, $server_id);
  394. }
  395. catch (ConsoleException $exception) {
  396. drush_print($exception->getMessage());
  397. }
  398. }
  399. /**
  400. * Returns an instance of the command helper.
  401. *
  402. * @return \Drupal\search_api\Utility\CommandHelper
  403. * An instance of the command helper class.
  404. */
  405. function _search_api_drush_command_helper() {
  406. $command_helper = new CommandHelper(\Drupal::entityTypeManager(), \Drupal::moduleHandler(), 'dt');
  407. $command_helper->setLogger(\Drupal::logger('search_api'));
  408. return $command_helper;
  409. }