Commit b1147f3b authored by Dave Reid's avatar Dave Reid

by Dave Reid: Automatically fetch sitemap URIs on load.

parent 87693800
......@@ -53,19 +53,16 @@ function xmlsitemap_sitemap_list_form() {
$query->fields('xmlsitemap_sitemap', array('smid'));
$query->extend('TableSort')->orderByHeader($header);
$smids = $query->execute()->fetchCol();
$sitemaps = xmlsitemap_sitemap_load_multiple($smids);
$options = array();
$destination = drupal_get_destination();
foreach ($smids as $smid) {
$sitemap = xmlsitemap_sitemap_load($smid);
$sitemap['uri'] = xmlsitemap_sitemap_uri($sitemap);
foreach ($sitemaps as $smid => $sitemap) {
$options[$smid]['url'] = array(
'data' => array(
'#type' => 'link',
'#title' => $sitemap['uri']['url'],
'#href' => $sitemap['uri']['path'],
'#options' => $sitemap['uri']['options'],
'#title' => $sitemap['url'],
'#href' => $sitemap['url'],
),
);
......@@ -240,8 +237,7 @@ function xmlsitemap_sitemap_edit_form_validate($form, &$form_state) {
function xmlsitemap_sitemap_edit_form_submit($form, &$form_state) {
form_state_values_clean($form_state);
xmlsitemap_sitemap_save($form_state['values']);
$uri = xmlsitemap_sitemap_uri($form_state['values']);
drupal_set_message(t('The sitemap %sitemap was saved.', array('%sitemap' => $uri['url'])));
drupal_set_message(t('The sitemap has been saved.'));
$form_state['redirect'] = 'admin/config/search/xmlsitemap';
}
......@@ -271,8 +267,7 @@ function xmlsitemap_sitemap_delete_form(&$form, $form_state, array $sitemap) {
function xmlsitemap_sitemap_delete_form_submit($form, $form_state) {
xmlsitemap_sitemap_delete($form_state['values']['smid']);
$uri = xmlsitemap_sitemap_uri($form['#sitemap']);
drupal_set_message(t('The sitemap %sitemap was deleted.', array('%sitemap' => $uri['url'])));
drupal_set_message(t('The sitemap %sitemap has been deleted.', array('%sitemap' => $form['#sitemap']['url'])));
$form_state['redirect'] = 'admin/config/search/xmlsitemap';
}
......
......@@ -401,7 +401,7 @@ function xmlsitemap_regenerate_batch_generate($smid, array &$context) {
$sitemap = &$context['sandbox']['sitemap'];
$links = xmlsitemap_generate($sitemap, $sitemap['chunks']);
$context['message'] = t('Now generating sitemap page @chunk.', array('@chunk' => $sitemap['chunks']));
$context['message'] = t('Now generating %sitemap-url.', array('%sitemap-url' => url('sitemap.xml', $sitemap['uri']['options'] + array('query' => array('page' => $sitemap['chunks'])))));
if ($links) {
$sitemap['links'] += $links;
......@@ -429,7 +429,7 @@ function xmlsitemap_regenerate_batch_generate_index($smid, array &$context) {
$sitemap = xmlsitemap_sitemap_load($smid);
if ($sitemap['chunks'] > 1) {
xmlsitemap_generate($sitemap, 'index');
$context['message'] = t('Now generating sitemap index for @count pages.', array('@count' => $sitemap['chunks']));
$context['message'] = t('Now generating sitemap index %sitemap-url.', array('%sitemap-url' => url('sitemap.xml', $sitemap['uri']['options'])));
}
}
......
......@@ -123,6 +123,8 @@ function xmlsitemap_sitemap_load_multiple(array $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]);
$sitemaps[$smid]['url'] = url($sitemaps[$smid]['uri']['path'], $sitemaps[$smid]['uri']['options']);
}
return $sitemaps;
}
......@@ -141,17 +143,25 @@ function xmlsitemap_sitemap_load_by_context(array $context = NULL) {
return xmlsitemap_sitemap_load($smid);
}
/**
* Returns the uri elements of an XML sitemap.
*
* @param $sitemap
* An unserialized data array for an XML sitemap.
* @return
* An array containing the 'path' and 'options' keys used to build the uri of
* the XML sitemap, and matching the signature of url().
*/
function xmlsitemap_sitemap_uri(array $sitemap) {
// @todo Why aren't all modules invoked sometimes? Why is this include needed?
module_load_all_includes('xmlsitemap.inc');
$uri = array();
$uri['path'] = 'sitemap.xml';
$uri['options'] = module_invoke_all('xmlsitemap_context_url_options', $sitemap['context']);
$uri['options'] += array(
'absolute' => TRUE,
'base_url' => xmlsitemap_var('base_url'),
);
$uri['url'] = url($uri['path'], $uri['options']);
return $uri;
}
......@@ -284,11 +294,8 @@ function xmlsitemap_cron() {
* Implements hook_robotstxt().
*/
function xmlsitemap_robotstxt() {
// @todo Simplify this down to one function somehow?
$context = xmlsitemap_get_current_context();
if ($sitemap = xmlsitemap_sitemap_load_by_context($context)) {
$uri = xmlsitemap_sitemap_uri($sitemap);
$robotstxt[] = 'Sitemap: ' . $uri['url'];
if ($sitemap = xmlsitemap_sitemap_load_by_context()) {
$robotstxt[] = 'Sitemap: ' . $sitemap['url'];
return $robotstxt;
}
}
......@@ -1063,6 +1070,8 @@ function xmlsitemap_get_bundle_path($entity, $bundle) {
function xmlsitemap_get_context_info($context = NULL, $reset = FALSE) {
global $language;
$info = &drupal_static(__FUNCTION__);
// @todo Why aren't all modules invoked sometimes? Why is this include needed?
module_load_all_includes('xmlsitemap.inc');
if ($reset) {
......@@ -1093,6 +1102,9 @@ function xmlsitemap_get_context_info($context = NULL, $reset = FALSE) {
function xmlsitemap_get_current_context() {
$context = &drupal_static(__FUNCTION__);
// @todo Why aren't all modules invoked sometimes? Why is this include needed?
module_load_all_includes('xmlsitemap.inc');
if (!isset($context)) {
$context = module_invoke_all('xmlsitemap_context');
drupal_alter('xmlsitemap_context', $context);
......@@ -1105,6 +1117,9 @@ function xmlsitemap_get_current_context() {
function xmlsitemap_get_default_context() {
$context = &drupal_static(__FUNCTION__);
// @todo Why aren't all modules invoked sometimes? Why is this include needed?
module_load_all_includes('xmlsitemap.inc');
if (!isset($context)) {
$context = module_invoke_all('xmlsitemap_context_fallback');
drupal_alter('xmlsitemap_context_fallback', $context);
......
......@@ -62,6 +62,9 @@ function xmlsitemap_xmlsitemap_links() {
return $links;
}
/**
* Implements hook_xmlsitemap_sitemap_operations().
*/
function xmlsitemap_xmlsitemap_sitemap_operations() {
$operations['update'] = array(
'label' => t('Update cached files'),
......@@ -71,20 +74,20 @@ function xmlsitemap_xmlsitemap_sitemap_operations() {
return $operations;
}
/**
* Sitemap operation callback; regenerate sitemap files using the batch API.
*
* @param $smids
* An array of XML sitemap IDs.
*
* @see xmlsitemap_regenerate_batch()
*/
function xmlsitemap_sitemap_multiple_update(array $smids) {
module_load_include('generate.inc', 'xmlsitemap');
$batch = xmlsitemap_regenerate_batch($smids);
batch_set($batch);
}
/**
* Implements hook_xmlsitemap_context_url_options().
*/
function xmlsitemap_xmlsitemap_context_url_options(array $context) {
$options['absolute'] = TRUE;
return $options;
}
function _xmlsitemap_sitemap_context_summary(array $sitemap, $context_key, array $context_info) {
$context_value = isset($sitemap['context'][$context_key]) ? $sitemap['context'][$context_key] : NULL;
......
......@@ -138,10 +138,9 @@ function xmlsitemap_engines_submit_engines(array $smids = array()) {
* @param $sitemaps
* An array of URLs of the sitemaps to submit.
*/
function xmlsitemap_engines_submit_sitemaps($url, array $sitemaps = array()) {
function xmlsitemap_engines_submit_sitemaps($url, array $sitemaps) {
foreach ($sitemaps as $sitemap) {
$sitemap_url = xmlsitemap_sitemap_uri($sitemap);
$submit_url = xmlsitemap_engines_prepare_url($url, $sitemap_url['url']);
$submit_url = xmlsitemap_engines_prepare_url($url, $sitemap['url']);
$request = drupal_http_request($submit_url);
watchdog('xmlsitemap', 'Submitted the sitemap to %url and received response @code.', array('%url' => $submit_url, '@code' => $request->code));
}
......
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