web/drupal/modules/xmlsitemap/xmlsitemap_engines/xmlsitemap_engines.module
branchdrupal
changeset 74 0ff3ba646492
equal deleted inserted replaced
73:fcf75e232c5b 74:0ff3ba646492
       
     1 <?php
       
     2 // $Id: xmlsitemap_engines.module,v 1.5.2.50 2009/07/24 17:59:27 earnie Exp $
       
     3 
       
     4 /**
       
     5  * @file
       
     6  * Define actions for Google, Yahoo!, Ask, and Bing.
       
     7  */
       
     8 
       
     9 /**
       
    10  * @addtogroup xmlsitemap
       
    11  * @{
       
    12  */
       
    13 
       
    14 /*****************************************************************************
       
    15  * Drupal hooks.
       
    16  ****************************************************************************/
       
    17 
       
    18 /**
       
    19  * Implementation of hook_cron().
       
    20  */
       
    21 function xmlsitemap_engines_cron() {
       
    22   if (variable_get('site_offline', 0)) {
       
    23     return;
       
    24   }
       
    25   $frequency = variable_get('xmlsitemap_engines_cron_submit_frequency', 3600);
       
    26   $last_ping = variable_get('xmlsitemap_engines_cron_timestamp_submit', 0);
       
    27   $submit_on_change = variable_get('xmlsitemap_engines_submit', FALSE);
       
    28   $content_changed = variable_get('xmlsitemap_sitemap_is_changed', FALSE);
       
    29   $ping_engines = ($submit_on_change && $content_changed) ||
       
    30     ((REQUEST_TIME - $last_ping) >= $frequency);
       
    31   if ($ping_engines) {
       
    32     xmlsitemap_engines_ping_sitemap();
       
    33     variable_set('xmlsitemap_sitemap_is_changed', FALSE);
       
    34     variable_set('xmlsitemap_engines_cron_timestamp_submit', REQUEST_TIME);
       
    35   }
       
    36 }
       
    37 
       
    38 /**
       
    39  * Implementation of hook_menu().
       
    40  */
       
    41 function xmlsitemap_engines_menu() {
       
    42   $items = array();
       
    43   $items['admin/settings/xmlsitemap/engines'] = array(
       
    44     'title' => 'Search engines',
       
    45     'description' => 'Configure the submission settings of the XML sitemap to the search engines.',
       
    46     'page callback' => 'drupal_get_form',
       
    47     'page arguments' => array('xmlsitemap_engines_settings'),
       
    48     'access arguments' => array('administer site configuration'),
       
    49     'type' => MENU_LOCAL_TASK,
       
    50     'file' => 'xmlsitemap_engines.admin.inc',
       
    51   );
       
    52   if ($verify = variable_get("xmlsitemap_engines_google_verify", '')) {
       
    53     $items[$verify] = array(
       
    54       'title' => 'Google verification page',
       
    55       'page callback' => 'xmlsitemap_engines_verify',
       
    56       'page arguments' => array('google'),
       
    57       'access arguments' => array('access content'),
       
    58       'type' => MENU_CALLBACK,
       
    59       'file' => 'xmlsitemap_engines.pages.inc',
       
    60     );
       
    61   }
       
    62   if ($verify = variable_get("xmlsitemap_engines_yahoo_verify", '')) {
       
    63     $items[$verify] = array(
       
    64       'title' => 'Yahoo! verification page',
       
    65       'page callback' => 'xmlsitemap_engines_verify',
       
    66       'page arguments' => array('yahoo'),
       
    67       'access arguments' => array('access content'),
       
    68       'type' => MENU_CALLBACK,
       
    69       'file' => 'xmlsitemap_engines.pages.inc',
       
    70     );
       
    71   }
       
    72   if ($verify = variable_get("xmlsitemap_engines_bing_verify", '')) {
       
    73     $items[$verify] = array(
       
    74       'title' => 'Bing verification page',
       
    75       'page callback' => 'xmlsitemap_engines_verify',
       
    76       'page arguments' => array('bing'),
       
    77       'access arguments' => array('access content'),
       
    78       'type' => MENU_CALLBACK,
       
    79       'file' => 'xmlsitemap_engines.pages.inc',
       
    80     );
       
    81   }
       
    82   return $items;
       
    83 }
       
    84 
       
    85 /**
       
    86  * Implementation of hook_xmlsitemap_operations().
       
    87  */
       
    88 function xmlsitemap_engines_xmlsitemap_operations() {
       
    89   return array(
       
    90     'submit_to_all' => array(
       
    91       'label' => t('Submit the sitemap to all the active search engines'),
       
    92       'callback' => 'xmlsitemap_engines_ping_sitemap',
       
    93     ),
       
    94     'submit_to_askcom' => array(
       
    95       'label' => t('Submit the sitemap to Ask.com'),
       
    96       'callback' => 'xmlsitemap_engines_ping_sitemap',
       
    97       'callback arguments' => array('engine' => 'ask'),
       
    98     ),
       
    99     'submit_to_bing' => array(
       
   100       'label' => t('Submit the sitemap to Bing'),
       
   101       'callback' => 'xmlsitemap_engines_ping_sitemap',
       
   102       'callback arguments' => array('engine' => 'bing'),
       
   103     ),
       
   104     'submit_to_google' => array(
       
   105       'label' => t('Submit the sitemap to Google'),
       
   106       'callback' => 'xmlsitemap_engines_ping_sitemap',
       
   107       'callback arguments' => array('engine' => 'google'),
       
   108     ),
       
   109     'submit_to_moreovercom' => array(
       
   110       'label' => t('Submit the sitemap to Moreover.com'),
       
   111       'callback' => 'xmlsitemap_engines_ping_sitemap',
       
   112       'callback arguments' => array('engine' => 'moreover'),
       
   113     ),
       
   114     'submit_to_yahoo' => array(
       
   115       'label' => t('Submit the sitemap to Yahoo!'),
       
   116       'callback' => 'xmlsitemap_engines_ping_sitemap',
       
   117       'callback arguments' => array('engine' => 'yahoo'),
       
   118     ),
       
   119   );
       
   120 }
       
   121 
       
   122 /*****************************************************************************
       
   123  * Public functions.
       
   124  ****************************************************************************/
       
   125 
       
   126 /**
       
   127  * Submit the sitemap to the selected engines.
       
   128  */
       
   129 function xmlsitemap_engines_ping_sitemap($engine = NULL) {
       
   130   $engines = array(
       
   131     'ask' => array(
       
   132       'Ask.com',
       
   133       'http://submissions.ask.com/ping?sitemap=[sitemap]',
       
   134     ),
       
   135     'bing' => array(
       
   136       'Bing (formerly Live Search)',
       
   137       'http://www.bing.com/webmaster/ping.aspx?siteMap=[sitemap]',
       
   138     ),
       
   139     'google' => array(
       
   140       'Google',
       
   141       'http://www.google.com/webmasters/tools/ping?sitemap=[sitemap]'
       
   142     ),
       
   143     'moreover' => array(
       
   144       'Moreover.com',
       
   145       'http://api.moreover.com/ping?u=[sitemap]'
       
   146     ),
       
   147     'yahoo' => array(
       
   148       'Yahoo!',
       
   149       'http://search.yahooapis.com/SiteExplorerService/V1/ping?sitemap=[sitemap]'
       
   150     ),
       
   151   );
       
   152 
       
   153   // Get a list of enabled languages.
       
   154   $languages = language_list('enabled');
       
   155   $languages = $languages[1];
       
   156 
       
   157   foreach ($languages as $language) {
       
   158     if (!isset($engine)) {
       
   159       foreach ($engines as $id => $info) {
       
   160         if (variable_get("xmlsitemap_engines_{$id}_submit", FALSE)) {
       
   161           xmlsitemap_engines_submit_sitemap($info[0], "xmlsitemap_engines_{$id}_url", $info[1], $language);
       
   162         }
       
   163       }
       
   164     }
       
   165     elseif (isset($engines[$engine])) {
       
   166       xmlsitemap_engines_submit_sitemap($engines[$engine][0], "xmlsitemap_engines_{$engine}_url", $engines[$engine][1], $language);
       
   167     }
       
   168   }
       
   169 }
       
   170 
       
   171 /**
       
   172  * Helper function for xmlsitemap_engines_ping_sitemap().
       
   173  * Submit the sitemap to the engine passed as argument, and write a message in
       
   174  * Drupal log.
       
   175  *
       
   176  * @param $engine
       
   177  *  The identifier for the search engine.
       
   178  * @param $url_var
       
   179  *  The variable name containing the submission URL used by the search engine.
       
   180  * @param $default_url
       
   181  *  The default submission URL.
       
   182  */
       
   183 function xmlsitemap_engines_submit_sitemap($engine, $url_var, $default_url, $language = null) {
       
   184   $url_options = array ('absolute' => TRUE);
       
   185   if (!is_null($language)) {
       
   186     $url_options['language'] = $language;
       
   187   }
       
   188   
       
   189   $url = url('sitemap.xml', $url_options);
       
   190   $url = strtr(variable_get($url_var, $default_url), array('[sitemap]' => $url));
       
   191   
       
   192   $result = drupal_http_request($url);
       
   193   if ($result->code == 200) {
       
   194     watchdog('xmlsitemap', 'Sitemap successfully submitted to !engine.',
       
   195       array('!engine' => $engine)
       
   196     );
       
   197   }
       
   198   else {
       
   199     watchdog('xmlsitemap', 'Error occurred submitting sitemap to !engine: !code.',
       
   200       array('!engine' => $engine, '!code' => 0 + $result->code), WATCHDOG_ERROR
       
   201     );
       
   202   }
       
   203 }
       
   204 
       
   205 /**
       
   206  * @} End of "addtogroup xmlsitemap".
       
   207  */