On a fresh D6.8 + xmlsitemap dev from 2.01.2009
I just enable xmlsitemap + xmlsitemap node modules. When xmlsitemap tries to update its links I get this error:

warning: Division by zero in /var/www/xmlsitemap_test/sites/all/modules/xmlsitemap/xmlsitemap_node/xmlsitemap_node.module on line 343

Maybe a fix could be that on line 342 instead of

if ($maxcomments != 1)

should be

if ($maxcomments != 1 && $maxcomments > 0)

I tried it and it works for me.

CommentFileSizeAuthor
#5 358033.patch2.27 KBdave reid

Comments

avpaderno’s picture

Title: Division by zero in xmlsitemap_node.module » Division by zero
Status: Active » Closed (duplicate)

This is a duplicate of #352046: Division by zero; see there for any informations.

The code should simply be

  if ($maxcomments > 1) {
strikehawkecomm’s picture

Issue tags: +xmlsitemap

Mine is failing in sites/all/modules/xmlsitemap/xmlsitemap/xmlsitemap.module on line 313

function xmlsitemap_chunk_count($reset = FALSE) {
  static $chunk_count;
  if (!isset($chunk_count) || $reset) {
    $chunk_size = variable_get('xmlsitemap_chunk_size', 1000);
    $chunks_info = variable_get('xmlsitemap_sitemap_chunks_info', array());
    $link_count = 0;
    $module_count = 0;
    foreach(module_implements('xmlsitemap_link_count') as $module) {
      if (!isset($chunks_info[$module]['id'])) {
        $chunks_info[$module]['id'] = substr(md5($module), 0, 8);
      }
      $result = module_invoke($module, 'xmlsitemap_link_count');
      $count = empty($result) ? 0 : $result;
      if (!isset($info['links']) && $info['links'] != $count) {
        $chunks_info[$module]['needs update'] = TRUE;
      }
      elseif (!isset($chunks_info[$module]['needs update'])) {
        $chunks_info[$module]['needs update'] = FALSE;
      }
      $chunks_info[$module]['links'] = $count;
      $link_count += $count;
      $module_count++;
    }
    $chunk_count = (integer) ($link_count / $chunk_size);
    // Adjust the chunk count because each module will not share its own
    // chunks with the other modules.
    $chunk_count += $module_count;
    if ($chunk_count > 1000) {
      $new_size = (integer) ceil($link_count / 100000) * 100;
      // Set which modules needs to update their cache files.
      foreach ($chunks_info as $module => $info) {
        if (!empty($info['links']) && $info['links'] > $old_size) {
          $chunks_info[$module]['needs update'] = TRUE;
        }
        elseif (!isset($chunks_info[$module]['needs update'])) {
          $chunks_info[$module]['needs update'] = FALSE;
        }
      }
      variable_set('xmlsitemap_chunk_size', $new_size);
      $chunk_size = $new_size;
    }
    $chunk_count = 0;
    foreach ($chunks_info as $module => $info) {
      // If a module is not enabled, its links will not be added to the
      // sitemap.
      if (!module_exists($module)) {
        $chunks_info[$module]['first chunk'] = -1;
      }
      else {
        $chunks_info[$module]['chunks'] = $chunks = ceil($info['links'] / $chunk_size);
        if ($chunks) {
          $chunks_info[$module]['first chunk'] = $chunk_count;
          $chunk_count += $chunks;
        }
        else {
          $chunks_info[$module]['first chunk'] = -1;
        }
      }
    }
    variable_set('xmlsitemap_sitemap_chunks_info', $chunks_info);
  }
  return $chunk_count;
}

It starts with:

    $chunk_count = (integer) ($link_count / $chunk_size);

So, why is the beta versions not alpha tested before released to be bug tested or is there something unique about my installation with no core changes?

strikehawkecomm’s picture

Version: 6.x-1.x-dev » 6.x-1.0-beta5
Status: Closed (duplicate) » Active
dave reid’s picture

@strikehawkecomm: Thanks for reporting this, I'll try and take a look at the versions code.

dave reid’s picture

Status: Active » Needs review
StatusFileSize
new2.27 KB

Line 313 is $chunk_count = (integer) ($link_count / $chunk_size);.

The variable $chunk_size comes directly from variable_get('xmlsitemap_chunk_size', 1000), but can also be set to a different value in some code just a little bit later:

      $new_size = (integer) ceil($link_count / 100000) * 100;
      ...
      variable_set('xmlsitemap_chunk_size', $new_size);
      $chunk_size = $new_size;

If $link_count is 0 (for instance when the modules are first installed), then $chunk_size is going to be set to 0, leading to a division by zero on the next time this code is executed.

If the attached patch looks good, I'll commit this later today.

avpaderno’s picture

The value for the chunk size is changed when the number of chunks is greater than 1000, which means that the number of links must be at least 1000001; in such case, $link_count is surely not 0.

Anonymous’s picture

Do we really need the variable_set in the hook_update_N? If we do then wouldn't we also need it for the hook_install?

avpaderno’s picture

The reported error is the division by zero, and who re-opened the issue pointed out that (integer) ($link_count / $chunk_size) is the line causing the error. If this is the case, then the division by zero is caused by $chunk_size being set to zero.

The code, as also reported by strikehawkecomm in #2, would change the value for the chunk size only when the numbers of chunks is greater than 1000, which means that $link_count must be at least 1000001. In these case, $new_size = (integer) ceil($link_count / 100000) * 100 would assign 1100 to $new_size. Changing the code from $new_size = (integer) ceil($link_count / 100000) * 100 to $new_size = (integer) ceil(max($link_count, 1) / 100000) * 100 doesn't change anything because max($link_count, 1), where $link_count is 1000001 will always use the value of $link_count.

To me, it seems that the error is caused by the fact the variable $chunk_size has a wrong value because the Drupal variable used to initialize it is set to 0 for some reasons; this is not necessarily caused by code executed in XML sitemap.

avpaderno’s picture

Status: Needs review » Needs work
Anonymous’s picture

Version: 6.x-1.0-beta5 » 6.x-1.x-dev
Status: Needs work » Active
Issue tags: -xmlsitemap

I need to review the current code base. This may become a duplicate of #485632: The code to automatically calculate the sitemap chunk size is wrong.

avpaderno’s picture

The code reported in the last comments doesn't even exist in the development snapshot, which uses different code.

avpaderno’s picture

Status: Active » Fixed

The reported code doesn't exist anymore in the last commits done.
I would rather see this bug report as fixed.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.