MediaWiki: namespace update bug fix
[lhc/web/wiklou.git] / includes / Article.php
index 5df7f16..50af8bf 100644 (file)
@@ -5,6 +5,13 @@
 # Note: edit user interface and cache support functions have been
 # moved to separate EditPage and CacheManager classes.
 
+/* CHECK MERGE @@@
+   TEST THIS @@@
+
+   * s/\$wgTitle/\$this->mTitle/ performed, many replacements
+   * mTitle variable added to class
+*/
+
 include_once( "CacheManager.php" );
 
 class Article {
@@ -12,7 +19,7 @@ class Article {
        /* private */ var $mUser, $mTimestamp, $mUserText;
        /* private */ var $mCounter, $mComment, $mCountAdjustment;
        /* private */ var $mMinorEdit, $mRedirectedFrom;
-       /* private */ var $mTouched, $mFileCache;
+       /* private */ var $mTouched, $mFileCache, $mTitle;
 
        function Article( &$title ) {
                $this->mTitle =& $title;
@@ -30,22 +37,23 @@ class Article {
        }
 
        # Note that getContent/loadContent may follow redirects if
-       # not told otherwise, and so may cause a change to wgTitle.
+       # not told otherwise, and so may cause a change to mTitle.
 
        function getContent( $noredir = false )
        {
                global $action,$section,$count; # From query string
-               wfProfileIn( "Article::getContent" );
+               $fname =  "Article::getContent"; 
+               wfProfileIn( $fname );
 
                if ( 0 == $this->getID() ) {
                        if ( "edit" == $action ) {
+                               wfProfileOut( $fname );
                                return ""; # was "newarticletext", now moved above the box)
                        }
-                       wfProfileOut();
+                       wfProfileOut( $fname );
                        return wfMsg( "noarticletext" );
                } else {
                        $this->loadContent( $noredir );
-                       wfProfileOut();
                                                
                        if(
                                # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
@@ -54,22 +62,29 @@ class Article {
                                  $action=="view"
                                ) 
                                {
+                               wfProfileOut( $fname );
                                return $this->mContent . "\n" .wfMsg("anontalkpagetext"); }
                        else {                          
                                if($action=="edit") {
                                        if($section!="") {
-                                               if($section=="new") { return ""; }
+                                               if($section=="new") { 
+                                                       wfProfileOut( $fname );
+                                                       return ""; 
+                                               }
 
                                                $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
                                                 $this->mContent, -1,
                                                 PREG_SPLIT_DELIM_CAPTURE);
                                                if($section==0) {
+                                                       wfProfileOut( $fname );
                                                        return trim($secs[0]);
                                                } else {
+                                                       wfProfileOut( $fname );
                                                        return trim($secs[$section*2-1] . $secs[$section*2]);
                                                }
                                        }
                                }
+                               wfProfileOut( $fname );
                                return $this->mContent;
                        }
                }
@@ -87,9 +102,15 @@ class Article {
                # fails we'll have something telling us what we intended.
 
                $t = $this->mTitle->getPrefixedText();
-               if ( $oldid ) { $t .= ",oldid={$oldid}"; }
-               if ( $redirect ) { $t .= ",redirect={$redirect}"; }
-               $this->mContent = str_replace( "$1", $t, wfMsg( "missingarticle" ) );
+               if ( isset( $oldid ) ) {
+                       $oldid = IntVal( $oldid );
+                       $t .= ",oldid={$oldid}";
+               }
+               if ( isset( $redirect ) ) {
+                       $redirect = ($redirect == "no") ? "no" : "yes";
+                       $t .= ",redirect={$redirect}";
+               }
+               $this->mContent = wfMsg( "missingarticle", $t );
 
                if ( ! $oldid ) {       # Retrieve current version
                        $id = $this->getID();
@@ -98,11 +119,13 @@ class Article {
                        $sql = "SELECT " .
                          "cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched " .
                          "FROM cur WHERE cur_id={$id}";
-                       $res = wfQuery( $sql, $fname );
-                       if ( 0 == wfNumRows( $res ) ) { return; }
+                       wfDebug( "$sql\n" );
+                       $res = wfQuery( $sql, DB_READ, $fname );
+                       if ( 0 == wfNumRows( $res ) ) { 
+                               return; 
+                       }
 
                        $s = wfFetchObject( $res );
-
                        # If we got a redirect, follow it (unless we've been told
                        # not to by either the function parameter or the query
                        if ( ( "no" != $redirect ) && ( false == $noredir ) &&
@@ -127,8 +150,8 @@ class Article {
                                        $rid = $rt->getArticleID();
                                        if ( 0 != $rid ) {
                                                $sql = "SELECT cur_text,cur_timestamp,cur_user," .
-                                                 "cur_counter,cur_touched FROM cur WHERE cur_id={$rid}";
-                                               $res = wfQuery( $sql, $fname );
+                                                 "cur_counter,cur_restrictions,cur_touched FROM cur WHERE cur_id={$rid}";
+                                               $res = wfQuery( $sql, DB_READ, $fname );
 
                                                if ( 0 != wfNumRows( $res ) ) {
                                                        $this->mRedirectedFrom = $this->mTitle->getPrefixedText();
@@ -138,6 +161,7 @@ class Article {
                                        }
                                }
                        }
+
                        $this->mContent = $s->cur_text;
                        $this->mUser = $s->cur_user;
                        $this->mCounter = $s->cur_counter;
@@ -149,7 +173,7 @@ class Article {
                } else { # oldid set, retrieve historical version
                        $sql = "SELECT old_text,old_timestamp,old_user FROM old " .
                          "WHERE old_id={$oldid}";
-                       $res = wfQuery( $sql, $fname );
+                       $res = wfQuery( $sql, DB_READ, $fname );
                        if ( 0 == wfNumRows( $res ) ) { return; }
 
                        $s = wfFetchObject( $res );
@@ -162,7 +186,13 @@ class Article {
                $this->mContentLoaded = true;
        }
 
-       function getID() { return $this->mTitle->getArticleID(); }
+       function getID() {
+               if( $this->mTitle ) {
+                       return $this->mTitle->getArticleID();
+               } else {
+                       return 0;
+               }
+       }
 
        function getCount()
        {
@@ -198,7 +228,7 @@ class Article {
                $sql = "SELECT cur_user,cur_user_text,cur_timestamp," .
                  "cur_comment,cur_minor_edit FROM cur WHERE " .
                  "cur_id=" . $this->getID();
-               $res = wfQuery( $sql, "Article::loadLastEdit" );
+               $res = wfQuery( $sql, DB_READ, "Article::loadLastEdit" );
 
                if ( wfNumRows( $res ) > 0 ) {
                        $s = wfFetchObject( $res );
@@ -247,8 +277,9 @@ class Article {
        {
                global $wgUser, $wgOut, $wgLang;
                global $oldid, $diff; # From query
-               global $wgLinkCache;
-               wfProfileIn( "Article::view" );
+               global $wgLinkCache, $IP;
+               $fname = "Article::view";
+               wfProfileIn( $fname );
 
                $wgOut->setArticleFlag( true );
                $wgOut->setRobotpolicy( "index,follow" );
@@ -260,10 +291,18 @@ class Article {
                        $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
                        $de = new DifferenceEngine( $oldid, $diff );
                        $de->showDiffPage();
-                       wfProfileOut();
+                       wfProfileOut( $fname );
                        return;
                }
-               $text = $this->getContent(); # May change wgTitle!
+
+               if ( !isset( $oldid ) ) {
+                       if( $this->checkTouched() ) {
+                               $wgOut->checkLastModified( $this->mTouched );
+                               $this->tryFileCache();
+                       }
+               }
+
+               $text = $this->getContent(); # May change mTitle
                $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
                $wgOut->setHTMLTitle( $this->mTitle->getPrefixedText() .
                  " - " . wfMsg( "wikititlesuffix" ) );
@@ -278,16 +317,14 @@ class Article {
                        $sk = $wgUser->getSkin();
                        $redir = $sk->makeKnownLink( $this->mRedirectedFrom, "",
                          "redirect=no" );
-                       $s = str_replace( "$1", $redir, wfMsg( "redirectedfrom" ) );
+                       $s = wfMsg( "redirectedfrom", $redir );
                        $wgOut->setSubtitle( $s );
                }
-               $wgOut->checkLastModified( $this->mTouched );
-               $this->tryFileCache();
                $wgLinkCache->preFill( $this->mTitle );
                $wgOut->addWikiText( $text );
 
                $this->viewUpdates();
-               wfProfileOut();
+               wfProfileOut( $fname );
        }
 
        # Theoretically we could defer these whole insert and update
@@ -298,8 +335,12 @@ class Article {
        /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
        {
                global $wgOut, $wgUser, $wgLinkCache, $wgMwRedir;
+               global $wgEnablePersistentLC;
+               
                $fname = "Article::insertNewArticle";
 
+               $this->mCountAdjustment = $this->isCountable( $text );
+
                $ns = $this->mTitle->getNamespace();
                $ttl = $this->mTitle->getDBkey();
                $text = $this->preSaveTransform( $text );
@@ -319,11 +360,18 @@ class Article {
                  $wgUser->getID() . "', '{$now}', " .
                  ( $isminor ? 1 : 0 ) . ", 0, '', '" .
                  wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
-               $res = wfQuery( $sql, $fname );
+               $res = wfQuery( $sql, DB_WRITE, $fname );
 
                $newid = wfInsertId();
                $this->mTitle->resetArticleID( $newid );
 
+               if ( $wgEnablePersistentLC ) {
+                       // Purge related entries in links cache on new page, to heal broken links
+                       $ptitle = wfStrencode( $ttl );
+                       wfQuery("DELETE linkscc FROM linkscc,brokenlinks ".
+                               "WHERE lcc_pageid=bl_from AND bl_to='{$ptitle}'", DB_WRITE);
+               }
+               
                $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
                  "rc_namespace,rc_title,rc_new,rc_minor,rc_cur_id,rc_user," .
                  "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid,rc_bot) VALUES (" .
@@ -332,7 +380,7 @@ class Article {
                  wfStrencode( $wgUser->getName() ) . "','" .
                  wfStrencode( $summary ) . "',0,0," .
                  ( $wgUser->isBot() ? 1 : 0 ) . ")";
-               wfQuery( $sql, $fname );
+               wfQuery( $sql, DB_WRITE, $fname );
                if ($watchthis) {               
                        if(!$this->mTitle->userIsWatching()) $this->watch(); 
                } else {
@@ -341,10 +389,15 @@ class Article {
                        }
                }
                
+               # The talk page isn't in the regular link tables, so we need to update manually:
+               $talkns = $ns ^ 1; # talk -> normal; normal -> talk
+               $sql = "UPDATE cur set cur_touched='$now' WHERE cur_namespace=$talkns AND cur_title='" . wfStrencode( $ttl ) . "'";
+               wfQuery( $sql, DB_WRITE );
+               
                $this->showArticle( $text, wfMsg( "newarticle" ) );
        }
 
-       function updateArticle( $text, $summary, $minor, $watchthis, $section="" )
+       function updateArticle( $text, $summary, $minor, $watchthis, $section = "")
        {
                global $wgOut, $wgUser, $wgLinkCache;
                global $wgDBtransactions, $wgMwRedir;
@@ -381,7 +434,7 @@ class Article {
 
                if( $wgDBtransactions ) {
                        $sql = "BEGIN";
-                       wfQuery( $sql );
+                       wfQuery( $sql, DB_WRITE );
                }
                $oldtext = $this->getContent( true );
 
@@ -399,7 +452,7 @@ class Article {
                          "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " .
                          "WHERE cur_id=" . $this->getID() .
                          " AND cur_timestamp='" . $this->getTimestamp() . "'";
-                       $res = wfQuery( $sql, $fname );
+                       $res = wfQuery( $sql, DB_WRITE, $fname );
                        
                        if( wfAffectedRows() == 0 ) {
                                /* Belated edit conflict! Run away!! */
@@ -417,7 +470,7 @@ class Article {
                          wfStrencode( $this->getUserText() ) . "', '" .
                          $this->getTimestamp() . "', " . $me1 . ", '" .
                          wfInvertTimestamp( $this->getTimestamp() ) . "')";
-                       $res = wfQuery( $sql, $fname );
+                       $res = wfQuery( $sql, DB_WRITE, $fname );
                        $oldid = wfInsertID( $res );
 
                        $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
@@ -429,21 +482,29 @@ class Article {
                          $this->getID() . "," . $wgUser->getID() . ",'" .
                          wfStrencode( $wgUser->getName() ) . "','" .
                          wfStrencode( $summary ) . "',0,{$oldid})";
-                       wfQuery( $sql, $fname );
+                       wfQuery( $sql, DB_WRITE, $fname );
 
                        $sql = "UPDATE recentchanges SET rc_this_oldid={$oldid} " .
                          "WHERE rc_namespace=" . $this->mTitle->getNamespace() . " AND " .
                          "rc_title='" . wfStrencode( $this->mTitle->getDBkey() ) . "' AND " .
                          "rc_timestamp='" . $this->getTimestamp() . "'";
-                       wfQuery( $sql, $fname );
+                       wfQuery( $sql, DB_WRITE, $fname );
 
                        $sql = "UPDATE recentchanges SET rc_cur_time='{$now}' " .
                          "WHERE rc_cur_id=" . $this->getID();
-                       wfQuery( $sql, $fname );
+                       wfQuery( $sql, DB_WRITE, $fname );
+
+                       global $wgEnablePersistentLC;
+                       if ( $wgEnablePersistentLC ) {
+                               // Purge link cache for this page
+                               $pageid=$this->getID();
+                               wfQuery("DELETE FROM linkscc WHERE lcc_pageid='{$pageid}'", DB_WRITE);
+                       }                       
                }
+
                if( $wgDBtransactions ) {
                        $sql = "COMMIT";
-                       wfQuery( $sql );
+                       wfQuery( $sql, DB_WRITE );
                }
                
                if ($watchthis) { 
@@ -538,7 +599,8 @@ class Article {
                # If page hasn't changed, client can cache this
                
                $wgOut->checkLastModified( $this->getTimestamp() );
-               wfProfileIn( "Article::history" );
+               $fname = "Article::history";
+               wfProfileIn( $fname );
 
                $wgOut->setPageTitle( $this->mTitle->getPRefixedText() );
                $wgOut->setSubtitle( wfMsg( "revhistory" ) );
@@ -547,7 +609,7 @@ class Article {
 
                if( $this->mTitle->getArticleID() == 0 ) {
                        $wgOut->addHTML( wfMsg( "nohistory" ) );
-                       wfProfileOut();
+                       wfProfileOut( $fname );
                        return;
                }
                
@@ -562,12 +624,12 @@ class Article {
                  "WHERE old_namespace={$namespace} AND " .
                  "old_title='" . wfStrencode( $this->mTitle->getDBkey() ) . "' " .
                  "ORDER BY inverse_timestamp LIMIT $offset, $limit";
-               $res = wfQuery( $sql, "Article::history" );
+               $res = wfQuery( $sql, DB_READ, "Article::history" );
 
                $revs = wfNumRows( $res );
                if( $this->mTitle->getArticleID() == 0 ) {
                        $wgOut->addHTML( wfMsg( "nohistory" ) );
-                       wfProfileOut();
+                       wfProfileOut( $fname );
                        return;
                }
                
@@ -595,7 +657,7 @@ class Article {
                $s .= $sk->endHistoryList();
                $s .= $numbar;
                $wgOut->addHTML( $s );
-               wfProfileOut();
+               wfProfileOut( $fname );
        }
 
        function protect( $limit = "sysop" )
@@ -617,8 +679,14 @@ class Article {
                }
         $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
                        "cur_restrictions='{$limit}' WHERE cur_id={$id}";
-               wfQuery( $sql, "Article::protect" );
+               wfQuery( $sql, DB_WRITE, "Article::protect" );
 
+               $log = new LogPage( wfMsg( "protectlogpage" ), wfMsg( "protectlogtext" ) );
+               if ( $limit === "" ) {
+                       $log->addEntry( wfMsg( "unprotectedarticle", $this->mTitle->getPrefixedText() ), "" );          
+               } else {
+                       $log->addEntry( wfMsg( "protectedarticle", $this->mTitle->getPrefixedText() ), "" );
+               }
                $wgOut->redirect( wfLocalUrl( $this->mTitle->getPrefixedURL() ) );
        }
 
@@ -632,10 +700,9 @@ class Article {
                global $wgUser, $wgOut;
                global $wpConfirm, $wpReason, $image, $oldimage;
 
-               # Anybody can delete old revisions of images; only sysops
-               # can delete articles and current images
-
-               if ( ( ! $oldimage ) && ( ! $wgUser->isSysop() ) ) {
+               # This code desperately needs to be totally rewritten
+               
+               if ( ( ! $wgUser->isSysop() ) ) {
                        $wgOut->sysopRequired();
                        return;
                }
@@ -652,19 +719,27 @@ class Article {
                        return;
                }
 
+               if ( $_POST["wpConfirm"] ) {
+                       $this->doDelete();
+                       return;
+               }
+
                # determine whether this page has earlier revisions
                # and insert a warning if it does
                # we select the text because it might be useful below
-               $sql="SELECT old_text FROM old WHERE old_namespace=0 and old_title='" . wfStrencode($this->mTitle->getPrefixedDBkey())."' ORDER BY inverse_timestamp LIMIT 1";
-               $res=wfQuery($sql,$fname);
+               $ns = $this->mTitle->getNamespace();
+               $title = $this->mTitle->getDBkey();
+               $etitle = wfStrencode( $title );
+               $sql = "SELECT old_text FROM old WHERE old_namespace=$ns and old_title='$etitle' ORDER BY inverse_timestamp LIMIT 1";
+               $res = wfQuery( $sql, DB_READ, $fname );
                if( ($old=wfFetchObject($res)) && !$wpConfirm ) {
                        $skin=$wgUser->getSkin();
                        $wgOut->addHTML("<B>".wfMsg("historywarning"));
                        $wgOut->addHTML( $skin->historyLink() ."</B><P>");
                }
 
-               $sql="SELECT cur_text FROM cur WHERE cur_namespace=0 and cur_title='" . wfStrencode($this->mTitle->getPrefixedDBkey())."'";
-               $res=wfQuery($sql,$fname);
+               $sql="SELECT cur_text FROM cur WHERE cur_namespace=$ns and cur_title='$etitle'";
+               $res=wfQuery($sql, DB_READ, $fname);
                if( ($s=wfFetchObject($res))) {
 
                        # if this is a mini-text, we can paste part of it into the deletion reason
@@ -713,6 +788,9 @@ class Article {
        function confirmDelete( $par = "" )
        {
                global $wgOut;
+               global $wpReason;
+
+               wfDebug( "Article::confirmDelete\n" );
                
                $sub = htmlspecialchars( $this->mTitle->getPrefixedText() );
                $wgOut->setSubtitle( wfMsg( "deletesub", $sub ) );
@@ -730,7 +808,7 @@ class Article {
 <form id=\"deleteconfirm\" method=\"post\" action=\"{$formaction}\">
 <table border=0><tr><td align=right>
 {$delcom}:</td><td align=left>
-<input type=text size=60 name=\"wpReason\" value=\"{$wpReason}\">
+<input type=text size=60 name=\"wpReason\" value=\"" . htmlspecialchars( $wpReason ) . "\">
 </td></tr><tr><td>&nbsp;</td></tr>
 <tr><td align=right>
 <input type=checkbox name=\"wpConfirm\" value='1' id=\"wpConfirm\">
@@ -747,6 +825,7 @@ class Article {
                global $wgOut, $wgUser, $wgLang;
                global $wpReason;
                $fname = "Article::doDelete";
+               wfDebug( "$fname\n" );
 
                $this->doDeleteArticle( $this->mTitle );
                $deleted = $this->mTitle->getPrefixedText();
@@ -759,8 +838,7 @@ class Article {
                  Namespace::getWikipedia() ) .
                  ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
 
-               $text = str_replace( "$1" , $deleted, wfMsg( "deletedtext" ) );
-               $text = str_replace( "$2", $loglink, $text );
+               $text = wfMsg( "deletedtext", $deleted, $loglink );
 
                $wgOut->addHTML( "<p>" . $text );
                $wgOut->returnToMain( false );
@@ -768,9 +846,12 @@ class Article {
 
        function doDeleteArticle( $title )
        {
-               global $wgUser, $wgOut, $wgLang, $wpReason, $wgDeferredUpdateList;
+               global $wgUser, $wgOut, $wgLang, $wpReason, $wgDeferredUpdateList, 
+                       $wgEnablePersistentLC;
 
                $fname = "Article::doDeleteArticle";
+               wfDebug( "$fname\n" );
+
                $ns = $title->getNamespace();
                $t = wfStrencode( $title->getDBkey() );
                $id = $title->getArticleID();
@@ -789,38 +870,47 @@ class Article {
                  "ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment," .
                  "cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur " .
                  "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
-               wfQuery( $sql, $fname );
+               wfQuery( $sql, DB_WRITE, $fname );
 
                $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
                  "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
                  "ar_flags) SELECT old_namespace,old_title,old_text,old_comment," .
                  "old_user,old_user_text,old_timestamp,old_minor_edit,old_flags " .
                  "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
-               wfQuery( $sql, $fname );
+               wfQuery( $sql, DB_WRITE, $fname );
 
                # Now that it's safely backed up, delete it
 
                $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
                  "cur_title='{$t}'";
-               wfQuery( $sql, $fname );
+               wfQuery( $sql, DB_WRITE, $fname );
 
                $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
                  "old_title='{$t}'";
-               wfQuery( $sql, $fname );
+               wfQuery( $sql, DB_WRITE, $fname );
                
                $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
                  "rc_title='{$t}'";
-        wfQuery( $sql, $fname );
+                       wfQuery( $sql, DB_WRITE, $fname );
 
                # Finally, clean up the link tables
 
                if ( 0 != $id ) {
+
                        $t = wfStrencode( $title->getPrefixedDBkey() );
+
+                       if ( $wgEnablePersistentLC ) {
+                               // Purge related entries in links cache on delete,
+                               wfQuery("DELETE linkscc FROM linkscc,links ".
+                                       "WHERE lcc_title=links.l_from AND l_to={$id}", DB_WRITE);
+                               wfQuery("DELETE FROM linkscc WHERE lcc_title='{$t}'", DB_WRITE);
+                       }
+
                        $sql = "SELECT l_from FROM links WHERE l_to={$id}";
-                       $res = wfQuery( $sql, $fname );
+                       $res = wfQuery( $sql, DB_READ, $fname );
 
                        $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
-            $now = wfTimestampNow();
+                       $now = wfTimestampNow();
                        $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
                        $first = true;
 
@@ -835,28 +925,28 @@ class Article {
                        }
                        $sql2 .= ")";
                        if ( ! $first ) {
-                               wfQuery( $sql, $fname );
-                               wfQuery( $sql2, $fname );
+                               wfQuery( $sql, DB_WRITE, $fname );
+                               wfQuery( $sql2, DB_WRITE, $fname );
                        }
                        wfFreeResult( $res );
 
                        $sql = "DELETE FROM links WHERE l_to={$id}";
-                       wfQuery( $sql, $fname );
+                       wfQuery( $sql, DB_WRITE, $fname );
 
                        $sql = "DELETE FROM links WHERE l_from='{$t}'";
-                       wfQuery( $sql, $fname );
+                       wfQuery( $sql, DB_WRITE, $fname );
 
                        $sql = "DELETE FROM imagelinks WHERE il_from='{$t}'";
-                       wfQuery( $sql, $fname );
+                       wfQuery( $sql, DB_WRITE, $fname );
 
                        $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
-                       wfQuery( $sql, $fname );
+                       wfQuery( $sql, DB_WRITE, $fname );
                }
                
                $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
                $art = $title->getPrefixedText();
                $wpReason = wfCleanQueryVar( $wpReason );
-               $log->addEntry( str_replace( "$1", $art, wfMsg( "deletedarticle" ) ), $wpReason );
+               $log->addEntry( wfMsg( "deletedarticle", $art ), $wpReason );
 
                # Clear the cached article id so the interface doesn't act like we exist
                $this->mTitle->resetArticleID( 0 );
@@ -878,7 +968,7 @@ class Article {
                
                # Get the last editor
                $sql = "SELECT cur_id,cur_user,cur_user_text,cur_comment FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
-               $res = wfQuery( $sql );
+               $res = wfQuery( $sql, DB_READ );
                if( ($x = wfNumRows( $res )) != 1 ) {
                        # Something wrong
                        $wgOut->addHTML( wfMsg( "notanarticle" ) );
@@ -910,7 +1000,7 @@ class Article {
                WHERE old_namespace={$n} AND old_title='{$tt}'
                AND (old_user <> {$uid} OR old_user_text <> '{$ut}')
                ORDER BY inverse_timestamp LIMIT 1";
-               $res = wfQuery( $sql );
+               $res = wfQuery( $sql, DB_READ );
                if( wfNumRows( $res ) != 1 ) {
                        # Something wrong
                        $wgOut->setPageTitle(wfMsg("rollbackfailed"));
@@ -920,12 +1010,17 @@ class Article {
                $s = wfFetchObject( $res );
        
                # Save it!
-               $newcomment = str_replace( "$1", $s->old_user_text, wfMsg( "revertpage" ) );
+               $newcomment = wfMsg( "revertpage", $s->old_user_text );
                $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
                $wgOut->setRobotpolicy( "noindex,nofollow" );
                $wgOut->addHTML( "<h2>" . $newcomment . "</h2>\n<hr>\n" );
                $this->updateArticle( $s->old_text, $newcomment, 1, $this->mTitle->userIsWatching() );
 
+               global $wgEnablePersistentLC;
+               if ( $wgEnablePersistentLC ) {
+                       wfQuery("DELETE FROM linkscc WHERE lcc_pageid='{$pid}'", DB_WRITE);
+               }
+       
                $wgOut->returnToMain( false );
        }
        
@@ -935,13 +1030,15 @@ class Article {
        /* private */ function viewUpdates()
        {
                global $wgDeferredUpdateList;
-
+               
                if ( 0 != $this->getID() ) {
-                       $u = new ViewCountUpdate( $this->getID() );
-                       array_push( $wgDeferredUpdateList, $u );
-                       $u = new SiteStatsUpdate( 1, 0, 0 );
-                       array_push( $wgDeferredUpdateList, $u );
-
+                       global $wgDisableCounters;
+                       if( !$wgDisableCounters ) {
+                               $u = new ViewCountUpdate( $this->getID() );
+                               array_push( $wgDeferredUpdateList, $u );
+                               $u = new SiteStatsUpdate( 1, 0, 0 );
+                               array_push( $wgDeferredUpdateList, $u );
+                       }
                        $u = new UserTalkUpdate( 0, $this->mTitle->getNamespace(),
                          $this->mTitle->getDBkey() );
                        array_push( $wgDeferredUpdateList, $u );
@@ -953,13 +1050,13 @@ class Article {
 
        /* private */ function editUpdates( $text )
        {
-               global $wgDeferredUpdateList;
+               global $wgDeferredUpdateList, $wgDBname, $wgMemc;
 
                wfSeedRandom();
                if ( 0 == mt_rand( 0, 999 ) ) {
                        $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
                        $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
-                       wfQuery( $sql );
+                       wfQuery( $sql, DB_WRITE );
                }
                $id = $this->getID();
                $title = $this->mTitle->getPrefixedDBkey();
@@ -976,6 +1073,15 @@ class Article {
                        $u = new UserTalkUpdate( 1, $this->mTitle->getNamespace(),
                          $this->mTitle->getDBkey() );
                        array_push( $wgDeferredUpdateList, $u );
+
+                       if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
+                               $messageCache = $wgMemc->get( "$wgDBname:messages" );
+                               if (!$messageCache) {
+                                       $messageCache = wfLoadAllMessages();
+                               }
+                               $messageCache[$title] = $text;
+                               $wgMemc->set( "$wgDBname:messages" );
+                       }
                }
        }
 
@@ -984,7 +1090,7 @@ class Article {
                global $wgLang, $wgOut;
 
                $td = $wgLang->timeanddate( $this->mTimestamp, true );
-               $r = str_replace( "$1", "{$td}", wfMsg( "revisionasof" ) );
+               $r = wfMsg( "revisionasof", $td );
                $wgOut->setSubtitle( "({$r})" );
        }
 
@@ -1034,12 +1140,13 @@ class Article {
                #
                $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
                $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
+               $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
                $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
 
                $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/";             # [[page (context)|]]
                $p2 = "/\[\[\\|({$tc}+)]]/";                                    # [[|page]]
-               $p3 = "/\[\[([A-Za-z _]+):({$np}+)\\|]]/";              # [[namespace:page|]]
-               $p4 = "/\[\[([A-Aa-z _]+):({$np}+) \\(({$np}+)\\)\\|]]/";
+               $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/";          # [[namespace:page|]]
+               $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
                                                                                                                # [[ns:page (cont)|]]
                $context = "";
                $t = $this->mTitle->getText();
@@ -1055,6 +1162,11 @@ class Article {
                } else {
                        $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
                }
+               
+               # {{SUBST:xxx}} variables
+               #
+               $mw =& MagicWord::get( MAG_SUBST );
+               $text = $mw->substituteCallback( $text, "wfReplaceSubstVar" );
 
                return $text;
        }
@@ -1062,6 +1174,12 @@ class Article {
        /* Caching functions */
        
        function tryFileCache() {
+               static $called = false;
+               if( $called ) {
+                       wfDebug( " tryFileCache() -- called twice!?\n" );
+                       return;
+               }
+               $called = true;
                if($this->isFileCacheable()) {
                        $touched = $this->mTouched;
                        if( strpos( $this->mContent, "{{" ) !== false ) {
@@ -1071,8 +1189,10 @@ class Article {
                        }
                        $cache = new CacheManager( $this->mTitle );
                        if($cache->isFileCacheGood( $touched )) {
+                               global $wgOut;
                                wfDebug( " tryFileCache() - about to load\n" );
                                $cache->loadFromFileCache();
+                               $wgOut->reportTime(); # For profiling
                                exit;
                        } else {
                                wfDebug( " tryFileCache() - starting buffer\n" );                       
@@ -1105,10 +1225,23 @@ class Article {
                        and (!isset($redirect))
                        and (!isset($printable))
                        and (!$this->mRedirectedFrom);
-                       
        }
        
+       function checkTouched() {
+               $id = $this->getID();
+               $sql = "SELECT cur_touched,cur_is_redirect FROM cur WHERE cur_id=$id";
+               $res = wfQuery( $sql, DB_READ, "Article::checkTouched" );
+               if( $s = wfFetchObject( $res ) ) {
+                       $this->mTouched = $s->cur_touched;
+                       return !$s->cur_is_redirect;
+               } else {
+                       return false;
+               }
+       }
+}
 
+function wfReplaceSubstVar( $matches ) {
+       return wfMsg( $matches[1] );
 }
 
 ?>