You are here

xmlsitemap_language_hierarchy.module in Language Hierarchy 7

Based on node_translation_sitemap module.

File

modules/xmlsitemap_language_hierarchy/xmlsitemap_language_hierarchy.module
View source
<?php

/**
 * @file
 * Based on node_translation_sitemap module.
 */

/**
 * Implements hook_entity_info_alter().
 */
function xmlsitemap_language_hierarchy_entity_info_alter(&$entity_info) {
  $entity_info['node']['xmlsitemap'] = array(
    'process callback' => 'xmlsitemap_language_hierarchy_xmlsitemap_process_node_links',
  );
}

/**
 * Process node sitemap links.
 *
 * @param $nids
 *   An array of node IDs.
 */
function xmlsitemap_language_hierarchy_xmlsitemap_process_node_links($nids) {
  xmlsitemap_node_xmlsitemap_process_node_links($nids);
  xmlsitemap_language_hierarchy_xmlsitemap_process_node_translation_links($nids);
}

/**
 * Implements hook_xmlsitemap_rebuild_clear().
 */
function xmlsitemap_language_hierarchy_xmlsitemap_rebuild_clear($entities, $save_custom) {
  if (in_array('node', $entities)) {
    $query = db_delete('xmlsitemap');
    $query
      ->condition('type', db_like('node_translation_') . '%', 'LIKE');

    // If we want to save the custom data, make sure to exclude any links
    // that are not using default inclusion or priority.
    if ($save_custom) {
      $query
        ->condition('status_override', 0);
      $query
        ->condition('priority_override', 0);
    }
    $query
      ->execute();
  }
}

/**
 * Implements hook_cron().
 */
function xmlsitemap_language_hierarchy_cron() {

  // Process old nodes not found in the {xmlsitemap} table.
  xmlsitemap_language_hierarchy_xmlsitemap_index_links(xmlsitemap_var('batch_limit'));
}

/**
 * Implements hook_xmlsitemap_index_links().
 */
function xmlsitemap_language_hierarchy_xmlsitemap_index_links($limit) {
  if ($types = xmlsitemap_get_link_type_enabled_bundles('node')) {
    $nids = db_query_range("SELECT n.nid\n      FROM {node} n\n      LEFT JOIN {xmlsitemap} x ON x.type = 'node' AND n.nid = x.id\n      WHERE x.id IS NULL AND n.type IN (:types)\n      ORDER BY n.nid DESC", 0, $limit, array(
      ':types' => $types,
    ))
      ->fetchCol();
    xmlsitemap_language_hierarchy_xmlsitemap_process_node_translation_links($nids);
  }
}

/**
 * Process node translation sitemap links.
 *
 * @param $nids
 *   An array of node IDs.
 */
function xmlsitemap_language_hierarchy_xmlsitemap_process_node_translation_links($nids) {
  $nodes = node_load_multiple($nids);
  foreach ($nodes as $node) {
    $links = xmlsitemap_language_hierarchy_create_link($node);
    foreach ($links as $link) {
      xmlsitemap_link_save($link);
    }
  }
}

/**
 * Implements hook_node_insert().
 */
function xmlsitemap_language_hierarchy_node_insert($node) {
  xmlsitemap_language_hierarchy_node_update($node);
}

/**
 * Implements hook_node_update().
 */
function xmlsitemap_language_hierarchy_node_update($node) {
  $links = xmlsitemap_language_hierarchy_create_link($node);
  foreach ($links as $link) {
    xmlsitemap_link_save($link);
  }
}

/**
 * Implements hook_node_delete().
 */
function xmlsitemap_language_hierarchy_node_delete($node) {
  xmlsitemap_link_delete('node', $node->nid);
}

/**
 * Implements hook_comment_update().
 */
function xmlsitemap_language_hierarchy_comment_update($comment) {
  if ($node = node_load($comment->nid, NULL, TRUE)) {
    xmlsitemap_language_hierarchy_node_update($node);
  }
}

/**
 * Implements hook_comment_publish().
 */
function xmlsitemap_language_hierarchy_comment_publish($comment) {
  xmlsitemap_language_hierarchy_comment_update($comment);
}

/**
 * Implements hook_comment_unpublish().
 */
function xmlsitemap_language_hierarchy_comment_unpublish($comment) {
  xmlsitemap_language_hierarchy_comment_update($comment);
}

/**
 * Implements hook_comment_delete().
 */
function xmlsitemap_language_hierarchy_comment_delete($comment) {
  xmlsitemap_language_hierarchy_comment_update($comment);
}

/**
 * Create a sitemap link from a node.
 *
 * The link will be saved as $node->xmlsitemap.
 *
 * @param object $node
 *   A node object.
 *
 * @return array
 *   Links to translations.
 */
function xmlsitemap_language_hierarchy_create_link($node) {

  // Get active languages list.
  $languages = language_list();

  // Get current node's original language, as per xmlsitemap_node_create_link.
  $original_language = isset($node->language) ? $node->language : LANGUAGE_NONE;

  // Remove original language form the language list.
  if (isset($languages[$original_language])) {
    unset($languages[$original_language]);
  }
  $xmlsitemap = array();

  // For each translation language.
  foreach ($languages as $language) {

    // If translation exists and is active.
    if (!isset($node->translations->data[$language->language]) || $node->translations->data[$language->language]['status'] != 1) {
      $language_candidate = entity_translation_hierarchy_get_candidate($node, 'node', $language->language);
      if (empty($language_candidate) || entity_translation_hierarchy_is_blocked($node, 'node', $language_candidate)) {
        continue;
      }
    }
    $xmlsitemap[$language->language] = array();
    if ($node->nid && ($link = xmlsitemap_link_load('node_translation_' . $language->language, $node->nid))) {
      $xmlsitemap[$language->language] = $link;
    }
    $settings = xmlsitemap_link_bundle_load('node', $node->type);
    $uri = entity_uri('node', $node);
    $xmlsitemap[$language->language] += array(
      'type' => 'node_translation_' . $language->language,
      'id' => $node->nid,
      'subtype' => $node->type,
      'status' => $settings['status'],
      'status_default' => $settings['status'],
      'status_override' => 0,
      'priority' => $settings['priority'],
      'priority_default' => $settings['priority'],
      'priority_override' => 0,
    );

    // Always recalculate changefreq and changecount.
    $timestamps = xmlsitemap_node_get_timestamps($node);
    $xmlsitemap[$language->language]['changefreq'] = $node->nid ? xmlsitemap_calculate_changefreq($timestamps) : 0;
    $xmlsitemap[$language->language]['changecount'] = $node->nid ? count($timestamps) - 1 : 0;

    // The following values must always be checked because they are volatile.
    $xmlsitemap[$language->language]['loc'] = $uri['path'];
    $xmlsitemap[$language->language]['lastmod'] = count($timestamps) ? max($timestamps) : 0;
    $xmlsitemap[$language->language]['access'] = $node->nid ? xmlsitemap_node_view_access($node, drupal_anonymous_user()) : 1;
    $xmlsitemap[$language->language]['language'] = $language->language;
  }
  return $xmlsitemap;
}