You are here

xmlsitemap_term.module in XML sitemap 5.2

Same filename and directory in other branches
  1. 5 xmlsitemap_term/xmlsitemap_term.module

Adds taxonomy terms to the sitemap.

File

xmlsitemap_term/xmlsitemap_term.module
View source
<?php

/**
 * @file
 * Adds taxonomy terms to the sitemap.
 */

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

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

/**
 * Implementation of hook_form_alter().
 */
function xmlsitemap_term_form_alter($form_id, &$form) {
  if (isset($form['type']['#value']) && $form_id == $form['type']['#value'] . '_node_form') {
    $node = $form['#node'];
    if (isset($form['type']) && isset($node->nid)) {
      if (isset($node->taxonomy)) {
        $terms = $node->taxonomy;
      }
      else {
        $terms = taxonomy_node_get_terms($node->nid);
      }
      $form['xmlsitemap_term_taxonomy'] = array(
        '#type' => 'value',
        '#value' => $terms,
      );
    }
  }
  else {
    switch ($form_id) {
      case 'taxonomy_form_term':
        $priority = isset($form['tid']['#value']) ? db_result(db_query("SELECT priority_override FROM {xmlsitemap_term} WHERE tid = %d", $form['tid']['#value'])) : 'NULL';
        if ($priority === FALSE) {
          $priority = 'NULL';
        }
        if (user_access('override term priority')) {
          $options = xmlsitemap_priority_options('both');
          $default = variable_get('xmlsitemap_term_vocabulary_priority_' . $form['vid']['#value'], '0.5');
          $form['xmlsitemap_term_priority'] = array(
            '#type' => 'select',
            '#title' => t('sitemap priority'),
            '#default_value' => $priority,
            '#options' => $options,
            '#description' => t('The default priority is %priority.', array(
              '%priority' => $options[$default],
            )),
          );
          $form['submit']['#weight'] = 1;
          $form['delete']['#weight'] = 1;
        }
        else {
          $form['xmlsitemap_term_priority'] = array(
            '#type' => 'value',
            '#value' => $priority,
          );
        }
        break;
      case 'taxonomy_form_vocabulary':
        $form['xmlsitemap_term_vocabulary_priority'] = array(
          '#type' => 'select',
          '#title' => t('sitemap priority'),
          '#default_value' => variable_get('xmlsitemap_term_vocabulary_priority_' . $form['vid']['#value'], 0.5),
          '#options' => xmlsitemap_priority_options('exclude'),
          '#description' => t('This will be the default priority of terms in this vocabulary.'),
        );
        $form['submit']['#weight'] = 1;
        $form['delete']['#weight'] = 1;
        break;
    }
  }
}

/**
 * Implementation of hook_nodeapi().
 */
function xmlsitemap_term_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'insert':
      if (!empty($node->taxonomy)) {
        $terms = array_keys($node->taxonomy);
        $query_args = array_merge(array(
          $node->changed,
        ), $terms);
      }
      break;
    case 'update':
      $taxonomy = empty($node->taxonomy) ? array() : array_keys($node->taxonomy);
      $xmlsitemap_term_taxonomy = empty($node->xmlsitemap_term_taxonomy) ? array() : array_keys($node->xmlsitemap_term_taxonomy);
      $terms = array_merge(array_diff($taxonomy, $xmlsitemap_term_taxonomy), array_diff($xmlsitemap_term_taxonomy, $taxonomy));
      $query_args = array_merge(array(
        $node->changed,
      ), $terms);
      break;
    case 'delete':
      if (!empty($node->taxonomy)) {
        $terms = array_keys($node->taxonomy);
        $query_args = array_merge(array(
          REQUEST_TIME,
        ), $terms);
      }
      break;
  }
  if (!empty($terms)) {
    db_query("UPDATE {xmlsitemap_term}\n      SET previously_changed = last_changed, last_changed = %d WHERE tid IN(" . xmlsitemap_placeholders($terms, 'int') . ")", $query_args);
    xmlsitemap_flag_sitemap();
  }
}

/**
 * Implementation of hook_taxonomy().
 */
