Commit dbcf508d authored by Dave Reid's avatar Dave Reid

Added option to limit drush rebuild commands to specific link types.

parent 73fbb26c
...@@ -20,6 +20,9 @@ function xmlsitemap_drush_command() { ...@@ -20,6 +20,9 @@ function xmlsitemap_drush_command() {
'description' => 'Dump and re-process all possible XML sitemap data, and then regenerate the files.', 'description' => 'Dump and re-process all possible XML sitemap data, and then regenerate the files.',
'callback' => 'drush_xmlsitemap_rebuild', 'callback' => 'drush_xmlsitemap_rebuild',
'drupal dependencies' => array('xmlsitemap'), 'drupal dependencies' => array('xmlsitemap'),
'options' => array(
'types' => 'The types of links to rebuild, comma separated. If not provided, all link types will be used.',
),
); );
$items['xmlsitemap-index'] = array( $items['xmlsitemap-index'] = array(
'description' => 'Process un-indexed XML sitemap links.', 'description' => 'Process un-indexed XML sitemap links.',
...@@ -32,6 +35,7 @@ function xmlsitemap_drush_command() { ...@@ -32,6 +35,7 @@ function xmlsitemap_drush_command() {
$items['xmlsitemap-queue-rebuild'] = array( $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.', '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( 'options' => array(
'types' => 'The types of links to queue for rebuild, comma separated. If not provided, all link types will be used.',
'limit' => 'The number of links to be processed in each queue task.', 'limit' => 'The number of links to be processed in each queue task.',
), ),
); );
...@@ -62,14 +66,20 @@ function drush_xmlsitemap_rebuild() { ...@@ -62,14 +66,20 @@ function drush_xmlsitemap_rebuild() {
module_load_include('generate.inc', 'xmlsitemap'); module_load_include('generate.inc', 'xmlsitemap');
// Build a list of rebuildable link types. // Build a list of rebuildable link types.
$rebuild_types = xmlsitemap_get_rebuildable_link_types(); $types = xmlsitemap_get_rebuildable_link_types();
if (empty($rebuild_types)) { if ($option_types = explode(',', drush_get_option('types', ''))) {
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)) {
return drush_set_error(dt('No link types are rebuildable.')); return drush_set_error(dt('No link types are rebuildable.'));
} }
// Run the batch process. // Run the batch process.
timer_start('xmlsitemap_rebuild'); timer_start('xmlsitemap_rebuild');
xmlsitemap_run_unprogressive_batch('xmlsitemap_rebuild_batch', $rebuild_types, TRUE); xmlsitemap_run_unprogressive_batch('xmlsitemap_rebuild_batch', $types, TRUE);
$vars = array( $vars = array(
'@timer' => timer_read('xmlsitemap_rebuild'), '@timer' => timer_read('xmlsitemap_rebuild'),
...@@ -101,30 +111,36 @@ function drush_xmlsitemap_index() { ...@@ -101,30 +111,36 @@ function drush_xmlsitemap_index() {
*/ */
function drush_xmlsitemap_queue_rebuild() { function drush_xmlsitemap_queue_rebuild() {
module_load_include('generate.inc', 'xmlsitemap'); module_load_include('generate.inc', 'xmlsitemap');
$entity_types = xmlsitemap_get_rebuildable_link_types();
if (empty($entity_types)) { $types = xmlsitemap_get_rebuildable_link_types();
if ($option_types = explode(',', drush_get_option('types', ''))) {
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)) {
return drush_set_error(dt('No link types are rebuildable.')); return drush_set_error(dt('No link types are rebuildable.'));
} }
$context = array(); xmlsitemap_rebuild_clear($types, TRUE);
xmlsitemap_rebuild_batch_clear($entity_types, TRUE, $context);
$link_count = 0; $link_count = 0;
$chunk_count = 0; $chunk_count = 0;
$chunk_size = (int) drush_get_option('limit', variable_get('xmlsitemap_batch_limit', 100)); $chunk_size = (int) drush_get_option('limit', variable_get('xmlsitemap_batch_limit', 100));
// @todo Figure out how to re-use this code with xmlsitemap_rebuild_batch_fetch() // @todo Figure out how to re-use this code with xmlsitemap_rebuild_batch_fetch()
foreach ($entity_types as $entity_type) { foreach ($types as $type) {
$info = xmlsitemap_get_link_info($entity_type); $info = xmlsitemap_get_link_info($type);
$query = new EntityFieldQuery(); $query = new EntityFieldQuery();
$query->entityCondition('entity_type', $entity_type); $query->entityCondition('entity_type', $type);
$query->entityCondition('entity_id', 0, '>'); $query->entityCondition('entity_id', 0, '>');
$query->addTag('xmlsitemap_link_bundle_access'); $query->addTag('xmlsitemap_link_bundle_access');
$query->addTag('xmlsitemap_rebuild'); $query->addTag('xmlsitemap_rebuild');
$query->addMetaData('entity', $entity_type); $query->addMetaData('entity', $type);
$query->addMetaData('entity_info', $info); $query->addMetaData('entity_info', $info);
if ($types = xmlsitemap_get_link_type_enabled_bundles($entity_type)) { if ($bundles = xmlsitemap_get_link_type_enabled_bundles($type)) {
$query->entityCondition('bundle', $types, 'IN'); $query->entityCondition('bundle', $bundles, 'IN');
} }
else { else {
// If no enabled bundle types, skip everything else. // If no enabled bundle types, skip everything else.
...@@ -132,13 +148,13 @@ function drush_xmlsitemap_queue_rebuild() { ...@@ -132,13 +148,13 @@ function drush_xmlsitemap_queue_rebuild() {
} }
$results = $query->execute(); $results = $query->execute();
if (!empty($results[$entity_type])) { if (!empty($results[$type])) {
$ids = array_keys($results[$entity_type]); $ids = array_keys($results[$type]);
$link_count += count($ids); $link_count += count($ids);
$chunks = array_chunk($ids, $chunk_size); $chunks = array_chunk($ids, $chunk_size);
$chunk_count += count($chunks); $chunk_count += count($chunks);
foreach ($chunks as $chunk) { foreach ($chunks as $chunk) {
xmlsitemap_link_enqueue($entity_type, $chunk); xmlsitemap_link_enqueue($type, $chunk);
} }
} }
} }
......
...@@ -424,22 +424,8 @@ function xmlsitemap_batch_variable_set(array $variables) { ...@@ -424,22 +424,8 @@ function xmlsitemap_batch_variable_set(array $variables) {
*/ */
function xmlsitemap_rebuild_batch_clear(array $entities, $save_custom, &$context) { function xmlsitemap_rebuild_batch_clear(array $entities, $save_custom, &$context) {
if (!empty($entities)) { if (!empty($entities)) {
// Let other modules respond to the rebuild clearing. xmlsitemap_rebuild_clear($entities, $save_custom);
module_invoke_all('xmlsitemap_rebuild_clear', $entities, $save_custom);
$query = db_delete('xmlsitemap');
$query->condition('type', $entities);
// If we want to save the custom data, make sure to exclude any links
// that are not using default inclusion or priority.
if ($save_custom) {
$query->condition('status_override', 0);
$query->condition('priority_override', 0);
}
$query->execute();
} }
$context['message'] = t('Purging links.'); $context['message'] = t('Purging links.');
} }
...@@ -534,3 +520,32 @@ function xmlsitemap_get_rebuildable_link_types() { ...@@ -534,3 +520,32 @@ function xmlsitemap_get_rebuildable_link_types() {
return $rebuild_types; return $rebuild_types;
} }
/**
* Clear all sitemap links for given entity types.
*
* @param array $types
* An array of link types.
* @param bool $save_custom
* A boolean if links with status or priority overridden should not be
* removed (and hence overridden values not lost).
*
* @return int
* The number of deleted links.
*/
function xmlsitemap_rebuild_clear(array $types, $save_custom) {
// Let other modules respond to the rebuild clearing.
module_invoke_all('xmlsitemap_rebuild_clear', $types, $save_custom);
$query = db_delete('xmlsitemap');
$query->condition('type', $types);
// If we want to save the custom data, make sure to exclude any links
// that are not using default inclusion or priority.
if ($save_custom) {
$query->condition('status_override', 0);
$query->condition('priority_override', 0);
}
return $query->execute();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment