Cleaned up OutputPage::replaceVariables() a bit. Some performance gain by doing sever...
authorMr. E23 <e23@users.mediawiki.org>
Sat, 10 Jan 2004 22:49:37 +0000 (22:49 +0000)
committerMr. E23 <e23@users.mediawiki.org>
Sat, 10 Jan 2004 22:49:37 +0000 (22:49 +0000)
includes/MagicWord.php
includes/OutputPage.php

index 4cc3c90..f0ed86d 100644 (file)
@@ -147,6 +147,24 @@ class MagicWord {
        function getWasModified(){
                return $this->mModified;
        }
+
+       # $magicarr is an associative array of (magic word ID => replacement)
+       # This method uses the php feature to do several replacements at the same time,
+       # thereby gaining some efficiency. The result is placed in the out variable
+       # $result. The return value is true if something was replaced.
+
+       /* static */ function replaceMultiple( $magicarr, $subject, &$result ){
+               $search = array();
+               $replace = array();
+               foreach( $magicarr as $id => $replacement ){
+                       $mw = MagicWord::get( $id );
+                       $search[] = $mw->getRegex();
+                       $replace[] = $replacement;
+               }
+
+               $result = preg_replace( $search, $replace, $subject );
+               return !($result === $subject);
+       }
 }
 
 # Used in matchAndRemove()
index 307148a..7006ab9 100644 (file)
@@ -1232,46 +1232,22 @@ $t[] = "</table>" ;
                $fname = "OutputPage::replaceVariables";
                wfProfileIn( $fname );
 
-               
+               $magic = array();
+
                # Basic variables
                # See Language.php for the definition of each magic word
-
                # As with sigs, this uses the server's local time -- ensure 
                # this is appropriate for your audience!
-               $v = date( "m" );
-               $mw =& MagicWord::get( MAG_CURRENTMONTH );
-               $text = $mw->replace( $v, $text );
-               if( $mw->getWasModified() ) { $this->mContainsOldMagic++; }
-
-               $v = $wgLang->getMonthName( date( "n" ) );
-               $mw =& MagicWord::get( MAG_CURRENTMONTHNAME );
-               $text = $mw->replace( $v, $text );
-               if( $mw->getWasModified() ) { $this->mContainsOldMagic++; }
-               
-               $v = $wgLang->getMonthNameGen( date( "n" ) );
-               $mw =& MagicWord::get( MAG_CURRENTMONTHNAMEGEN );
-               $text = $mw->replace( $v, $text );
-               if( $mw->getWasModified() ) { $this->mContainsOldMagic++; }
-               
-               $v = date( "j" );
-               $mw = MagicWord::get( MAG_CURRENTDAY );
-               $text = $mw->replace( $v, $text );
-               if( $mw->getWasModified() ) { $this->mContainsOldMagic++; }
-               
-               $v = $wgLang->getWeekdayName( date( "w" )+1 );
-               $mw =& MagicWord::get( MAG_CURRENTDAYNAME );
-               $text = $mw->replace( $v, $text );
-               if( $mw->getWasModified() ) { $this->mContainsOldMagic++; }
+
+               $magic[MAG_CURRENTMONTH] = date( "m" );
+               $magic[MAG_CURRENTMONTHNAME] = $wgLang->getMonthName( date("n") );
+               $magic[MAG_CURRENTMONTHNAMEGEN] = $wgLang->getMonthNameGen( date("n") );
+               $magic[MAG_CURRENTDAY] = date("j");
+               $magic[MAG_CURRENTDAYNAME] = $wgLang->getWeekdayName( date("w")+1 );
+               $magic[MAG_CURRENTYEAR] = date( "Y" );
+               $magic[MAG_CURRENTTIME] = $wgLang->time( wfTimestampNow(), false );
                
-               $v = date( "Y" );
-               $mw =& MagicWord::get( MAG_CURRENTYEAR );
-               $text = $mw->replace( $v, $text );
-               if( $mw->getWasModified() ) { $this->mContainsOldMagic++; }
-       
-               $v = $wgLang->time( wfTimestampNow(), false );
-               $mw =& MagicWord::get( MAG_CURRENTTIME );
-               $text = $mw->replace( $v, $text );
-               if( $mw->getWasModified() ) { $this->mContainsOldMagic++; }
+               $this->mContainsOldMagic += MagicWord::replaceMultiple($magic, $text, $text);
 
                $mw =& MagicWord::get( MAG_NUMBEROFARTICLES );
                if ( $mw->match( $text ) ) {