Commit a50c2df4 authored by dww's avatar dww Committed by pifagor

Issue #2026551 by dww, Chris Matthews, pifagor: Add an API and drush command...

Issue #2026551 by dww, Chris Matthews, pifagor: Add an API and drush command to scan a directory for managing custom links for static files
parent 4c980404
<?php
/**
* @file
* Drush integration functions for the xmlsitemap_custom module.
*
* @ingroup xmlsitemap
*/
/**
* Implements hook_drush_command().
*/
function xmlsitemap_custom_drush_command() {
$items['xmlsitemap-custom-scan'] = array(
'description' => 'Scan a directory for static files to include as custom XML sitemap links.',
'arguments' => array(
'directory' => 'The base directory to scan, without trailing slash.',
'mask' => 'The preg_match() regular expression of the files to find.',
),
'required-arguments' => TRUE,
'callback' => 'drush_xmlsitemap_custom_scan',
'drupal dependencies' => array('xmlsitemap_custom'),
);
return $items;
}
/**
* Scan a directory for static files to include via custom XML sitemap links.
*/
function drush_xmlsitemap_custom_scan($directory, $mask) {
module_load_include('scan.inc', 'xmlsitemap_custom');
drush_print(dt('About to scan @directory with @mask.', array('@directory' => $directory, '@mask' => $mask)));
$results = xml_custom_scan_directories(array($directory => $mask));
drush_print(dt('Scanned @directory: added @add_count and removed @delete_count.', array('@directory' => $directory, '@add_count' => count($results[$directory]['add']), '@delete_count' => count($results[$directory]['delete']))));
}
<?php
/**
* @file
* Code for injecting custom XML sitemap entries for legacy stories.
*/
/**
* Scan directories and keep matching subtrees of the custom XML sitemap.
*
* This will ensure that there are XML sitemap custom links for every file
* that matches, and remove any custom links that point to files that don't
* exist. Note that if you call this with a more restrictive mask for a given
* directory, the scan might remove custom links that point to existing files
* (they just don't match the given $mask).
*
* $param array $directories
* An associative array keyed by directory names where the values are
* preg_match() regular expressions of the files to find.
*
* @return array
* A nested array of results, keyed by directory name, where the values are
* associative arrays with the keys 'add' or 'delete' where the values are
* filenames of links that were added or deleted during the scan.
*
* @see file_scan_directory()
*/
function xml_custom_scan_directories(array $directories) {
$results = array();
foreach ($directories as $directory => $mask) {
$files = file_scan_directory($directory, $mask);
$custom_links = db_select('xmlsitemap', 'x')
->fields('x', array('loc', 'id'))
->condition('x.type', 'custom')
->condition('x.subtype', $directory)
->execute()
->fetchAllAssoc('loc', PDO::FETCH_ASSOC);
watchdog('xmlsitemap', 'Files: <pre>' . var_export($files, TRUE), WATCHDOG_NOTICE);
watchdog('xmlsitemap', 'Custom: <pre>' . var_export($custom_links, TRUE), WATCHDOG_NOTICE);
foreach (array_diff_key($files, $custom_links) as $missing => $info) {
$link = array(
'type' => 'custom',
'subtype' => $directory,
'loc' => $missing,
'id' => db_query("SELECT MAX(id) FROM {xmlsitemap} WHERE type = 'custom'")->fetchField() + 1,
'priority' => XMLSITEMAP_PRIORITY_DEFAULT,
'lastmod' => 0,
'changefreq' => 0,
'changecount' => 0,
'language' => LANGUAGE_NONE,
);
xmlsitemap_link_save($link);
watchdog('xmlsitemap', 'Added a custom link for %loc.', array('%loc' => $link['loc']), WATCHDOG_NOTICE);
$results[$directory]['add'][] = $link['loc'];
}
foreach (array_diff_key($custom_links, $files) as $extra => $info) {
xmlsitemap_link_delete('custom', $custom_links[$extra]);
$results[$directory]['delete'][] = $extra;
watchdog('xmlsitemap', 'Deleted the custom link for %loc.', array('%loc' => $extra), WATCHDOG_NOTICE);
}
}
return $results;
}
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