[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/xmlsitemap/ -> xmlsitemap.generate.inc (source)

   1  <?php
   2  // $Id: xmlsitemap.generate.inc,v 1.1.2.14 2010/04/29 16:22:09 davereid Exp $
   3  
   4  /**
   5   * @file
   6   * Sitemap generation and rebuilding functions for the xmlsitemap module.
   7   *
   8   * @ingroup xmlsitemap
   9   */
  10  
  11  /**
  12   * Given an internal Drupal path, return the alias for the path.
  13   *
  14   * This is similar to drupal_get_path_alias(), but designed to fetch all alises
  15   * at once so that only one database query is executed instead of several or
  16   * possibly thousands during sitemap generation.
  17   *
  18   * @param $path
  19   *   An internal Drupal path.
  20   * @param $language
  21   *   A language code to look use when looking up the paths.
  22   */
  23  function xmlsitemap_get_path_alias($path, $language) {
  24    static $aliases;
  25    static $last_language;
  26  
  27    if (!isset($aliases)) {
  28      $aliases['all'] = array();
  29      $query = db_query("SELECT src, dst FROM {url_alias} WHERE language = '' ORDER BY pid");
  30      while ($alias = db_fetch_array($query)) {
  31        $aliases['all'][$alias['src']] = $alias['dst'];
  32      }
  33    }
  34    if ($language && $last_language != $language) {
  35      unset($aliases[$last_language]);
  36      $aliases[$language] = array();
  37      $query = db_query("SELECT src, dst FROM {url_alias} WHERE language = '%s' ORDER BY pid", $language);
  38      while ($alias = db_fetch_array($query)) {
  39        $aliases[$language][$alias['src']] = $alias['dst'];
  40      }
  41      $last_language = $language;
  42    }
  43  
  44    if ($language && isset($aliases[$language][$path])) {
  45      return $aliases[$language][$path];
  46    }
  47    elseif (isset($aliases['all'][$path])) {
  48      return $aliases['all'][$path];
  49    }
  50    else {
  51      return $path;
  52    }
  53  }
  54  
  55  /**
  56   * Perform operations before rebuilding the sitemap.
  57   */
  58  function _xmlsitemap_regenerate_before() {
  59    // Attempt to increase the available processing time and memory limit.
  60    @set_time_limit(240);
  61    _xmlsitemap_set_memory_limit();
  62  
  63    // Set a timer so we can track how long this takes.
  64    timer_start('xmlsitemap_regenerate');
  65  
  66    if (variable_get('xmlsitemap_developer_mode', 0)) {
  67      watchdog('xmlsitemap', 'Starting XML sitemap generation. Memory usage: @memory-peak.', array(
  68          '@memory-peak' => format_size(_xmlsitemap_memory_get_peak_usage()),
  69        ),
  70        WATCHDOG_DEBUG
  71      );
  72    }
  73  
  74    // Clear the maximum chunk and file size variables.
  75    variable_set('xmlsitemap_max_chunks', 0);
  76    variable_set('xmlsitemap_max_filesize', 0);
  77  }
  78  
  79  /**
  80   * Wrapper function for memory_get_peak_usage() for PHP versions below 5.2.
  81   */
  82  function _xmlsitemap_memory_get_peak_usage() {
  83    if (function_exists('memory_get_peak_usage')) {
  84      return memory_get_peak_usage(TRUE);
  85    }
  86    else {
  87      return 'N/A';
  88    }
  89  }
  90  
  91  function _xmlsitemap_get_memory_usage($start = FALSE) {
  92    static $memory_start;
  93    $current = 0;
  94    if (function_exists('memory_get_peak_usage')) {
  95      $current = memory_get_peak_usage(TRUE);
  96    }
  97    if (function_exists('memory_get_usage')) {
  98      $current = version_compare(PHP_VERSION, '5.2') ? memory_get_usage(TRUE) : memory_get_usage();
  99    }
 100    if (!isset($memory_start) || $start) {
 101      $memory_start = $current;
 102    }
 103    return $current - $memory_start;
 104  }
 105  
 106  /**
 107   * Calculate the optimal PHP memory limit for sitemap generation.
 108   *
 109   * This function just makes a guess. It does not take into account
 110   * the currently loaded modules.
 111   */
 112  function _xmlsitemap_get_optimal_memory_limit() {
 113    $optimal_limit = &xmlsitemap_static(__FUNCTION__);
 114    if (!isset($optimal_limit)) {
 115      // Set the base memory amount from the provided core constant.
 116      $optimal_limit = parse_size(DRUPAL_MINIMUM_PHP_MEMORY_LIMIT);
 117  
 118      // Add memory based on the chunk size.
 119      $optimal_limit += xmlsitemap_get_chunk_size() * 500;
 120  
 121      // Add memory for storing the url aliases.
 122      if (variable_get('xmlsitemap_prefetch_aliases', 1)) {
 123        $aliases = db_result(db_query("SELECT COUNT(pid) FROM {url_alias}"));
 124        $optimal_limit += $aliases * 250;
 125      }
 126    }
 127    return $optimal_limit;
 128  }
 129  
 130  /**
 131   * Calculate the optimal memory level for sitemap generation.
 132   *
 133   * @param $new_limit
 134   *   An optional PHP memory limit in bytes. If not provided, the value of
 135   *   _xmlsitemap_get_optimal_memory_limit() will be used.
 136   */
 137  function _xmlsitemap_set_memory_limit($new_limit = NULL) {
 138    $current_limit = @ini_get('memory_limit');
 139    if ($current_limit && $current_limit != -1) {
 140      if (!is_null($new_limit)) {
 141        $new_limit = _xmlsitemap_get_optimal_memory_limit();
 142      }
 143      if (parse_size($current_limit) < $new_limit) {
 144        return @ini_set('memory_limit', $new_limit);
 145      }
 146    }
 147  }
 148  
 149  /**
 150   * Perform operations after rebuilding the sitemap.
 151   */
 152  function _xmlsitemap_regenerate_after() {
 153    // Show a watchdog message that the sitemap was regenerated.
 154    watchdog('xmlsitemap',
 155      'Finished XML sitemap generation in @timer ms. Memory usage: @memory-peak.',
 156      array(
 157        '@timer' => timer_read('xmlsitemap_regenerate'),
 158        '@memory-peak' => format_size(_xmlsitemap_memory_get_peak_usage()),
 159      ),
 160      WATCHDOG_NOTICE
 161    );
 162  
 163    // Unset the regenerate flag.
 164    variable_set('xmlsitemap_regenerate_needed', FALSE);
 165  
 166    variable_set('xmlsitemap_generated_last', REQUEST_TIME);
 167  }
 168  
 169  /**
 170   * Fetch the data from {xmlsitemap}, generates the sitemap, then caches it.
 171   *
 172   * @param $sitemap
 173   *   An unserialized data array for an XML sitemap.
 174   * @param $chunk
 175   *   An integer representing the integer of the sitemap page chunk.
 176   * @return
 177   *   TRUE on success; otherwise FALSE
 178   *
 179   * @todo Revise/simplify or remove the function.
 180   */
 181  function xmlsitemap_generate(array $sitemap, $chunk) {
 182    if ($chunk != 'index' && !is_numeric($chunk)) {
 183      // Don't bother translating this string.
 184      trigger_error('Improper condition hit in xmlsitemap_generate(). Chunk: ' . $chunk);
 185      return FALSE;
 186    }
 187  
 188    $file = xmlsitemap_sitemap_get_file($sitemap, $chunk);
 189  
 190    if (!$handle = fopen($file, 'wb')) {
 191      trigger_error(t('Could not open file @file for writing.', array('@file' => $file)));
 192      return FALSE;
 193    }
 194  
 195    $status = TRUE;
 196    if ($chunk == 'index') {
 197      xmlsitemap_generate_index($sitemap, $handle, $status);
 198    }
 199    else {
 200      $links = xmlsitemap_generate_chunk($sitemap, $handle, $status, $chunk);
 201      // @todo Fix this up.
 202      fclose($handle);
 203      return $links;
 204    }
 205    fclose($handle);
 206  
 207    // Track the maximum filesize.
 208    $filesize = filesize($file);
 209    if ($filesize > variable_get('xmlsitemap_max_filesize', 0)) {
 210      variable_set('xmlsitemap_max_filesize', $filesize);
 211    }
 212  
 213    if (!$status) {
 214      trigger_error(t('Unknown error occurred while writing to file @file.', array('@file' => $file)));
 215    }
 216    elseif (xmlsitemap_var('gz')) {
 217      $file_gz = $file . '.gz';
 218      file_put_contents($file_gz, gzencode(file_get_contents($file), 9));
 219    }
 220  
 221    return $status;
 222  }
 223  
 224  /**
 225   * Write the proper XML sitemap header.
 226   *
 227   * @param $sitemap
 228   *   An unserialized data array for an XML sitemap.
 229   * @param $type
 230   * @param $handle
 231   *   A file system pointer resource that is typically created using fopen().
 232   * @param $status
 233   */
 234  function xmlsitemap_generate_chunk_header($type, array $sitemap, $handle, &$status) {
 235    $output = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
 236  
 237    // Add the stylesheet link.
 238    if (variable_get('xmlsitemap_xsl', 1)) {
 239      $xsl_url = url('sitemap.xsl', $sitemap['uri']['options'] + array('alias' => TRUE));
 240      $output .= '<?xml-stylesheet type="text/xsl" href="' . $xsl_url . '"?>' . PHP_EOL;
 241    }
 242  
 243    $output .= '<' . $type . ' xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . PHP_EOL;
 244  
 245    // This is the full XML header required for schema validation.
 246    //$schemas = array('sitemapindex' => 'siteindex.xsd', 'urlset' => 'sitemap.xsd');
 247    //$output .= '<' . $type . ' xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"' . PHP_EOL;
 248    //$output .= '  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' . PHP_EOL;
 249    //$output .= '  xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9' . PHP_EOL;
 250    //$output .= '  http://www.sitemaps.org/schemas/sitemap/0.9/' . $schemas[$type] . '">' . PHP_EOL;
 251  
 252    $status &= (bool) fwrite($handle, $output);
 253    return $status;
 254  }
 255  
 256  /**
 257   * Generate one page (chunk) of the sitemap.
 258   *
 259   * @param $sitemap
 260   *   An unserialized data array for an XML sitemap.
 261   * @param $handle
 262   *   A file system pointer resource that is typically created using fopen().
 263   * @param $status
 264   *   A boolean that will be altered by reference with the success status of
 265   *   writing to $handle.
 266   * @param $chunk
 267   *   An integer representing the integer of the sitemap page chunk.
 268   */
 269  function xmlsitemap_generate_chunk(array $sitemap, $handle, &$status, $chunk) {
 270    $lastmod_format = variable_get('xmlsitemap_lastmod_format', XMLSITEMAP_LASTMOD_MEDIUM);
 271  
 272    $url_options = $sitemap['uri']['options'];
 273    $url_options += array(
 274      'absolute' => TRUE,
 275      'base_url' => variable_get('xmlsitemap_base_url', $GLOBALS['base_url']),
 276      'language' => language_default(),
 277      'alias' => variable_get('xmlsitemap_prefetch_aliases', TRUE),
 278    );
 279  
 280    $last_url = '';
 281    $link_count = 0;
 282  
 283    $query = array(
 284      'SELECT'   => 'SELECT x.loc, x.lastmod, x.changefreq, x.changecount, x.priority, x.language',
 285      'FROM'     => 'FROM {xmlsitemap} x',
 286      'WHERE'    => 'WHERE x.access = 1 AND x.status = 1',
 287      'ORDER BY' => 'ORDER BY x.language, x.loc',
 288    );
 289    $args = array();
 290  
 291    // Allow other modules to alter the sitemap query SQL and arguments.
 292    static $alter;
 293    if (!isset($alter)) {
 294      // Skip altering if there are no modules to invoke.
 295      xmlsitemap_load_all_includes();
 296      $alter = (bool) module_implements('query_xmlsitemap_generate_alter');
 297    }
 298    if ($alter) {
 299      $data = &$query;
 300      $data['__drupal_alter_by_ref'] = array(&$args);
 301      drupal_alter('query_xmlsitemap_generate', $data, $sitemap);
 302    }
 303  
 304    $sql = implode($query, ' ');
 305    $offset = max($chunk - 1, 0) * xmlsitemap_get_chunk_size();
 306    $limit = xmlsitemap_get_chunk_size();
 307    $query = db_query_range($sql, $args, $offset, $limit);
 308  
 309    // Add the XML header and XSL if desired.
 310    xmlsitemap_generate_chunk_header('urlset', $sitemap, $handle, $status);
 311  
 312    while ($link = db_fetch_array($query)) {
 313      $link['language'] = $link['language'] ? xmlsitemap_language_load($link['language']) : $url_options['language'];
 314      if ($url_options['alias']) {
 315        $link['loc'] = xmlsitemap_get_path_alias($link['loc'], $link['language']->language);
 316      }
 317      $link_url = url($link['loc'], array('language' => $link['language']) + $url_options);
 318  
 319      // Skip this link if it was a duplicate of the last one.
 320      // @todo Figure out a way to do this before generation so we can report
 321      // back to the user about this.
 322      if ($link_url == $last_url) {
 323        continue;
 324      }
 325      else {
 326        $last_url = $link_url;
 327        // Keep track of the total number of links written.
 328        $link_count++;
 329      }
 330  
 331      $link_output = '<url><loc>' . $link_url . '</loc>';
 332      if ($link['lastmod']) {
 333        $link_output .= '<lastmod>' . gmdate($lastmod_format, $link['lastmod']) . '</lastmod>';
 334        // If the link has a lastmod value, update the changefreq so that links
 335        // with a short changefreq but updated two years ago show decay.
 336        // We use abs() here just incase items were created on this same cron run
 337        // because lastmod would be greater than REQUEST_TIME.
 338        $link['changefreq'] = (abs(REQUEST_TIME - $link['lastmod']) + $link['changefreq']) / 2;
 339      }
 340      if ($link['changefreq']) {
 341        $link_output .= '<changefreq>' . xmlsitemap_get_changefreq($link['changefreq']) . '</changefreq>';
 342      }
 343      if (isset($link['priority']) && $link['priority'] != 0.5) {
 344        // Don't output the priority value for links that have 0.5 priority. This
 345        // is the default 'assumed' value if priority is not included as per the
 346        // sitemaps.org specification.
 347        $link_output .= '<priority>' . number_format($link['priority'], 1) . '</priority>';
 348      }
 349      $link_output .= '</url>' . PHP_EOL;
 350      $status &= (bool) fwrite($handle, $link_output);
 351    }
 352  
 353    // Close the XML file.
 354    $status &= (bool) fwrite($handle, '</urlset>' . PHP_EOL);
 355  
 356    return $link_count;
 357  }
 358  
 359  /**
 360   * Generate the index sitemap.
 361   *
 362   * @param $sitemap
 363   *   An unserialized data array for an XML sitemap.
 364   * @param $handle
 365   *   A file system pointer resource that is typically created using fopen().
 366   * @param $status
 367   */
 368  function xmlsitemap_generate_index(array $sitemap, $handle, &$status) {
 369    $lastmod_format = variable_get('xmlsitemap_lastmod_format', XMLSITEMAP_LASTMOD_MEDIUM);
 370  
 371    $url_options = $sitemap['uri']['options'];
 372    $url_options += array(
 373      'absolute' => TRUE,
 374      'base_url' => variable_get('xmlsitemap_base_url', $GLOBALS['base_url']),
 375      'language' => language_default(),
 376      'alias' => TRUE,
 377    );
 378  
 379    // Add the XML header and XSL if desired.
 380    xmlsitemap_generate_chunk_header('sitemapindex', $sitemap, $handle, $status);
 381  
 382    for ($i = 1; $i <= $sitemap['chunks']; $i++) {
 383      $output = '<sitemap>';
 384      $url_options['query']['page'] = $i;
 385      $output .= '<loc>' . url('sitemap.xml', $url_options) . '</loc>';
 386      // @todo Use the actual lastmod value of the chunk file.
 387      $output .= '<lastmod>' . gmdate($lastmod_format, REQUEST_TIME) . '</lastmod>';
 388      $output .= '</sitemap>' . PHP_EOL;
 389      $status &= (bool) fwrite($handle, $output);
 390    }
 391  
 392    // Close the XML file.
 393    $status &= (bool) fwrite($handle, '</sitemapindex>' . PHP_EOL);
 394  
 395    return $sitemap['chunks'];
 396  }
 397  
 398  // BATCH OPERATIONS ------------------------------------------------------------
 399  
 400  /**
 401   * Batch information callback for regenerating the sitemap files.
 402   *
 403   * @param $smids
 404   *   An optional array of XML sitemap IDs. If not provided, it will load all
 405   *   existing XML sitemaps.
 406   */
 407  function xmlsitemap_regenerate_batch(array $smids = array()) {
 408    if (empty($smids)) {
 409      $smids = xmlsitemap_sitemap_get_all_smids();
 410    }
 411  
 412    //$t = get_t();
 413    $batch = array(
 414      'operations' => array(),
 415      //'error_message' => $t('An error has occurred.'),
 416      'finished' => 'xmlsitemap_regenerate_batch_finished',
 417      'title' => t('Regenerating Sitemap'),
 418      'file' => drupal_get_path('module', 'xmlsitemap') . '/xmlsitemap.generate.inc',
 419    );
 420  
 421    // Generate all the sitemap pages for each context.
 422    $batch['operations'][] = array('_xmlsitemap_regenerate_before', array());
 423    foreach ($smids as $smid) {
 424      $batch['operations'][] = array('xmlsitemap_regenerate_batch_generate', array($smid));
 425      $batch['operations'][] = array('xmlsitemap_regenerate_batch_generate_index', array($smid));
 426    }
 427    $batch['operations'][] = array('_xmlsitemap_regenerate_after', array());
 428  
 429    return $batch;
 430  }
 431  
 432  /**
 433   * Batch callback; generate all pages of a sitemap.
 434   */
 435  function xmlsitemap_regenerate_batch_generate($smid, array &$context) {
 436    if (!isset($context['sandbox']['sitemap'])) {
 437      $context['sandbox']['sitemap'] = xmlsitemap_sitemap_load($smid);
 438      $context['sandbox']['sitemap']['chunks'] = 1;
 439      $context['sandbox']['sitemap']['links'] = 0;
 440      $context['sandbox']['max'] = XMLSITEMAP_MAX_SITEMAP_LINKS;
 441  
 442      // Clear the cache directory for this sitemap before generating any files.
 443      xmlsitemap_check_directory($context['sandbox']['sitemap']);
 444      xmlsitemap_clear_directory($context['sandbox']['sitemap']);
 445    }
 446  
 447    $sitemap = &$context['sandbox']['sitemap'];
 448    $links = xmlsitemap_generate($sitemap, $sitemap['chunks']);
 449    $context['message'] = t('Now generating %sitemap-url.', array('%sitemap-url' => url('sitemap.xml', $sitemap['uri']['options'] + array('query' => array('page' => $sitemap['chunks'])))));
 450  
 451    if ($links) {
 452      $sitemap['links'] += $links;
 453      $sitemap['chunks']++;
 454    }
 455    else {
 456      // Cleanup the 'extra' empty file.
 457      $file = xmlsitemap_sitemap_get_file($sitemap, $sitemap['chunks']);
 458      file_delete($file);
 459      $sitemap['chunks']--;
 460  
 461      // Save the updated chunks and links values.
 462      $context['sandbox']['max'] = $sitemap['chunks'];
 463      $sitemap['updated'] = REQUEST_TIME;
 464      xmlsitemap_sitemap_save($sitemap);
 465    }
 466  
 467    if ($sitemap['chunks'] != $context['sandbox']['max']) {
 468      $context['finished'] = $sitemap['chunks'] / $context['sandbox']['max'];
 469    }
 470  }
 471  
 472  /**
 473   * Batch callback; generate the index page of a sitemap.
 474   */
 475  function xmlsitemap_regenerate_batch_generate_index($smid, array &$context) {
 476    $sitemap = xmlsitemap_sitemap_load($smid);
 477    if ($sitemap['chunks'] > 1) {
 478      xmlsitemap_generate($sitemap, 'index');
 479      $context['message'] = t('Now generating sitemap index %sitemap-url.', array('%sitemap-url' => url('sitemap.xml', $sitemap['uri']['options'])));
 480    }
 481  }
 482  
 483  /**
 484   * Batch callback; sitemap regeneration finished.
 485   */
 486  function xmlsitemap_regenerate_batch_finished($success, $results, $operations) {
 487    if ($success) {
 488      // Reset the rebuild flag since it was successful.
 489      variable_set('xmlsitemap_regenerate_needed', FALSE);
 490      //drupal_set_message(t('The sitemaps were regenerated.'));
 491    }
 492    else {
 493      drupal_set_message(t('The sitemaps was not successfully regenerated.'), 'error');
 494    }
 495  }
 496  
 497  /**
 498   * Batch information callback for rebuilding the sitemap data.
 499   */
 500  function xmlsitemap_rebuild_batch(array $entities, $save_custom = FALSE) {
 501    $batch = array(
 502      'operations' => array(),
 503      'finished' => 'xmlsitemap_rebuild_batch_finished',
 504      'title' => t('Rebuilding Sitemap'),
 505      'file' => drupal_get_path('module', 'xmlsitemap') . '/xmlsitemap.generate.inc',
 506    );
 507  
 508    $batch['operations'][] = array('xmlsitemap_rebuild_batch_prerebuild', array());
 509  
 510    // Purge any links first.
 511    $batch['operations'][] = array('xmlsitemap_rebuild_batch_clear', array($entities, (bool) $save_custom));
 512  
 513    // Fetch all the sitemap links and save them to the {xmlsitemap} table.
 514    foreach ($entities as $entity) {
 515      $info = xmlsitemap_get_link_info($entity);
 516      $batch['operations'][] = array($info['xmlsitemap']['rebuild callback'], array($entity));
 517    }
 518  
 519    // Add the regeneration batch.
 520    $regenerate_batch = xmlsitemap_regenerate_batch();
 521    $batch['operations'] = array_merge($batch['operations'], $regenerate_batch['operations']);
 522  
 523    return $batch;
 524  }
 525  
 526  /**
 527   * Batch callback; perform operations before rebuilding the sitemap data.
 528   */
 529  function xmlsitemap_rebuild_batch_prerebuild() {
 530    // Set the rebuild flag in case something fails during the rebuild.
 531    variable_set('xmlsitemap_rebuild_needed', TRUE);
 532  
 533    timer_start('xmlsitemap_rebuild');
 534  }
 535  
 536  /**
 537   * Batch callback; clear sitemap links for entites.
 538   */
 539  function xmlsitemap_rebuild_batch_clear(array $entities, $save_custom, &$context) {
 540    if (!empty($entities)) {
 541      $sql = "DELETE FROM {xmlsitemap} WHERE type IN (" . db_placeholders($entities, 'varchar') . ')';
 542  
 543      // If we want to save the custom data, make sure to exclude any links
 544      // that are not using default inclusion or priority.
 545      if ($save_custom) {
 546        $sql .= ' AND status_override = 0 AND priority_override = 0';
 547      }
 548  
 549      db_query($sql, $entities);
 550    }
 551  
 552    $context['message'] = t('Purging links.');
 553  }
 554  
 555  /**
 556   * Batch callback; fetch and add the sitemap links for a specific entity.
 557   */
 558  function xmlsitemap_rebuild_batch_fetch($entity, &$context) {
 559    if (!isset($context['sandbox']['info'])) {
 560      $context['sandbox']['info'] = xmlsitemap_get_link_info($entity);
 561      $context['sandbox']['progress'] = 0;
 562      $context['sandbox']['last_id'] = 0;
 563    }
 564    $info = $context['sandbox']['info'];
 565  
 566    // Build the generic query.
 567    $base_table = db_escape_table($info['base table']);
 568    $id_key = db_escape_string($info['entity keys']['id']);
 569    $query = $args = $ids = array();
 570    $query['SELECT'] = "SELECT $id_key";
 571    $query['FROM']   = "FROM {{$base_table}}";
 572    $query['WHERE']  = "WHERE $id_key > %d";
 573    $args[] = $context['sandbox']['last_id'];
 574  
 575    if (!empty($info['entity keys']['bundle'])) {
 576      $bundle_key = db_escape_string($info['entity keys']['bundle']);
 577      $bundle_type = _xmlsitemap_get_field_type($info['base table'], $info['entity keys']['bundle']);
 578      $bundles = xmlsitemap_get_link_type_enabled_bundles($entity);
 579      $query['WHERE'] .= " AND $bundle_key IN (" . db_placeholders($bundles, $bundle_type) . ")";
 580      $args = array_merge($args, $bundles);
 581    }
 582  
 583    if (!isset($context['sandbox']['max'])) {
 584      $count_query = $query;
 585      $count_query['SELECT'] = "SELECT COUNT($id_key)";
 586      $sql = implode(' ', $count_query);
 587      $context['sandbox']['max'] = (int) db_result(db_query($sql, $args));
 588      if (!$context['sandbox']['max']) {
 589        // If there are no items to process, skip everything else.
 590        return;
 591      }
 592    }
 593  
 594    // PostgreSQL cannot have the ORDERED BY in the count query.
 595    $query['ORDER BY'] = "ORDER BY $id_key";
 596  
 597    $limit = 20; //variable_get('xmlsitemap_batch_limit', 100)
 598  
 599    $sql = implode(' ', $query);
 600    $query = db_query_range($sql, $args, 0, $limit);
 601    while ($id = db_result($query)) {
 602      $ids[] = $id;
 603    }
 604    $info['xmlsitemap']['process callback']($ids);
 605    $context['sandbox']['last_id'] = end($ids);
 606    $context['sandbox']['progress'] += count($ids);
 607    $context['message'] = t('Now processing %entity @last_id (@progress of @count).', array('%entity' => $entity, '@last_id' => $context['sandbox']['last_id'], '@progress' => $context['sandbox']['progress'], '@count' => $context['sandbox']['max']));
 608  
 609    if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
 610      $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
 611    }
 612  }
 613  
 614  /**
 615   * Batch callback; sitemap rebuild finished.
 616   */
 617  function xmlsitemap_rebuild_batch_finished($success, $results, $operations) {
 618    if ($success) {
 619      // Reset the rebuild flag since it was successful.
 620      variable_set('xmlsitemap_rebuild_needed', FALSE);
 621      drupal_set_message(t('The sitemap was rebuilt.'));
 622    }
 623    else {
 624      drupal_set_message(t('The sitemap was not successfully rebuilt.'), 'error');
 625    }
 626  }
 627  
 628  function xmlsitemap_get_rebuildable_link_types() {
 629    $rebuild_types = array();
 630    $entities = xmlsitemap_get_link_info();
 631  
 632    foreach ($entities as $entity => $info) {
 633      if (empty($info['xmlsitemap']['rebuild callback'])) {
 634        // If the entity is missing a rebuild callback, skip.
 635        continue;
 636      }
 637      if (!empty($info['entity keys']['bundle']) && !xmlsitemap_get_link_type_enabled_bundles($entity)) {
 638        // If the entity has bundles, but no enabled bundles, skip since
 639        // rebuilding wouldn't get any links.
 640        continue;
 641      }
 642      else {
 643        $rebuild_types[] = $entity;
 644      }
 645    }
 646  
 647    return $rebuild_types;
 648  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7