function xmlsitemap_term_taxonomy($op, $type, $array = NULL) {
  if ($type == 'vocabulary') {
    switch ($op) {
      case 'delete':
        variable_del('xmlsitemap_term_vocabulary_priority_' . $array['vid']);
        xmlsitemap_flag_sitemap();
        break;
      case 'insert':
      case 'update':
        if (variable_get('xmlsitemap_term_vocabulary_priority_' . $array['vid'], 0.5) != $array['xmlsitemap_term_vocabulary_priority']) {
          variable_set('xmlsitemap_term_vocabulary_priority_' . $array['vid'], $array['xmlsitemap_term_vocabulary_priority']);
          xmlsitemap_flag_sitemap();
        }
        break;
    }
  }
  else {
    switch ($op) {
      case 'insert':
        $priority = isset($array['xmlsitemap_term_priority']) ? $array['xmlsitemap_term_priority'] : 'NULL';
        db_query("INSERT INTO {xmlsitemap_term} (tid, last_changed, priority_override) VALUES (%d, %d, %s)", $array['tid'], REQUEST_TIME, $priority);
        break;
      case 'update':
        if (!isset($array['xmlsitemap_term_priority'])) {
          $priority = db_result(db_query("SELECT priority_override FROM {xmlsitemap_term} WHERE tid = %d", $array['tid']));
          $array['xmlsitemap_term_priority'] = isset($priority) && $priority !== FALSE ? $priority : 'NULL';
        }
        db_query("UPDATE {xmlsitemap_term}\n          SET priority_override = %s\n          WHERE tid = %d", $array['xmlsitemap_term_priority'], $array['tid']);
        break;
      case 'delete':
        db_query("DELETE FROM {xmlsitemap_term} WHERE tid = %d", $array['tid']);
        break;
    }
    xmlsitemap_flag_sitemap();
  }
}

/**
 * Implementation of hook_xmlsitemap_description().
 */
function xmlsitemap_term_xmlsitemap_description() {
  return '<dt>' . t('XML sitemap term') . '</dt>' . '<dd>' . t('It adds <a href="@terms">taxonomy terms</a> (categories) to the sitemap. You can change the default priority when you add or edit a vocabulary, and you can override the default priority when you add or edit individual terms.', array(
    '@terms' => url('admin/content/taxonomy'),
  )) . '</dd>';
}

/**
 * Implementation of hook_xmlsitemap_links().
 */
function xmlsitemap_term_xmlsitemap_links() {
  $excludes = array();
  $result = db_query("SELECT vid FROM {vocabulary}");
  while ($vocabulary = db_fetch_object($result)) {
    if (variable_get('xmlsitemap_term_vocabulary_priority_' . $vocabulary->vid, 0.5) < 0) {
      $excludes[] = $vocabulary->vid;
    }
  }
  if (empty($excludes)) {
    $excludes = array(
      0,
    );
  }
  $tid = 0;
  $frontpage = explode('/', drupal_get_normal_path(variable_get('site_frontpage', 'node')));
  if (count($frontpage) == 3 && $frontpage[0] == 'taxonomy' && $frontpage[1] == 'term' && is_numeric($frontpage[2])) {
    $tid = $frontpage[2];
  }
  elseif (count($frontpage) == 2 && $frontpage[0] == 'forum' && is_numeric($frontpage[1])) {
    $tid = $frontpage[1];
  }
  $query_args = array_merge($excludes, array(
    $tid,
  ));
  $result = db_query(db_rewrite_sql("SELECT t.tid, t.vid, v.module, xt.last_changed, xt.previously_changed, xt.priority_override\n    FROM {term_data} t\n    LEFT JOIN {vocabulary} v ON t.vid = v.vid\n    LEFT JOIN {xmlsitemap_term} xt ON t.tid = xt.tid\n    WHERE (t.vid NOT IN (" . xmlsitemap_placeholders($excludes, 'int') . ") AND xt.priority_override IS NULL OR xt.priority_override >= 0)\n    AND t.tid <> %d\n    GROUP BY t.tid, t.vid, v.module, xt.last_changed, xt.previously_changed, xt.priority_override", 't', 'tid'), $query_args);
  while ($term = db_fetch_object($result)) {
    $path = taxonomy_term_path($term);
    $alias = drupal_lookup_path('alias', $path);
    if ($alias === FALSE) {
      $alias = NULL;
    }
    $url = xmlsitemap_url($path, $alias, NULL, NULL, TRUE);
    $age = REQUEST_TIME - $term->last_changed;
    $interval = empty($term->previously_changed) ? 0 : $term->last_changed - $term->previously_changed;
    if (isset($term->priority_override)) {
      $priority = $term->priority_override;
    }
    else {
      $priority = min(variable_get("xmlsitemap_term_vocabulary_priority_{$term->vid}", 0.5), 1);
    }
    db_query("INSERT INTO {xmlsitemap} (loc, lastmod, changefreq, priority) VALUES ('%s', %d, %d, %f)", $url, $term->last_changed, max($age, $interval), $priority);
  }
}

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

Functions

Namesort descending Description
xmlsitemap_term_form_alter Implementation of hook_form_alter().
xmlsitemap_term_nodeapi Implementation of hook_nodeapi().
xmlsitemap_term_taxonomy Implementation of hook_taxonomy().
xmlsitemap_term_xmlsitemap_description Implementation of hook_xmlsitemap_description().
xmlsitemap_term_xmlsitemap_links Implementation of hook_xmlsitemap_links().