Do final link replacement with preg_replace_callback instead of str_replace on giant...
authorBrion Vibber <brion@users.mediawiki.org>
Mon, 16 Aug 2004 23:57:25 +0000 (23:57 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Mon, 16 Aug 2004 23:57:25 +0000 (23:57 +0000)
Speeds up this part on a Village Pump copy by a factor of 20 over the
previous explode() hack and by a factor of 200 over the original method.

includes/OutputPage.php

index a958397..bd1a012 100644 (file)
@@ -875,46 +875,31 @@ class OutputPage {
                        
                        # Construct search and replace arrays
                        wfProfileIn( "$fname-construct" );
-                       $search = $replace = array();
+                       global $outputReplace;
+                       $outputReplace = array();
                        foreach ( $namespaces as $key => $ns ) {
                                $pdbk = $pdbks[$key];
-                               $search[] = $tmpLinks[0][$key];
+                               $searchkey = $tmpLinks[0][$key];
                                $title = $titles[$key];
                                if ( empty( $colours[$pdbk] ) ) {
                                        $wgLinkCache->addBadLink( $pdbk );
                                        $colours[$pdbk] = 0;
-                                       $replace[] = $sk->makeBrokenLinkObj( $title, $texts[$key], $queries[$key] );
+                                       $outputReplace[$searchkey] = $sk->makeBrokenLinkObj( $title, $texts[$key], $queries[$key] );
                                } elseif ( $colours[$pdbk] == 1 ) {
-                                       $replace[] = $sk->makeKnownLinkObj( $title, $texts[$key], $queries[$key] );
+                                       $outputReplace[$searchkey] = $sk->makeKnownLinkObj( $title, $texts[$key], $queries[$key] );
                                } elseif ( $colours[$pdbk] == 2 ) {
-                                       $replace[] = $sk->makeStubLinkObj( $title, $texts[$key], $queries[$key] );
+                                       $outputReplace[$searchkey] = $sk->makeStubLinkObj( $title, $texts[$key], $queries[$key] );
                                }
                        }
                        wfProfileOut( "$fname-construct" );
+
                        # Do the thing
                        wfProfileIn( "$fname-replace" );
                        
-                       # Dirrrty hack
-                       $arr = explode ( "<!--LINK" , $this->mBodytext ) ;
-                       $tarr = array() ;
-                       foreach ( $arr AS $k => $v )
-                               {
-                               $t = explode ( "-->" , $v , 2 ) ;
-                               $tarr[$k] = $t[0]  ;
-                               }
-                       while ( count ( $search ) > 0 )
-                               {
-                               $s = array_pop ( $search ) ;
-                               $r = array_pop ( $replace ) ;
-                               $s = substr ( $s , 8 , strlen ( $s ) - 11 ) ;
-                               $k = array_search ( $s , $tarr ) ;
-                               if ( $k === false ) continue ;
-                               $arr[$k] = substr_replace ( $arr[$k] , $r , 0 , strlen ( $s ) + 3 ) ;
-                               unset ( $tarr[$k] ) ;
-                               }
-                       $this->mBodytext = implode ( "" , $arr ) ;
-                       
-#                      $this->mBodytext = str_replace( $search, $replace, $this->mBodytext );
+                       $this->mBodytext = preg_replace_callback(
+                               '/(<!--LINK .*? .*? .*? .*?-->)/',
+                               "outputReplaceMatches",
+                               $this->mBodytext);
                        wfProfileOut( "$fname-replace" );
                }
                wfProfileOut( $fname );
@@ -922,5 +907,10 @@ class OutputPage {
        }
 }
 
+function &outputReplaceMatches($matches) {
+       global $outputReplace;
+       return $outputReplace[$matches[1]];
+}
+
 }
 ?>