You are here

googlenews.module in Google News sitemap 5

Same filename and directory in other branches
  1. 6 googlenews.module
  2. 7 googlenews.module

Provides a Google News sitemap within your site using the url: http://www.yoursite.com/googlenews.xml

Webopius Ltd, www.webopius.com, info@webopius.com

File

googlenews.module
View source
<?php

/**
 * @file
 * Provides a Google News sitemap within your site using the url:
 * http://www.yoursite.com/googlenews.xml
 *
 * Webopius Ltd, www.webopius.com, info@webopius.com
 */

/**
 * PHP 5.2 backport to define DATE_W3C.
 */
if (!defined('DATE_W3C')) {
  define('DATE_W3C', 'Y-m-d\\TH:i:s+00:00');
}

/**
 * Implementation of hook_menu().
 */
function googlenews_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'googlenews.xml',
      'callback' => '_googlenews_getgooglenews',
      'access' => user_access('access content'),
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => 'admin/settings/googlenews',
      'title' => 'Google News sitemap',
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'googlenews_admin_settings',
      ),
      'access' => user_access('administer site configuration'),
    );
  }
  return $items;
}

/**
 * Generate the news feed.
 */
function _googlenews_getgooglenews() {
  global $locale;
  $content = '<?xml version="1.0" encoding="UTF-8"?>';
  $content .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"';
  $content .= '  xmlns:n="http://www.google.com/schemas/sitemap-news/0.9">';
  $node_types = variable_get('googlenews_node_types', array_keys(node_get_types()));
  $args = array_merge($node_types, array(
    time() - 172800,
  ));
  $sql = "SELECT n.nid, n.created, n.title FROM {node} n WHERE n.type IN (" . implode(',', array_fill(0, count($node_types), "'%s'")) . ") AND n.status = 1 AND n.created >= %d ORDER BY n.created DESC";
  $query = db_query_range(db_rewrite_sql($sql), $args, 0, 1000);
  while ($node = db_fetch_object($query)) {
    $content .= '<url>';
    $content .= '<loc>' . url('node/' . $node->nid, NULL, NULL, TRUE) . '</loc>';
    $content .= '<n:news>';
    $content .= '<n:publication>';
    $content .= '<n:name>' . check_plain(variable_get('site_name', 'Drupal')) . '</n:name>';
    $content .= '<n:language>' . check_plain($locale) . '</n:language>';
    $content .= '</n:publication>';
    $content .= '<n:title>' . check_plain($node->title) . '</n:title>';
    $content .= '<n:publication_date>' . gmdate(DATE_W3C, $node->created) . '</n:publication_date>';
    $content .= '</n:news>';
    $content .= '</url>';
  }
  $content .= '</urlset>';
  drupal_set_header('Content-Type: text/xml');
  print $content;
}

/**
 * Form builder; administration settings.
 */
function googlenews_admin_settings() {
  $node_types = node_get_types('names');
  $form['googlenews_node_types'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Select the content types to include in the <a href="@news-sitemap">news sitemap</a>', array(
      '@news-sitemap' => url('googlenews.xml'),
    )),
    '#default_value' => variable_get('googlenews_node_types', array_keys($node_types)),
    '#options' => $node_types,
  );
  $form['array_filter'] = array(
    '#type' => 'value',
    '#value' => TRUE,
  );
  return system_settings_form($form);
}

Functions

Namesort descending Description
googlenews_admin_settings Form builder; administration settings.
googlenews_menu Implementation of hook_menu().
_googlenews_getgooglenews Generate the news feed.