Index: contrib/image_gallery/image_gallery.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/image/contrib/image_gallery/image_gallery.module,v
retrieving revision 1.31
diff -u -p -r1.31 image_gallery.module
--- contrib/image_gallery/image_gallery.module	17 Jan 2009 22:29:47 -0000	1.31
+++ contrib/image_gallery/image_gallery.module	9 Apr 2009 01:17:19 -0000
@@ -163,8 +163,7 @@ function image_gallery_page($type = NULL
   $images = array();
   if ($tid) {
     // Allow images to be sorted in a useful order.
-    $query = "SELECT n.nid FROM {term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 AND n.type = 'image' AND t.tid = %d ";
-    $count_query = "SELECT COUNT(DISTINCT(n.nid)) FROM {term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 AND n.type = 'image' AND t.tid = %d ";
+    $query = "SELECT DISTINCT(n.nid) FROM {node} n INNER JOIN {term_node} t ON t.nid = n.nid WHERE n.status = 1 AND n.type = 'image' AND t.tid = %d ";
     $args = array($tid);
     switch (variable_get('image_gallery_sort_order', IMAGE_GALLERY_SORT_CREATE_DESC)) {
       case IMAGE_GALLERY_SORT_CREATE_DESC:
@@ -176,10 +175,7 @@ function image_gallery_page($type = NULL
         break;
 
       case IMAGE_GALLERY_SORT_FILENAME:
-        $query = "SELECT n.nid FROM {term_node} t INNER JOIN {node} n ON t.nid = n.nid
-          INNER JOIN {image} i ON n.nid = i.nid INNER JOIN {files} f ON i.fid = f.fid
-          WHERE n.status = 1 AND n.type = 'image' AND t.tid = %d AND f.filename = '%s'
-          ORDER BY n.sticky DESC, f.filepath";
+        $query = "SELECT DISTINCT(n.nid) FROM {node} n INNER JOIN {term_node} t ON t.nid = n.nid INNER JOIN {image} i ON i.nid = n.nid INNER JOIN {files} f ON f.fid = i.fid WHERE n.status = 1 AND n.type = 'image' AND t.tid = %d AND f.filename = '%s' ORDER BY n.sticky DESC, f.filepath";
         $args[] = IMAGE_ORIGINAL;
         break;
 
@@ -187,7 +183,9 @@ function image_gallery_page($type = NULL
         $query .= 'ORDER BY n.sticky DESC, n.title ASC';
         break;
     }
-    $result = pager_query(db_rewrite_sql($query), variable_get('image_images_per_page', 6), 0, db_rewrite_sql($count_query), $args);
+    $query = db_rewrite_sql($query);
+    $count_query = preg_replace(array('/SELECT.*?FROM /As', '/ORDER BY .*/'), array('SELECT COUNT(DISTINCT(n.nid)) FROM ', ''), $query);
+    $result = pager_query($query, variable_get('image_images_per_page', 6), 0, $count_query, $args);
     while ($node = db_fetch_object($result)) {
       $images[] = node_load(array('nid' => $node->nid));
     }
