search.drush.inc

  1. 8.0.x commands/core/search.drush.inc
  2. 6.x commands/core/search.drush.inc
  3. 7.x commands/core/search.drush.inc
  4. 3.x commands/core/search.drush.inc
  5. 4.x commands/core/search.drush.inc
  6. 5.x commands/core/search.drush.inc
  7. master commands/core/search.drush.inc

Functions

Namesort descending Description
drush_search_index
drush_search_reindex
drush_search_status
search_drush_command
search_drush_help Implementation of hook_drush_help().
system_nodeapi Fake an implementation of hook_nodeapi() for Drupal 6.
system_node_update_index Fake an implementation of hook_node_update_index() for Drupal 7.
_drush_search_index
_drush_search_status

File

commands/core/search.drush.inc
View source
  1. <?php
  2. use Drush\Log\LogLevel;
  3. /**
  4. * Implementation of hook_drush_help().
  5. */
  6. function search_drush_help($section) {
  7. switch ($section) {
  8. case 'meta:search:title':
  9. return dt('Search commands');
  10. case 'meta:search:summary':
  11. return dt('Interact with Drupal\'s core search system.');
  12. }
  13. }
  14. function search_drush_command() {
  15. $items['search-status'] = array(
  16. 'description' => 'Show how many items remain to be indexed out of the total.',
  17. 'drupal dependencies' => array('search'),
  18. 'outputformat' => array(
  19. 'default' => 'message',
  20. 'pipe-format' => 'message',
  21. 'field-labels' => array('remaining' => 'Items not yet indexed', 'total' => 'Total items'),
  22. 'message-template' => 'There are !remaining items out of !total still to be indexed.',
  23. 'pipe-metadata' => array(
  24. 'message-template' => '!remaining/!total',
  25. ),
  26. 'output-data-type' => 'format-list',
  27. ),
  28. );
  29. $items['search-index'] = array(
  30. 'description' => 'Index the remaining search items without wiping the index.',
  31. 'drupal dependencies' => array('search'),
  32. );
  33. $items['search-reindex'] = array(
  34. 'description' => 'Force the search index to be rebuilt.',
  35. 'drupal dependencies' => array('search'),
  36. 'options' => array(
  37. 'immediate' => 'Rebuild the index immediately, instead of waiting for cron.',
  38. ),
  39. );
  40. return $items;
  41. }
  42. function drush_search_status() {
  43. list($remaining, $total) = _drush_search_status();
  44. return array(
  45. 'remaining' => $remaining,
  46. 'total' => $total,
  47. );
  48. }
  49. function _drush_search_status() {
  50. $remaining = 0;
  51. $total = 0;
  52. if (drush_drupal_major_version() >= 8) {
  53. $search_page_repository = \Drupal::service('search.search_page_repository');
  54. foreach ($search_page_repository->getIndexableSearchPages() as $entity) {
  55. $status = $entity->getPlugin()->indexStatus();
  56. $remaining += $status['remaining'];
  57. $total += $status['total'];
  58. }
  59. }
  60. elseif (drush_drupal_major_version() == 7) {
  61. foreach (variable_get('search_active_modules', array('node', 'user')) as $module) {
  62. drush_include_engine('drupal', 'environment');
  63. $status = drush_module_invoke($module, 'search_status');
  64. $remaining += $status['remaining'];
  65. $total += $status['total'];
  66. }
  67. }
  68. else {
  69. drush_include_engine('drupal', 'environment');
  70. foreach (drush_module_implements('search') as $module) {
  71. // Special case. Apachesolr recommends disabling core indexing with
  72. // search_cron_limit = 0. Need to avoid infinite status loop.
  73. if ($module == 'node' && variable_get('search_cron_limit', 10) == 0) {
  74. continue;
  75. }
  76. $status = drush_module_invoke($module, 'search', 'status');
  77. if (isset($status['remaining']) && isset($status['total'])) {
  78. $remaining += $status['remaining'];
  79. $total += $status['total'];
  80. }
  81. }
  82. }
  83. return array($remaining, $total);
  84. }
  85. function drush_search_index() {
  86. drush_op('_drush_search_index');
  87. drush_log(dt('The search index has been built.'), LogLevel::OK);
  88. }
  89. function _drush_search_index() {
  90. list($remaining, $total) = _drush_search_status();
  91. register_shutdown_function('search_update_totals');
  92. $failures = 0;
  93. while ($remaining > 0) {
  94. $done = $total - $remaining;
  95. $percent = $done / $total * 100;
  96. drush_log(dt('!percent complete. Remaining items to be indexed: !count', array('!percent' => number_format($percent, 2), '!count' => $remaining)), LogLevel::OK);
  97. $eval = "register_shutdown_function('search_update_totals');";
  98. // Use drush_invoke_process() to start subshell. Avoids out of memory issue.
  99. if (drush_drupal_major_version() >= 8) {
  100. $eval = "drush_module_invoke('search', 'cron');";
  101. }
  102. elseif (drush_drupal_major_version() == 7) {
  103. // If needed, prod drush_module_implements() to recognize our
  104. // hook_node_update_index() implementations.
  105. drush_include_engine('drupal', 'environment');
  106. $implementations = drush_module_implements('node_update_index');
  107. if (!in_array('system', $implementations)) {
  108. // Note that this resets module_implements cache.
  109. drush_module_implements('node_update_index', FALSE, TRUE);
  110. }
  111. foreach (variable_get('search_active_modules', array('node', 'user')) as $module) {
  112. // TODO: Make sure that drush_module_invoke is really available when doing this eval().
  113. $eval .= " drush_module_invoke('$module', 'update_index');";
  114. }
  115. }
  116. else {
  117. // If needed, prod module_implements() to recognize our hook_nodeapi()
  118. // implementations.
  119. $implementations = module_implements('nodeapi');
  120. if (!in_array('system', $implementations)) {
  121. // Note that this resets module_implements cache.
  122. module_implements('nodeapi', FALSE, TRUE);
  123. }
  124. $eval .= " module_invoke_all('update_index');";
  125. }
  126. drush_invoke_process('@self', 'php-eval', array($eval));
  127. $previous_remaining = $remaining;
  128. list($remaining, ) = _drush_search_status();
  129. // Make sure we're actually making progress.
  130. if ($remaining == $previous_remaining) {
  131. $failures++;
  132. if ($failures == 3) {
  133. drush_log(dt('Indexing stalled with @number items remaining.', array(
  134. '@number' => $remaining,
  135. )), LogLevel::ERROR);
  136. return;
  137. }
  138. }
  139. // Only count consecutive failures.
  140. else {
  141. $failures = 0;
  142. }
  143. }
  144. }
  145. function drush_search_reindex() {
  146. drush_print(dt("The search index must be fully rebuilt before any new items can be indexed."));
  147. if (drush_get_option('immediate')) {
  148. drush_print(dt("Rebuilding the index may take a long time."));
  149. }
  150. if (!drush_confirm(dt('Do you really want to continue?'))) {
  151. return drush_user_abort();
  152. }
  153. if (drush_drupal_major_version() == 8) {
  154. // D8 CR: https://www.drupal.org/node/2326575
  155. $search_page_repository = \Drupal::service('search.search_page_repository');
  156. foreach ($search_page_repository->getIndexableSearchPages() as $entity) {
  157. $entity->getPlugin()->markForReindex();
  158. }
  159. }
  160. elseif (drush_drupal_major_version() == 7) {
  161. drush_op('search_reindex');
  162. }
  163. else {
  164. drush_op('search_wipe');
  165. }
  166. if (drush_get_option('immediate')) {
  167. drush_op('_drush_search_index');
  168. drush_log(dt('The search index has been rebuilt.'), LogLevel::OK);
  169. }
  170. else {
  171. drush_log(dt('The search index will be rebuilt.'), LogLevel::OK);
  172. }
  173. }
  174. /**
  175. * Fake an implementation of hook_node_update_index() for Drupal 7.
  176. */
  177. function system_node_update_index($node) {
  178. // Verbose output.
  179. if (drush_get_context('DRUSH_VERBOSE')) {
  180. drush_log(dt('Indexing node !nid.', array('!nid' => $node->nid)), LogLevel::OK);
  181. }
  182. }
  183. /**
  184. * Fake an implementation of hook_nodeapi() for Drupal 6.
  185. */
  186. function system_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  187. if ($op == 'update index') {
  188. // Verbose output.
  189. if (drush_get_context('DRUSH_VERBOSE')) {
  190. drush_log(dt('Indexing node !nid.', array('!nid' => $node->nid)), LogLevel::OK);
  191. }
  192. }
  193. }