You are here

function xmlsitemap_generate_chunk in XML sitemap 7.2

Same name and namespace in other branches
  1. 6.2 xmlsitemap.generate.inc \xmlsitemap_generate_chunk()

Generate chunk.

1 call to xmlsitemap_generate_chunk()
XMLSitemapWriter::generateXML in ./xmlsitemap.xmlsitemap.inc
Generate XML.

File

./xmlsitemap.generate.inc, line 161
Sitemap generation and rebuilding functions for the xmlsitemap module.

Code

function xmlsitemap_generate_chunk(stdClass $sitemap, XMLSitemapWriter $writer, $chunk) {
  global $base_url;
  $output_elements = drupal_map_assoc(variable_get('xmlsitemap_output_elements', array(
    'lastmod',
    'changefreq',
    'priority',
  )));
  $lastmod_format = variable_get('xmlsitemap_lastmod_format', XMLSITEMAP_LASTMOD_MEDIUM);
  $url_options = $sitemap->uri['options'];
  $url_options += array(
    'absolute' => TRUE,
    'base_url' => variable_get('xmlsitemap_base_url', $base_url),
    'language' => language_default(),
    'alias' => variable_get('xmlsitemap_prefetch_aliases', TRUE),
  );
  $last_url = '';
  $link_count = 0;
  $query = db_select('xmlsitemap', 'x');
  $query
    ->fields('x', array(
    'id',
    'type',
    'subtype',
    'loc',
    'lastmod',
    'changefreq',
    'changecount',
    'priority',
    'language',
    'access',
    'status',
  ));
  $query
    ->condition('x.access', 1);
  $query
    ->condition('x.status', 1);
  $query
    ->orderBy('x.language', 'DESC');
  $query
    ->orderBy('x.loc');
  $query
    ->addTag('xmlsitemap_generate');
  $query
    ->addMetaData('sitemap', $sitemap);
  $offset = max($chunk - 1, 0) * xmlsitemap_get_chunk_size();
  $limit = xmlsitemap_get_chunk_size();
  $query
    ->range($offset, $limit);
  $links = $query
    ->execute();
  while ($link = $links
    ->fetchAssoc()) {
    $link['language'] = $link['language'] != LANGUAGE_NONE ? xmlsitemap_language_load($link['language']) : $url_options['language'];
    $parsed_url = drupal_parse_url($link['loc']);

    // Skip nodes which are 301 redirected.
    if (variable_get('xmlsitemap_redirect')) {
      $relative_redirect = redirect_fetch_rids_by_path($link['loc'], $link['language']->language, TRUE);
      $alias_redirect = redirect_fetch_rids_by_path(ltrim(url($link['loc']), '/'), $link['language']->language, TRUE);

      // If node contains a 301 redirect we skip it.
      if (!empty($relative_redirect) || !empty($alias_redirect)) {
        continue;
      }
    }

    // Remove query or fragment.
    $link['loc'] = $parsed_url['path'];
    if ($url_options['alias']) {
      $link['loc'] = xmlsitemap_get_path_alias($link['loc'], $link['language']->language);
    }
    $link_options = array(
      'language' => $link['language'],
      'xmlsitemap_link' => $link,
      'xmlsitemap_sitemap' => $sitemap,
      'query' => $parsed_url['query'],
      'fragment' => $parsed_url['fragment'],
    );

    // @todo Add a separate hook_xmlsitemap_link_url_alter() here?
    $link_url = url($link['loc'], $link_options + $url_options);

    // Skip this link if it was a duplicate of the last one.
    // @todo Figure out a way to do this before generation so we can report
    // back to the user about this.
    if ($link_url == $last_url) {
      continue;
    }
    else {
      $last_url = $link_url;

      // Keep track of the total number of links written.
      $link_count++;
    }
    $element = array();
    $element['loc'] = urldecode($link_url);
    if ($link['lastmod']) {
      if (!empty($output_elements['lastmod'])) {
        $element['lastmod'] = gmdate($lastmod_format, $link['lastmod']);
      }

      // If the link has a lastmod value, update the changefreq so that links
      // with a short changefreq but updated two years ago show decay.
      // We use abs() here just incase items were created on this same cron run
      // because lastmod would be greater than REQUEST_TIME.
      $link['changefreq'] = (abs(REQUEST_TIME - $link['lastmod']) + $link['changefreq']) / 2;
    }
    if (!empty($output_elements['changefreq']) && $link['changefreq']) {
      $element['changefreq'] = xmlsitemap_get_changefreq($link['changefreq']);
    }
    if (!empty($output_elements['priority']) && isset($link['priority']) && $link['priority'] != 0.5) {

      // Don't output the priority value for links that have 0.5 priority. This
      // is the default 'assumed' value if priority is not included as per the
      // sitemaps.org specification.
      $element['priority'] = number_format($link['priority'], 1);
    }

    // @todo Should this be moved to XMLSitemapWritier::writeSitemapElement()?
    drupal_alter('xmlsitemap_element', $element, $link, $sitemap);
    if (!empty($element)) {
      $writer
        ->writeSitemapElement('url', $element);
    }
  }
  return $link_count;
}