tweak some comments (schema blah blah)
[lhc/web/wiklou.git] / includes / Article.php
index cf226d8..f45e090 100644 (file)
@@ -732,6 +732,8 @@ class Article {
                ), $fname );
                $newid = $dbw->insertId();
                
+               $this->mTitle->resetArticleId( $newid );
+               
                wfProfileOut( $fname );
                return $newid;
        }
@@ -741,26 +743,33 @@ class Article {
         *
         * @param Database $dbw
         * @param int $revId
-        * @param int $lastRevision
-        * @param bool $isRedirect
+        * @param string $text -- used to set length and redirect status if given
+        * @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, $isRedirect = false ) {
+       function updateRevisionOn( &$dbw, $revId, $text = '', $lastRevision = null ) {
                $fname = 'Article::updateToRevision';
                wfProfileIn( $fname );
                
+               $conditions = array( 'page_id' => $this->getId() );
+               if( !is_null( $lastRevision ) ) {
+                       # An extra check against threads stepping on each other
+                       $conditions['page_latest'] = $lastRevision;
+               }
                $dbw->update( 'page',
                        array( /* SET */
                                'page_latest'      => $revId,
                                'page_touched'     => $dbw->timestamp(),
-                               'page_is_new'      => ($lastRevision == 0),
-                               'page_is_redirect' => $isRedirect,
-                       ), array( /* WHERE */
-                               'page_id'          => $this->getId(),
-                               'page_latest'      => $lastRevision, # Paranoia
-                       ), $fname
-               );
+                               'page_is_new'      => ($lastRevision === 0) ? 0 : 1,
+                               'page_is_redirect' => Article::isRedirect( $text ),
+                               'page_len'         => strlen( $text ),
+                       ),
+                       $conditions,
+                       $fname );
                
                wfProfileOut( $fname );
                return ( $dbw->affectedRows() != 0 );
@@ -803,7 +812,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, $revisionId, $text, 0 );
 
                Article::onArticleCreate( $this->mTitle );
                RecentChange::notifyNew( $now, $this->mTitle, $isminor, $wgUser, $summary );
@@ -1001,7 +1010,7 @@ class Article {
                        $revisionId = $revision->insertOn( $dbw );
                        
                        # Update page
-                       $ok = $this->updateRevisionOn( $dbw, $revisionId, $lastRevision, $redir );
+                       $ok = $this->updateRevisionOn( $dbw, $revisionId, $text, $lastRevision );
 
                        if( !$ok ) {
                                /* Belated edit conflict! Run away!! */
@@ -1677,70 +1686,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 );
        }
 
@@ -1922,50 +1937,29 @@ class Article {
 
        /**
         * Edit an article without doing all that other stuff
+        * The article must already exist; link tables etc
+        * are not updated, caches are not flushed.
         *
         * @param string $text text submitted
         * @param string $comment comment submitted
-        * @param integer $minor whereas it's a minor modification
+        * @param bool $minor whereas it's a minor modification
         */
        function quickEdit( $text, $comment = '', $minor = 0 ) {
-               global $wgUser;
                $fname = 'Article::quickEdit';
-
-               #wfDebugDieBacktrace( "$fname called." );
-
                wfProfileIn( $fname );
 
                $dbw =& wfGetDB( DB_MASTER );
-               $ns = $this->mTitle->getNamespace();
-               $dbkey = $this->mTitle->getDBkey();
-               $encDbKey = $dbw->strencode( $dbkey );
-               $timestamp = $dbw->timestamp();
-               # insert new text
-               $dbw->insert( 'text', array(
-                               'old_text' => $text,
-                               'old_flags' => "" ), $fname );
-               $text_id = $dbw->insertID();
-
-               # update page
-               $dbw->update( 'page', array(
-                       'page_is_new' => 0,
-                       'page_touched' => $timestamp,
-                       'page_is_redirect' => $this->isRedirect( $text ) ? 1 : 0,
-                       'page_latest' => $text_id ),
-                       array( 'page_namespace' => $ns, 'page_title' => $dbkey ), $fname );
-               # Retrieve page ID
-               $page_id = $dbw->selectField( 'page', 'page_id', array( 'page_namespace' => $ns, 'page_title' => $dbkey ), $fname );
-
-               # update revision
-               $dbw->insert( 'revision', array(
-                       'rev_id' => $text_id,
-                       'rev_page' => $page_id,
-                       'rev_comment' => $comment,
-                       'rev_user' => $wgUser->getID(),
-                       'rev_user_text' => $wgUser->getName(),
-                       'rev_timestamp' => $timestamp,
-                       'rev_minor_edit' => intval($minor) ),
-                       $fname );
+               $dbw->begin();
+               $revision = new Revision( array(
+                       'page'       => $this->getId(),
+                       'text'       => $text,
+                       'comment'    => $comment,
+                       'minor_edit' => $minor ? 1 : 0,
+                       ) );
+               $revisionId = $revision->insertOn( $dbw );
+               $this->updateRevisionOn( $dbw, $revisionId, $text );
+               $dbw->commit();
+               
                wfProfileOut( $fname );
        }
 
@@ -2153,32 +2147,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;
-}
 
 ?>