xmlsitemap.drush.inc 2.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

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

/**
 * Implements hook_drush_command().
 */
function xmlsitemap_drush_command() {
14
  $items['xmlsitemap-regenerate'] = array(
15
    'description' => 'Regenerate the XML sitemap files.',
16
    'callback' => 'drush_xmlsitemap_regenerate',
17
    'drupal dependencies' => array('xmlsitemap'),
18
  );
19
  $items['xmlsitemap-rebuild'] = array(
20
    'description' => 'Dump and re-process all possible XML sitemap data, and then regenerate the files.',
21
    'callback' => 'drush_xmlsitemap_rebuild',
22
    'drupal dependencies' => array('xmlsitemap'),
23
  );
24 25
  $items['xmlsitemap-index'] = array(
    'description' => 'Process un-indexed XML sitemap links.',
26
    'callback' => 'drush_xmlsitemap_index',
27 28
    'drupal dependencies' => array('xmlsitemap'),
    'options' => array(
29
      'limit' => 'The limit of links of each type to process. Default value: ' . variable_get('xmlsitemap_batch_limit', 100),
30 31
    ),
  );
32 33 34 35 36 37 38
  return $items;
}

/**
 * Regenerate the sitemap files from existing data.
 */
function drush_xmlsitemap_regenerate() {
39 40 41
  module_load_include('generate.inc', 'xmlsitemap');

  // Run the batch process.
42
  timer_start('xmlsitemap_regenerate');
43
  xmlsitemap_run_unprogressive_batch('xmlsitemap_regenerate_batch');
44 45 46 47

  $vars = array(
    '@timer' => timer_read('xmlsitemap_regenerate'),
    '@memory-peak' => format_size(memory_get_peak_usage(TRUE)),
48
  );
49 50 51 52 53 54 55
  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() {
56
  module_load_include('generate.inc', 'xmlsitemap');
57

58 59
  // Build a list of rebuildable link types.
  $rebuild_types = xmlsitemap_get_rebuildable_link_types();
60
  if (empty($rebuild_types)) {
Dave Reid's avatar
Dave Reid committed
61
    return drush_set_error(dt('No link types are rebuildable.'));
62
  }
63 64

  // Run the batch process.
65
  timer_start('xmlsitemap_rebuild');
66
  xmlsitemap_run_unprogressive_batch('xmlsitemap_rebuild_batch', $rebuild_types, TRUE);
67 68 69 70

  $vars = array(
    '@timer' => timer_read('xmlsitemap_rebuild'),
    '@memory-peak' => format_size(memory_get_peak_usage(TRUE)),
71
  );
72 73
  drush_print(dt('XML sitemap files rebuilt in @timer ms. Peak memory usage: @memory-peak.', $vars));
}
74 75 76 77 78

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

82
  module_invoke_all('xmlsitemap_index_links', $limit);
83 84 85 86 87 88 89 90
  $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)));
  }
91
}