Commit 6e50221d authored by Dave Reid's avatar Dave Reid

by Dave Reid: Initial context API and UI rewrite.

parent b2c89320
......@@ -8,6 +8,204 @@
* @ingroup xmlsitemap
*/
function xmlsitemap_sitemap_list_form() {
// Build the 'Update options' form.
$operations = array();
foreach (module_invoke_all('xmlsitemap_sitemap_operations') as $operation => $operation_info) {
$operations[$operation] = $operation_info['label'];
}
asort($operations);
$form['options'] = array(
'#type' => 'fieldset',
'#title' => t('Update options'),
'#prefix' => '<div class="container-inline">',
'#suffix' => '</div>',
);
$form['options']['operation'] = array(
'#type' => 'select',
'#options' => $operations,
'#default_value' => 'update',
);
$form['options']['submit'] = array(
'#type' => 'submit',
'#value' => t('Update'),
'#validate' => array('xmlsitemap_sitemap_list_validate'),
'#submit' => array('xmlsitemap_sitemap_list_submit'),
);
$query = db_select('xmlsitemap_sitemap');
$query->fields('xmlsitemap_sitemap', array('smid'));
$smids = $query->execute()->fetchCol();
$contexts = xmlsitemap_get_context_info();
$header = array();
$header['url'] = array('data' => t('URL'), 'field' => 'url');
foreach ($contexts as $context_key => $context_info) {
if (!empty($context_info['summary callback'])) {
$header['context_' . $context_key] = $context_info['label'];
}
}
$header['updated'] = array('data' => t('Last updated'), 'field' => 'updated');
$header['links'] = array('data' => t('Links'), 'field' => 'links');
$header['chunks'] = array('data' => t('Pages'), 'field' => 'chunks');
$header['operations'] = array('data' => t('Operations'));
$options = array();
$destination = drupal_get_destination();
foreach ($smids as $smid) {
$sitemap = xmlsitemap_sitemap_load($smid);
$sitemap['uri'] = xmlsitemap_sitemap_uri($sitemap);
$options[$smid]['url'] = array(
'data' => array(
'#type' => 'link',
'#title' => url($sitemap['uri']['path'], $sitemap['uri']['options']),
'#href' => $sitemap['uri']['path'],
'#options' => $sitemap['uri']['options'],
),
);
foreach ($contexts as $context_key => $context_info) {
$options[$smid]['context_' . $context_key] = _xmlsitemap_sitemap_context_summary($sitemap, $context_key, $context_info);
}
$options[$smid]['updated'] = $sitemap['updated'] ? format_date($sitemap['updated'], 'short') : t('Never');
$options[$smid]['links'] = $sitemap['updated'] ? $sitemap['links'] : '-';
$options[$smid]['chunks'] = $sitemap['updated'] ? $sitemap['chunks'] : '-';
$operations = array();
//if (!empty($sitemap['context'])) {
$operations['edit'] = array(
'title' => t('Edit'),
'href' => 'admin/config/search/xmlsitemap/edit/' . $smid,
'query' => $destination,
);
//}
$operations['delete'] = array(
'title' => t('Delete'),
'href' => 'admin/config/search/xmlsitemap/delete/' . $smid,
'query' => $destination,
);
if ($operations) {
$options[$smid]['operations'] = array(
'data' => array(
'#theme' => 'links',
'#links' => $operations,
'#attributes' => array('class' => array('links', 'inline')),
),
);
}
else {
$options[$smid]['operations'] = t('None (sitemap locked)');
}
}
$form['sitemaps'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#empty' => t('No XML sitemaps available.') . ' ' . l('Add a new XML sitemap', 'admin/config/search/xmlsitemap/add'),
);
return $form;
}
function xmlsitemap_sitemap_edit_form(&$form, $form_state, array $sitemap = array()) {
$sitemap += array(
'smid' => NULL,
'context' => array(),
);
$form['#sitemap'] = $sitemap;
$form['smid'] = array(
'#type' => 'value',
'#value' => $sitemap['smid'],
);
// The context settings should be form_alter'ed by the context modules.
$form['context'] = array(
'#tree' => TRUE,
);
$form['actions'] = array(
'#type' => 'container',
'#attributes' => array('class' => array('form-actions')),
'#weight' => 100,
);
$form['actions']['save'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
$form['actions']['cancel'] = array(
'#type' => 'link',
'#href' => isset($_GET['destination']) ? $_GET['destination'] : 'admin/config/search/xmlsitemap',
'#title' => t('Cancel'),
);
// Let other modules alter this form with their context settings.
$form['#pre_render'][] = 'xmlsitemap_sitemap_edit_form_pre_render';
return $form;
}
function xmlsitemap_sitemap_edit_form_pre_render($form) {
$visible_children = element_get_visible_children($form['context']);
if (empty($visible_children)) {
$form['context']['empty'] = array(
'#type' => 'markup',
'#markup' => '<p>' . t('There are currently no XML sitemap contexts available.') . '</p>',
);
}
return $form;
}
function xmlsitemap_sitemap_edit_form_validate($form, &$form_state) {
// If there are no context options, the $form_state['values']['context']
// disappears.
$form_state['values'] += array('context' => array());
$existing = xmlsitemap_sitemap_load_by_context($form_state['values']['context']);
if ($existing && $existing['smid'] != $form_state['values']['smid']) {
form_set_error('context', t('A sitemap with the same context already exists.'));
}
}
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' => url($uri['path'], $uri['options']))));
$form_state['redirect'] = 'admin/config/search/xmlsitemap';
}
function xmlsitemap_sitemap_delete_form(&$form, $form_state, array $sitemap) {
$count = (int) db_query("SELECT COUNT(smid) FROM {xmlsitemap_sitemap}")->fetchField();
if ($count === 1 && empty($_POST)) {
drupal_set_message('It is not recommended to delete the only XML sitemap.', 'error');
}
$form['#sitemap'] = $sitemap;
$form['smid'] = array(
'#type' => 'value',
'#value' => $sitemap['smid'],
);
return confirm_form(
$form,
t('Are you sure you want to delete the sitemap?'),
'admin/config/search/xmlsitemap',
'',
t('Delete'),
t('Cancel')
);
}
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' => url($uri['path'], $uri['options']))));
$form_state['redirect'] = 'admin/config/search/xmlsitemap';
}
/**
* Form builder; Administration settings form.
*
......@@ -33,14 +231,6 @@ function xmlsitemap_settings_form() {
'#default_value' => xmlsitemap_var('xsl'),
'#description' => t('Using the stylesheet will add formatting and tables with sorting to make it easier to view the XML sitemap data instead of viewing raw XML output. Search engines will ignore any formatting.')
);
$form['xmlsitemap']['xmlsitemap_languages'] = array(
'#type' => 'checkboxes',
'#title' => t('Generate sitemaps for the following languages'),
'#options' => array(language_default('language') => language_default('name')),
'#default_value' => xmlsitemap_var('languages'),
'#process' => array('form_process_checkboxes', '_xmlsitemap_process_language_checkboxes'),
'#description' => !module_exists('xmlsitemap_i18n') ? t('To enable multilingual features, enable the XML sitemap internationalization module.') : '',
);
$form['advanced'] = array(
'#type' => 'fieldset',
......@@ -86,10 +276,10 @@ function xmlsitemap_settings_form() {
);
$form['advanced']['xmlsitemap_base_url'] = array(
'#type' => 'textfield',
'#title' => t('Base URL'),
'#title' => t('Default base URL'),
'#default_value' => xmlsitemap_var('base_url'),
'#size' => 30,
'#description' => t('This is the base URL for links generated in the sitemap.'),
'#description' => t('This is the default base URL used for sitemaps and sitemap links.'),
'#required' => TRUE,
);
$form['advanced']['xmlsitemap_lastmod_format'] = array(
......@@ -150,23 +340,6 @@ function xmlsitemap_settings_form() {
return $form;
}
/**
* Show a link to each languages' sitemap and disable the default language
* checkbox.
*/
function _xmlsitemap_process_language_checkboxes($element) {
foreach (element_children($element) as $key) {
if ($key == language_default('language')) {
$element[$key]['#disabled'] = TRUE;
$element[$key]['#default_value'] = TRUE;
$element[$key]['#weight'] = -1;
}
$link = url('sitemap.xml', array('absolute' => TRUE, 'language' => xmlsitemap_language_load($key)));
$element[$key]['#description'] = l($link, $link);
}
return $element;
}
/**
* Form validator; Check the sitemap files directory.
*
......
......@@ -8,6 +8,7 @@ files[] = xmlsitemap.inc
files[] = xmlsitemap.admin.inc
files[] = xmlsitemap.drush.inc
files[] = xmlsitemap.generate.inc
files[] = xmlsitemap.xmlsitemap.inc
files[] = xmlsitemap.pages.inc
files[] = xmlsitemap.install
files[] = xmlsitemap.test
......
......@@ -198,6 +198,7 @@ function xmlsitemap_uninstall() {
* Implements hook_schema().
*/
function xmlsitemap_schema() {
// @todo Rename to xmlsitemap_link
$schema['xmlsitemap'] = array(
'description' => 'The base table for xmlsitemap links.',
'fields' => array(
......@@ -303,6 +304,52 @@ function xmlsitemap_schema() {
),
);
$schema['xmlsitemap_sitemap'] = array(
'fields' => array(
'smid' => array(
'description' => 'Sitemap ID',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'context_hash' => array(
'description' => 'The MD5 hash of the context field.',
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => '',
),
'context' => array(
'description' => 'Serialized array with the sitemaps context',
'type' => 'text',
'not null' => TRUE,
'serialize' => TRUE,
),
'updated' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'links' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'chunks' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array('smid'),
'unique keys' => array(
'context_hash' => array('context_hash'),
),
);
return $schema;
}
......@@ -510,7 +557,6 @@ function xmlsitemap_update_18() {
* Empty update.
*/
function xmlsitemap_update_6200() {
return array();
}
/**
......@@ -522,3 +568,57 @@ function xmlsitemap_update_7200() {
->condition('language', '')
->execute();
}
/**
* Create the {xmlsitemap_sitemap} table.
*/
function xmlsitemap_update_7201() {
if (db_table_exists('xmlsitemap_sitemap')) {
return;
}
$schema['xmlsitemap_sitemap'] = array(
'fields' => array(
'smid' => array(
'description' => 'Sitemap ID',
'type' => 'serial',
'not null' => TRUE,
),
'context_hash' => array(
'description' => 'The MD5 hash of the context field.',
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => '',
),
'context' => array(
'description' => 'Serialized array with the sitemaps context',
'type' => 'text',
'not null' => TRUE,
),
'updated' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'links' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'chunks' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array('smid'),
'unique keys' => array(
'context_hash' => array('context_hash'),
),
);
db_create_table('xmlsitemap_sitemap', $schema['xmlsitemap_sitemap']);
}
This diff is collapsed.
......@@ -8,51 +8,6 @@
* @ingroup xmlsitemap
*/
/**
* Get the sitemap context of the current request.
*/
function xmlsitemap_get_current_context() {
static $context;
if (!isset($context)) {
$context = module_invoke_all('xmlsitemap_context');
drupal_alter('xmlsitemap_context', $context);
asort($context);
}
return $context;
}
/**
* Validate the context and use the default context if it fails validation.
*
* @todo Merge into xmlsitemap_get_current_context()?
* @todo Use real default context variable instead of hard-coded default.
*/
function xmlsitemap_context_check(array $context) {
$hash = md5(serialize($context));
$default = array();
if (module_exists('xmlsitemap_i18n')) {
$default['language'] = language_default('language');
}
$default_hash = md5(serialize($default));
module_load_include('inc', 'xmlsitemap', 'xmlsitemap.generate');
$valid_contexts = array_keys(xmlsitemap_get_contexts());
if (in_array($hash, $valid_contexts)) {
return $context;
}
elseif (in_array($default_hash, $valid_contexts)) {
return $default;
}
else {
trigger_error("Could not find fallback XML sitemap context. Context $hash: " . print_r($context, TRUE) . ". Default context $default_hash: " . print_r($default, TRUE), E_USER_ERROR);
return array();
}
}
/**
* Get the sitemap chunk/page of the current request.
*/
......@@ -81,16 +36,14 @@ function xmlsitemap_get_current_chunk(array $context) {
* @see xmlsitemap_output_file()
*/
function xmlsitemap_output_chunk() {
$original_context = xmlsitemap_get_current_context();
$context = xmlsitemap_context_check($original_context);
$context = xmlsitemap_get_current_context();
$chunk = xmlsitemap_get_current_chunk($context);
$file = xmlsitemap_get_file_from_context($context, $chunk);
// Provide debugging information if enabled.
if (variable_get('xmlsitemap_developer_mode', 0) && isset($_GET['debug'])) {
if (variable_get('xmlsitemap_developer_mode', 0) || isset($_GET['debug'])) {
$output = array();
$output[] = "Chunk: $chunk";
$output[] = "Original context: " . print_r($original_context, TRUE);
$output[] = "Context: " . print_r($context, TRUE);
$output[] = "Cache file location: $file";
$output[] = "Cache file exists: " . (file_exists($file) ? 'Yes' : 'No');
......
<?php
// $Id$
/**
* Implements hook_xmlsitemap_link_info().
*/
function xmlsitemap_xmlsitemap_link_info() {
return array(
'frontpage' => array(
'label' => t('Frontpage'),
'xmlsitemap' => array(
'settings callback' => 'xmlsitemap_link_frontpage_settings',
),
),
);
}
/**
* XML sitemap link type settings callback for frontpage link entity.
*/
function xmlsitemap_link_frontpage_settings(&$form) {
module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
if (user_access('administer site configuration')) {
$form['#description'] = t('The front page path can be changed at <a href="@url-frontpage">@url-frontpage</a>.', array('@url-frontpage' => url('admin/settings/site-information')));
}
$form['xmlsitemap_frontpage_priority'] = array(
'#type' => 'select',
'#title' => t('Priority'),
'#options' => xmlsitemap_get_priority_options(),
'#default_value' => variable_get('xmlsitemap_frontpage_priority', 1.0),
);
$form['xmlsitemap_frontpage_changefreq'] = array(
'#type' => 'select',
'#title' => t('Change frequency'),
'#options' => xmlsitemap_get_changefreq_options(),
'#default_value' => variable_get('xmlsitemap_frontpage_changefreq', XMLSITEMAP_FREQUENCY_DAILY),
);
return $form;
}
/**
* Implements hook_xmlsitemap_link_alter().
*/
function xmlsitemap_xmlsitemap_link_alter(&$link) {
// Alter the frontpage priority.
if ($link['type'] == 'frontpage' || $link['loc'] == '' || $link['loc'] == drupal_get_normal_path(variable_get('site_frontpage', 'node'))) {
$link['priority'] = xmlsitemap_var('frontpage_priority');
$link['changefreq'] = xmlsitemap_var('frontpage_changefreq');
}
}
/**
* Implements hook_xmlsitemap_links().
*/
function xmlsitemap_xmlsitemap_links() {
// Frontpage link.
$links[] = array(
'type' => 'frontpage',
'id' => 0,
'loc' => '',
);
return $links;
}
function xmlsitemap_xmlsitemap_sitemap_operations() {
$operations['update'] = array(
'label' => t('Update cached files'),
'callback' => 'xmlsitemap_sitemap_multiple_update',
);
return $operations;
}
/**
* 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;
if (!isset($context_value)) {
return t('Default');
}
elseif (!empty($context_info['summary callback'])) {
return $context_info['summary callback']($context_value);
}
else {
return $context_value;
}
}
......@@ -208,3 +208,15 @@ function xmlsitemap_engines_variables() {
);
return $variables;
}
function xmlsitemap_engines_xmlsitemap_sitemap_operations() {
$operations['xmlsitemap_engines_submit'] = array(
'label' => t('Submit to search engines'),
'callback' => 'xmlsitemap_engines_sitemap_multiple_submit',
);
return $operations;
}
function xmlsitemap_engines_sitemap_multiple_submit(array $sitemaps) {
}
......@@ -11,6 +11,9 @@ function xmlsitemap_i18n_xmlsitemap_context() {
return $context;
}
/**
* Implements xmlsitemap_context_url_options().
*/
function xmlsitemap_i18n_xmlsitemap_context_url_options(array $context) {
$options = array();
if (isset($context['language'])) {
......@@ -20,13 +23,16 @@ function xmlsitemap_i18n_xmlsitemap_context_url_options(array $context) {
}
/**
* Implements hook_xmlsitemap_context().
* Implements hook_xmlsitemap_context_fallback().
*/
function xmlsitemap_i18n_xmlsitemap_context_fallback() {
$context['language'] = language_default('language');
return $context;
}
/**
* Implements hook_xmlsitemap_context_info().
*/
function xmlsitemap_i18n_xmlsitemap_context_info() {
$context['language'] = array(
'label' => t('Language'),
......
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