Don't force edit encoding when LanguageEo.php is loaded; defer until $wgContLang...
[lhc/web/wiklou.git] / includes / Article.php
index cf3592b..4e56f69 100644 (file)
@@ -732,6 +732,8 @@ class Article {
                ), $fname );
                $newid = $dbw->insertId();
                
+               $this->mTitle->resetArticleId( $newid );
+               
                wfProfileOut( $fname );
                return $newid;
        }
@@ -740,13 +742,16 @@ class Article {
         * Update the page record to point to a newly saved revision.
         *
         * @param Database $dbw
-        * @param int $revId
-        * @param int $lastRevision
-        * @param bool $isRedirect
+        * @param Revision $revision -- for ID number, and text used to set
+                                       length and redirect status fields
+        * @param int $lastRevision -- if given, will not overwrite the page field
+        *                             when different from the currently set value.
+        *                             Giving 0 indicates the new page flag should
+        *                             be set on.
         * @return bool true on success, false on failure
         * @access private
         */
-       function updateRevisionOn( &$dbw, $revId, $lastRevision = null, $isRedirect = false ) {
+       function updateRevisionOn( &$dbw, $revision, $lastRevision = null ) {
                $fname = 'Article::updateToRevision';
                wfProfileIn( $fname );
                
@@ -755,12 +760,14 @@ class Article {
                        # An extra check against threads stepping on each other
                        $conditions['page_latest'] = $lastRevision;
                }
+               $text = $revision->getText();
                $dbw->update( 'page',
                        array( /* SET */
-                               'page_latest'      => $revId,
+                               'page_latest'      => $revision->getId(),
                                'page_touched'     => $dbw->timestamp(),
                                'page_is_new'      => ($lastRevision === 0) ? 0 : 1,
-                               'page_is_redirect' => $isRedirect ? 1 : 0,
+                               'page_is_redirect' => Article::isRedirect( $text ),
+                               'page_len'         => strlen( $text ),
                        ),
                        $conditions,
                        $fname );
@@ -769,6 +776,40 @@ class Article {
                return ( $dbw->affectedRows() != 0 );
        }
        
+       /**
+        * If the given revision is newer than the currently set page_latest,
+        * update the page record. Otherwise, do nothing.
+        *
+        * @param Database $dbw
+        * @param Revision $revision
+        */
+       function updateIfNewerOn( &$dbw, $revision ) {
+               $fname = 'Article::updateIfNewerOn';
+               wfProfileIn( $fname );
+               
+               $row = $dbw->selectRow(
+                       array( 'revision', 'page' ),
+                       array( 'rev_id', 'rev_timestamp' ),
+                       array(
+                               'page_id' => $this->getId(),
+                               'page_latest=rev_id' ),
+                       $fname );
+               if( $row ) {
+                       if( $row->rev_timestamp >= $revision->getTimestamp() ) {
+                               wfProfileOut( $fname );
+                               return false;
+                       }
+                       $prev = $row->rev_id;
+               } else {
+                       # No or missing previous revision; mark the page as new
+                       $prev = 0;
+               }
+               
+               $ret = $this->updateRevisionOn( $dbw, $revision, $prev );
+               wfProfileOut( $fname );
+               return $ret;
+       }
+       
        /**
         * Theoretically we could defer these whole insert and update
         * functions for after display, but that's taking a big leap
@@ -806,7 +847,7 @@ class Article {
                $this->mTitle->resetArticleID( $newid );
                
                # Update the page record with revision data
-               $this->updateRevisionOn( $dbw, $revisionId, 0, $this->isRedirect( $text ) );
+               $this->updateRevisionOn( $dbw, $revision, 0 );
 
                Article::onArticleCreate( $this->mTitle );
                RecentChange::notifyNew( $now, $this->mTitle, $isminor, $wgUser, $summary );
@@ -1004,7 +1045,7 @@ class Article {
                        $revisionId = $revision->insertOn( $dbw );
                        
                        # Update page
-                       $ok = $this->updateRevisionOn( $dbw, $revisionId, $lastRevision, $redir );
+                       $ok = $this->updateRevisionOn( $dbw, $revision, $lastRevision );
 
                        if( !$ok ) {
                                /* Belated edit conflict! Run away!! */
@@ -1153,13 +1194,13 @@ class Article {
                        return;
                }
 
-               if (wfRunHooks('WatchArticle', $wgUser, $this)) {
+               if (wfRunHooks('WatchArticle', array(&$wgUser, &$this))) {
                        
                        $wgUser->addWatch( $this->mTitle );
                        $wgUser->saveSettings();
 
-                       wfRunHooks('WatchArticleComplete', $wgUser, $this);
-                       
+                       wfRunHooks('WatchArticleComplete', array(&$wgUser, &$this));
+
                        $wgOut->setPagetitle( wfMsg( 'addedwatch' ) );
                        $wgOut->setRobotpolicy( 'noindex,follow' );
                        
@@ -1188,12 +1229,12 @@ class Article {
                        return;
                }
 
-               if (wfRunHooks('UnwatchArticle', $wgUser, $this)) {
+               if (wfRunHooks('UnwatchArticle', array(&$wgUser, &$this))) {
                        
                        $wgUser->removeWatch( $this->mTitle );
                        $wgUser->saveSettings();
                        
-                       wfRunHooks('UnwatchArticleComplete', $wgUser, $this);
+                       wfRunHooks('UnwatchArticleComplete', array(&$wgUser, &$this));
                        
                        $wgOut->setPagetitle( wfMsg( 'removedwatch' ) );
                        $wgOut->setRobotpolicy( 'noindex,follow' );
@@ -1246,7 +1287,7 @@ class Article {
                        if( !$moveonly ) {
                                $restrictions .= ":edit=" . $limit;
                        }
-                       if (wfRunHooks('ArticleProtect', $this, $wgUser, $limit == 'sysop', $reason, $moveonly)) {
+                       if (wfRunHooks('ArticleProtect', array(&$this, &$wgUser, $limit == 'sysop', $reason, $moveonly))) {
                                
                                $dbw =& wfGetDB( DB_MASTER );
                                $dbw->update( 'page',
@@ -1258,7 +1299,7 @@ class Article {
                                                                                           ), 'Article::protect'
                                                          );
                                
-                               wfRunHooks('ArticleProtectComplete', $this, $wgUser, $limit == 'sysop', $reason, $moveonly);
+                               wfRunHooks('ArticleProtectComplete', array(&$this, &$wgUser, $limit == 'sysop', $reason, $moveonly));
                                
                                $log = new LogPage( 'protect' );
                                if ( $limit === '' ) {
@@ -1521,7 +1562,7 @@ class Article {
                $fname = 'Article::doDelete';
                wfDebug( $fname."\n" );
 
-               if (wfRunHooks('ArticleDelete', $this, $wgUser, $reason)) {
+               if (wfRunHooks('ArticleDelete', array(&$this, &$wgUser, &$reason))) {
                        if ( $this->doDeleteArticle( $reason ) ) {
                                $deleted = $this->mTitle->getPrefixedText();
                                
@@ -1537,7 +1578,7 @@ class Article {
                                
                                $wgOut->addHTML( '<p>' . $text . "</p>\n" );
                                $wgOut->returnToMain( false );
-                               wfRunHooks('ArticleDeleteComplete', $this, $wgUser, $reason);
+                               wfRunHooks('ArticleDeleteComplete', array(&$this, &$wgUser, $reason));
                        } else {
                                $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
                        }
@@ -1680,70 +1721,76 @@ class Article {
                $n = $this->mTitle->getNamespace();
 
                # Get the last editor, lock table exclusively
-               $s = $dbw->selectRow( array( 'page', 'revision' ),
-                       array( 'page_id','rev_user','rev_user_text','rev_comment' ),
-                       array( 'page_title' => $tt, 'page_namespace' => $n ),
-                       $fname, 'FOR UPDATE'
-               );
-               if( $s === false ) {
-                       # Something wrong
+               $dbw->begin();
+               $current = Revision::newFromTitle( $this->mTitle );
+               if( is_null( $current ) ) {
+                       # Something wrong... no page?
+                       $dbw->rollback();
                        $wgOut->addHTML( wfMsg( 'notanarticle' ) );
                        return;
                }
-               $ut = $dbw->strencode( $s->cur_user_text );
-               $uid = $s->rev_user;
-               $pid = $s->page_id;
 
                $from = str_replace( '_', ' ', $wgRequest->getVal( 'from' ) );
-               if( $from != $s->rev_user_text ) {
+               if( $from != $current->getUserText() ) {
                        $wgOut->setPageTitle(wfmsg('rollbackfailed'));
                        $wgOut->addWikiText( wfMsg( 'alreadyrolled',
                                htmlspecialchars( $this->mTitle->getPrefixedText()),
                                htmlspecialchars( $from ),
-                               htmlspecialchars( $s->rev_user_text ) ) );
-                       if($s->rev_comment != '') {
+                               htmlspecialchars( $current->getUserText() ) ) );
+                       if( $current->getComment() != '') {
                                $wgOut->addHTML(
-                                       wfMsg('editcomment',
-                                       htmlspecialchars( $s->rev_comment ) ) );
+                                       wfMsg( 'editcomment',
+                                       htmlspecialchars( $current->getComment() ) ) );
                        }
                        return;
                }
 
                # Get the last edit not by this guy
-               $s = $dbw->selectRow( 'old',
-                       array( 'old_text','old_user','old_user_text','old_timestamp','old_flags' ),
+               $user = IntVal( $current->getUser() );
+               $user_text = $dbw->addQuotes( $current->getUserText() );
+               $s = $dbw->selectRow( 'revision',
+                       array( 'rev_id', 'rev_timestamp' ),
                        array(
-                               'old_namespace' => $n,
-                               'old_title' => $tt,
-                               "old_user <> {$uid} OR old_user_text <> '{$ut}'"
-                       ), $fname, array( 'FOR UPDATE', 'USE INDEX' => 'name_title_timestamp' )
-               );
+                               'rev_page' => $current->getPage(),
+                               "rev_user <> {$user} OR rev_user_text <> {$user_text}"
+                       ), $fname,
+                       array(
+                               'USE INDEX' => 'page_timestamp',
+                               'ORDER BY'  => 'rev_timestamp DESC' )
+                       );
                if( $s === false ) {
                        # Something wrong
+                       $dbw->rollback();
                        $wgOut->setPageTitle(wfMsg('rollbackfailed'));
                        $wgOut->addHTML( wfMsg( 'cantrollback' ) );
                        return;
                }
-
+               
                if ( $bot ) {
                        # Mark all reverted edits as bot
                        $dbw->update( 'recentchanges',
                                array( /* SET */
                                        'rc_bot' => 1
                                ), array( /* WHERE */
-                                       'rc_user' => $uid,
-                                       "rc_timestamp > '{$s->old_timestamp}'",
+                                       'rc_cur_id'    => $current->getPage(),
+                                       'rc_user_text' => $current->getUserText(),
+                                       "rc_timestamp > '{$s->rev_timestamp}'",
                                ), $fname
                        );
                }
 
                # Save it!
-               $newcomment = wfMsg( 'revertpage', $s->old_user_text, $from );
+               $target = Revision::newFromId( $s->rev_id );
+               $newcomment = wfMsg( 'revertpage', $target->getUserText(), $from );
+               
                $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
                $wgOut->setRobotpolicy( 'noindex,nofollow' );
                $wgOut->addHTML( '<h2>' . htmlspecialchars( $newcomment ) . "</h2>\n<hr />\n" );
-               $this->updateArticle( Revision::getRevisionText( $s ), $newcomment, 1, $this->mTitle->userIsWatching(), $bot );
+               
+               $this->updateArticle( $target->getText(), $newcomment, 1, $this->mTitle->userIsWatching(), $bot );
                Article::onArticleEdit( $this->mTitle );
+               
+               $dbw->commit();
                $wgOut->returnToMain( false );
        }
 
@@ -1945,8 +1992,7 @@ class Article {
                        'minor_edit' => $minor ? 1 : 0,
                        ) );
                $revisionId = $revision->insertOn( $dbw );
-               $this->updateRevisionOn( $dbw, $revisionId, null,
-                       Article::isRedirect( $text ) );
+               $this->updateRevisionOn( $dbw, $revision );
                $dbw->commit();
                
                wfProfileOut( $fname );
@@ -2136,32 +2182,5 @@ class Article {
        }
 }
 
-/**
- * Check whether an article is a stub
- *
- * @public
- * @param integer $articleID   ID of the article that is to be checked
- */
-function wfArticleIsStub( $articleID ) {
-       global $wgUser;
-       $fname = 'wfArticleIsStub';
-
-       wfDebugDieBacktrace( 'This function seems to be unused. Pending removal.' );
-
-       $threshold = $wgUser->getOption('stubthreshold') ;
-       if ( $threshold > 0 ) {
-               $dbr =& wfGetDB( DB_SLAVE );
-               $s = $dbr->selectRow( array('page', 'text'),
-                       array( 'LENGTH(old_text) AS len', 'page_namespace', 'page_is_redirect' ),
-                       array( 'page_id' => $articleID, "page.page_latest=text.old_id" ),
-                       $fname ) ;
-               if ( $s == false OR $s->page_is_redirect OR $s->page_namespace != NS_MAIN ) {
-                       return false;
-               }
-               $size = $s->len;
-               return ( $size < $threshold );
-       }
-       return false;
-}
 
 ?>