Index: /branches/release-36/php/lib/block.mtpagerblock.php
===================================================================
--- /branches/release-36/php/lib/block.mtpagerblock.php (revision 2103)
+++ /branches/release-36/php/lib/block.mtpagerblock.php (revision 2103)
@@ -0,0 +1,54 @@
+<?php
+# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
+# This program is distributed under the terms of the
+# GNU General Public License, version 2.
+#
+# $Id$
+
+function smarty_block_mtpagerblock($args, $content, &$ctx, &$repeat) {
+    $localvars = array('__out', '__pager_limit', '__pager_count', '__pager_pages', '__first__', '__last__', '__odd__', '__even__', '__value__', '__counter__');
+
+    if (!isset($content)) {
+        $ctx->localize($localvars);
+        // first invocation; setup loop
+        $limit = $ctx->stash('__pager_limit');
+        $offset = $ctx->stash('__pager_offset');
+        $count = $ctx->stash('__pager_total_count');
+        $pages = $limit ? ceil( $count / $limit ) : 1;
+        $counter = 1;
+        $ctx->stash('__pager_pages', $pages);
+        $ctx->stash('__out', false);
+    } else {
+        $counter = $ctx->__stash['vars']['__counter__'] + 1;
+        $limit = $ctx->stash('__pager_limit');
+        $offset = $ctx->stash('__pager_offset');
+        $count = $ctx->stash('__pager_total_count');
+        $pages = $ctx->stash('__pager_pages');
+        $out = $ctx->stash('__out');
+    }
+
+    if ($counter <= $pages) {
+        $ctx->__stash['vars']['__value__'] = $counter;
+        $ctx->__stash['vars']['__counter__'] = $counter;
+        $ctx->__stash['vars']['__odd__'] = ($counter % 2) == 1;
+        $ctx->__stash['vars']['__even__'] = ($counter % 2) == 0;
+        $ctx->__stash['vars']['__first__'] = $counter == 1;
+        $ctx->__stash['vars']['__last__'] = $counter == $pages;
+        if (isset($args['glue']) && !empty($content)) {
+            if ($out)
+                $content = $args['glue'] . $content;
+            else
+                $ctx->stash('__out', true);
+        }
+        $repeat = true;
+    } else {
+        if (isset($args['glue']) && $out && !empty($content))
+            $content = $args['glue'] . $content;
+        $ctx->restore($localvars);
+        $repeat = false;
+    }
+
+    return $content;
+}
+?>
+
Index: /branches/release-36/php/lib/block.mtifcurrentpage.php
===================================================================
--- /branches/release-36/php/lib/block.mtifcurrentpage.php (revision 2103)
+++ /branches/release-36/php/lib/block.mtifcurrentpage.php (revision 2103)
@@ -0,0 +1,20 @@
+<?php
+# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
+# This program is distributed under the terms of the
+# GNU General Public License, version 2.
+#
+# $Id$
+
+function smarty_block_mtifcurrentpage($args, $content, &$ctx, &$repeat) {
+    if (!isset($content)) {
+        $limit = $ctx->stash('__pager_limit');
+        $offset = $ctx->stash('__pager_offset');
+        $value = $ctx->__stash['vars']['__value__'];
+        $current = $limit ? $offset / $limit + 1 : 1;
+        return $ctx->_hdlr_if($args, $content, $ctx, $repeat, $value == $current);
+    } else {
+        return $ctx->_hdlr_if($args, $content, $ctx, $repeat);
+    }
+}
+?>
+
Index: /branches/release-36/php/lib/function.mtcurrentpage.php
===================================================================
--- /branches/release-36/php/lib/function.mtcurrentpage.php (revision 2103)
+++ /branches/release-36/php/lib/function.mtcurrentpage.php (revision 2103)
@@ -0,0 +1,14 @@
+<?php
+# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
+# This program is distributed under the terms of the
+# GNU General Public License, version 2.
+#
+# $Id$
+
+function smarty_function_mtcurrentpage($args, &$ctx) {
+    $limit = $ctx->stash('__pager_limit');
+    $offset = $ctx->stash('__pager_offset');
+    return $limit ? $offset / $limit + 1 : 1;
+}
+?>
+
Index: /branches/release-36/php/lib/block.mtifmoreresults.php
===================================================================
--- /branches/release-36/php/lib/block.mtifmoreresults.php (revision 2103)
+++ /branches/release-36/php/lib/block.mtifmoreresults.php (revision 2103)
@@ -0,0 +1,19 @@
+<?php
+# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
+# This program is distributed under the terms of the
+# GNU General Public License, version 2.
+#
+# $Id$
+
+function smarty_block_mtifmoreresults($args, $content, &$ctx, &$repeat) {
+    if (!isset($content)) {
+        $limit = $ctx->stash('__pager_limit');
+        $count = $ctx->stash('__pager_total_count');
+        $offset = $ctx->stash('__pager_offset');
+        if ( $limit && !$offset ) $offset += $limit;
+        return $ctx->_hdlr_if($args, $content, $ctx, $repeat, ( $limit + $offset ) < $count);
+    } else {
+        return $ctx->_hdlr_if($args, $content, $ctx, $repeat);
+    }
+}
+?>
Index: /branches/release-36/php/lib/function.mtnextlink.php
===================================================================
--- /branches/release-36/php/lib/function.mtnextlink.php (revision 2103)
+++ /branches/release-36/php/lib/function.mtnextlink.php (revision 2103)
@@ -0,0 +1,27 @@
+<?php
+# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
+# This program is distributed under the terms of the
+# GNU General Public License, version 2.
+#
+# $Id$
+
+function smarty_function_mtnextlink($args, &$ctx) {
+
+    $limit = $ctx->stash('__pager_limit');
+    $offset = $ctx->stash('__pager_offset');
+    $offset += $limit;
+
+    if ( strpos($link, '?') ) {
+        $link .= '&';
+    }
+    else {
+        $link .= '?';
+    }
+
+    $link .= "limit=$limit";
+    if ( $offset )
+        $link .= "&offset=$offset";
+    return $link;
+}
+?>
+
Index: /branches/release-36/php/lib/function.mttotalpages.php
===================================================================
--- /branches/release-36/php/lib/function.mttotalpages.php (revision 2103)
+++ /branches/release-36/php/lib/function.mttotalpages.php (revision 2103)
@@ -0,0 +1,15 @@
+<?php
+# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
+# This program is distributed under the terms of the
+# GNU General Public License, version 2.
+#
+# $Id$
+
+function smarty_function_mttotalpages($args, &$ctx) {
+    $limit = $ctx->stash('__pager_limit');
+    if (!$limit) return 1;
+    $offset = $ctx->stash('__pager_offset');
+    ceil( $count / $limit );
+}
+?>
+
Index: /branches/release-36/php/lib/function.mtpreviouslink.php
===================================================================
--- /branches/release-36/php/lib/function.mtpreviouslink.php (revision 2103)
+++ /branches/release-36/php/lib/function.mtpreviouslink.php (revision 2103)
@@ -0,0 +1,30 @@
+<?php
+# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
+# This program is distributed under the terms of the
+# GNU General Public License, version 2.
+#
+# $Id$
+
+function smarty_function_mtpreviouslink($args, &$ctx) {
+    $limit = $ctx->stash('__pager_limit');
+    $offset = $ctx->stash('__pager_offset');
+
+    if ( $offset <= $limit )
+        $offset = 0;
+    else
+        $offset -= $limit;
+
+    if ( strpos($link, '?') ) {
+        $link .= '&';
+    }
+    else {
+        $link .= '?';
+    }
+
+    $link .= "limit=$limit";
+    if ( $offset )
+        $link .= "&offset=$offset";
+    return $link;
+}
+?>
+
Index: /branches/release-36/php/lib/mtdb_base.php
===================================================================
--- /branches/release-36/php/lib/mtdb_base.php (revision 2091)
+++ /branches/release-36/php/lib/mtdb_base.php (revision 2103)
@@ -413,5 +413,5 @@
     }
 
