123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- <?php
- /**
- * @file
- * Main file for XML sitemap engines.
- */
- /**
- * Implements hook_hook_info().
- */
- function xmlsitemap_engines_hook_info() {
- $hooks['xmlsitemap_engine_info'] = array(
- 'group' => 'xmlsitemap',
- );
- $hooks['xmlsitemap_engine_info_alter'] = array(
- 'group' => 'xmlsitemap',
- );
- return $hooks;
- }
- /**
- * Implements hook_help().
- */
- function xmlsitemap_engines_help($path, $arg) {
- $output = '';
- switch ($path) {
- case 'admin/config/search/xmlsitemap/engines':
- if (!module_exists('site_verify')) {
- $output .= '<p>' . t('In order to verify site ownership with the search engines listed below, it is highly recommended to download and install the <a href="@site-verify">Site verification module</a>.', array('@site-verify' => 'https://www.drupal.org/project/site_verify')) . '</p>';
- }
- break;
- }
- return $output;
- }
- /**
- * Implements hook_menu().
- */
- function xmlsitemap_engines_menu() {
- $items['admin/config/search/xmlsitemap/engines'] = array(
- 'title' => 'Search Engines',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('xmlsitemap_engines_settings_form'),
- 'access arguments' => array('administer xmlsitemap'),
- 'type' => MENU_LOCAL_TASK,
- 'file' => 'xmlsitemap_engines.admin.inc',
- );
- // @code
- // $items['admin/config/search/xmlsitemap/engines/submit'] = array(
- // 'page callback' => 'xmlsitemap_engines_submit',
- // 'access callback' => 'xmlsitemap_engines_submit_access',
- // 'type' => MENU_CALLBACK,
- // );
- // @endcode
- return $items;
- }
- /**
- * Implements hook_cron().
- */
- function xmlsitemap_engines_cron() {
- if (xmlsitemap_engines_submit_access()) {
- xmlsitemap_engines_submit_engines();
- }
- }
- /**
- * Check if can submit.
- */
- function xmlsitemap_engines_can_submit() {
- // Skip if the site is offline since search engines will not be able to
- // access the site's content.
- if (variable_get('maintenance_mode', 0) || defined('MAINTENANCE_MODE')) {
- return FALSE;
- }
- if (!variable_get('xmlsitemap_engines_engines', array()) && !variable_get('xmlsitemap_engines_custom_urls', '')) {
- return FALSE;
- }
- return TRUE;
- }
- /**
- * Submit access.
- */
- function xmlsitemap_engines_submit_access() {
- if (!xmlsitemap_engines_can_submit()) {
- return FALSE;
- }
- // Allow manual submissions to run.
- // @code
- // @codingStandardsIgnoreLine
- // if ($_GET['q'] == 'admin/config/search/xmlsitemap/engines/submit' && user_access('administer xmlsitemap')) {
- // return TRUE;
- // }
- // @endcode
- $submit_updated = variable_get('xmlsitemap_engines_submit_updated', TRUE);
- $submitted_last = variable_get('xmlsitemap_engines_submit_last', 0);
- $minimum_lifetime = variable_get('xmlsitemap_engines_minimum_lifetime', 86400);
- // Skip if sitemap data has not been updated since last submission.
- if ($submit_updated && variable_get('xmlsitemap_generated_last', 0) <= $submitted_last) {
- return FALSE;
- }
- // Skip if the time since last submission is less than the minimum lifetime.
- if ((REQUEST_TIME - $submitted_last) < $minimum_lifetime) {
- return FALSE;
- }
- return TRUE;
- }
- /**
- * Submit the sitemaps to all the specified search engines.
- *
- * @param array $smids
- * An optional array of XML sitemap IDs. If not provided, it will load all
- * existing XML sitemaps.
- */
- function xmlsitemap_engines_submit_engines(array $smids = array()) {
- if (empty($smids)) {
- $smids = FALSE;
- }
- $sitemaps = xmlsitemap_sitemap_load_multiple($smids);
- $engines = variable_get('xmlsitemap_engines_engines', array());
- $engine_info = xmlsitemap_engines_get_engine_info();
- foreach ($engines as $engine) {
- if (isset($engine_info[$engine]['url'])) {
- xmlsitemap_engines_submit_sitemaps($engine_info[$engine]['url'], $sitemaps);
- }
- }
- $custom_urls = variable_get('xmlsitemap_engines_custom_urls', '');
- $custom_urls = preg_split('/[\r\n]+/', $custom_urls, -1, PREG_SPLIT_NO_EMPTY);
- foreach ($custom_urls as $custom_url) {
- xmlsitemap_engines_submit_sitemaps($custom_url, $sitemaps);
- }
- variable_set('xmlsitemap_engines_submit_last', REQUEST_TIME);
- }
- /**
- * Submit the sitemaps to a specific URL.
- *
- * @param string $url
- * The URL for sitemap submission.
- * @param array $sitemaps
- * An array of URLs of the sitemaps to submit.
- */
- 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);
- $request = drupal_http_request($submit_url);
- watchdog('xmlsitemap', 'Submitted the sitemap to %url and received response @code.', array('%url' => $submit_url, '@code' => $request->code));
- }
- }
- /**
- * Replace valid tokens in the URL with their appropriate values.
- *
- * @param string $url
- * An un-tokenized URL.
- *
- * @return string
- * A tokenized URL.
- */
- function xmlsitemap_engines_prepare_url($url, $sitemap) {
- return str_replace('[sitemap]', $sitemap, $url);
- }
- /**
- * Returns information about supported search engines.
- *
- * @param string $engine
- * (optional) The engine to return information for. If omitted, information
- * for all engines is returned.
- *
- * @see hook_xmlsitemap_engines_info()
- * @see hook_xmlsitemap_engines_info_alter()
- */
- function xmlsitemap_engines_get_engine_info($engine = NULL) {
- global $language;
- $engines = &drupal_static(__FUNCTION__);
- if (!isset($engines)) {
- if ($cached = cache_get('xmlsitemap:engines:' . $language->language)) {
- $engines = $cached->data;
- }
- else {
- // Fetch the results of all hook_xmlsitemap_engine_info() implementations.
- $engines = module_invoke_all('xmlsitemap_engine_info');
- // Allow other modulse to alter the engine info.
- drupal_alter('xmlsitemap_engine_info', $engines);
- // Cache by language since engine names are translated.
- cache_set('xmlsitemap:engines:' . $language->language, $engines);
- }
- }
- if (isset($engine)) {
- return isset($engines[$engine]) ? $engines[$engine] : NULL;
- }
- else {
- return $engines;
- }
- }
- /**
- * Implements hook_xmlsitemap_engine_info().
- */
- function xmlsitemap_engines_xmlsitemap_engine_info() {
- $engines['google'] = array(
- 'name' => t('Google'),
- 'url' => 'https://www.google.com/webmasters/tools/ping?sitemap=[sitemap]',
- 'help url' => 'https://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=156184',
- );
- $engines['bing'] = array(
- 'name' => t('Bing'),
- 'url' => 'https://www.bing.com/webmaster/ping.aspx?siteMap=[sitemap]',
- 'help url' => 'https://www.bing.com/webmaster',
- );
- return $engines;
- }
- /**
- * Implements hook_variables().
- */
- function xmlsitemap_engines_variables() {
- $variables = array(
- 'xmlsitemap_engines_engines' => array(),
- 'xmlsitemap_engines_custom_urls' => '',
- 'xmlsitemap_engines_minimum_lifetime' => 86400,
- 'xmlsitemap_engines_submit_last' => 0,
- 'xmlsitemap_engines_submit_updated' => TRUE,
- );
- return $variables;
- }
- /**
- * Implements hook_xmlsitemap_sitemap_operations().
- */
- function xmlsitemap_engines_xmlsitemap_sitemap_operations() {
- if (xmlsitemap_engines_can_submit()) {
- $operations['xmlsitemap_engines_submit'] = array(
- 'label' => t('Submit to search engines'),
- 'action past' => t('Submitted'),
- 'callback' => 'xmlsitemap_engines_submit_engines',
- );
- return $operations;
- }
- }
|