xmlsitemap.drush.inc 2.71 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
  xmlsitemap_run_unprogressive_batch('xmlsitemap_regenerate_batch');
43 44 45 46

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

57 58
  // Build a list of rebuildable link types.
  $rebuild_types = xmlsitemap_get_rebuildable_link_types();
59 60

  // Run the batch process.
61
  xmlsitemap_run_unprogressive_batch('xmlsitemap_rebuild_batch', $rebuild_types, TRUE);
62 63 64 65

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

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

77
  module_invoke_all('xmlsitemap_index_links', $limit);
78 79 80 81 82 83 84 85
  $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)));
  }
86
}