xmlsitemap.drush.inc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * @file
  4. * Drush integration functions for the xmlsitemap module.
  5. *
  6. * @ingroup xmlsitemap
  7. */
  8. /**
  9. * The default batch limit.
  10. */
  11. define('XMLSITEMAP_BATCH_LIMIT', 100);
  12. /**
  13. * Implements hook_drush_command().
  14. */
  15. function xmlsitemap_drush_command() {
  16. $items['xmlsitemap-regenerate'] = array(
  17. 'description' => 'Regenerate the XML sitemap files.',
  18. 'callback' => 'drush_xmlsitemap_regenerate',
  19. 'drupal dependencies' => array('xmlsitemap'),
  20. );
  21. $items['xmlsitemap-rebuild'] = array(
  22. 'description' => 'Dump and re-process all possible XML sitemap data, and then regenerate the files.',
  23. 'callback' => 'drush_xmlsitemap_rebuild',
  24. 'drupal dependencies' => array('xmlsitemap'),
  25. 'options' => array(
  26. 'types' => 'The types of links to rebuild, comma separated. If not provided, all link types will be used.',
  27. ),
  28. );
  29. $items['xmlsitemap-index'] = array(
  30. 'description' => 'Process un-indexed XML sitemap links.',
  31. 'callback' => 'drush_xmlsitemap_index',
  32. 'drupal dependencies' => array('xmlsitemap'),
  33. 'options' => array(
  34. 'limit' => 'The limit of links of each type to process. Default value: ' . variable_get('xmlsitemap_batch_limit', XMLSITEMAP_BATCH_LIMIT),
  35. ),
  36. );
  37. $items['xmlsitemap-queue-rebuild'] = array(
  38. 'description' => 'Dump and queues all possible XML sitemap data to be re-processed via the xmlsitemap_link_process queue. This command does not regenerate the sitemap files.',
  39. 'options' => array(
  40. 'types' => 'The types of links to queue for rebuild, comma separated. If not provided, all link types will be used.',
  41. 'limit' => 'The number of links to be processed in each queue task.',
  42. ),
  43. );
  44. return $items;
  45. }
  46. /**
  47. * Regenerate the sitemap files from existing data.
  48. */
  49. function drush_xmlsitemap_regenerate() {
  50. module_load_include('generate.inc', 'xmlsitemap');
  51. // Run the batch process.
  52. timer_start('xmlsitemap_regenerate');
  53. xmlsitemap_run_unprogressive_batch('xmlsitemap_regenerate_batch');
  54. $vars = array(
  55. '@timer' => timer_read('xmlsitemap_regenerate'),
  56. '@memory-peak' => format_size(memory_get_peak_usage(TRUE)),
  57. );
  58. drush_print(dt('XML sitemap files regenerated in @timer ms. Peak memory usage: @memory-peak.', $vars));
  59. }
  60. /**
  61. * Dump and rebuild all the sitemap data, then regenerate the files.
  62. */
  63. function drush_xmlsitemap_rebuild() {
  64. module_load_include('generate.inc', 'xmlsitemap');
  65. // Build a list of rebuildable link types.
  66. $types = xmlsitemap_get_rebuildable_link_types();
  67. if ($option_types = drush_get_option('types', '')) {
  68. $option_types = explode(',', $option_types);
  69. if ($invalid_types = array_diff($option_types, $types)) {
  70. drush_set_error(dt('The following link types are invalid: @types', array('@types' => implode(', ', $invalid_types))));
  71. }
  72. $types = array_intersect($types, $option_types);
  73. }
  74. if (empty($types)) {
  75. return drush_set_error(dt('No link types are rebuildable.'));
  76. }
  77. // Run the batch process.
  78. timer_start('xmlsitemap_rebuild');
  79. xmlsitemap_run_unprogressive_batch('xmlsitemap_rebuild_batch', $types, TRUE);
  80. $vars = array(
  81. '@timer' => timer_read('xmlsitemap_rebuild'),
  82. '@memory-peak' => format_size(memory_get_peak_usage(TRUE)),
  83. );
  84. drush_print(dt('XML sitemap files rebuilt in @timer ms. Peak memory usage: @memory-peak.', $vars));
  85. }
  86. /**
  87. * Process un-indexed XML sitemap links.
  88. */
  89. function drush_xmlsitemap_index() {
  90. $limit = (int) drush_get_option('limit', variable_get('xmlsitemap_batch_limit', XMLSITEMAP_BATCH_LIMIT));
  91. $count_before = db_query("SELECT COUNT(id) FROM {xmlsitemap}")->fetchField();
  92. module_invoke_all('xmlsitemap_index_links', $limit);
  93. $count_after = db_query("SELECT COUNT(id) FROM {xmlsitemap}")->fetchField();
  94. if ($count_after == $count_before) {
  95. drush_print(dt('No new XML sitemap links to index.'));
  96. }
  97. else {
  98. drush_print(dt('Indexed @count new XML sitemap links.', array('@count' => $count_after - $count_before)));
  99. }
  100. }
  101. /**
  102. * Dump and queue all the sitemap links to be rebuilt in a queue process.
  103. */
  104. function drush_xmlsitemap_queue_rebuild() {
  105. module_load_include('generate.inc', 'xmlsitemap');
  106. $types = xmlsitemap_get_rebuildable_link_types();
  107. if ($option_types = drush_get_option('types', '')) {
  108. $option_types = explode(',', $option_types);
  109. if ($invalid_types = array_diff($option_types, $types)) {
  110. drush_set_error(dt('The following link types are invalid: @types', array('@types' => implode(', ', $invalid_types))));
  111. }
  112. $types = array_intersect($types, $option_types);
  113. }
  114. if (empty($types)) {
  115. return drush_set_error(dt('No link types are rebuildable.'));
  116. }
  117. xmlsitemap_rebuild_clear($types, TRUE);
  118. $link_count = 0;
  119. $chunk_count = 0;
  120. $chunk_size = (int) drush_get_option('limit', variable_get('xmlsitemap_batch_limit', XMLSITEMAP_BATCH_LIMIT));
  121. // @todo Figure out how to re-use this code with xmlsitemap_rebuild_batch_fetch()
  122. foreach ($types as $type) {
  123. $info = xmlsitemap_get_link_info($type);
  124. $query = new EntityFieldQuery();
  125. $query->entityCondition('entity_type', $type);
  126. $query->entityCondition('entity_id', 0, '>');
  127. $query->addTag('xmlsitemap_link_bundle_access');
  128. $query->addTag('xmlsitemap_rebuild');
  129. $query->addMetaData('entity', $type);
  130. $query->addMetaData('entity_info', $info);
  131. if ($bundles = xmlsitemap_get_link_type_enabled_bundles($type)) {
  132. $query->entityCondition('bundle', $bundles, 'IN');
  133. }
  134. else {
  135. // If no enabled bundle types, skip everything else.
  136. continue;
  137. }
  138. $results = $query->execute();
  139. if (!empty($results[$type])) {
  140. $ids = array_keys($results[$type]);
  141. $link_count += count($ids);
  142. $chunks = array_chunk($ids, $chunk_size);
  143. $chunk_count += count($chunks);
  144. foreach ($chunks as $chunk) {
  145. xmlsitemap_link_enqueue($type, $chunk);
  146. }
  147. }
  148. }
  149. if ($link_count) {
  150. drush_log(dt('Queued @link_count links for rebuild processing in the xmlsitemap_link_process (in @chunk_count chunks of up to @chunk_size links each).', array(
  151. '@link_count' => $link_count,
  152. '@chunk_count' => $chunk_count,
  153. '@chunk_size' => $chunk_size,
  154. )), 'success');
  155. }
  156. else {
  157. drush_log(dt('No links to queue for rebuild processing.'), 'ok');
  158. }
  159. variable_set('xmlsitemap_rebuild_needed', FALSE);
  160. }