web/drupal/modules/xmlsitemap/xmlsitemap_engines/xmlsitemap_engines.module
author cavaliet@caf4f556-3d62-0410-8435-a86758001935
Fri, 21 Aug 2009 16:26:26 +0000
branchdrupal
changeset 74 0ff3ba646492
permissions -rw-r--r--
Create branch for drupal with first commit from local working copy

<?php
// $Id: xmlsitemap_engines.module,v 1.5.2.50 2009/07/24 17:59:27 earnie Exp $

/**
 * @file
 * Define actions for Google, Yahoo!, Ask, and Bing.
 */

/**
 * @addtogroup xmlsitemap
 * @{
 */

/*****************************************************************************
 * Drupal hooks.
 ****************************************************************************/

/**
 * Implementation of hook_cron().
 */
function xmlsitemap_engines_cron() {
  if (variable_get('site_offline', 0)) {
    return;
  }
  $frequency = variable_get('xmlsitemap_engines_cron_submit_frequency', 3600);
  $last_ping = variable_get('xmlsitemap_engines_cron_timestamp_submit', 0);
  $submit_on_change = variable_get('xmlsitemap_engines_submit', FALSE);
  $content_changed = variable_get('xmlsitemap_sitemap_is_changed', FALSE);
  $ping_engines = ($submit_on_change && $content_changed) ||
    ((REQUEST_TIME - $last_ping) >= $frequency);
  if ($ping_engines) {
    xmlsitemap_engines_ping_sitemap();
    variable_set('xmlsitemap_sitemap_is_changed', FALSE);
    variable_set('xmlsitemap_engines_cron_timestamp_submit', REQUEST_TIME);
  }
}

/**
 * Implementation of hook_menu().
 */
function xmlsitemap_engines_menu() {
  $items = array();
  $items['admin/settings/xmlsitemap/engines'] = array(
    'title' => 'Search engines',
    'description' => 'Configure the submission settings of the XML sitemap to the search engines.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('xmlsitemap_engines_settings'),
    'access arguments' => array('administer site configuration'),
    'type' => MENU_LOCAL_TASK,
    'file' => 'xmlsitemap_engines.admin.inc',
  );
  if ($verify = variable_get("xmlsitemap_engines_google_verify", '')) {
    $items[$verify] = array(
      'title' => 'Google verification page',
      'page callback' => 'xmlsitemap_engines_verify',
      'page arguments' => array('google'),
      'access arguments' => array('access content'),
      'type' => MENU_CALLBACK,
      'file' => 'xmlsitemap_engines.pages.inc',
    );
  }
  if ($verify = variable_get("xmlsitemap_engines_yahoo_verify", '')) {
    $items[$verify] = array(
      'title' => 'Yahoo! verification page',
      'page callback' => 'xmlsitemap_engines_verify',
      'page arguments' => array('yahoo'),
      'access arguments' => array('access content'),
      'type' => MENU_CALLBACK,
      'file' => 'xmlsitemap_engines.pages.inc',
    );
  }
  if ($verify = variable_get("xmlsitemap_engines_bing_verify", '')) {
    $items[$verify] = array(
      'title' => 'Bing verification page',
      'page callback' => 'xmlsitemap_engines_verify',
      'page arguments' => array('bing'),
      'access arguments' => array('access content'),
      'type' => MENU_CALLBACK,
      'file' => 'xmlsitemap_engines.pages.inc',
    );
  }
  return $items;
}

/**
 * Implementation of hook_xmlsitemap_operations().
 */
