bug fix I (replaced -\>mTitle with )
[lhc/web/wiklou.git] / includes / Article.php
index 7b122d1..b588066 100644 (file)
@@ -20,16 +20,17 @@ class Article {
        /* private */ var $mCounter, $mComment, $mCountAdjustment;
        /* private */ var $mMinorEdit, $mRedirectedFrom;
        /* private */ var $mTouched, $mFileCache, $mTitle;
-
+       /* private */ var $mId, $mTable;
+       
        function Article( &$title ) {
                $this->mTitle =& $title;
                $this->clear();
        }
-
+       
        /* private */ function clear()
        {
                $this->mContentLoaded = false;
-               $this->mUser = $this->mCounter = -1; # Not loaded
+               $this->mCurID = $this->mUser = $this->mCounter = -1; # Not loaded
                $this->mRedirectedFrom = $this->mUserText =
                $this->mTimestamp = $this->mComment = $this->mFileCache = "";
                $this->mCountAdjustment = 0;
@@ -67,6 +68,7 @@ class Article {
        # Note that getContent/loadContent may follow redirects if
        # not told otherwise, and so may cause a change to mTitle.
 
+       # Return the text of this revision
        function getContent( $noredir = false )
        {
                global $action,$section,$count; # From query string
@@ -117,7 +119,8 @@ class Article {
                        }
                }
        }
-
+       
+       # Load the revision (including cur_text) into this object
        function loadContent( $noredir = false )
        {
                global $wgOut, $wgMwRedir;
@@ -125,21 +128,8 @@ class Article {
 
                if ( $this->mContentLoaded ) return;
                $fname = "Article::loadContent";
-
-               # 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 );
-
+               $success = true;
+               
                if ( ! $oldid ) {       # Retrieve current version
                        $id = $this->getID();
                        if ( 0 == $id ) return;
@@ -212,6 +202,22 @@ class Article {
                        $this->mTimestamp = $s->old_timestamp;
                        wfFreeResult( $res );
                }
+
+               # 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 ( isset( $redirect ) ) {
+                               $redirect = ($redirect == "no") ? "no" : "yes";
+                               $t .= ",redirect={$redirect}";
+                       }
+                       $this->mContent = wfMsg( "missingarticle", $t );
+               }
+
                $this->mContentLoaded = true;
        }
 
@@ -246,7 +252,7 @@ class Article {
                return 1;
        }
 
-       # Load the field related to the last edit time of the article.
+       # Loads everything from cur except cur_text
        # This isn't necessary for all uses, so it's only done if needed.
 
        /* private */ function loadLastEdit()
@@ -384,6 +390,7 @@ class Article {
                $won = wfInvertTimestamp( $now );
                wfSeedRandom();
                $rand = number_format( mt_rand() / mt_getrandmax(), 12, ".", "" );
+               $isminor = ( $isminor && $wgUser->getID() ) ? 1 : 0;
                $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
                  "cur_comment,cur_user,cur_timestamp,cur_minor_edit,cur_counter," .
                  "cur_restrictions,cur_user_text,cur_is_redirect," .
@@ -391,24 +398,16 @@ class Article {
                  wfStrencode( $text ) . "', '" .
                  wfStrencode( $summary ) . "', '" .
                  $wgUser->getID() . "', '{$now}', " .
-                 ( $isminor ? 1 : 0 ) . ", 0, '', '" .
+                 $isminor . ", 0, '', '" .
                  wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
                $res = wfQuery( $sql, DB_WRITE, $fname );
 
                $newid = wfInsertId();
                $this->mTitle->resetArticleID( $newid );
 
-               Article::onArticleCreate( $this->mTitle );
+               Article::onArticleCreate( $this->mTitle, $text );
+               RecentChange::notifyNew( $now, $this->mTitle, $isminor, $wgUser, $summary );
                
-               $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 (" .
-                 "'{$now}','{$now}',{$ns},'" . wfStrencode( $ttl ) . "',1," .
-                 ( $isminor ? 1 : 0 ) . ",{$newid}," . $wgUser->getID() . ",'" .
-                 wfStrencode( $wgUser->getName() ) . "','" .
-                 wfStrencode( $summary ) . "',0,0," .
-                 ( $wgUser->isBot() ? 1 : 0 ) . ")";
-               wfQuery( $sql, DB_WRITE, $fname );
                if ($watchthis) {               
                        if(!$this->mTitle->userIsWatching()) $this->watch(); 
                } else {
@@ -449,7 +448,7 @@ class Article {
                        }
                }
                if ( $this->mMinorEdit ) { $me1 = 1; } else { $me1 = 0; }
-               if ( $minor ) { $me2 = 1; } else { $me2 = 0; }          
+               if ( $minor && $wgUser->getID() ) { $me2 = 1; } else { $me2 = 0; }              
                if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ")[^\\n]+)/i", $text, $m ) ) {
                        $redir = 1;
                        $text = $m[1] . "\n"; # Remove all content but redirect
@@ -505,27 +504,8 @@ class Article {
                        $oldid = wfInsertID( $res );
 
                        $bot = (int)($wgUser->isBot() || $forceBot);
-                       
-                       $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
-                         "rc_namespace,rc_title,rc_new,rc_minor,rc_bot,rc_cur_id,rc_user," .
-                         "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid) VALUES (" .
-                         "'{$now}','{$now}'," . $this->mTitle->getNamespace() . ",'" .
-                         wfStrencode( $this->mTitle->getDBkey() ) . "',0,{$me2}," .
-                         "$bot," . $this->getID() . "," . $wgUser->getID() . ",'" .
-                         wfStrencode( $wgUser->getName() ) . "','" .
-                         wfStrencode( $summary ) . "',0,{$oldid})";
-                       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, DB_WRITE, $fname );
-
-                       $sql = "UPDATE recentchanges SET rc_cur_time='{$now}' " .
-                         "WHERE rc_cur_id=" . $this->getID();
-                       wfQuery( $sql, DB_WRITE, $fname );
-
+                       RecentChange::notifyEdit( $now, $this->mTitle, $me2, $wgUser, $summary, 
+                               $oldid, $this->getTimestamp(), $bot );
                        Article::onArticleEdit( $this->mTitle );
                }
 
@@ -564,7 +544,14 @@ class Article {
                $wgOut = new OutputPage();
                $wgOut->addWikiText( $text );
 
-               $this->editUpdates( $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
@@ -921,7 +908,7 @@ class Article {
                        return;
                }
                
-               # Secret enhanced rollback, marks edits rc_bot=1
+               # Enhanced rollback, marks edits rc_bot=1
                $bot = !!$_REQUEST['bot'];
                
                # Replace all this user's current edits with the next one down
@@ -952,7 +939,7 @@ class Article {
                                $wgOut->addHTML(
                                        wfMsg("editcomment",
                                        htmlspecialchars( $s->cur_comment ) ) );
-                               }
+                       }
                        return;
                }
                
@@ -979,7 +966,7 @@ class Article {
                }
 
                # Save it!
-               $newcomment = wfMsg( "revertpage", $s->old_user_text );
+               $newcomment = wfMsg( "revertpage", $s->old_user_text, $from );
                $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
                $wgOut->setRobotpolicy( "noindex,nofollow" );
                $wgOut->addHTML( "<h2>" . $newcomment . "</h2>\n<hr>\n" );
@@ -1008,42 +995,7 @@ 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()
        {
@@ -1166,13 +1118,6 @@ class Article {
                                return true;
                        } else {
                                wfDebug( " tryFileCache() - starting buffer\n" );                       
-                               if($cache->useGzip() && wfClientAcceptsGzip()) {
-                                       /* For some reason, adding this header line over in
-                                          CacheManager::saveToFileCache() fails on my test
-                                          setup at home, though it works on the live install.
-                                          Make double-sure...  --brion */
-                                       header( "Content-Encoding: gzip" );
-                               }
                                ob_start( array(&$cache, 'saveToFileCache' ) );
                        }
                } else {
@@ -1212,13 +1157,21 @@ class Article {
        /* static */ function incViewCount( $id )
        {
                $id = intval( $id );
+               global $wgHitcounterUpdateFreq;
+
+               if( $wgHitcounterUpdateFreq <= 1 ){ // 
+                       wfQuery("UPDATE cur SET cur_counter = cur_counter + 1 " .
+                               "WHERE cur_id = $id", DB_WRITE);
+                       return;
+               }
 
                # Not important enough to warrant an error page in case of failure
                $oldignore = wfIgnoreSQLErrors( true ); 
 
                wfQuery("INSERT INTO hitcounter (hc_id) VALUES ({$id})", DB_WRITE);
 
-               if( (rand() % 23 != 0) or (wfLastErrno() != 0) ){
+               $checkfreq = intval( $wgHitcounterUpdateFreq/25 + 1 );
+               if( (rand() % $checkfreq != 0) or (wfLastErrno() != 0) ){
                        # Most of the time (or on SQL errors), skip row count check
                        wfIgnoreSQLErrors( $oldignore );
                        return;
@@ -1227,7 +1180,7 @@ class Article {
                $res = wfQuery("SELECT COUNT(*) as n FROM hitcounter", DB_WRITE);
                $row = wfFetchObject( $res );
                $rown = intval( $row->n );
-               if( $rown > 1000 ){ // update counters after ~1000 hits
+               if( $rown >= $wgHitcounterUpdateFreq ){ 
                        wfProfileIn( "Article::incViewCount-collect" );
                        $old_user_abort = ignore_user_abort( true );
 
@@ -1253,33 +1206,122 @@ class Article {
        #
        # This is a good place to put code to clear caches, for instance. 
 
-       /* static */ function onArticleCreate($title_obj){
-               global $wgEnablePersistentLC, $wgEnableParserCache;
+       /* 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 );
+
+                       if ( $title_obj->getNamespace() == NS_MEDIAWIKI ) {
+                               $wgMessageCache->replace( $shortTitle, $text );
+                       }
+               }
                if ( $wgEnablePersistentLC ) {
-                       LinkCache::linksccClearBrokenLinksTo( $title_obj->getPrefixedDBkey() );
+                       LinkCache::linksccClearBrokenLinksTo( $title );
                }
                if ( $wgEnableParserCache ) {
-                       OutputPage::parsercacheClearBrokenLinksTo( $title_obj->getPrefixedDBkey() );
+                       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 );
                }
        }
 
-       /* static */ function onArticleDelete($title_obj){
-               global $wgEnablePersistentLC, $wgEnableParserCache;
+       /* 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();
+
                if ( $wgEnablePersistentLC ) {
-                       LinkCache::linksccClearLinksTo( $title_obj->getArticleID() );
+                       LinkCache::linksccClearLinksTo( $id );
                }
                if ( $wgEnableParserCache ) {
-                       OutputPage::parsercacheClearLinksTo( $title_obj->getArticleID() );
+                       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 );
+
                }
        }
 
-       /* static */ function onArticleEdit($title_obj){
-               global $wgEnablePersistentLC, $wgEnableParserCache;
+       /* 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 );
+                       }
+               }
                if ( $wgEnablePersistentLC ) {
-                       LinkCache::linksccClearPage( $title_obj->getArticleID() );
+                       LinkCache::linksccClearPage( $id );
                }
                if ( $wgEnableParserCache ) {
-                       OutputPage::parsercacheClearPage( $title_obj->getArticleID() );
+                       OutputPage::parsercacheClearPage( $id );
+               }
+               if ( $wgUseSquid ) {
+                       $urlArr = Array( 
+                               $wgInternalServer.wfLocalUrl( $title_obj->getPrefixedURL()),
+                       );
+                       wfPurgeSquidServers($urlArr);
                }
        }
 }