? gsitemap-DRUPAL-5.patch
Index: gsitemap.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/gsitemap/Attic/gsitemap.module,v
retrieving revision 1.56.2.1
diff -u -p -r1.56.2.1 gsitemap.module
--- gsitemap.module	11 Jan 2007 07:04:16 -0000	1.56.2.1
+++ gsitemap.module	14 Mar 2007 05:47:20 -0000
@@ -196,6 +196,14 @@ function gsitemap_admin_settings() {
     '#title' => t('Priority Settings'),
     '#collapsible' => TRUE,
   );
+  $form['priority_settings']['gsitemap_chunk_size'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Chunk size'),
+    '#default_value' => variable_get('gsitemap_chunk_size', '10000'),
+    '#size' => 10,
+    '#maxlength' => 5,
+    '#description' => t('This is the number of links to send at one time.  Values can range between 1 and 50,000.'),
+  );
   $form['priority_settings']['gsitemap_frontpage'] = array(
     '#type' => 'textfield',
     '#title' => t('Front page priority'),
@@ -292,16 +300,84 @@ function gsitemap_admin_settings() {
   return system_settings_form($form);
 }
 
+function gsitemap_admin_settings_validate($form_id, $form_values) {
+  if ($form_values['gsitemap_chunk_size'] > 50000) {
+    form_set_error('priority_settings][gsitemap_chunk_size', t('Cannot send more than 50,000 links at one time.'));
+  }
+}
+
 function gsitemap_cron() {
   if (variable_get('gsitemap_cron_submit', 0)) {
     _gsitemap_submit();
   }
 }
 
-function gsitemap_output() {
+function gsitemap_output($chunk=NULL) {
+  if (!isset($chunk)) {
+    gsitemap_output_index();
+    $type = "Sitemap index";
+  } else {
+    gsitemap_output_chunk($chunk);
+    $type = "Sitemap chunk $chunk";
+  }
+
+  if(variable_get('gsitemap_logacc',0)) {
+    if(strpos(getenv('HTTP_USER_AGENT'),'Googlebot')) {
+      watchdog('gsitemap',$type . ' downloaded by Google.');
+    }
+    else {
+      watchdog('gsitemap',$type . ' downloaded by ' . getenv('HTTP_USER_AGENT') . ' at ' . getenv('REMOTE_ADDR') . '.');
+    }
+  }
+}
+
+function gsitemap_get_chunk_range($chunk) {
+  $range->low = $chunk * variable_get('gsitemap_chunk_size', '10,000');
+  $range->high = ($chunk + 1) * variable_get('gsitemap_chunk_size', '10,000');
+  return $range;
+}
+
+function gsitemap_output_index() {
+  $countcom = variable_get('gsitemap_countcom',1);
+
+  header("Content-type: text/xml");
+  print '<?xml version="1.0" encoding="UTF-8"?>';
+  print '<?xml-stylesheet type="text/xsl" href="' . url(drupal_get_path('module', 'gsitemap').'/gss.xsl') . '"?>';
+
+  $result = db_fetch_object(db_query("SELECT MAX(nid) as max FROM {node}"));
+  $max_nid = $result->max;
+
+  print '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd">';
+  for ($i = 0; $i < $max_nid / variable_get('gsitemap_chunk_size', '10,000'); $i++) {
+    print '<sitemap>';
+    print '<loc>' . url("gsitemap/" . $i, NULL, NULL, TRUE) . '</loc>';
+
+    $range = gsitemap_get_chunk_range($i);
+    $result = db_fetch_object(
+      db_query(sprintf("SELECT MAX(changed) as changed from {node} WHERE nid >= %d AND nid < %d",
+		       $range->low, $range->high)));
+    $last_changed_node = $result->changed;
+
+    if ($countcom) {
+      $result = db_fetch_object(
+	db_query(sprintf("SELECT MAX(timestamp) as timestamp from {comments} WHERE nid >= %d AND nid < %d",
+			 $range->low, $range->high)));
+      $last_changed_comment = $result->timestamp;
+    } else {
+      $last_changed_comment = 0;
+    }
+
+    print '<lastmod>' . gmdate("Y-m-d\TH:i:s+00:00", max($last_changed_node, $last_changed_comment)) . '</lastmod>';
+    print '</sitemap>';
+  }
+  print '</sitemapindex>';
+}
+
+function gsitemap_output_chunk($chunk) {
   if (!ini_get('safe_mode')) {
     set_time_limit(240);
   }
+  $range = gsitemap_get_chunk_range($chunk);
   $countcom = variable_get('gsitemap_countcom', 1);
   $excluded_types = array("''");
   foreach (node_get_types() as $type => $name) {
@@ -315,13 +391,14 @@ function gsitemap_output() {
   print '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
   print '<url><loc>' . url('', NULL, NULL, TRUE) . '</loc><changefreq>always</changefreq><priority>' . variable_get('gsitemap_frontpage', '1.0') . '</priority></url>';
   $count = array();
-  $result = db_query("SELECT COUNT(*) AS count FROM {node}");
+  $result = db_query(sprintf("SELECT COUNT(*) AS count FROM {node} WHERE nid >= %d AND nid < %d",
+			     $range->low, $range->high));
   $count[0] = db_fetch_object($result);
   $result = db_query("SELECT COUNT(*) AS count FROM {gsitemap}");
   $count[1] = db_fetch_object($result); 
   if (module_exists('comment')) {
     if ($count[0]->count != $count[1]->count) {
-      $result = db_query("SELECT {node}.nid, changed FROM {node} LEFT JOIN {gsitemap} USING(nid) WHERE {gsitemap}.nid IS NULL");
+      $result = db_query(sprintf("SELECT {node}.nid, changed FROM {node} LEFT JOIN {gsitemap} USING(nid) WHERE {gsitemap}.nid IS NULL AND {node}.nid >= %d AND {node}.nid < %d", $range->low, $range->high));
       while($node = db_fetch_object($result)) {
         db_query("INSERT INTO {gsitemap} (nid, last_changed) VALUES(%d, %d)", $node->nid, $node->changed);
       }
@@ -331,7 +408,7 @@ function gsitemap_output() {
       }
     }
     $maxcomments = db_fetch_object(db_query("SELECT MAX(comment_count) AS max_comments FROM {node_comment_statistics}"));
-    $result = db_query(db_rewrite_sql("SELECT DISTINCT(n.nid), n.type, n.status, n.promote, s.comment_count, n.changed, g.previously_changed, s.last_comment_timestamp, g.previous_comment, g.priority_override FROM {node} n LEFT JOIN {node_comment_statistics} s ON n.nid=s.nid LEFT JOIN {gsitemap} g ON n.nid=g.nid LEFT JOIN {url_alias} u ON src=CONCAT('node/', n.nid) WHERE n.status > 0 AND (g.priority_override >= 0 OR g.priority_override IS NULL) AND n.type NOT IN (" . $excludes . ")", 'n'));
+    $result = db_query(sprintf("SELECT {node}.nid, changed FROM {node} LEFT JOIN {gsitemap} USING(nid) WHERE {gsitemap}.nid IS NULL AND {node}.nid >= %d AND {node}.nid < %d", $range->low, $range->high));
   }
   else {
     if ($count[0]->count != $count[1]->count) {
@@ -340,7 +417,7 @@ function gsitemap_output() {
         db_query("INSERT INTO {gsitemap} (nid, last_changed) VALUES(%d, %d)", $node->nid, $node->changed);
       }
     }
-    $result = db_query(db_rewrite_sql("SELECT DISTINCT(n.nid), n.type, n.status, n.promote, n.changed, g.previously_changed, g.priority_override FROM {node} n LEFT JOIN {gsitemap} g ON n.nid=g.nid LEFT JOIN {url_alias} u ON src=CONCAT('node/', n.nid) WHERE n.status > 0 AND (g.priority_override >=0 OR g.priority_override IS NULL) AND n.type NOT IN (" . $excludes . ")", 'n'));
+    $result = db_query(db_rewrite_sql(sprintf("SELECT DISTINCT(n.nid), n.type, n.status, n.promote, n.changed, g.previously_changed, g.priority_override FROM {node} n LEFT JOIN {gsitemap} g ON n.nid=g.nid LEFT JOIN {url_alias} u ON src=CONCAT('node/',n.nid) WHERE n.status > 0 AND (g.priority_override >=0 OR g.priority_override IS NULL) AND n.type NOT IN (" . $excludes . ") AND n.nid >= %d AND n.nid < %d", $range->low, $range->high), 'n'));
   } 
   while($node = db_fetch_object($result)) {
     $pri = _gsitemap_calc_priority($node, $maxcomments->max_comments);
