hide print-specific output from cologneblue and nostalgia, too
[lhc/web/wiklou.git] / includes / Article.php
index b588066..a57b040 100644 (file)
@@ -1,17 +1,10 @@
-<?
+<?php
 # Class representing a Wikipedia article and history.
 # See design.doc for an overview.
 
 # 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 {
@@ -128,8 +121,21 @@ class Article {
 
                if ( $this->mContentLoaded ) return;
                $fname = "Article::loadContent";
-               $success = true;
                
+               # Pre-fill content with error message so that if something       
+               # fails we'll have something telling us what we intended.        
+
+               $t = $this->mTitle->getPrefixedText();   
+               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();
                        if ( 0 == $id ) return;
@@ -161,8 +167,7 @@ class Article {
                                                        return;
                                                }
                                                if ( $rt->getNamespace() == Namespace::getSpecial() ) {
-                                                       $wgOut->redirect( wfLocalUrl(
-                                                         $rt->getPrefixedURL() ) );
+                                                       $wgOut->redirect( $rt->getFullURL() );
                                                        return;
                                                }
                                                $rid = $rt->getArticleID();
@@ -202,23 +207,85 @@ class Article {
                        $this->mTimestamp = $s->old_timestamp;
                        wfFreeResult( $res );
                }
+               $this->mContentLoaded = true;
+               return $this->mContent;
+       }
+
+       # Gets the article text without using so many damn globals
+       # Returns false on error
+       function getContentWithoutUsingSoManyDamnGlobals( $oldid = 0, $noredir = false ) {
+               global $wgMwRedir;
 
-               # Return error message :P
-               # Horrible, confusing UI and data. I think this should return false on error -- TS
-               if ( !$success ) {
-                       $t = $this->mTitle->getPrefixedText();
-                       if ( isset( $oldid ) ) {
-                               $oldid = IntVal( $oldid );
-                               $t .= ",oldid={$oldid}";
+               if ( $this->mContentLoaded ) {
+                       return $this->mContent;
+               }
+               $this->mContent = false;
+               
+               $fname = "Article::loadContent";
+               
+               if ( ! $oldid ) {       # Retrieve current version
+                       $id = $this->getID();
+                       if ( 0 == $id ) {
+                               return false;
                        }
-                       if ( isset( $redirect ) ) {
-                               $redirect = ($redirect == "no") ? "no" : "yes";
-                               $t .= ",redirect={$redirect}";
+
+                       $sql = "SELECT " .
+                         "cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched " .
+                         "FROM cur WHERE cur_id={$id}";
+                       $res = wfQuery( $sql, DB_READ, $fname );
+                       if ( 0 == wfNumRows( $res ) ) { 
+                               return false; 
+                       }
+
+                       $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 ( !$noredir && $wgMwRedir->matchStart( $s->cur_text ) ) {
+                               if ( preg_match( "/\\[\\[([^\\]\\|]+)[\\]\\|]/",
+                                 $s->cur_text, $m ) ) {
+                                       $rt = Title::newFromText( $m[1] );
+                                       if( $rt &&  $rt->getInterwiki() == "" && $rt->getNamespace() != Namespace::getSpecial() ) {
+                                               $rid = $rt->getArticleID();
+                                               if ( 0 != $rid ) {
+                                                       $sql = "SELECT cur_text,cur_timestamp,cur_user," .
+                                                         "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();
+                                                               $this->mTitle = $rt;
+                                                               $s = wfFetchObject( $res );
+                                                       }
+                                               }
+                                       }
+                               }
                        }
-                       $this->mContent = wfMsg( "missingarticle", $t );
-               }
 
+                       $this->mContent = $s->cur_text;
+                       $this->mUser = $s->cur_user;
+                       $this->mCounter = $s->cur_counter;
+                       $this->mTimestamp = $s->cur_timestamp;
+                       $this->mTouched = $s->cur_touched;
+                       $this->mTitle->mRestrictions = explode( ",", trim( $s->cur_restrictions ) );
+                       $this->mTitle->mRestrictionsLoaded = true;
+                       wfFreeResult( $res );
+               } else { # oldid set, retrieve historical version
+                       $sql = "SELECT old_text,old_timestamp,old_user,old_flags FROM old " .
+                         "WHERE old_id={$oldid}";
+                       $res = wfQuery( $sql, DB_READ, $fname );
+                       if ( 0 == wfNumRows( $res ) ) { 
+                               return false; 
+                       }
+
+                       $s = wfFetchObject( $res );
+                       $this->mContent = Article::getRevisionText( $s );
+                       $this->mUser = $s->old_user;
+                       $this->mCounter = 0;
+                       $this->mTimestamp = $s->old_timestamp;
+                       wfFreeResult( $res );
+               }
                $this->mContentLoaded = true;
+               return $this->mContent;
        }
 
        function getID() {
@@ -312,7 +379,8 @@ class Article {
        {
                global $wgUser, $wgOut, $wgLang;
                global $oldid, $diff; # From query
-               global $wgLinkCache, $IP;
+               global $wgLinkCache, $IP, $wgEnableParserCache;
+               
                $fname = "Article::view";
                wfProfileIn( $fname );
 
@@ -361,7 +429,15 @@ class Article {
                }
 
                $wgLinkCache->preFill( $this->mTitle );
-               $wgOut->addWikiText( $text );
+               
+               if( $wgEnableParserCache && intval($wgUser->getOption( "stubthreshold" )) == 0 ){
+                       $wgOut->addWikiText( $text, true, $this );
+               } else {
+                       $wgOut->addWikiText( $text );
+               }
+
+               # Add link titles as META keywords
+               $wgOut->addMetaTags() ;
 
                $this->viewUpdates();
                wfProfileOut( $fname );
@@ -375,6 +451,7 @@ class Article {
        /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
        {
                global $wgOut, $wgUser, $wgLinkCache, $wgMwRedir;
+               global $wgUseSquid, $wgDeferredUpdateList, $wgInternalServer;
                
                $fname = "Article::insertNewArticle";
 
@@ -405,7 +482,7 @@ class Article {
                $newid = wfInsertId();
                $this->mTitle->resetArticleID( $newid );
 
-               Article::onArticleCreate( $this->mTitle, $text );
+               Article::onArticleCreate( $this->mTitle );
                RecentChange::notifyNew( $now, $this->mTitle, $isminor, $wgUser, $summary );
                
                if ($watchthis) {               
@@ -421,21 +498,18 @@ class Article {
                $sql = "UPDATE cur set cur_touched='$now' WHERE cur_namespace=$talkns AND cur_title='" . wfStrencode( $ttl ) . "'";
                wfQuery( $sql, DB_WRITE );
                
+               # standard deferred updates
+               $this->editUpdates( $text );
+
                $this->showArticle( $text, wfMsg( "newarticle" ) );
        }
 
-       function updateArticle( $text, $summary, $minor, $watchthis, $section = "", $forceBot = false )
-       {
-               global $wgOut, $wgUser, $wgLinkCache;
-               global $wgDBtransactions, $wgMwRedir;
-               $fname = "Article::updateArticle";
 
+       /* Side effects: loads last edit */
+       function getTextOfLastEditWithSectionReplacedOrAdded($section, $text, $summary = ""){
                $this->loadLastEdit();
-
-               // insert updated section into old text if we have only edited part 
-               // of the article               
-               if ($section != "") {                   
-                       $oldtext=$this->getContent();
+               $oldtext = $this->getContent();
+               if ($section != "") {
                        if($section=="new") {
                                if($summary) $subject="== {$summary} ==\n\n";
                                $text=$oldtext."\n\n".$subject.$text;
@@ -447,6 +521,16 @@ class Article {
                                $text=join("",$secs);           
                        }
                }
+               return $text;
+       }
+
+       function updateArticle( $text, $summary, $minor, $watchthis, $forceBot = false )
+       {
+               global $wgOut, $wgUser, $wgLinkCache;
+               global $wgDBtransactions, $wgMwRedir;
+               global $wgUseSquid, $wgInternalServer;
+               $fname = "Article::updateArticle";
+
                if ( $this->mMinorEdit ) { $me1 = 1; } else { $me1 = 0; }
                if ( $minor && $wgUser->getID() ) { $me2 = 1; } else { $me2 = 0; }              
                if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ")[^\\n]+)/i", $text, $m ) ) {
@@ -521,6 +605,29 @@ class Article {
                                $this->unwatch();
                        }
                }
+               # standard deferred updates
+               $this->editUpdates( $text );
+               
+               
+               $urls = array();
+               # Template namespace
+               # Purge all articles linking here
+               if ( $this->mTitle->getNamespace() == NS_TEMPLATE) {
+                       $titles = $this->mTitle->getLinksTo();
+                       Title::touchArray( $titles );
+                       if ( $wgUseSquid ) {
+                               foreach ( $titles as $title ) {
+                                       $urls[] = $title->getInternalURL();
+                               }
+                       }
+               }
+       
+               # Squid updates
+               if ( $wgUseSquid ) {
+                       $urls = array_merge( $urls, $this->mTitle->getSquidURLs() );
+                       $u = new SquidUpdate( $urls );
+                       $u->doUpdate();
+               }
 
                $this->showArticle( $text, wfMsg( "updated" ) );
                return true;
@@ -544,19 +651,11 @@ class Article {
                $wgOut = new OutputPage();
                $wgOut->addWikiText( $text );
 
-               # Every 1000th edit, prune the recent changes table.
-               wfSeedRandom();
-               if ( 0 == mt_rand( 0, 999 ) ) {
-                       $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
-                       $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
-                       wfQuery( $sql, DB_WRITE );
-               }
-
                if( $wgMwRedir->matchStart( $text ) )
                        $r = "redirect=no";
                else
                        $r = "";
-               $wgOut->redirect( wfLocalUrl( $this->mTitle->getPrefixedURL(), $r ) );
+               $wgOut->redirect( $this->mTitle->getFullURL( $r ) );
        }
 
        # Add this page to my watchlist
@@ -629,7 +728,7 @@ class Article {
                } else {
                        $log->addEntry( wfMsg( "protectedarticle", $this->mTitle->getPrefixedText() ), "" );
                }
-               $wgOut->redirect( wfLocalUrl( $this->mTitle->getPrefixedURL() ) );
+               $wgOut->redirect( $this->mTitle->getFullURL() );
        }
 
        function unprotect()
@@ -641,6 +740,7 @@ class Article {
        {
                global $wgUser, $wgOut, $wgMessageCache;
                global $wpConfirm, $wpReason, $image, $oldimage;
+               $fname = "Article::delete";
 
                # This code desperately needs to be totally rewritten
                
@@ -653,12 +753,6 @@ class Article {
                        return;
                }
 
-               # Can't delete cached MediaWiki namespace (i.e. vital messages)
-               if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI && $wgMessageCache->isCacheable( $this->mTitle->getDBkey() ) ) {
-                       $wgOut->fatalError( wfMsg( "cannotdelete" ) );
-                       return;
-               }
-
                # Better double-check that it hasn't been deleted yet!
                $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
                if ( ( "" == trim( $this->mTitle->getText() ) )
@@ -667,7 +761,7 @@ class Article {
                        return;
                }
 
-               if ( $_POST["wpConfirm"] ) {
+               if ( @$_POST["wpConfirm"] ) {
                        $this->doDelete();
                        return;
                }
@@ -693,12 +787,13 @@ class Article {
                        # if this is a mini-text, we can paste part of it into the deletion reason
 
                        #if this is empty, an earlier revision may contain "useful" text
+                       $blanked = false;
                        if($s->cur_text!="") {
                                $text=$s->cur_text;
                        } else {
                                if($old) {
                                        $text = Article::getRevisionText( $old );
-                                       $blanked=1;
+                                       $blanked = true;
                                }
                                
                        }
@@ -745,9 +840,8 @@ class Article {
                $wgOut->setRobotpolicy( "noindex,nofollow" );
                $wgOut->addWikiText( wfMsg( "confirmdeletetext" ) );
 
-               $t = $this->mTitle->getPrefixedURL();
-
-               $formaction = wfEscapeHTML( wfLocalUrl( $t, "action=delete" . $par ) );
+               $formaction = $this->mTitle->escapeLocalURL( "action=delete" . $par );
+               
                $confirm = wfMsg( "confirm" );
                $check = wfMsg( "confirmcheck" );
                $delcom = wfMsg( "deletecomment" );
@@ -775,41 +869,65 @@ class Article {
                $fname = "Article::doDelete";
                wfDebug( "$fname\n" );
 
-               $this->doDeleteArticle( $this->mTitle );
-               $deleted = $this->mTitle->getPrefixedText();
+               if ( $this->doDeleteArticle() ) {       
+                       $deleted = $this->mTitle->getPrefixedText();
 
-               $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
-               $wgOut->setRobotpolicy( "noindex,nofollow" );
+                       $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
+                       $wgOut->setRobotpolicy( "noindex,nofollow" );
 
-               $sk = $wgUser->getSkin();
-               $loglink = $sk->makeKnownLink( $wgLang->getNsText(
-                 Namespace::getWikipedia() ) .
-                 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
+                       $sk = $wgUser->getSkin();
+                       $loglink = $sk->makeKnownLink( $wgLang->getNsText(
+                         Namespace::getWikipedia() ) .
+                         ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
 
-               $text = wfMsg( "deletedtext", $deleted, $loglink );
+                       $text = wfMsg( "deletedtext", $deleted, $loglink );
 
-               $wgOut->addHTML( "<p>" . $text );
-               $wgOut->returnToMain( false );
+                       $wgOut->addHTML( "<p>" . $text );
+                       $wgOut->returnToMain( false );
+               } else {
+                       $wgOut->fatalError( wfMsg( "cannotdelete" ) );
+               }
        }
 
-       function doDeleteArticle( $title )
+       # Delete the article, returns success
+       function doDeleteArticle()
        {
-               global $wgUser, $wgOut, $wgLang, $wpReason, $wgDeferredUpdateList;
+               global $wgUser, $wgLang, $wgRequest;
+               global  $wgUseSquid, $wgDeferredUpdateList, $wgInternalServer;
 
                $fname = "Article::doDeleteArticle";
                wfDebug( "$fname\n" );
 
-               $ns = $title->getNamespace();
-               $t = wfStrencode( $title->getDBkey() );
-               $id = $title->getArticleID();
+               $ns = $this->mTitle->getNamespace();
+               $t = wfStrencode( $this->mTitle->getDBkey() );
+               $id = $this->mTitle->getArticleID();
 
-               if ( "" == $t ) {
-                       $wgOut->fatalError( wfMsg( "cannotdelete" ) );
-                       return;
+               if ( "" == $t || $id == 0 ) {
+                       return false;
                }
 
                $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
                array_push( $wgDeferredUpdateList, $u );
+               
+               $linksTo = $this->mTitle->getLinksTo();
+
+               # Squid purging
+               if ( $wgUseSquid ) {
+                       $urls = array(  
+                               $this->mTitle->getInternalURL(),
+                               $this->mTitle->getInternalURL( "history" ) 
+                       );
+                       foreach ( $linksTo as $linkTo ) {
+                               $urls[] = $linkTo->getInternalURL();
+                       }
+
+                       $u = new SquidUpdate( $urls );
+                       array_push( $wgDeferredUpdateList, $u );
+
+               }
+
+               # Client and file cache invalidation
+               Title::touchArray( $linksTo );
 
                # Move article and history to the "archive" table
                $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
@@ -841,63 +959,50 @@ class Article {
                        wfQuery( $sql, DB_WRITE, $fname );
 
                # Finally, clean up the link tables
+               $t = wfStrencode( $this->mTitle->getPrefixedDBkey() );
 
-               if ( 0 != $id ) {
-
-                       $t = wfStrencode( $title->getPrefixedDBkey() );
-
-                       Article::onArticleDelete( $title );
-
-                       $sql = "SELECT l_from FROM links WHERE l_to={$id}";
-                       $res = wfQuery( $sql, DB_READ, $fname );
-
-                       $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
-                       $now = wfTimestampNow();
-                       $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
-                       $first = true;
-
-                       while ( $s = wfFetchObject( $res ) ) {
-                               $nt = Title::newFromDBkey( $s->l_from );
-                               $lid = $nt->getArticleID();
-
-                               if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
-                               $first = false;
-                               $sql .= "({$lid},'{$t}')";
-                               $sql2 .= "{$lid}";
-                       }
-                       $sql2 .= ")";
-                       if ( ! $first ) {
-                               wfQuery( $sql, DB_WRITE, $fname );
-                               wfQuery( $sql2, DB_WRITE, $fname );
-                       }
-                       wfFreeResult( $res );
+               Article::onArticleDelete( $this->mTitle );
+               
+               $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
+               $first = true;
 
-                       $sql = "DELETE FROM links WHERE l_to={$id}";
+               foreach ( $linksTo as $titleObj ) {
+                       if ( ! $first ) { $sql .= ","; }
+                       $first = false;
+                       # Get article ID. Efficient because it was loaded into the cache by getLinksTo().
+                       $linkID = $titleObj->getArticleID(); 
+                       $sql .= "({$linkID},'{$t}')";
+               }
+               if ( ! $first ) {
                        wfQuery( $sql, DB_WRITE, $fname );
+               }
 
-                       $sql = "DELETE FROM links WHERE l_from='{$t}'";
-                       wfQuery( $sql, DB_WRITE, $fname );
+               $sql = "DELETE FROM links WHERE l_to={$id}";
+               wfQuery( $sql, DB_WRITE, $fname );
 
-                       $sql = "DELETE FROM imagelinks WHERE il_from='{$t}'";
-                       wfQuery( $sql, DB_WRITE, $fname );
+               $sql = "DELETE FROM links WHERE l_from={$id}";
+               wfQuery( $sql, DB_WRITE, $fname );
 
-                       $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
-                       wfQuery( $sql, DB_WRITE, $fname );
-               }
+               $sql = "DELETE FROM imagelinks WHERE il_from={$id}";
+               wfQuery( $sql, DB_WRITE, $fname );
+
+               $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
+               wfQuery( $sql, DB_WRITE, $fname );
                
                $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
-               $art = $title->getPrefixedText();
-               $wpReason = wfCleanQueryVar( $wpReason );
+               $art = $this->mTitle->getPrefixedText();
+               $wpReason = $wgRequest->getText( "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 );
                $this->mTitle->mArticleID = 0;
+               return true;
        }
 
        function rollback()
        {
-               global $wgUser, $wgLang, $wgOut, $from;
+               global $wgUser, $wgLang, $wgOut, $wgRequest;
 
                if ( ! $wgUser->isSysop() ) {
                        $wgOut->sysopRequired();
@@ -909,7 +1014,7 @@ class Article {
                }
                
                # Enhanced rollback, marks edits rc_bot=1
-               $bot = !!$_REQUEST['bot'];
+               $bot = $wgRequest->getBool( 'bot' );
                
                # Replace all this user's current edits with the next one down
                $tt = wfStrencode( $this->mTitle->getDBKey() );
@@ -928,7 +1033,7 @@ class Article {
                $uid = $s->cur_user;
                $pid = $s->cur_id;
                
-               $from = str_replace( '_', ' ', wfCleanQueryVar( $from ) );
+               $from = str_replace( '_', ' ', $wgRequest->getVal( "from" ) );
                if( $from != $s->cur_user_text ) {
                        $wgOut->setPageTitle(wfmsg("rollbackfailed"));
                        $wgOut->addWikiText( wfMsg( "alreadyrolled",
@@ -970,8 +1075,7 @@ class Article {
                $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
                $wgOut->setRobotpolicy( "noindex,nofollow" );
                $wgOut->addHTML( "<h2>" . $newcomment . "</h2>\n<hr>\n" );
-               $this->updateArticle( Article::getRevisionText( $s ), $newcomment, 1, $this->mTitle->userIsWatching(), "", $bot );
-               
+               $this->updateArticle( Article::getRevisionText( $s ), $newcomment, 1, $this->mTitle->userIsWatching(), $bot );
                Article::onArticleEdit( $this->mTitle );
                $wgOut->returnToMain( false );
        }
@@ -995,7 +1099,42 @@ class Article {
                }
        }
 
+       # Do standard deferred updates after page edit.
+       # Every 1000th edit, prune the recent changes table.
+
+       /* private */ function editUpdates( $text )
+       {
+               global $wgDeferredUpdateList, $wgDBname, $wgMemc;
+               global $wgMessageCache;
+
+               wfSeedRandom();
+               if ( 0 == mt_rand( 0, 999 ) ) {
+                       $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
+                       $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
+                       wfQuery( $sql, DB_WRITE );
+               }
+               $id = $this->getID();
+               $title = $this->mTitle->getPrefixedDBkey();
+               $shortTitle = $this->mTitle->getDBkey();
+               
+               $adj = $this->mCountAdjustment;
 
+               if ( 0 != $id ) {
+                       $u = new LinksUpdate( $id, $title );
+                       array_push( $wgDeferredUpdateList, $u );
+                       $u = new SiteStatsUpdate( 0, 1, $adj );
+                       array_push( $wgDeferredUpdateList, $u );
+                       $u = new SearchUpdate( $id, $title, $text );
+                       array_push( $wgDeferredUpdateList, $u );
+
+                       $u = new UserTalkUpdate( 1, $this->mTitle->getNamespace(), $shortTitle );
+                       array_push( $wgDeferredUpdateList, $u );
+
+                       if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
+                               $wgMessageCache->replace( $shortTitle, $text );
+                       }
+               }
+       }
 
        /* private */ function setOldSubtitle()
        {
@@ -1011,89 +1150,13 @@ class Article {
 
        function preSaveTransform( $text )
        {
-               $s = "";
-               while ( "" != $text ) {
-                       $p = preg_split( "/<\\s*nowiki\\s*>/i", $text, 2 );
-                       $s .= $this->pstPass2( $p[0] );
-
-                       if ( ( count( $p ) < 2 ) || ( "" == $p[1] ) ) { $text = ""; }
-                       else {
-                               $q = preg_split( "/<\\/\\s*nowiki\\s*>/i", $p[1], 2 );
-                               $s .= "<nowiki>{$q[0]}</nowiki>";
-                               $text = $q[1];
-                       }
-               }
-               return rtrim( $s );
+               global $wgParser, $wgUser;
+               return $wgParser->preSaveTransform( $text, $this->mTitle, $wgUser, ParserOptions::newFromUser( $wgUser ) );
        }
-
-       /* private */ function pstPass2( $text )
-       {
-               global $wgUser, $wgLang, $wgLocaltimezone;
-
-               # Signatures
-               #
-               $n = $wgUser->getName();
-               $k = $wgUser->getOption( "nickname" );
-               if ( "" == $k ) { $k = $n; }
-               if(isset($wgLocaltimezone)) {
-                       $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
-               }
-               /* Note: this is an ugly timezone hack for the European wikis */
-               $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
-                 " (" . date( "T" ) . ")";
-               if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
-
-               $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
-                 Namespace::getUser() ) . ":$n|$k]] $d", $text );
-               $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
-                 Namespace::getUser() ) . ":$n|$k]]", $text );
-
-               # Context links: [[|name]] and [[name (context)|]]
-               #
-               $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 = "/\[\[($namespacechar+):({$np}+)\\|]]/";          # [[namespace:page|]]
-               $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
-                                                                                                               # [[ns:page (cont)|]]
-               $context = "";
-               $t = $this->mTitle->getText();
-               if ( preg_match( $conpat, $t, $m ) ) {
-                       $context = $m[2];
-               }
-               $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
-               $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
-               $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
-
-               if ( "" == $context ) {
-                       $text = preg_replace( $p2, "[[\\1]]", $text );
-               } else {
-                       $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
-               }
-               
-               # {{SUBST:xxx}} variables
-               #
-               $mw =& MagicWord::get( MAG_SUBST );
-               $text = $mw->substituteCallback( $text, "wfReplaceSubstVar" );
-
-/* Experimental:
-               # Trim trailing whitespace
-               # MAG_END (__END__) tag allows for trailing 
-               # whitespace to be deliberately included
-               $text = rtrim( $text );
-               $mw =& MagicWord::get( MAG_END );
-               $mw->matchAndRemove( $text );
-*/
-               return $text;
-       }
-
+       
        /* Caching functions */
 
-       # checkLastModified returns true iff it has taken care of all
+       # checkLastModified returns true if it has taken care of all
        # output to the client that is necessary for this request.
        # (that is, it has sent a cached version of the page)
        function tryFileCache() {
@@ -1206,128 +1269,52 @@ class Article {
        #
        # This is a good place to put code to clear caches, for instance. 
 
-       /* static */ function onArticleCreate($title_obj,$text=''){
-               global $wgEnablePersistentLC, $wgEnableParserCache, $wgUseSquid;
-               global $wgDeferredUpdateList, $wgDBname, $wgMemc;
-               global $wgMessageCache, $wgInternalServer;
-               # Do standard deferred updates after page edit.
-               $id = $title_obj->getArticleID();
-               $title = $title_obj->getPrefixedDBkey();
-               $shortTitle = $title_obj->getDBkey();
-
-               $adj = $this->mCountAdjustment;
-
-               if ( 0 != $id ) {
-                       $u = new LinksUpdate( $id, $title );
-                       array_push( $wgDeferredUpdateList, $u );
-                       $u = new SiteStatsUpdate( 0, 1, $adj );
-                       array_push( $wgDeferredUpdateList, $u );
-                       $u = new SearchUpdate( $id, $title, $text );
-                       array_push( $wgDeferredUpdateList, $u );
-
-                       $u = new UserTalkUpdate( 1, $title_obj->getNamespace(), $shortTitle );
-                       array_push( $wgDeferredUpdateList, $u );
+       # This is called on page move and undelete, as well as edit     
+       /* static */ function onArticleCreate($title_obj){
+               global $wgEnablePersistentLC, $wgEnableParserCache, $wgUseSquid, $wgDeferredUpdateList;
 
-                       if ( $title_obj->getNamespace() == NS_MEDIAWIKI ) {
-                               $wgMessageCache->replace( $shortTitle, $text );
+               $titles = $title_obj->getBrokenLinksTo();
+               
+               # Purge squid 
+               if ( $wgUseSquid ) {
+                       $urls = $title_obj->getSquidURLs();
+                       foreach ( $titles as $linkTitle ) {
+                               $urls[] = $linkTitle->getInternalURL();
                        }
+                       $u = new SquidUpdate( $urls );
+                       array_push( $wgDeferredUpdateList, $u );
                }
+
+               # Clear persistent link cache
                if ( $wgEnablePersistentLC ) {
-                       LinkCache::linksccClearBrokenLinksTo( $title );
+                       LinkCache::linksccClearBrokenLinksTo( $title_obj->getPrefixedDBkey() );
                }
+
+               # Clear parser cache (not really used)
                if ( $wgEnableParserCache ) {
-                       OutputPage::parsercacheClearBrokenLinksTo( $title );
-               }
-               if ( $wgUseSquid ) {
-                       $urlArr = Array( 
-                               $wgInternalServer.wfLocalUrl( $title_obj->getPrefixedURL())
-                       );                      
-                       wfPurgeSquidServers($urlArr);
-                       /* this needs to be done after LinksUpdate */
-                       $u = new SquidUpdate($title_obj);
-                       array_push( $wgDeferredUpdateList, $u );
+                       OutputPage::parsercacheClearBrokenLinksTo( $title_obj->getPrefixedDBkey() );
                }
        }
 
-       /* static */ function onArticleDelete($title_obj,$text=''){
-               global $wgEnablePersistentLC, $wgEnableParserCache, $wgUseSquid, $wgDeferredUpdateList;
-               global $wgDeferredUpdateList, $wgDBname, $wgMemc;
-               global $wgMessageCache, $wgInternalServer;
-
-               $id = $title_obj->getArticleID();
-               $title = $title_obj->getPrefixedDBkey();
-               $shortTitle = $title_obj->getDBkey();
-
+       /* static */ function onArticleDelete($title_obj){
+               global $wgEnablePersistentLC, $wgEnableParserCache;
                if ( $wgEnablePersistentLC ) {
-                       LinkCache::linksccClearLinksTo( $id );
+                       LinkCache::linksccClearLinksTo( $title_obj->getArticleID() );
                }
                if ( $wgEnableParserCache ) {
-                       OutputPage::parsercacheClearLinksTo( $id );
-               }
-               if ( $wgUseSquid ) {
-                       $urlArr = Array(
-                               $wgInternalServer.wfLocalUrl( $title_obj->getPrefixedURL())
-                       );
-                       wfPurgeSquidServers($urlArr);
-
-                       /* prepare the list of urls to purge */
-                       $sql = "SELECT l_from FROM links WHERE l_to={$id}" ;
-                       $res = wfQuery ( $sql, DB_READ ) ;
-                       while ( $BL = wfFetchObject ( $res ) )
-                       {
-                               $t = Title::newFromDBkey( $BL->l_from) ; 
-                               $blurlArr[] = $wgInternalServer.wfLocalUrl( $t->getPrefixedURL() );
-                       }
-                       wfFreeResult ( $res ) ;
-                       $u = new SquidUpdate( $title_obj, $blurlArr );
-                       array_push( $wgDeferredUpdateList, $u );
-
+                       OutputPage::parsercacheClearLinksTo( $title_obj->getArticleID() );
                }
        }
 
-       /* static */ function onArticleEdit($title_obj,$text=''){
-               global $wgEnablePersistentLC, $wgEnableParserCache, $wgUseSquid;
-               global $wgDeferredUpdateList, $wgDBname, $wgMemc;
-               global $wgMessageCache, $wgInternalServer;
-
-               $id = $title_obj->getArticleID();
-               $title = $title_obj->getPrefixedDBkey();
-               $shortTitle = $title_obj->getDBkey();
-
-               $adj = $this->mCountAdjustment;
-
-               if ( 0 != $id ) {
-                       $u = new LinksUpdate( $id, $title );
-                       array_push( $wgDeferredUpdateList, $u );
-                       $u = new SiteStatsUpdate( 0, 1, $adj );
-                       array_push( $wgDeferredUpdateList, $u );
-                       $u = new SearchUpdate( $id, $title, $text );
-                       array_push( $wgDeferredUpdateList, $u );
-
-                       $u = new UserTalkUpdate( 1, $title_obj->getNamespace(), $shortTitle );
-                       array_push( $wgDeferredUpdateList, $u );
-
-                       if ( $title_obj->getNamespace() == NS_MEDIAWIKI ) {
-                               $wgMessageCache->replace( $shortTitle, $text );
-                       }
-               }
+       /* static */ function onArticleEdit($title_obj){
+               global $wgEnablePersistentLC, $wgEnableParserCache;
                if ( $wgEnablePersistentLC ) {
-                       LinkCache::linksccClearPage( $id );
+                       LinkCache::linksccClearPage( $title_obj->getArticleID() );
                }
                if ( $wgEnableParserCache ) {
-                       OutputPage::parsercacheClearPage( $id );
-               }
-               if ( $wgUseSquid ) {
-                       $urlArr = Array( 
-                               $wgInternalServer.wfLocalUrl( $title_obj->getPrefixedURL()),
-                       );
-                       wfPurgeSquidServers($urlArr);
+                       OutputPage::parsercacheClearPage( $title_obj->getArticleID(), $title_obj->getNamespace() );
                }
        }
 }
 
-function wfReplaceSubstVar( $matches ) {
-       return wfMsg( $matches[1] );
-}
-
 ?>