Commit a2ea3100 authored by Dave Reid's avatar Dave Reid

by Dave Reid: Convert to using objects for XML sitemaps instead of arrays.

parent 66132e70
......@@ -62,13 +62,13 @@ function xmlsitemap_sitemap_list_form() {
$options = array();
foreach ($sitemaps as $smid => $sitemap) {
$sitemap['url'] = url($sitemap['uri']['path'], $sitemap['uri']['options']);
$sitemap->url = url($sitemap->uri['path'], $sitemap->uri['options']);
$options[$smid]['url'] = array(
'data' => array(
'#type' => 'link',
'#title' => $sitemap['url'],
'#href' => $sitemap['url'],
'#title' => $sitemap->url,
'#href' => $sitemap->url,
),
);
......@@ -76,9 +76,9 @@ function xmlsitemap_sitemap_list_form() {
$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'] : '-';
$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 : '-';
// @todo Highlight sitemaps that need updating.
//$options[$smid]['#attributes']['class'][] = 'warning';
......@@ -156,19 +156,20 @@ function xmlsitemap_sitemap_list_form_submit($form, &$form_state) {
}
}
function xmlsitemap_sitemap_edit_form($form, &$form_state, array $sitemap = array()) {
function xmlsitemap_sitemap_edit_form($form, &$form_state, stdClass $sitemap = NULL) {
_xmlsitemap_set_breadcrumb();
$sitemap += array(
'smid' => NULL,
'context' => array(),
);
if (!isset($sitemap)) {
$sitemap = new stdClass();
$sitemap->smid = NULL;
$sitemap->context = array();
}
$form['#sitemap'] = $sitemap;
$form['smid'] = array(
'#type' => 'value',
'#value' => $sitemap['smid'],
'#value' => $sitemap->smid,
);
// The context settings should be form_alter'ed by the context modules.
$form['context'] = array(
......@@ -223,7 +224,7 @@ function xmlsitemap_sitemap_edit_form_submit($form, &$form_state) {
// @todo If context was changed, needs to be regenerated.
}
function xmlsitemap_sitemap_delete_form($form, &$form_state, array $sitemap) {
function xmlsitemap_sitemap_delete_form($form, &$form_state, stdClass $sitemap) {
_xmlsitemap_set_breadcrumb();
$count = (int) db_query("SELECT COUNT(smid) FROM {xmlsitemap_sitemap}")->fetchField();
......@@ -234,7 +235,7 @@ function xmlsitemap_sitemap_delete_form($form, &$form_state, array $sitemap) {
$form['#sitemap'] = $sitemap;
$form['smid'] = array(
'#type' => 'value',
'#value' => $sitemap['smid'],
'#value' => $sitemap->smid,
);
return confirm_form(
$form,
......
......@@ -145,10 +145,10 @@ function hook_xmlsitemap_context_url_options_alter(array &$options, array $conte
*/
function hook_query_xmlsitemap_generate_alter(QueryAlterableInterface $query) {
$sitemap = $query->getMetaData('sitemap');
if (!empty($sitemap['context']['vocabulary'])) {
if (!empty($sitemap->context['vocabulary'])) {
$node_condition = db_and();
$node_condition->condition('type', 'taxonomy_term');
$node_condition->condition('subtype', $sitemap['context']['vocabulary']);
$node_condition->condition('subtype', $sitemap->context['vocabulary']);
$normal_condition = db_and();
$normal_condition->condition('type', 'taxonomy_term', '<>');
$condition = db_or();
......@@ -173,8 +173,8 @@ function hook_xmlsitemap_sitemap_operations() {
* @param $sitemap
* The XML sitemap array that was deleted.
*/
function hook_xmlsitemap_sitemap_delete(array $sitemap) {
db_query("DELETE FROM {mytable} WHERE smid = '%s'", $sitemap['smid']);
function hook_xmlsitemap_sitemap_delete(stdClass $sitemap) {
db_query("DELETE FROM {mytable} WHERE smid = '%s'", $sitemap->smid);
}
/**
......
......@@ -144,7 +144,7 @@ function _xmlsitemap_regenerate_after() {
* @param $page
* An integer of the specific page of the sitemap to generate.
*/
function xmlsitemap_generate_page(array $sitemap, $page) {
function xmlsitemap_generate_page(stdClass $sitemap, $page) {
try {
$writer = new XMLSitemapWriter($sitemap, $page);
$writer->startDocument();
......@@ -160,10 +160,10 @@ function xmlsitemap_generate_page(array $sitemap, $page) {
return $writer->getSitemapElementCount();
}
function xmlsitemap_generate_chunk(array $sitemap, XMLSitemapWriter $writer, $chunk) {
function xmlsitemap_generate_chunk(stdClass $sitemap, XMLSitemapWriter $writer, $chunk) {
$lastmod_format = variable_get('xmlsitemap_lastmod_format', XMLSITEMAP_LASTMOD_MEDIUM);
$url_options = $sitemap['uri']['options'];
$url_options = $sitemap->uri['options'];
$url_options += array(
'absolute' => TRUE,
'base_url' => variable_get('xmlsitemap_base_url', $GLOBALS['base_url']),
......@@ -238,7 +238,7 @@ function xmlsitemap_generate_chunk(array $sitemap, XMLSitemapWriter $writer, $ch
* @param $sitemap
* An unserialized data array for an XML sitemap.
*/
function xmlsitemap_generate_index(array $sitemap) {
function xmlsitemap_generate_index(stdClass $sitemap) {
try {
$writer = new XMLSitemapIndexWriter($sitemap);
$writer->startDocument();
......@@ -294,8 +294,8 @@ function xmlsitemap_regenerate_batch(array $smids = array()) {
function xmlsitemap_regenerate_batch_generate($smid, array &$context) {
if (!isset($context['sandbox']['sitemap'])) {
$context['sandbox']['sitemap'] = xmlsitemap_sitemap_load($smid);
$context['sandbox']['sitemap']['chunks'] = 1;
$context['sandbox']['sitemap']['links'] = 0;
$context['sandbox']['sitemap']->chunks = 1;
$context['sandbox']['sitemap']->links = 0;
$context['sandbox']['max'] = XMLSITEMAP_MAX_SITEMAP_LINKS;
// Clear the cache directory for this sitemap before generating any files.
......@@ -304,30 +304,30 @@ function xmlsitemap_regenerate_batch_generate($smid, array &$context) {
}
$sitemap = &$context['sandbox']['sitemap'];
$links = xmlsitemap_generate_page($sitemap, $sitemap['chunks']);
$context['message'] = t('Now generating %sitemap-url.', array('%sitemap-url' => url('sitemap.xml', $sitemap['uri']['options'] + array('query' => array('page' => $sitemap['chunks'])))));
$links = xmlsitemap_generate_page($sitemap, $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;
$sitemap['chunks']++;
$sitemap->links += $links;
$sitemap->chunks++;
}
else {
// Cleanup the 'extra' empty file.
$file = xmlsitemap_sitemap_get_file($sitemap, $sitemap['chunks']);
if (file_exists($file) && $sitemap['chunks'] > 1) {
$file = xmlsitemap_sitemap_get_file($sitemap, $sitemap->chunks);
if (file_exists($file) && $sitemap->chunks > 1) {
file_unmanaged_delete($file);
}
$sitemap['chunks']--;
$sitemap->chunks--;
// Save the updated chunks and links values.
$context['sandbox']['max'] = $sitemap['chunks'];
$sitemap['updated'] = REQUEST_TIME;
$context['sandbox']['max'] = $sitemap->chunks;
$sitemap->updated = REQUEST_TIME;
xmlsitemap_sitemap_get_max_filesize($sitemap);
xmlsitemap_sitemap_save($sitemap);
}
if ($sitemap['chunks'] != $context['sandbox']['max']) {
$context['finished'] = $sitemap['chunks'] / $context['sandbox']['max'];
if ($sitemap->chunks != $context['sandbox']['max']) {
$context['finished'] = $sitemap->chunks / $context['sandbox']['max'];
}
}
......@@ -336,9 +336,9 @@ function xmlsitemap_regenerate_batch_generate($smid, array &$context) {
*/
function xmlsitemap_regenerate_batch_generate_index($smid, array &$context) {
$sitemap = xmlsitemap_sitemap_load($smid);
if ($sitemap['chunks'] > 1) {
if ($sitemap->chunks > 1) {
xmlsitemap_generate_index($sitemap);
$context['message'] = t('Now generating sitemap index %sitemap-url.', array('%sitemap-url' => url('sitemap.xml', $sitemap['uri']['options'])));
$context['message'] = t('Now generating sitemap index %sitemap-url.', array('%sitemap-url' => url('sitemap.xml', $sitemap->uri['options'])));
}
}
......
......@@ -464,8 +464,8 @@ function xmlsitemap_update_6203() {
foreach ($sitemaps as $sitemap) {
xmlsitemap_sitemap_get_max_filesize($sitemap);
db_update('xmlsitemap_sitemap')
->fields(array('max_filesize' => $sitemap['max_filesize']))
->condition('smid', $sitemap['smid'])
->fields(array('max_filesize' => $sitemap->max_filesize))
->condition('smid', $sitemap->smid)
->execute();
}
......@@ -539,8 +539,8 @@ function _xmlsitemap_sitemap_rehash_all() {
// 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']) {
$hash = xmlsitemap_sitemap_get_context_hash($sitemap->context);
if ($hash != $sitemap->smid) {
xmlsitemap_sitemap_save($sitemap);
}
}
......
......@@ -264,7 +264,7 @@ function xmlsitemap_modules_disabled(array $modules) {
*/
function xmlsitemap_robotstxt() {
if ($sitemap = xmlsitemap_sitemap_load_by_context()) {
$robotstxt[] = 'Sitemap: ' . url($sitemap['uri']['path'], $sitemap['uri']['options']);
$robotstxt[] = 'Sitemap: ' . url($sitemap->uri['path'], $sitemap->uri['options']);
return $robotstxt;
}
}
......@@ -328,6 +328,9 @@ function xmlsitemap_var($name, $default = NULL) {
*
* @param $smid
* An XML sitemap ID.
*
* @return
* The XML sitemap object.
*/
function xmlsitemap_sitemap_load($smid) {
$sitemap = xmlsitemap_sitemap_load_multiple(array($smid));
......@@ -341,6 +344,9 @@ function xmlsitemap_sitemap_load($smid) {
* An array of XML sitemap IDs, or FALSE to load all XML sitemaps.
* @param $conditions
* An array of conditions in the form 'field' => $value.
*
* @return
* An array of XML sitemap objects.
*/
function xmlsitemap_sitemap_load_multiple($smids = array(), array $conditions = array()) {
if ($smids !== FALSE) {
......@@ -353,10 +359,10 @@ function xmlsitemap_sitemap_load_multiple($smids = array(), array $conditions =
$query->condition($field, $value);
}
$sitemaps = $query->execute()->fetchAllAssoc('smid', PDO::FETCH_ASSOC);
$sitemaps = $query->execute()->fetchAllAssoc('smid');
foreach ($sitemaps as $smid => $sitemap) {
$sitemaps[$smid]['context'] = unserialize($sitemap['context']);
$sitemaps[$smid]['uri'] = xmlsitemap_sitemap_uri($sitemaps[$smid]);
$sitemaps[$smid]->context = unserialize($sitemap->context);
$sitemaps[$smid]->uri = xmlsitemap_sitemap_uri($sitemaps[$smid]);
}
return $sitemaps;
......@@ -384,18 +390,18 @@ function xmlsitemap_sitemap_load_by_context(array $context = NULL) {
* Save changes to an XML sitemap or add a new XML sitemap.
*
* @param $sitemap
* The XML sitemap array to be saved. If $sitemap['smid'] is omitted, a new
* The XML sitemap array to be saved. If $sitemap->smid is omitted, a new
* XML sitemap will be added.
*
* @todo Save the sitemap's URL as a column?
*/
function xmlsitemap_sitemap_save(array &$sitemap) {
function xmlsitemap_sitemap_save(stdClass $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']);
$smid_old = isset($sitemap->smid) ? $sitemap->smid : NULL;
$sitemap->smid = xmlsitemap_sitemap_get_context_hash($sitemap->context);
// If the context was changed, we need to perform additional actions.
if (isset($smid_old) && $sitemap['smid'] != $smid_old) {
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);
......@@ -403,7 +409,7 @@ function xmlsitemap_sitemap_save(array &$sitemap) {
// Change the smid field so drupal_write_record() does not fail.
db_update('xmlsitemap_sitemap')
->fields(array('smid' => $sitemap['smid']))
->fields(array('smid' => $sitemap->smid))
->condition('smid', $smid_old)
->execute();
......@@ -411,7 +417,7 @@ function xmlsitemap_sitemap_save(array &$sitemap) {
variable_set('xmlsitemap_regenerate_needed', TRUE);
}
if (empty($sitemap['smid'])) {
if (empty($sitemap->smid)) {
drupal_write_record('xmlsitemap_sitemap', $sitemap);
module_invoke_all('xmlsitemap_sitemap_insert', $sitemap);
}
......@@ -461,7 +467,7 @@ function xmlsitemap_sitemap_delete_multiple(array $smids) {
* @param $chunk
* An optional specific chunk in the sitemap. Defaults to the index page.
*/
function xmlsitemap_sitemap_get_file(array $sitemap, $chunk = 'index') {
function xmlsitemap_sitemap_get_file(stdClass $sitemap, $chunk = 'index') {
return xmlsitemap_get_directory($sitemap) . "/{$chunk}.xml";
}
......@@ -471,13 +477,13 @@ function xmlsitemap_sitemap_get_file(array $sitemap, $chunk = 'index') {
* @param $sitemap
* The XML sitemap array.
*/
function xmlsitemap_sitemap_get_max_filesize(array &$sitemap) {
function xmlsitemap_sitemap_get_max_filesize(stdClass $sitemap) {
$dir = xmlsitemap_get_directory($sitemap);
$sitemap['max_filesize'] = 0;
$sitemap->max_filesize = 0;
foreach (file_scan_directory($dir, '/\.xml$/') as $file) {
$sitemap['max_filesize'] = max($sitemap['max_filesize'], filesize($file->uri));
$sitemap->max_filesize = max($sitemap->max_filesize, filesize($file->uri));
}
return $sitemap['max_filesize'];
return $sitemap->max_filesize;
}
function xmlsitemap_sitemap_get_context_hash(array &$context) {
......@@ -494,10 +500,10 @@ function xmlsitemap_sitemap_get_context_hash(array &$context) {
* 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) {
function xmlsitemap_sitemap_uri(stdClass $sitemap) {
$uri['path'] = 'sitemap.xml';
$uri['options'] = module_invoke_all('xmlsitemap_context_url_options', $sitemap['context']);
drupal_alter('xmlsitemap_context_url_options', $uri['options'], $sitemap['context']);
$uri['options'] = module_invoke_all('xmlsitemap_context_url_options', $sitemap->context);
drupal_alter('xmlsitemap_context_url_options', $uri['options'], $sitemap->context);
$uri['options'] += array(
'absolute' => TRUE,
'base_url' => variable_get('xmlsitemap_base_url', $GLOBALS['base_url']),
......@@ -766,15 +772,15 @@ function _xmlsitemap_check_changed_link(array $link, $original_link = NULL, $fla
* @} End of "defgroup xmlsitemap_link_api"
*/
function xmlsitemap_get_directory(array $sitemap = NULL) {
function xmlsitemap_get_directory(stdClass $sitemap = NULL) {
$directory = &drupal_static(__FUNCTION__);
if (!isset($directory)) {
$directory = variable_get('xmlsitemap_path', 'xmlsitemap');
}
if (!empty($sitemap['smid'])) {
return file_build_uri($directory . '/' . $sitemap['smid']);
if (!empty($sitemap->smid)) {
return file_build_uri($directory . '/' . $sitemap->smid);
}
else {
return file_build_uri($directory);
......@@ -784,7 +790,7 @@ function xmlsitemap_get_directory(array $sitemap = NULL) {
/**
* Check that the sitemap files directory exists and is writable.
*/
function xmlsitemap_check_directory(array $sitemap = NULL) {
function xmlsitemap_check_directory(stdClass $sitemap = NULL) {
$directory = xmlsitemap_get_directory($sitemap);
$result = file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
if (!$result) {
......@@ -815,7 +821,7 @@ function xmlsitemap_check_all_directories() {
return $directories;
}
function xmlsitemap_clear_directory(array $sitemap = NULL, $delete = FALSE) {
function xmlsitemap_clear_directory(stdClass $sitemap = NULL, $delete = FALSE) {
$directory = xmlsitemap_get_directory($sitemap);
return _xmlsitemap_delete_recursive($directory, $delete);
}
......@@ -1390,8 +1396,8 @@ function xmlsitemap_get_current_context() {
return $context;
}
function _xmlsitemap_sitemap_context_summary(array $sitemap, $context_key, array $context_info) {
$context_value = isset($sitemap['context'][$context_key]) ? $sitemap['context'][$context_key] : NULL;
function _xmlsitemap_sitemap_context_summary(stdClass $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');
......
......@@ -11,10 +11,10 @@
/**
* Get the sitemap chunk/page of the current request.
*/
function xmlsitemap_get_current_chunk(array $sitemap) {
function xmlsitemap_get_current_chunk(stdClass $sitemap) {
// Check if we should be displaing the index.
if (!isset($_GET['page']) || !is_numeric($_GET['page'])) {
if ($sitemap['chunks'] > 1) {
if ($sitemap->chunks > 1) {
return 'index';
}
else {
......
......@@ -63,7 +63,7 @@ class XMLSitemapTestHelper extends DrupalWebTestCase {
* An optional array of the XML sitemap's context.
* @param $options
* Options to be forwarded to url(). These values will be merged with, but
* always override $sitemap['uri']['options'].
* always override $sitemap->uri['options'].
* @param $headers
* An array containing additional HTTP request headers, each formatted as
* "name: value".
......@@ -75,7 +75,7 @@ class XMLSitemapTestHelper extends DrupalWebTestCase {
if (!$sitemap) {
return $this->fail('Could not load sitemap by context.');
}
return $this->drupalGet($sitemap['uri']['path'], $options + $sitemap['uri']['options'], $headers);
return $this->drupalGet($sitemap->uri['path'], $options + $sitemap->uri['options'], $headers);
}
/**
......
......@@ -26,7 +26,7 @@ class XMLSitemapWriter extends XMLWriter {
* @param $page
* The current page of the sitemap being generated.
*/
function __construct(array $sitemap, $page) {
function __construct(stdClass $sitemap, $page) {
$this->sitemap = $sitemap;
$this->sitemap_page = $page;
$this->uri = xmlsitemap_sitemap_get_file($sitemap, $page);
......@@ -167,7 +167,7 @@ class XMLSitemapWriter extends XMLWriter {
class XMLSitemapIndexWriter extends XMLSitemapWriter {
protected $rootElement = 'sitemapindex';
function __construct(array $sitemap, $page = 'index') {
function __construct(stdClass $sitemap, $page = 'index') {
parent::__construct($sitemap, 'index');
}
......
......@@ -140,8 +140,8 @@ function xmlsitemap_engines_submit_engines(array $smids = array()) {
*/
function xmlsitemap_engines_submit_sitemaps($url, array $sitemaps) {
foreach ($sitemaps as $sitemap) {
$sitemap['url'] = url($sitemap['uri']['path'], $sitemap['uri']['options']);
$submit_url = xmlsitemap_engines_prepare_url($url, $sitemap['url']);
$sitemap->url = url($sitemap->uri['path'], $sitemap->uri['options']);
$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));
}
......
......@@ -71,12 +71,12 @@ function xmlsitemap_i18n_query_xmlsitemap_generate_alter(QueryAlterableInterface
$mode = variable_get('i18n_selection_mode', 'simple');
$sitemap = $query->getMetaData('sitemap');
if (!isset($sitemap['context']['language']) || $mode == 'off') {
if (!isset($sitemap->context['language']) || $mode == 'off') {
return;
}
// Get languages to simplify query building.
$current = $sitemap['context']['language'];
$current = $sitemap->context['language'];
$default = i18n_default_language();
if ($mode == 'mixed' && $current == $default) {
......
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