-    function &fetch_entries($args) {
+    function &fetch_entries($args, &$total_count = NULL) {
         if ($sql = $this->include_exclude_blogs($args)) {
             $blog_filter = 'and entry_blog_id ' . $sql;
@@ -422,6 +422,8 @@
         }
 
+        $pagination = 0;
         # automatically include offset if in request
         if ($args['offset'] == 'auto') {
+            $pagination = 1;
             $args['offset'] = 0;
             if ($args['limit'] || $args['lastn']) {
@@ -801,4 +803,7 @@
             $post_select_limit = $rco;
             $no_resort = 1;
+        } elseif ( !is_null($total_count) ) {
+            $orig_limit = $limit;
+            $orig_offset = $offset;
         } else {
             $sql = $this->apply_limit_sql($sql . " <LIMIT>", $limit, $offset);
@@ -810,7 +815,8 @@
         $entries = array();
         $j = 0;
-        $offset = $post_select_offset ? $post_select_offset : 0;
+        $offset = $post_select_offset ? $post_select_offset : $orig_offset;
         $limit = $post_select_limit ? $post_select_limit : 0;
         $id_list = array();
+        $_total_count = 0;
         while (true) {
             $e = $this->query_fetch(ARRAY_A);
@@ -826,4 +832,12 @@
                 }
             }
+            $_total_count++;
+            if ( !is_null($total_count) ) {
+                if ( ($orig_limit > 0)
+                  && ( ($_total_count-$offset) > $orig_limit) ) {
+                    // collected all the entries; only count numbers;
+                    continue;
+                }
+            }
             if ($offset && ($j++ < $offset)) continue;
             $e['entry_authored_on'] = $this->db2ts($e['entry_authored_on']);
@@ -833,5 +847,11 @@
             $this->_comment_count_cache[$e['entry_id']] = $e['entry_comment_count'];
             $this->_ping_count_cache[$e['entry_id']] = $e['entry_ping_count'];
-            if (($limit > 0) && (count($entries) >= $limit)) break;
+            if ( is_null($total_count) ) {
+                // the request does not want total count; break early
+                if (($limit > 0) && (count($entries) >= $limit)) break;
+            }
+        }
+        if ( !is_null($total_count) ) {
+            $total_count = $_total_count;
         }
 
Index: /branches/release-36/php/lib/function.mtpagerlink.php
===================================================================
--- /branches/release-36/php/lib/function.mtpagerlink.php (revision 2103)
+++ /branches/release-36/php/lib/function.mtpagerlink.php (revision 2103)
@@ -0,0 +1,28 @@
+<?php
+# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
+# This program is distributed under the terms of the
+# GNU General Public License, version 2.
+#
+# $Id$
+
+function smarty_function_mtpagerlink($args, &$ctx) {
+    $page = $ctx->__stash['vars']['__value__'];
+    if ( !$page ) return '';
+
+    $limit = $ctx->stash('__pager_limit');
+    $offset = ( $page - 1 ) * $limit;
+
+    if ( strpos($link, '?') ) {
+        $link .= '&';
+    }
+    else {
+        $link .= '?';
+    }
+
+    $link .= "limit=$limit";
+    if ( $offset )
+        $link .= "&offset=$offset";
+    return $link;
+}
+?>
+
Index: /branches/release-36/php/lib/block.mtentries.php
===================================================================
--- /branches/release-36/php/lib/block.mtentries.php (revision 1926)
+++ /branches/release-36/php/lib/block.mtentries.php (revision 2103)
@@ -39,4 +39,26 @@
     }
 
+    if ( isset($args['offset']) && ($args['offset'] == 'auto') ) {
+        $l = 0;
+        if ( $args['limit'] ) {
+            if ( $args['limit'] == 'auto' ) {
+                if ( $_REQUEST['limit'] )
+                    $l = $_REQUEST['limit'];
+                else {
+                    $blog_id = intval($ctx->stash('blog_id'));
+                    $blog = $ctx->mt->db->fetch_blog($blog_id);
+                    $l = $blog['blog_entries_on_index'];
+                }
+            }
+            else
+                $l = $args['limit'];
+        }
+        if ( !$l )
+            $l = 20;
+        $ctx->stash('__pager_limit', $l);
+        if ( $_REQUEST['offset'] )
+            $ctx->stash('__pager_offset', $_REQUEST['offset']);
+    }
+
     $entries = $ctx->stash('entries');
     if (!isset($entries)) {
@@ -77,5 +99,9 @@
             $args['tag'] or $args['tags'] or $args['tags'] = is_array($tag) ? $tag['tag_name'] : $tag;
         }
-        $entries =& $ctx->mt->db->fetch_entries($args);
+        if ( isset($args['offset']) && ($args['offset'] == 'auto') )
+            $total_count = 0;
+        $entries =& $ctx->mt->db->fetch_entries($args, $total_count);
+        if ( isset($args['offset']) && ($args['offset'] == 'auto') )
+            $ctx->stash('__pager_total_count', $total_count);
         $ctx->stash('entries', $entries);
     }
