' . format_plural(count($sitemaps), 'Your sitemap is located at !sitemap.', 'Your sitemaps are located at: !sitemaps', array('!sitemaps' => theme('item_list', array('items' => $sitemaps)), '!sitemap' => current($sitemaps))) . '
'; break; case 'admin/config/search/xmlsitemap': break; case 'admin/config/search/xmlsitemap/rebuild': $output .= '' . t("This action rebuilds your site's XML sitemap and regenerates the cached files, and may be a lengthy process. If you just installed XML sitemap, this can be helpful to import all your site's content into the sitemap. Otherwise, this should only be used in emergencies.") . '
'; } if (arg(0) == 'admin' && strpos($path, 'xmlsitemap') !== FALSE) { if ($arg[1] == 'config' && user_access('administer xmlsitemap')) { module_load_install('xmlsitemap'); xmlsitemap_check_status(); } module_load_include('inc', 'xmlsitemap'); if ($blurb = _xmlsitemap_get_blurb()) { $output .= $blurb; } } return $output; } /** * Implements hook_perm(). */ function xmlsitemap_permission() { return array( 'administer xmlsitemap' => array( 'title' => t('Administer XML sitemap settings.'), ), ); } /** * Implements hook_menu(). */ function xmlsitemap_menu() { $items['admin/config/search/xmlsitemap'] = array( 'title' => 'XML sitemap', 'description' => 'Configure the XML sitemap.', 'page callback' => 'drupal_get_form', 'page arguments' => array('xmlsitemap_settings_form'), 'access arguments' => array('administer xmlsitemap'), 'file' => 'xmlsitemap.admin.inc', ); $items['admin/config/search/xmlsitemap/settings'] = array( 'title' => 'Settings', 'access arguments' => array('administer xmlsitemap'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'file' => 'xmlsitemap.admin.inc', 'weight' => -10, ); $items['admin/config/search/xmlsitemap/rebuild'] = array( 'title' => 'Rebuild', 'description' => 'Rebuild the site map.', 'page callback' => 'drupal_get_form', 'page arguments' => array('xmlsitemap_rebuild_form'), 'access arguments' => array('administer xmlsitemap'), 'type' => MENU_LOCAL_TASK, 'file' => 'xmlsitemap.admin.inc', 'weight' => 10, ); $items['sitemap.xml'] = array( 'page callback' => 'xmlsitemap_output_chunk', 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, 'file' => 'xmlsitemap.pages.inc', ); $chunks = xmlsitemap_get_chunk_count(); if ($chunks > 1) { for ($i = 1; $i <= $chunks; $i++) { $items['sitemap-' . $i . '.xml'] = array( 'page callback' => 'xmlsitemap_output_chunk', 'page arguments' => array((string) $i), 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, 'file' => 'xmlsitemap.pages.inc', ); } } $items['sitemap.xsl'] = array( 'page callback' => 'xmlsitemap_output_xsl', 'access callback' => TRUE, 'type' => MENU_CALLBACK, 'file' => 'xmlsitemap.pages.inc', ); return $items; } /** * Implements hook_cron(). */ function xmlsitemap_cron() { // If there were no new or changed links, skip. if (!variable_get('xmlsitemap_regenerate_needed', FALSE)) { return; } // If the minimum sitemap lifetime hasn't been passed, skip. $lifetime = REQUEST_TIME - variable_get('xmlsitemap_generated_last', 0); if ($lifetime < variable_get('xmlsitemap_minimum_lifetime', 0)) { return; } // Regenerate the sitemap XML files. module_load_include('inc', 'xmlsitemap'); xmlsitemap_regenerate(); } /** * Implements hook_xmlsitemap_links(). */ function xmlsitemap_xmlsitemap_links() { // Frontpage link. $links[] = array( 'type' => 'frontpage', 'id' => 0, 'loc' => '', ); return $links; } /** * Implements hook_xmlsitemap_link_alter(). */ function xmlsitemap_xmlsitemap_link_alter(&$link) { // Alter the frontpage priority. if ($link['type'] == 'frontpage' || $link['loc'] == '' || $link['loc'] == variable_get('site_frontpage', 'node')) { $link['priority'] = xmlsitemap_var('frontpage_priority'); $link['changefreq'] = xmlsitemap_var('frontpage_changefreq'); } } /** * Implements hook_robotstxt(). */ function xmlsitemap_robotstxt() { module_load_include('inc', 'xmlsitemap'); $sitemaps = xmlsitemap_get_sitemaps(); foreach ($sitemaps as $index => $sitemap) { $sitemaps[$index] = 'Sitemap: ' . $sitemap; } return $sitemaps; } /** * Get an array of the current site's sitemaps. * * @param $links * A boolean if TRUE, the array elements will be HTML links. * @return * An array of sitemaps. */ function xmlsitemap_get_sitemaps($links = FALSE) { static $sitemaps = array(); if (!$sitemaps) { $url_options = xmlsitemap_get_url_options(); $sitemap_languages = xmlsitemap_var('languages'); natsort($sitemap_languages); foreach ($sitemap_languages as $language) { $url_options['language'] = xmlsitemap_language_load($language); $sitemap = url('sitemap.xml', $url_options); $sitemaps[$language] = $links ? l($sitemap, $sitemap) : $sitemap; } } return $sitemaps; } /** * Return a list of commonly used parameters for url() used by XML sitemap. * * @see url() */ function xmlsitemap_get_url_options($options = array()) { return $options + array( 'absolute' => TRUE, 'base_url' => xmlsitemap_var('base_url'), ); } /** * Determine the frequency of updates to a link. * * @param $interval * An interval value in seconds. * @return * A string representing the update frequency according to the sitemaps.org * protocol. */ function xmlsitemap_get_changefreq($interval) { if ($interval <= 0 || !is_numeric($interval)) { return FALSE; } foreach (xmlsitemap_get_changefreq_options() as $value => $frequency) { if ($interval <= $value) { return $frequency; } } return 'never'; } /** * Get the current number of sitemap chunks. */ function xmlsitemap_get_chunk_count($reset = FALSE) { static $chunks; if (!isset($chunks) || $reset) { $count = max(xmlsitemap_get_link_count($reset), 1); $chunks = ceil($count / xmlsitemap_get_chunk_size($reset)); } return $chunks; } /** * Get the current number of sitemap links. */ function xmlsitemap_get_link_count($reset = FALSE) { static $count; if (!isset($count) || $reset) { $count = db_query("SELECT COUNT(id) FROM {xmlsitemap} WHERE access = 1 AND status = 1")->fetchField(); } return $count; } /** * Get the sitemap chunk size. * * This function is useful with the chunk size is set to automatic as it will * calculate the appropriate value. Use this function instead of @code * xmlsitemap_var('chunk_size') @endcode when the actual value is needed. * * @param $reset * A boolean to reset the saved, static result. Defaults to FALSE. * @return * An integer with the number of links in each sitemap page. */ function xmlsitemap_get_chunk_size($reset = FALSE) { static $size; if (!isset($size) || $reset) { $size = xmlsitemap_var('chunk_size'); if ($size === 'auto') { $count = max(xmlsitemap_get_link_count($reset), 1); // Prevent divide by zero. $size = min(ceil($count / 10000) * 5000, XMLSITEMAP_MAX_SITEMAP_LINKS); } } return $size; } /** * Recalculate the changefreq of a sitemap link. * * @param $link * A sitemap link array. */ function xmlsitemap_recalculate_changefreq(&$link) { $link['changefreq'] = round((($link['changefreq'] * $link['changecount']) + (REQUEST_TIME - $link['lastmod'])) / ($link['changecount'] + 1)); $link['changecount']++; $link['lastmod'] = REQUEST_TIME; } /** * Calculates the average interval between UNIX timestamps. * * @param $timestamps * An array of UNIX timestamp integers. * @return * An integer of the average interval. */ function xmlsitemap_calculate_changefreq($timestamps) { sort($timestamps); $count = count($timestamps) - 1; $diff = 0; for ($i = 0; $i < $count; $i++) { $diff += $timestamps[$i + 1] - $timestamps[$i]; } return $count > 0 ? round($diff / $count) : 0; } /** * Check if there is a visible sitemap link given a certain set of conditions. * * @param $conditions * An array of values to match keyed by field. * @param $flag * An optional boolean that if TRUE, will set the regenerate needed flag if * there is a match. Defaults to FALSE. * @return * TRUE if there is a visible link, or FALSE otherwise. */ function _xmlsitemap_check_changed_links(array $conditions = array(), array $updates = array(), $flag = FALSE) { // If we are changing status or access, check for negative current values. $conditions['status'] = (!empty($updates['status']) && empty($condition['status'])) ? 0 : 1; $conditions['access'] = (!empty($updates['access']) && empty($condition['access'])) ? 0 : 1; $query = db_select('xmlsitemap'); $query->addExpression('1'); foreach ($conditions as $field => $value) { $query->condition($field, $value); } $query->range(0, 1); $changed = $query->execute()->fetchField(); if ($changed && $flag) { variable_set('xmlsitemap_regenerate_needed', TRUE); } return $changed; } /** * Check if there is sitemap link is changed from the existing data. * * @param $link * An array of the sitemap link. * @param $original_link * An optional array of the existing data. This should only contain the * fields necessary for comparison. If not provided the existing data will be * loaded from the database. * @param $flag * An optional boolean that if TRUE, will set the regenerate needed flag if * there is a match. Defaults to FALSE. * @return * TRUE if the link is changed, or FALSE otherwise. */ function _xmlsitemap_check_changed_link(array $link, $original_link = NULL, $flag = FALSE) { $changed = FALSE; if ($original_link === NULL) { // Load only the fields necessary for data to be changed in the sitemap. $original_link = db_query_range("SELECT loc, access, status, lastmod, priority, changefreq, changecount, language FROM {xmlsitemap} WHERE type = :type AND id = :id", 0, 1, array(':type' => $link['type'], ':id' => $link['id']))->fetchAssoc(); } if (!$original_link) { if ($link['access'] && $link['status']) { // Adding a new visible link. $changed = TRUE; } } else { if (!($original_link['access'] && $original_link['status']) && $link['access'] && $link['status']) { // Changing a non-visible link to a visible link. $changed = TRUE; } elseif ($original_link['access'] && $original_link['status'] && array_diff_assoc($original_link, $link)) { // Changing a visible link $changed = TRUE; } } if ($changed && $flag) { variable_set('xmlsitemap_regenerate_needed', TRUE); } return $changed; } /** * Load a specific sitemap link. * * @param $conditions * An array of values to match keyed by field. * @return * An array representing the first sitemap link matching the conditions found. * * @todo Convert to use $type and $id as parameters. */ function xmlsitemap_load_link(array $conditions) { $query = db_select('xmlsitemap', 'x'); $query->fields('x'); foreach ($conditions as $field => $value) { $query->condition($field, $value); } $query->range(0, 1); $link = $query->execute()->fetchAssoc(); // Allow other modules to respond after loading the link. //module_invoke_all('xmlsitemap_load_link', $link, $conditions, $args); return $link; } /** * Saves or updates a sitemap link. * * @param $link * An array with a sitemap link. */ function xmlsitemap_save_link(array $link) { $link += array( 'access' => 1, 'status' => 1, 'status_override' => 0, 'lastmod' => 0, 'priority' => 0.5, 'priority_override' => 0, 'changefreq' => 0, 'changecount' => 0, 'language' => LANGUAGE_NONE, ); // Allow other modules to alter the link before saving. drupal_alter('xmlsitemap_link', $link); // Temporary validation checks. // @todo Remove in final? if ($link['priority'] < 0 || $link['priority'] > 1) { trigger_error(t('Invalid sitemap link priority %priority.