function xmlsitemap_engines_xmlsitemap_operations() {
  return array(
    'submit_to_all' => array(
      'label' => t('Submit the sitemap to all the active search engines'),
      'callback' => 'xmlsitemap_engines_ping_sitemap',
    ),
    'submit_to_askcom' => array(
      'label' => t('Submit the sitemap to Ask.com'),
      'callback' => 'xmlsitemap_engines_ping_sitemap',
      'callback arguments' => array('engine' => 'ask'),
    ),
    'submit_to_bing' => array(
      'label' => t('Submit the sitemap to Bing'),
      'callback' => 'xmlsitemap_engines_ping_sitemap',
      'callback arguments' => array('engine' => 'bing'),
    ),
    'submit_to_google' => array(
      'label' => t('Submit the sitemap to Google'),
      'callback' => 'xmlsitemap_engines_ping_sitemap',
      'callback arguments' => array('engine' => 'google'),
    ),
    'submit_to_moreovercom' => array(
      'label' => t('Submit the sitemap to Moreover.com'),
      'callback' => 'xmlsitemap_engines_ping_sitemap',
      'callback arguments' => array('engine' => 'moreover'),
    ),
    'submit_to_yahoo' => array(
      'label' => t('Submit the sitemap to Yahoo!'),
      'callback' => 'xmlsitemap_engines_ping_sitemap',
      'callback arguments' => array('engine' => 'yahoo'),
    ),
  );
}

/*****************************************************************************
 * Public functions.
 ****************************************************************************/

/**
 * Submit the sitemap to the selected engines.
 */
function xmlsitemap_engines_ping_sitemap($engine = NULL) {
  $engines = array(
    'ask' => array(
      'Ask.com',
      'http://submissions.ask.com/ping?sitemap=[sitemap]',
    ),
    'bing' => array(
      'Bing (formerly Live Search)',
      'http://www.bing.com/webmaster/ping.aspx?siteMap=[sitemap]',
    ),
    'google' => array(
      'Google',
      'http://www.google.com/webmasters/tools/ping?sitemap=[sitemap]'
    ),
    'moreover' => array(
      'Moreover.com',
      'http://api.moreover.com/ping?u=[sitemap]'
    ),
    'yahoo' => array(
      'Yahoo!',
      'http://search.yahooapis.com/SiteExplorerService/V1/ping?sitemap=[sitemap]'
    ),
  );

  // Get a list of enabled languages.
  $languages = language_list('enabled');
  $languages = $languages[1];

  foreach ($languages as $language) {
    if (!isset($engine)) {
      foreach ($engines as $id => $info) {
        if (variable_get("xmlsitemap_engines_{$id}_submit", FALSE)) {
          xmlsitemap_engines_submit_sitemap($info[0], "xmlsitemap_engines_{$id}_url", $info[1], $language);
        }
      }
    }
    elseif (isset($engines[$engine])) {
      xmlsitemap_engines_submit_sitemap($engines[$engine][0], "xmlsitemap_engines_{$engine}_url", $engines[$engine][1], $language);
    }
  }
}

/**
 * Helper function for xmlsitemap_engines_ping_sitemap().
 * Submit the sitemap to the engine passed as argument, and write a message in
 * Drupal log.
 *
 * @param $engine
 *  The identifier for the search engine.
 * @param $url_var
 *  The variable name containing the submission URL used by the search engine.
 * @param $default_url
 *  The default submission URL.
 */
function xmlsitemap_engines_submit_sitemap($engine, $url_var, $default_url, $language = null) {
  $url_options = array ('absolute' => TRUE);
  if (!is_null($language)) {
    $url_options['language'] = $language;
  }
  
  $url = url('sitemap.xml', $url_options);
  $url = strtr(variable_get($url_var, $default_url), array('[sitemap]' => $url));
  
  $result = drupal_http_request($url);
  if ($result->code == 200) {
    watchdog('xmlsitemap', 'Sitemap successfully submitted to !engine.',
      array('!engine' => $engine)
    );
  }
  else {
    watchdog('xmlsitemap', 'Error occurred submitting sitemap to !engine: !code.',
      array('!engine' => $engine, '!code' => 0 + $result->code), WATCHDOG_ERROR
    );
  }
}

/**
 * @} End of "addtogroup xmlsitemap".
 */