xmlsitemap.drush.inc 6.24 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Drush integration functions for the xmlsitemap module.
 *
 * @ingroup xmlsitemap
 */

10 11 12 13 14
/**
 * The default batch limit.
 */
define('XMLSITEMAP_BATCH_LIMIT', 100);

15 16 17 18
/**
 * Implements hook_drush_command().
 */
function xmlsitemap_drush_command() {
19

20
  $items['xmlsitemap-regenerate'] = array(
21
    'description' => 'Regenerate the XML sitemap files.',
22
    'callback' => 'drush_xmlsitemap_regenerate',
23
    'drupal dependencies' => array('xmlsitemap'),
24
  );
25
  $items['xmlsitemap-rebuild'] = array(
26
    'description' => 'Dump and re-process all possible XML sitemap data, and then regenerate the files.',
27
    'callback' => 'drush_xmlsitemap_rebuild',
28
    'drupal dependencies' => array('xmlsitemap'),
29 30 31
    'options' => array(
      'types' => 'The types of links to rebuild, comma separated. If not provided, all link types will be used.',
    ),
32
  );
33 34
  $items['xmlsitemap-index'] = array(
    'description' => 'Process un-indexed XML sitemap links.',
35
    'callback' => 'drush_xmlsitemap_index',
36 37
    'drupal dependencies' => array('xmlsitemap'),
    'options' => array(
38
      'limit' => 'The limit of links of each type to process. Default value: ' . variable_get('xmlsitemap_batch_limit', XMLSITEMAP_BATCH_LIMIT),
39 40
    ),
  );
41 42 43
  $items['xmlsitemap-queue-rebuild'] = array(
    '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.',
    'options' => array(
44
      'types' => 'The types of links to queue for rebuild, comma separated. If not provided, all link types will be used.',
45 46 47
      'limit' => 'The number of links to be processed in each queue task.',
    ),
  );
48 49 50 51 52 53 54
  return $items;
}

/**
 * Regenerate the sitemap files from existing data.
 */
function drush_xmlsitemap_regenerate() {
55 56 57
  module_load_include('generate.inc', 'xmlsitemap');

  // Run the batch process.
58
  timer_start('xmlsitemap_regenerate');
59
  xmlsitemap_run_unprogressive_batch('xmlsitemap_regenerate_batch');
60 61 62 63

  $vars = array(
    '@timer' => timer_read('xmlsitemap_regenerate'),
    '@memory-peak' => format_size(memory_get_peak_usage(TRUE)),
64
  );
65 66 67 68 69 70 71
  drush_print(dt('XML sitemap files regenerated in @timer ms. Peak memory usage: @memory-peak.', $vars));
}

/**
 * Dump and rebuild all the sitemap data, then regenerate the files.
 */
function drush_xmlsitemap_rebuild() {
72
  module_load_include('generate.inc', 'xmlsitemap');
73

74
  // Build a list of rebuildable link types.
75
  $types = xmlsitemap_get_rebuildable_link_types();
Dave Reid's avatar
Dave Reid committed
76 77
  if ($option_types = drush_get_option('types', '')) {
    $option_types = explode(',', $option_types);
78 79 80 81 82 83
    if ($invalid_types = array_diff($option_types, $types)) {
      drush_set_error(dt('The following link types are invalid: @types', array('@types' => implode(', ', $invalid_types))));
    }
    $types = array_intersect($types, $option_types);
  }
  if (empty($types)) {
84
    return drush_log(dt('No link types are rebuildable.'), 'warning');
85
  }
86 87

  // Run the batch process.
88
  timer_start('xmlsitemap_rebuild');
89
  xmlsitemap_run_unprogressive_batch('xmlsitemap_rebuild_batch', $types, TRUE);
90 91 92 93

  $vars = array(
    '@timer' => timer_read('xmlsitemap_rebuild'),
    '@memory-peak' => format_size(memory_get_peak_usage(TRUE)),
94
  );
95 96
  drush_print(dt('XML sitemap files rebuilt in @timer ms. Peak memory usage: @memory-peak.', $vars));
}
97 98 99 100 101

/**
 * Process un-indexed XML sitemap links.
 */
function drush_xmlsitemap_index() {
102
  $limit = (int) drush_get_option('limit', variable_get('xmlsitemap_batch_limit', XMLSITEMAP_BATCH_LIMIT));
103 104
  $count_before = db_query("SELECT COUNT(id) FROM {xmlsitemap}")->fetchField();

105
  module_invoke_all('xmlsitemap_index_links', $limit);
106 107 108 109 110 111 112 113
  $count_after = db_query("SELECT COUNT(id) FROM {xmlsitemap}")->fetchField();

  if ($count_after == $count_before) {
    drush_print(dt('No new XML sitemap links to index.'));
  }
  else {
    drush_print(dt('Indexed @count new XML sitemap links.', array('@count' => $count_after - $count_before)));
  }
114
}
115 116 117 118 119 120

/**
 * Dump and queue all the sitemap links to be rebuilt in a queue process.
 */
function drush_xmlsitemap_queue_rebuild() {
  module_load_include('generate.inc', 'xmlsitemap');
121 122

  $types = xmlsitemap_get_rebuildable_link_types();
Dave Reid's avatar
Dave Reid committed
123 124
  if ($option_types = drush_get_option('types', '')) {
    $option_types = explode(',', $option_types);
125 126 127 128 129 130
    if ($invalid_types = array_diff($option_types, $types)) {
      drush_set_error(dt('The following link types are invalid: @types', array('@types' => implode(', ', $invalid_types))));
    }
    $types = array_intersect($types, $option_types);
  }
  if (empty($types)) {
131
    return drush_log(dt('No link types are rebuildable.'), 'warning');
132 133
  }

134
  xmlsitemap_rebuild_clear($types, TRUE);
135 136 137

  $link_count = 0;
  $chunk_count = 0;
138
  $chunk_size = (int) drush_get_option('limit', variable_get('xmlsitemap_batch_limit', XMLSITEMAP_BATCH_LIMIT));
139 140

  // @todo Figure out how to re-use this code with xmlsitemap_rebuild_batch_fetch()
141 142
  foreach ($types as $type) {
    $info = xmlsitemap_get_link_info($type);
143
    $query = new EntityFieldQuery();
144
    $query->entityCondition('entity_type', $type);
145 146 147
    $query->entityCondition('entity_id', 0, '>');
    $query->addTag('xmlsitemap_link_bundle_access');
    $query->addTag('xmlsitemap_rebuild');
148
    $query->addMetaData('entity', $type);
149
    $query->addMetaData('entity_info', $info);
150 151
    if ($bundles = xmlsitemap_get_link_type_enabled_bundles($type)) {
      $query->entityCondition('bundle', $bundles, 'IN');
152 153 154 155 156 157 158
    }
    else {
      // If no enabled bundle types, skip everything else.
      continue;
    }

    $results = $query->execute();
159 160
    if (!empty($results[$type])) {
      $ids = array_keys($results[$type]);
161 162 163 164
      $link_count += count($ids);
      $chunks = array_chunk($ids, $chunk_size);
      $chunk_count += count($chunks);
      foreach ($chunks as $chunk) {
165
        xmlsitemap_link_enqueue($type, $chunk);
166 167 168 169 170
      }
    }
  }

  if ($link_count) {
171 172 173 174 175
    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');
176 177 178 179
  }
  else {
    drush_log(dt('No links to queue for rebuild processing.'), 'ok');
  }
180
  variable_set('xmlsitemap_rebuild_needed', FALSE);
181
}