src] = $alias->dst;
}
}
if ($language && $last_language != $language) {
unset($aliases[$last_language]);
$aliases[$language] = array();
$query = db_query("SELECT src, dst FROM {url_alias} WHERE language = '%s' ORDER BY pid", $language);
while ($alias = db_fetch_object($query)) {
$aliases[$language][$alias->src] = $alias->dst;
}
$last_language = $language;
}
if ($language && isset($aliases[$language][$path])) {
return $aliases[$language][$path];
}
elseif (isset($aliases['all'][$path])) {
return $aliases['all'][$path];
}
else {
return $path;
}
}
/**
* Delete and regenerate the sitemap files.
*/
function xmlsitemap_regenerate() {
_xmlsitemap_regenerate_before();
// Generate the sitemap pages.
$languages = language_list();
$chunk_count = xmlsitemap_get_chunk_count(TRUE);
if ($chunk_count > 1) {
// If we have more than one chunk, we need to increment this value by one
// since the index page (chunk 0) will also need to be generated.
$chunk_count++;
}
foreach (xmlsitemap_var('languages') as $language) {
for ($i = 0; $i < $chunk_count; $i++) {
xmlsitemap_generate($i, $languages[$language]);
}
}
_xmlsitemap_regenerate_after();
}
/**
* Perform operations before rebuilding the sitemap.
*/
function _xmlsitemap_regenerate_before() {
// Attempt to increase the available processing time and memory limit.
@set_time_limit(240);
_xmlsitemap_set_memory_limit();
// Set a timer so we can track how long this takes.
timer_start('xmlsitemap_regenerate');
// Get the current memory usage so we can track how much memory is used.
_xmlsitemap_get_memory_usage(TRUE);
// Clear all cached sitemap files.
xmlsitemap_clear_cache();
// Save custom data into a temporary table so it can be re-loaded later.
//db_query_temporary("SELECT type, id, priority, changefreq, changecount FROM {xmlsitemap}", 'xmlsitemap_temp');
}
function _xmlsitemap_get_memory_usage($start = FALSE) {
static $memory_start;
$current = 0;
if (function_exists('memory_get_peak_usage')) {
$current = memory_get_peak_usage(TRUE);
}
if (function_exists('memory_get_usage')) {
$current = version_compare(PHP_VERSION, '5.2') ? memory_get_usage(TRUE) : memory_get_usage();
}
if (!isset($memory_start) || $start) {
$memory_start = $current;
}
return $current - $memory_start;
}
function _xmlsitemap_get_optimal_memory_limit() {
static $optimal_limit;
if (!isset($optimal_limit)) {
// Set the base memory amount from the provided core constant.
$optimal_limit = parse_size(DRUPAL_MINIMUM_PHP_MEMORY_LIMIT);
// Add memory based on the chunk size.
$optimal_limit += xmlsitemap_get_chunk_size() * 500;
// Add memory for storing the url aliases.
$aliases = db_result(db_query("SELECT COUNT(pid) FROM {url_alias} WHERE language = ''"));
$aliases += db_result(db_query("SELECT COUNT(pid) as pid_count FROM {url_alias} WHERE language <> '' GROUP BY language ORDER BY pid_count DESC"));
$optimal_limit += $aliases * 250;
}
return $optimal_limit;
}
/**
* Calculate the optimal memory level for sitemap generation.
*/
function _xmlsitemap_set_memory_limit() {
$memory_limit = @ini_get('memory_limit');
if ($memory_limit && $memory_limit != -1) {
$optimal_limit = _xmlsitemap_get_optimal_memory_limit();
if (parse_size($memory_limit) < $optimal_limit) {
@ini_set('memory_limit', $optimal_limit);
}
}
}
/**
* Perform operations after rebuilding the sitemap.
*/
function _xmlsitemap_regenerate_after() {
// Show a watchdog message that the sitemap was regenerated.
watchdog('xmlsitemap',
'XML sitemaps regenerated in @timer ms. Memory used: @memory-usage, peak: @memory-peak, optimal: @memory-optimal, limit: @memory-limit.',
array(
'@timer' => timer_read('xmlsitemap_regenerate'),
'@memory-usage' => format_size(_xmlsitemap_get_memory_usage()),
'@memory-peak' => format_size(memory_get_peak_usage(TRUE)),
'@memory-limit' => format_size(parse_size(@ini_get('memory_limit'))),
'@memory-optimal' => format_size(_xmlsitemap_get_optimal_memory_limit()),
),
WATCHDOG_NOTICE,
l(t('View sitemap'), 'sitemap.xml')
);
// Unset the regenerate flag.
variable_set('xmlsitemap_regenerate_needed', FALSE);
// If the chunk count has changed, we will need to rebuild the menu.
variable_set('menu_rebuild_needed', TRUE);
variable_set('xmlsitemap_generated_last', REQUEST_TIME);
}
/**
* Fetch the data from {xmlsitemap}, generates the sitemap, then caches it.
*
* @param $chunk
* An integer representing the integer of the sitemap page chunk.
* @param $language
* A language object, defaults to the default language.
* @return
* TRUE on success; otherwise FALSE
*
* @todo Revise/simplify or remove the function.
*/
function xmlsitemap_generate($chunk = 0, $language = NULL) {
if (!is_numeric($chunk) || $chunk > xmlsitemap_get_chunk_count()) {
// Don't bother translating this string.
trigger_error('Improper condition hit in xmlsitemap_generate(). Chunk: ' . $chunk . ', Chunk Count: ' . xmlsitemap_get_chunk_count());
return FALSE;
}
if (!isset($language)) {
$language = language_default();
}
$file = xmlsitemap_get_chunk_file($chunk, $language);
if (!$handle = fopen($file, 'wb')) {
watchdog('xmlsitemap', 'Could not open file @file for writing.', array('@file' => $file), WATCHDOG_ERROR);
return FALSE;
}
$status = TRUE;
if (xmlsitemap_get_chunk_count() > 1 && !$chunk) {
xmlsitemap_generate_index($handle, $status, $language);
}
else {
xmlsitemap_generate_chunk($handle, $status, $chunk, $language);
}
fclose($handle);
if (!$status) {
watchdog('xmlsitemap', 'Unknown error occurred while writing to file @file.', array('@file' => $file), WATCHDOG_ERROR);
}
elseif (xmlsitemap_var('gz')) {
$file_gz = xmlsitemap_get_chunk_file($chunk, $language, TRUE);
file_put_contents($file_gz, gzencode(file_get_contents($file), 9));
}
return $status;
}
//function xmlsitemap_fwrite($handle, &$status, $string) {
// $status &= (bool) fwrite($handle, $string);
//}
/**
* Write the proper XML sitemap header.
*
* @param $handle
* A file system pointer resource that is typically created using fopen().
* @param $status
* @param $index
*/
function xmlsitemap_generate_chunk_header($handle, &$status, $index = FALSE) {
$status &= (bool) fwrite($handle, '' . "\n");
if (xmlsitemap_var('xsl')) {
$status &= (bool) fwrite($handle, '' . "\n");
}
$status &= (bool) fwrite($handle, '<' . ($index ? 'sitemapindex' : 'urlset') . ' xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n");
//$status &= (bool) fwrite($handle, '<' . ($index ? 'sitemapindex' : 'urlset') . ' xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"' . "\n");
//$status &= (bool) fwrite($handle, ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' . "\n");
//$status &= (bool) fwrite($handle, ' xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9' . "\n");
//$status &= (bool) fwrite($handle, ' http://www.sitemaps.org/schemas/sitemap/0.9/' . ($index ? 'siteindex.xsd' : 'sitemap.xsd') . '">' . "\n");
return $status;
}
/**
* Generate one page (chunk) of the sitemap.
*
* @param $handle
* A file system pointer resource that is typically created using fopen().
* @param $status
* @param $chunk
* An integer representing the integer of the sitemap page chunk.
* @param $language
* A language object, defaults to the default language.
*/
function xmlsitemap_generate_chunk($handle, &$status, $chunk, $language = NULL) {
// @todo When should "AND x.language IN ('%s', '')" be used? Drupal still lists English content when viewing site in French.
$sql = "SELECT x.loc, x.lastmod, x.changefreq, x.changecount, x.priority FROM {xmlsitemap} x
WHERE x.access = 1 AND x.status = 1
ORDER BY x.loc";
$args = array();
$url_options = xmlsitemap_get_url_options(array('language' => $language, 'alias' => TRUE));
$offset = max($chunk - 1, 0) * xmlsitemap_get_chunk_size();
$limit = xmlsitemap_get_chunk_size();
$query = db_query_range($sql, $args, $offset, $limit);
xmlsitemap_generate_chunk_header($handle, $status);
while ($link = db_fetch_array($query)) {
$link['alias'] = xmlsitemap_get_path_alias($link['loc'], $language->language);
$link_output = '
Thank you for helping test the XML sitemap module rewrite. Please consider helping offset developer free time by donating or if your company is interested in sponsoring the rewrite or a specific feature, please contact the developer. Thank you to the following current sponsors: ' . implode(', ', $sponsors) . ', and all the indivduals that have donated. This message will not be seen in the stable versions.
'; //http://drupalmodules.com/module/xml-sitemap } } return $blurb; } function _xmlsitemap_get_version() { $file = db_fetch_object(db_query("SELECT filename, name, type, status, schema_version FROM {system} WHERE type = 'module' AND name = 'xmlsitemap'")); $info = drupal_parse_info_file(drupal_get_path('module', 'xmlsitemap') . '/xmlsitemap.info'); drupal_alter('system_info', $info, $file); return $info['version']; } /** * Check the status of all hook_requirements() from xmlsitemap modules. * * @param $return_only * If TRUE, will return the result, otherwise it will show a message. * @return * TRUE if there is a warning or error requirement, or FALSE otherwise. */ function xmlsitemap_check_status($return_only = FALSE) { // Load .install files include_once './includes/install.inc'; drupal_load_updates(); $warnings = FALSE; foreach (module_implements('requirements') as $module) { if (strpos($module, 'xmlsitemap') !== FALSE) { $requirements = module_invoke($module, 'requirements', 'runtime'); if (drupal_requirements_severity($requirements) >= REQUIREMENT_WARNING) { $warnings = TRUE; break; } } } if ($warnings && !$return_only && user_access('administer site configuration')) { drupal_set_message(t('One or more problems were detected with your sitemap configuration. Please check the status report for more information.', array('@status-report' => url('admin/reports/status'))), 'warning', FALSE); } return $warnings; }