Commit 365d2a98 authored by Dave Reid's avatar Dave Reid

by Dave Reid: Added requirements check for all sitemap directories' writeability.

by Dave Reid: Use entity_uri().
by Dave Reid: Added hook_query_xmlsitemap_link_bundle_access_alter() for any queries selecting from an entity's base table and the bundle value is in another table (e.g. vocabulary machine name for taxonomy terms).
parent b91b15e9
......@@ -33,13 +33,27 @@ function xmlsitemap_requirements($phase) {
'severity' => REQUIREMENT_ERROR,
);
}
$requirements['xmlsitemap_directory'] = array(
'title' => $t('XML sitemap cache directory'),
'value' => $t('Writable'),
);
if (!xmlsitemap_check_directory()) {
$requirements['xmlsitemap_directory'] = array(
'title' => $t('XML sitemap cache directory'),
'value' => $t('Not found or not writable'),
'severity' => REQUIREMENT_ERROR,
'description' => $t('The directory %directory was not found or is not writable by the server. See <a href="@docpage">@docpage</a> for more information.', array('%directory' => xmlsitemap_get_directory(), '@docpage' => 'http://drupal.org/node/34025')),
);
$requirements['xmlsitemap_directory']['value'] = $t('Not found or not writable');
$requirements['xmlsitemap_directory']['severity'] = REQUIREMENT_ERROR;
$requirements['xmlsitemap_directory']['description'] = $t('The directory %directory was not found or is not writable by the server. See <a href="@docpage">@docpage</a> for more information.', array('%directory' => xmlsitemap_get_directory(), '@docpage' => 'http://drupal.org/node/34025'));
}
else {
$directories = xmlsitemap_check_all_directories();
foreach ($directories as $directory => $writable) {
if ($writable) {
unset($directories[$directory]);
}
}
if (!empty($directories)) {
$requirements['xmlsitemap_directory']['value'] = $t('Not found or not writable');
$requirements['xmlsitemap_directory']['severity'] = REQUIREMENT_ERROR;
$requirements['xmlsitemap_directory']['description'] = $t('The following directories were not found or are not writable by the server. See <a href="@docpage">@docpage</a> for more information. !directories', array('!directories' => theme('item_list', array('items' => array_keys($directories))), '@docpage' => 'http://drupal.org/node/34025'));
}
}
$max_links = XMLSITEMAP_MAX_SITEMAP_LINKS * XMLSITEMAP_MAX_SITEMAP_LINKS;
if (xmlsitemap_get_link_count() > $max_links) {
......
......@@ -335,17 +335,26 @@ function xmlsitemap_sitemap_load($smid) {
* Load multiple XML sitemaps from the database.
*
* @param $smids
* An array of XML sitemap IDs.
* An array of XML sitemap IDs, or FALSE to load all XML sitemaps.
* @param $conditions
* An array of conditions in the form 'field' => $value.
*/
function xmlsitemap_sitemap_load_multiple(array $smids) {
$sitemaps = array();
if ($smids) {
$sitemaps = db_query("SELECT * FROM {xmlsitemap_sitemap} WHERE smid IN (:smids)", array(':smids' => $smids))->fetchAllAssoc('smid', PDO::FETCH_ASSOC);
foreach ($sitemaps as $smid => $sitemap) {
$sitemaps[$smid]['context'] = unserialize($sitemap['context']);
$sitemaps[$smid]['uri'] = xmlsitemap_sitemap_uri($sitemaps[$smid]);
}
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;
}
foreach ($conditions as $field => $value) {
$query->condition($field, $value);
}
$sitemaps = $query->execute()->fetchAllAssoc('smid', PDO::FETCH_ASSOC);
foreach ($sitemaps as $smid => $sitemap) {
$sitemaps[$smid]['context'] = unserialize($sitemap['context']);
$sitemaps[$smid]['uri'] = xmlsitemap_sitemap_uri($sitemaps[$smid]);
}
return $sitemaps;
}
......@@ -733,6 +742,28 @@ function xmlsitemap_check_directory(array $sitemap = NULL) {
return $result;
}
function xmlsitemap_check_all_directories() {
$directories = array();
$sitemaps = xmlsitemap_sitemap_load_multiple(FALSE);
foreach ($sitemaps as $smid => $sitemap) {
$directory = xmlsitemap_get_directory($sitemap);
$directories[$directory] = $directory;
}
foreach ($directories as $directory) {
$result = file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
if ($result) {
$directories[$directory] = TRUE;
}
else {
$directories[$directory] = FALSE;
}
}
return $directories;
}
function xmlsitemap_clear_directory(array $sitemap = NULL, $delete = FALSE) {
$directory = xmlsitemap_get_directory($sitemap);
return _xmlsitemap_delete_recursive($directory, $delete);
......@@ -868,15 +899,8 @@ function xmlsitemap_get_link_type_indexed_status($entity_type, $bundle = '') {
$total = db_select($info['base table'], 'base');
$total->addField('base', $info['entity keys']['id']);
$total->condition($info['entity keys']['id'], 0, '>');
if (!empty($info['bundle keys']['bundle'])) {
$total->condition($info['bundle keys']['bundle'], $bundle);
}
elseif (!empty($info['entity keys']['bundle'])) {
$total->condition($info['entity keys']['bundle'], $bundle);
}
$total->addTag('xmlsitemap_link_index_count');
$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);
......@@ -885,6 +909,24 @@ function xmlsitemap_get_link_type_indexed_status($entity_type, $bundle = '') {
return $status;
}
/**
* Implements hook_query_TAG_alter().
*/
function xmlsitemap_query_xmlsitemap_link_bundle_access_alter(QueryAlterableInterface $query) {
if ($entity = $query->getMetaData('entity_info')) {
$bundle = $query->getMetaData('bundle');
if (empty($bundle)) {
$bundle = xmlsitemap_get_link_type_enabled_bundles($entity);
}
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);
}
}
}
function xmlsitemap_link_bundle_settings_save($entity, $bundle, array $settings, $update_links = TRUE) {
if ($update_links) {
$old_settings = xmlsitemap_link_bundle_load($entity, $bundle);
......
......@@ -197,12 +197,12 @@ function xmlsitemap_node_create_link(stdClass &$node) {
}
$settings = xmlsitemap_link_bundle_load('node', $node->type);
$uri = entity_uri('node', $node);
$node->xmlsitemap += array(
'type' => 'node',
'id' => $node->nid,
'subtype' => $node->type,
'loc' => 'node/'. $node->nid,
'status' => $settings['status'],
'status_default' => $settings['status'],
'status_override' => 0,
......@@ -222,6 +222,7 @@ function xmlsitemap_node_create_link(stdClass &$node) {
//node_access_acquire_grants($node);
// The following values must always be checked because they are volatile.
$node->xmlsitemap['loc'] = $uri['path'];
$node->xmlsitemap['lastmod'] = count($timestamps) ? max($timestamps) : 0;
$node->xmlsitemap['access'] = $node->nid ? xmlsitemap_node_view_access($node, drupal_anonymous_user()) : 1;
$node->xmlsitemap['language'] = isset($node->language) ? $node->language : LANGUAGE_NONE;
......
......@@ -169,12 +169,12 @@ function xmlsitemap_taxonomy_create_link(stdClass &$term) {
}
$settings = xmlsitemap_link_bundle_load('taxonomy_term', $term->vocabulary_machine_name);
$uri = entity_uri('taxonomy_term', $term);
$term->xmlsitemap += array(
'id' => $term->tid,
'type' => 'taxonomy_term',
'subtype' => $term->vocabulary_machine_name,
'loc' => 'taxonomy/term/' . $term->tid,
'status' => $settings['status'],
'status_default' => $settings['status'],
'status_override' => 0,
......@@ -185,6 +185,7 @@ function xmlsitemap_taxonomy_create_link(stdClass &$term) {
// The following values must always be checked because they are volatile.
// @todo How can/should we check taxonomy term access?
$term->xmlsitemap['loc'] = $uri['path'];
$term->xmlsitemap['access'] = 1;
$term->xmlsitemap['language'] = isset($term->language) ? $term->language : LANGUAGE_NONE;
......@@ -245,7 +246,7 @@ function xmlsitemap_taxonomy_get_node_count(stdClass $term) {
/**
* Implements hook_query_TAG_alter().
*/
function xmlsitemap_taxonomy_query_xmlsitemap_link_index_count_alter(QueryAlterableInterface $query) {
function xmlsitemap_taxonomy_query_xmlsitemap_link_bundle_access_alter(QueryAlterableInterface $query) {
if ($query->getMetaData('entity') == 'taxonomy_term') {
$query->innerJoin('taxonomy_vocabulary', 'v', 'base.vid = v.vid');
$query->addField('v', 'machine_name', 'vocabulary_machine_name');
......
......@@ -127,12 +127,12 @@ function xmlsitemap_user_create_link(stdClass &$account) {
}
$settings = xmlsitemap_link_bundle_load('user', 'user');
$uri = entity_uri('user', $account);
$account->xmlsitemap += array(
'type' => 'user',
'subtype' => 'user',
'id' => $account->uid,
'loc' => 'user/' . $account->uid,
'subtype' => 'user',
'status' => $settings['status'],
'status_default' => $settings['status'],
'status_override' => 0,
......@@ -142,6 +142,7 @@ function xmlsitemap_user_create_link(stdClass &$account) {
);
// The following values must always be checked because they are volatile.
$account->xmlsitemap['loc'] = $uri['path'];
$account->xmlsitemap['access'] = $account->uid && $account->status && $account->login && $account->access;
$account->xmlsitemap['language'] = !empty($account->language) ? $account->language : LANGUAGE_NONE;
......
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