Index: /branches/release-36/php/lib/block.mtifpreviousresults.php
===================================================================
--- /branches/release-36/php/lib/block.mtifpreviousresults.php (revision 2103)
+++ /branches/release-36/php/lib/block.mtifpreviousresults.php (revision 2103)
@@ -0,0 +1,16 @@
+<?php
+# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
+# This program is distributed under the terms of the
+# GNU General Public License, version 2.
+#
+# $Id$
+
+function smarty_block_mtifpreviousresults($args, $content, &$ctx, &$repeat) {
+    if (!isset($content)) {
+        $offset = $ctx->stash('__pager_offset');
+        return $ctx->_hdlr_if($args, $content, $ctx, $repeat, $offset ? true : false);
+    } else {
+        return $ctx->_hdlr_if($args, $content, $ctx, $repeat);
+    }
+}
+?>
Index: /branches/release-36/lib/MT/CMS/Blog.pm
===================================================================
--- /branches/release-36/lib/MT/CMS/Blog.pm (revision 2051)
+++ /branches/release-36/lib/MT/CMS/Blog.pm (revision 2103)
@@ -2211,5 +2211,6 @@
   RewriteCond %{REQUEST_FILENAME} !-f
   # anything else is handed to mtview.php for resolution
-  RewriteRule ^(.*)\$ $mtview_server_url [L,QSA]
+  # passthrough query parameters
+  RewriteRule ^(.*)(\\?.*)\?\$ $mtview_server_url\$2 [L,QSA]
 </IfModule>
 
Index: /branches/release-36/lib/MT/Template/ContextHandlers.pm
===================================================================
--- /branches/release-36/lib/MT/Template/ContextHandlers.pm (revision 2062)
+++ /branches/release-36/lib/MT/Template/ContextHandlers.pm (revision 2103)
@@ -561,8 +561,9 @@
                 my $limit = $_[0]->stash('limit');
                 my $offset = $_[0]->stash('offset');
-                $limit ? $offset / $limit + 1 : 1
+                $limit ? $offset / $limit + 1 : 1;
             },
             TotalPages => sub {
                 my $limit = $_[0]->stash('limit');
+                return 1 unless $limit;
                 my $count = $_[0]->stash('count');
                 require POSIX;
