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
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. function search_drush_command() {
  3. $items['search-status'] = array(
  4. 'description' => 'Show how many items remain to be indexed out of the total.',
  5. 'drupal dependencies' => array('search'),
  6. 'outputformat' => array(
  7. 'default' => 'message',
  8. 'pipe-format' => 'message',
  9. 'field-labels' => array('remaining' => 'Items not yet indexed', 'total' => 'Total items'),
  10. 'message-template' => 'There are !remaining items out of !total still to be indexed.',
  11. 'pipe-metadata' => array(
  12. 'message-template' => '!remaining/!total',
  13. ),
  14. 'output-data-type' => 'format-list',
  15. ),
  16. );
  17. $items['search-index'] = array(
  18. 'description' => 'Index the remaining search items without wiping the index.',
  19. 'drupal dependencies' => array('search'),
  20. );
  21. $items['search-reindex'] = array(
  22. 'description' => 'Force the search index to be rebuilt.',
  23. 'drupal dependencies' => array('search'),
  24. 'options' => array(
  25. 'immediate' => 'Rebuild the index immediately, instead of waiting for cron.',
  26. ),
  27. );
  28. return $items;
  29. }
  30. function drush_search_status() {
  31. list($remaining, $total) = _drush_search_status();
  32. return array(
  33. 'remaining' => $remaining,
  34. 'total' => $total,
  35. );
  36. }
  37. function _drush_search_status() {
  38. $remaining = 0;
  39. $total = 0;
  40. if (drush_drupal_major_version() >= 7) {
  41. foreach (variable_get('search_active_modules', array('node', 'user')) as $module) {
  42. $status = module_invoke($module, 'search_status');
  43. $remaining += $status['remaining'];
  44. $total += $status['total'];
  45. }
  46. }
  47. else {
  48. foreach (module_implements('search') as $module) {
  49. // Special case. Apachesolr recommends disabling core indexing with
  50. // search_cron_limit = 0. Need to avoid infinite status loop.
  51. if ($module == 'node' && variable_get('search_cron_limit', 10) == 0) {
  52. continue;
  53. }
  54. $status = module_invoke($module, 'search', 'status');
  55. if (isset($status['remaining']) && isset($status['total'])) {
  56. $remaining += $status['remaining'];
  57. $total += $status['total'];
  58. }
  59. }
  60. }
  61. return array($remaining, $total);
  62. }
  63. function drush_search_index() {
  64. drush_op('_drush_search_index');
  65. drush_log(dt('The search index has been built.'), 'ok');
  66. }
  67. function _drush_search_index() {
  68. list($remaining, ) = _drush_search_status();
  69. register_shutdown_function('search_update_totals');
  70. $failures = 0;
  71. while ($remaining > 0) {
  72. drush_log(dt('Remaining items to be indexed: ' . $remaining), 'ok');
  73. // Use drush_invoke_process() to start subshell. Avoids out of memory issue.
  74. $eval = "register_shutdown_function('search_update_totals');";
  75. if (drush_drupal_major_version() >= 7) {
  76. // If needed, prod module_implements() to recognize our
  77. // hook_node_update_index() implementations.
  78. $implementations = module_implements('node_update_index');
  79. if (!in_array('system', $implementations)) {
  80. // Note that this resets module_implements cache.
  81. module_implements('node_update_index', FALSE, TRUE);
  82. }
  83. foreach (variable_get('search_active_modules', array('node', 'user')) as $module) {
  84. $eval .= " module_invoke('$module', 'update_index');";
  85. }
  86. }
  87. else {
  88. // If needed, prod module_implements() to recognize our hook_nodeapi()
  89. // implementations.
  90. $implementations = module_implements('nodeapi');
  91. if (!in_array('system', $implementations)) {
  92. // Note that this resets module_implements cache.
  93. module_implements('nodeapi', FALSE, TRUE);
  94. }
  95. $eval .= " module_invoke_all('update_index');";
  96. }
  97. drush_invoke_process('@self', 'php-eval', array($eval));
  98. $previous_remaining = $remaining;
  99. list($remaining, ) = _drush_search_status();
  100. // Make sure we're actually making progress.
  101. if ($remaining == $previous_remaining) {
  102. $failures++;
  103. if ($failures == 3) {
  104. drush_log(dt('Indexing stalled with @number items remaining.', array(
  105. '@number' => $remaining,
  106. )), 'error');
  107. return;
  108. }
  109. }
  110. // Only count consecutive failures.
  111. else {
  112. $failures = 0;
  113. }
  114. }
  115. }
  116. function drush_search_reindex() {
  117. drush_print(dt("The search index must be fully rebuilt before any new items can be indexed."));
  118. if (drush_get_option('immediate')) {
  119. drush_print(dt("Rebuilding the index may take a long time."));
  120. }
  121. if (!drush_confirm(dt('Do you really want to continue?'))) {
  122. return drush_user_abort();
  123. }
  124. if (drush_drupal_major_version() >= 7) {
  125. drush_op('search_reindex');
  126. }
  127. else {
  128. drush_op('search_wipe');
  129. }
  130. if (drush_get_option('immediate')) {
  131. drush_op('_drush_search_index');
  132. drush_log(dt('The search index has been rebuilt.'), 'ok');
  133. }
  134. else {
  135. drush_log(dt('The search index will be rebuilt.'), 'ok');
  136. }
  137. }
  138. /**
  139. * Fake an implementation of hook_node_update_index() for Drupal 7.
  140. */
  141. function system_node_update_index($node) {
  142. // Verbose output.
  143. if (drush_get_context('DRUSH_VERBOSE')) {
  144. drush_log(dt('Indexing node !nid.', array('!nid' => $node->nid)), 'ok');
  145. }
  146. }
  147. /**
  148. * Fake an implementation of hook_nodeapi() for Drupal 6.
  149. */
  150. function system_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  151. if ($op == 'update index') {
  152. // Verbose output.
  153. if (drush_get_context('DRUSH_VERBOSE')) {
  154. drush_log(dt('Indexing node !nid.', array('!nid' => $node->nid)), 'ok');
  155. }
  156. }
  157. }