You are here

xmlsitemap_engines.module in XML sitemap 6

Define actions for Google, Yahoo!, Ask, and Bing.

File

xmlsitemap_engines/xmlsitemap_engines.module
View source
<?php

/**
 * @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['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',
  );
  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]',
    ),
  );
  if (variable_get('xmlsitemap_all_links_to_default_language', 0)) {

    // Only submit the default language sitemap since it contains every link.
    $languages = array(
      language_default(),
    );
  }
  else {

    // 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.<br />Request: @url', array(
      '@engine' => $engine,
      '@url' => $url,
    ));
  }
  else {
    watchdog('xmlsitemap', 'Error occurred submitting sitemap to @engine: @code.<br />Request: @url', array(
      '@engine' => $engine,
      '@code' => 0 + $result->code,
      '@url' => $url,
    ), WATCHDOG_ERROR);
  }
}

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

Functions

Namesort descending Description
xmlsitemap_engines_cron Implementation of hook_cron().
xmlsitemap_engines_menu Implementation of hook_menu().
xmlsitemap_engines_ping_sitemap Submit the sitemap to the selected engines.
xmlsitemap_engines_submit_sitemap Helper function for xmlsitemap_engines_ping_sitemap(). Submit the sitemap to the engine passed as argument, and write a message in Drupal log.
xmlsitemap_engines_xmlsitemap_operations Implementation of hook_xmlsitemap_operations().