Commit 66132e70 authored by Dave Reid's avatar Dave Reid

#768182 by Dave Reid: Handle sitemap directory hash changes.

parent 544c6fba
......@@ -477,38 +477,25 @@ function xmlsitemap_update_6203() {
* Convert {xmlsitemap}.context_hash to replace {xmlsitemap}.smid.
*/
function xmlsitemap_update_6204() {
if (!db_field_exists('xmlsitemap_sitemap', 'context_hash')) {
return;
}
db_drop_unique_key('xmlsitemap_sitemap', 'context_hash');
db_drop_field('xmlsitemap_sitemap', 'smid');
// Rename context_hash to the new smid column.
$smid_field = array(
'description' => 'The sitemap ID (the hashed value of {xmlsitemap}.context.',
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
);
db_change_field('xmlsitemap_sitemap', 'context_hash', 'smid', $smid_field);
// Remove and re-add the primary key now that the smid field is changed.
db_drop_primary_key('xmlsitemap_sitemap');
db_add_primary_key('xmlsitemap_sitemap', array('smid'));
if (db_field_exists('xmlsitemap_sitemap', 'context_hash')) {
db_drop_unique_key('xmlsitemap_sitemap', 'context_hash');
db_drop_field('xmlsitemap_sitemap', 'smid');
// Rename context_hash to the new smid column.
$smid_field = array(
'description' => 'The sitemap ID (the hashed value of {xmlsitemap}.context.',
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
);
db_change_field('xmlsitemap_sitemap', 'context_hash', 'smid', $smid_field);
// Reload the schema cache and reprocess all sitemap hashes into smids.
drupal_load('module', 'xmlsitemap');
drupal_get_schema(NULL, TRUE);
$sitemaps = xmlsitemap_sitemap_load_multiple(FALSE);
foreach ($sitemaps as $sitemap) {
$sitemap['smid_old'] = $sitemap['smid'];
$sitemap['smid'] = xmlsitemap_sitemap_get_context_hash($sitemap['context']);
db_update('xmlsitemap_sitemap')
->fields(array('smid' => $sitemap['smid']))
->condition('smid', $sitemap['smid_old'])
->execute();
// Remove and re-add the primary key now that the smid field is changed.
db_drop_primary_key('xmlsitemap_sitemap');
db_add_primary_key('xmlsitemap_sitemap', array('smid'));
}
_xmlsitemap_sitemap_rehash_all();
}
/**
......@@ -541,4 +528,20 @@ function xmlsitemap_update_7202() {
*/
function xmlsitemap_update_7203() {
xmlsitemap_update_6204();
_xmlsitemap_sitemap_rehash_all();
}
function _xmlsitemap_sitemap_rehash_all() {
// Reload the schema cache and reprocess all sitemap hashes into smids.
drupal_load('module', 'xmlsitemap');
drupal_get_schema(NULL, TRUE);
// Force a rehash of all sitemaps.
$sitemaps = xmlsitemap_sitemap_load_multiple(FALSE);
foreach ($sitemaps as $sitemap) {
$hash = xmlsitemap_sitemap_get_context_hash($sitemap['context']);
if ($hash != $sitemap['smid']) {
xmlsitemap_sitemap_save($sitemap);
}
}
}
......@@ -64,6 +64,8 @@ function xmlsitemap_hook_info() {
'xmlsitemap_context_info',
'xmlsitemap_context_info_alter',
'xmlsitemap_context',
'xmlsitemap_sitemap_insert',
'xmlsitemap_sitemap_update',
'xmlsitemap_sitemap_operations',
'xmlsitemap_sitemap_delete',
'xmlsitemap_sitemap_uri_alter',
......@@ -341,11 +343,12 @@ function xmlsitemap_sitemap_load($smid) {
* An array of conditions in the form 'field' => $value.
*/
function xmlsitemap_sitemap_load_multiple($smids = array(), array $conditions = array()) {
$query = db_select('xmlsitemap_sitemap');
$query->fields('xmlsitemap_sitemap');
if ($smids !== FALSE) {
$conditions['smid'] = $smids;
}
$query = db_select('xmlsitemap_sitemap');
$query->fields('xmlsitemap_sitemap');
foreach ($conditions as $field => $value) {
$query->condition($field, $value);
}
......@@ -386,15 +389,35 @@ function xmlsitemap_sitemap_load_by_context(array $context = NULL) {
*
* @todo Save the sitemap's URL as a column?
*/
function xmlsitemap_sitemap_save(array $sitemap) {
function xmlsitemap_sitemap_save(array &$sitemap) {
// Make sure context is sorted before saving the hash.
$smid_old = isset($sitemap['smid']) ? $sitemap['smid'] : NULL;
$sitemap['smid'] = xmlsitemap_sitemap_get_context_hash($sitemap['context']);
if (!empty($sitemap['smid'])) {
drupal_write_record('xmlsitemap_sitemap', $sitemap, array('smid'));
// If the context was changed, we need to perform additional actions.
if (isset($smid_old) && $sitemap['smid'] != $smid_old) {
// Rename the files directory so the sitemap does not break.
$old_dir = xmlsitemap_get_directory(array('smid' => $smid_old));
$new_dir = xmlsitemap_get_directory($sitemap);
xmlsitemap_directory_move($old_dir, $new_dir);
// Change the smid field so drupal_write_record() does not fail.
db_update('xmlsitemap_sitemap')
->fields(array('smid' => $sitemap['smid']))
->condition('smid', $smid_old)
->execute();
// Mark the sitemaps as needing regeneration.
variable_set('xmlsitemap_regenerate_needed', TRUE);
}
else {
if (empty($sitemap['smid'])) {
drupal_write_record('xmlsitemap_sitemap', $sitemap);
module_invoke_all('xmlsitemap_sitemap_insert', $sitemap);
}
else {
drupal_write_record('xmlsitemap_sitemap', $sitemap, array('smid'));
module_invoke_all('xmlsitemap_sitemap_update', $sitemap);
}
return $sitemap;
......@@ -447,7 +470,6 @@ function xmlsitemap_sitemap_get_file(array $sitemap, $chunk = 'index') {
*
* @param $sitemap
* The XML sitemap array.
*
*/
function xmlsitemap_sitemap_get_max_filesize(array &$sitemap) {
$dir = xmlsitemap_get_directory($sitemap);
......@@ -658,6 +680,7 @@ function xmlsitemap_link_delete_multiple(array $conditions) {
foreach ($conditions as $field => $value) {
$query->condition($field, $value);
}
return $query->execute();
}
......@@ -747,10 +770,15 @@ function xmlsitemap_get_directory(array $sitemap = NULL) {
$directory = &drupal_static(__FUNCTION__);
if (!isset($directory)) {
$directory = file_directory_path() . '/' . variable_get('xmlsitemap_path', 'xmlsitemap');
$directory = variable_get('xmlsitemap_path', 'xmlsitemap');
}
return $directory . (!empty($sitemap) ? '/' . $sitemap['smid'] : '');
if (!empty($sitemap['smid'])) {
return file_build_uri($directory . '/' . $sitemap['smid']);
}
else {
return file_build_uri($directory);
}
}
/**
......@@ -792,6 +820,39 @@ function xmlsitemap_clear_directory(array $sitemap = NULL, $delete = FALSE) {
return _xmlsitemap_delete_recursive($directory, $delete);
}
/**
* Move a directory to a new location.
*
* @param $old_dir
* A string specifying the filepath or URI of the original directory.
* @param $new_dir
* A string specifying the filepath or URI of the new directory.
* @param $replace
* Replace behavior when the destination file already exists.
*
* @return
* TRUE if the directory was moved successfully. FALSE otherwise.
*/
function xmlsitemap_directory_move($old_dir, $new_dir, $replace = FILE_EXISTS_REPLACE) {
$success = file_prepare_directory($new_dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
$old_path = drupal_realpath($old_dir);
$new_path = drupal_realpath($new_dir);
if (!is_dir($old_path) || !is_dir($new_path) || !$success) {
return FALSE;
}
$files = file_scan_directory($old_dir, '/.*/');
foreach ($files as $file) {
$file->uri_new = $new_dir . '/' . basename($file->filename);
$success &= (bool) file_unmanaged_move($file->uri, $file->uri_new, $replace);
}
// The remove the directory.
$success &= drupal_rmdir($old_dir);
return $success;
}
/**
* Recursively delete all files and folders in the specified filepath.
*
......@@ -907,30 +968,25 @@ function xmlsitemap_get_link_type_enabled_bundles($entity_type) {
function xmlsitemap_get_link_type_indexed_status($entity_type, $bundle = '') {
$info = xmlsitemap_get_link_info($entity_type);
$status = array();
$indexed = db_select('xmlsitemap');
$indexed->condition('type', $entity_type);
$indexed->condition('subtype', $bundle);
$visible = clone $indexed;
$visible->condition('status', 1);
$visible->condition('access', 1);
$status['indexed'] = $indexed->countQuery()->execute()->fetchField();
$status['visible'] = $visible->countQuery()->execute()->fetchField();
$status['indexed'] = db_query("SELECT COUNT(id) FROM {xmlsitemap} WHERE type = :entity AND subtype = :bundle", array(':entity' => $entity_type, ':bundle' => $bundle))->fetchField();
$status['visible'] = db_query("SELECT COUNT(id) FROM {xmlsitemap} WHERE type = :entity AND subtype = :bundle AND status = 1 AND access = 1", array(':entity' => $entity_type, ':bundle' => $bundle))->fetchField();
// @todo Convert this query to a countQuery().
$total = db_select($info['base table'], 'base');
$total->addField('base', $info['entity keys']['id']);
$total->addExpression('COUNT(:id)', 'count_expression', array(':id' => $info['entity keys']['id']));
//$total->addField('base', $info['entity keys']['id']);
$total->condition($info['entity keys']['id'], 0, '>');
$total->addTag('xmlsitemap_link_bundle_access');
$total->addTag('xmlsitemap_link_indexed_status');
$total->addMetaData('entity', $entity_type);
$total->addMetaData('bundle', $bundle);
$total->addMetaData('entity_info', $info);
$status['total'] = $total->countQuery()->execute()->fetchField();
$status['total'] = $total->/*countQuery()->*/execute()->fetchField();
return $status;
}
function xmlsitemap_link_bundle_settings_save($entity, $bundle, array $settings, $update_links = TRUE) {
if ($update_links) {
$old_settings = xmlsitemap_link_bundle_load($entity, $bundle);
......
......@@ -294,7 +294,8 @@ function xmlsitemap_sitemap_multiple_update(array $smids) {
* Implements hook_query_TAG_alter().
*/
function xmlsitemap_query_xmlsitemap_link_bundle_access_alter(QueryAlterableInterface $query) {
if ($entity = $query->getMetaData('entity_info')) {
if ($entity = $query->getMetaData('entity')) {
$info = $query->getMetaData('entity_info');
$bundle = $query->getMetaData('bundle');
if (empty($bundle)) {
$bundle = xmlsitemap_get_link_type_enabled_bundles($entity);
......@@ -302,8 +303,8 @@ function xmlsitemap_query_xmlsitemap_link_bundle_access_alter(QueryAlterableInte
if (!empty($info['bundle keys']['bundle'])) {
$query->condition($info['bundle keys']['bundle'], $bundle);
}
if (!empty($info['entity keys']['bundle'])) {
$query->condition($info['entity keys']['bundle'], $bundles);
elseif (!empty($info['entity keys']['bundle'])) {
$query->condition($info['entity keys']['bundle'], $bundle);
}
}
}
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