Index: /branches/release-38/php/lib/MTViewer.php
===================================================================
--- /branches/release-38/php/lib/MTViewer.php (revision 2160)
+++ /branches/release-38/php/lib/MTViewer.php (revision 2333)
@@ -70,4 +70,5 @@
         'wrap_text' => 1,
         'setvar' => 1,
+        'numify' => 1,
          # native smarty modifiers
         'regex_replace' => 1,
Index: /branches/release-38/php/lib/modifier.numify.php
===================================================================
--- /branches/release-38/php/lib/modifier.numify.php (revision 2333)
+++ /branches/release-38/php/lib/modifier.numify.php (revision 2333)
@@ -0,0 +1,11 @@
+<?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_modifier_numify($text, $attr = ',') {
+    if ($attr == "1") $attr = ',';
+    return preg_replace('/(^[â+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/', '\\1' . $attr, $text);
+}
Index: /branches/release-38/lib/MT/Template/ContextHandlers.pm
===================================================================
--- /branches/release-38/lib/MT/Template/ContextHandlers.pm (revision 2329)
+++ /branches/release-38/lib/MT/Template/ContextHandlers.pm (revision 2333)
@@ -561,4 +561,5 @@
         },
         modifier => {
+            'numify' => \&_fltr_numify,
             'mteval' => \&_fltr_mteval,
             'filters' => \&_fltr_filters,
@@ -608,4 +609,21 @@
 ###########################################################################
 
+=head2 numify
+
+Adds commas to a number. Converting "12345" into "12,345" for instance.
+The argument for the numify attribute is the separator character to use
+(ie, "," or "."); "," is the default.
+
+=cut
+
+sub _fltr_numify {
+    my ($str, $arg, $ctx) = @_;
+    $arg = ',' if (!defined $arg) || ($arg eq '1');
+    $str =~ s/(^[â+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1$arg/g;
+    return $str;
+}
+
+###########################################################################
+
 =head2 mteval
 
@@ -1109,13 +1127,9 @@
         my $re = eval { qr/$patt/ };
         if (defined $re) {
-            $replace =~ s!\\\\(\d+)!\$$1!g; # for php, \\1 is how you write $1
-            if ($global) {
-                $str =~ s/$re/$replace/g;
-                my @matches = ($&, $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20);
-                $str =~ s/\$(\d+)/$matches[$1]/g;
-            } else {
-                $str =~ s/$re/$replace/;
-                my @matches = ($&, $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20);
-                $str =~ s/\$(\d+)/$matches[$1]/g;
+            $replace =~ s!\\\\(\d+)!\$1!g; # for php, \\1 is how you write $1
+            $replace =~ s!/!\\/!g;
+            eval '$str =~ s/$re/' . $replace . '/' . ($global ? 'g' : '');
+            if ($@) {
+                return $ctx->error("Invalid regular expression: $@");
             }
         }
@@ -11998,4 +12012,15 @@
         $class_type eq 'category' ? 'entry' : 'page');
 
+    # issue a single count_group_by for all categories
+    my $cnt_iter = MT::Placement->count_group_by({
+        %terms
+    }, { group => [ 'category_id' ],
+        join => $entry_class->join_on('id', { status => MT::Entry::RELEASE() }),
+    });
+    my %counts;
+    while (my ($count, $cat_id) = $cnt_iter->()) {
+        $counts{$cat_id} = $count;
+    }
+
     my $iter = $class->load_iter(\%terms, \%args);
     my $res = '';
@@ -12030,12 +12055,5 @@
         local $vars->{__even__} = ($i % 2) == 0;
         local $vars->{__counter__} = $i;
-        my @args = (
-            { blog_id => $cat->blog_id,
-              status => MT::Entry::RELEASE() },
-            { 'join' => [ 'MT::Placement', 'entry_id',
-                          { category_id => $cat->id } ],
-              'sort' => 'authored_on',
-              direction => 'descend', });
-        $ctx->{__stash}{category_count} = $entry_class->count(@args);
+        $ctx->{__stash}{category_count} = $counts{$cat->id};
         $cat = $next_cat,next unless $ctx->{__stash}{category_count} || $args->{show_empty};
         defined(my $out = $builder->build($ctx, $tokens,
