xmlsitemap.drush.inc 6.1 KB

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