Added wfAbruptExit() function, to replace exit() calls with.
[lhc/web/wiklou.git] / includes / Article.php
index dcf84b6..84bbdad 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,7 +37,7 @@ 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 )
        {
@@ -95,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();
@@ -106,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}";
+                       wfDebug( "$sql\n" );
                        $res = wfQuery( $sql, DB_READ, $fname );
-                       if ( 0 == wfNumRows( $res ) ) { return; }
+                       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 ) &&
@@ -135,7 +150,7 @@ 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}";
+                                                 "cur_counter,cur_restrictions,cur_touched FROM cur WHERE cur_id={$rid}";
                                                $res = wfQuery( $sql, DB_READ, $fname );
 
                                                if ( 0 != wfNumRows( $res ) ) {
@@ -146,6 +161,7 @@ class Article {
                                        }
                                }
                        }
+
                        $this->mContent = $s->cur_text;
                        $this->mUser = $s->cur_user;
                        $this->mCounter = $s->cur_counter;
@@ -170,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()
        {
@@ -255,7 +277,7 @@ class Article {
        {
                global $wgUser, $wgOut, $wgLang;
                global $oldid, $diff; # From query
-               global $wgLinkCache;
+               global $wgLinkCache, $IP;
                $fname = "Article::view";
                wfProfileIn( $fname );
 
@@ -272,7 +294,15 @@ class Article {
                        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" ) );
@@ -287,11 +317,9 @@ 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 );
 
@@ -307,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 );
@@ -333,6 +365,13 @@ class Article {
                $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 (" .
@@ -350,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;
@@ -449,7 +493,15 @@ class Article {
                        $sql = "UPDATE recentchanges SET rc_cur_time='{$now}' " .
                          "WHERE rc_cur_id=" . $this->getID();
                        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, DB_WRITE );
@@ -629,6 +681,12 @@ class Article {
                        "cur_restrictions='{$limit}' WHERE cur_id={$id}";
                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() ) );
        }
 
@@ -642,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;
                }
@@ -662,18 +719,26 @@ 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, DB_READ, $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())."'";
+               $sql="SELECT cur_text FROM cur WHERE cur_namespace=$ns and cur_title='$etitle'";
                $res=wfQuery($sql, DB_READ, $fname);
                if( ($s=wfFetchObject($res))) {
 
@@ -723,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 ) );
@@ -740,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\">
@@ -757,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();
@@ -769,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 );
@@ -778,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();
@@ -820,17 +891,26 @@ class Article {
                
                $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
                  "rc_title='{$t}'";
-        wfQuery( $sql, DB_WRITE, $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, 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;
 
@@ -866,7 +946,7 @@ class Article {
                $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 );
@@ -881,6 +961,10 @@ class Article {
                        $wgOut->sysopRequired();
                        return;
                }
+               if ( wfReadOnly() ) {
+                       $wgOut->readOnlyPage( $this->getContent() );
+                       return;
+               }
 
                # Replace all this user's current edits with the next one down
                $tt = wfStrencode( $this->mTitle->getDBKey() );
@@ -930,12 +1014,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 );
        }
        
@@ -945,13 +1034,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 );
@@ -987,9 +1078,20 @@ class Article {
                          $this->mTitle->getDBkey() );
                        array_push( $wgDeferredUpdateList, $u );
 
-                       if ( $this->getNamespace == NS_MEDIAWIKI ) {
-                               $key = "$wgDBname:MediaWiki:title:" . $this->mTitle->getDBkey();
-                               $wgMemc->delete( $key );
+                       if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
+                               $messageCache = $wgMemc->get( "$wgDBname:messages" );
+
+                               # If another thread is loading, poll
+                               for ( $i=0; $i<70 && $messageCache == 'loading'; $i++ ) {
+                                       sleep(1);
+                                       $messageCache = $wgMemc->get( "$wgDBname:messages" );
+                               }
+                               
+                               if ( !$messageCache || $messageCache == 'loading' ) {
+                                       $messageCache = wfLoadAllMessages();
+                               }
+                               $messageCache[$this->mTitle->getDBkey()] = $text;
+                               $wgMemc->set( "$wgDBname:messages", $messageCache, 86400 );
                        }
                }
        }
@@ -999,7 +1101,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})" );
        }
 
@@ -1049,12 +1151,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();
@@ -1074,14 +1177,28 @@ class Article {
                # {{SUBST:xxx}} variables
                #
                $mw =& MagicWord::get( MAG_SUBST );
-               $text = $mw->substituteCallback( $text, "replaceMsgVar" );
-
+               $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 */
        
        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 ) {
@@ -1091,8 +1208,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" );                       
@@ -1125,10 +1244,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] );
 }
 
 ?>