* Confirmation is now required when deleting old versions of files
[lhc/web/wiklou.git] / includes / Article.php
index 9f3795e..6960b0c 100644 (file)
@@ -36,6 +36,18 @@ class Article {
        var $mUserText;                 //!<
        /**@}}*/
 
+       /**
+        * Constants used by internal components to get rollback results
+        */
+       const SUCCESS = 0;                      // Operation successful
+       const PERM_DENIED = 1;          // Permission denied
+       const BLOCKED = 2;                      // User has been blocked
+       const READONLY = 3;                     // Wiki is in read-only mode
+       const BAD_TOKEN = 4;            // Invalid token specified
+       const BAD_TITLE = 5;            // $this is not a valid Article
+       const ALREADY_ROLLED = 6;       // Someone else already rolled this back. $from and $summary will be set
+       const ONLY_AUTHOR = 7;          // User is the only author of the page
        /**
         * Constructor and clear the article
         * @param $title Reference to a Title object.
@@ -257,13 +269,16 @@ class Article {
                                'page_random',
                                'page_touched',
                                'page_latest',
-                               'page_len' ) ;
-               wfRunHooks( 'ArticlePageDataBefore', array( &$this , &$fields ) )       ;
-               $row = $dbr->selectRow( 'page',
+                               'page_len',
+               );
+               wfRunHooks( 'ArticlePageDataBefore', array( &$this, &$fields ) );
+               $row = $dbr->selectRow(
+                       'page',
                        $fields,
                        $conditions,
-                       'Article::pageData' );
-               wfRunHooks( 'ArticlePageDataAfter', array( &$this , &$row ) )   ;
+                       __METHOD__
+               );
+               wfRunHooks( 'ArticlePageDataAfter', array( &$this, &$row ) );
                return $row ;
        }
 
@@ -1238,7 +1253,10 @@ class Article {
                                }
                        }
 
-                       $this->doRedirect( $this->isRedirect( $text ), $sectionanchor );
+                       $extraq = ''; // Give extensions a chance to modify URL query on update
+                       wfRunHooks( 'ArticleUpdateBeforeRedirect', array( $this, &$sectionanchor, &$extraq ) );
+
+                       $this->doRedirect( $this->isRedirect( $text ), $sectionanchor, $extraq );
                }
                return $good;
        }
@@ -1445,19 +1463,16 @@ class Article {
                        # Clear caches
                        Article::onArticleCreate( $this->mTitle );
 
-                       wfRunHooks( 'ArticleInsertComplete', array( &$this, &$wgUser, $text,
-                               $summary, $flags & EDIT_MINOR,
-                               null, null, &$flags ) );
+                       wfRunHooks( 'ArticleInsertComplete', array( &$this, &$wgUser, $text, $summary,
+                        $flags & EDIT_MINOR, null, null, &$flags, $revision ) );
                }
 
                if ( $good && !( $flags & EDIT_DEFER_UPDATES ) ) {
                        wfDoUpdates();
                }
 
-               wfRunHooks( 'ArticleSaveComplete',
-                       array( &$this, &$wgUser, $text,
-                       $summary, $flags & EDIT_MINOR,
-                       null, null, &$flags ) );
+               wfRunHooks( 'ArticleSaveComplete', array( &$this, &$wgUser, $text, $summary,
+                       $flags & EDIT_MINOR, null, null, &$flags, $revision ) );
 
                wfProfileOut( __METHOD__ );
                return $good;
@@ -1477,12 +1492,14 @@ class Article {
         * @param boolean $noRedir Add redirect=no
         * @param string $sectionAnchor section to redirect to, including "#"
         */
-       function doRedirect( $noRedir = false, $sectionAnchor = '' ) {
+       function doRedirect( $noRedir = false, $sectionAnchor = '', $extraq = '' ) {
                global $wgOut;
                if ( $noRedir ) {
                        $query = 'redirect=no';
+                       if( $extraq )
+                               $query .= "&$query";
                } else {
-                       $query = '';
+                       $query = $extraq;
                }
                $wgOut->redirect( $this->mTitle->getFullURL( $query ) . $sectionAnchor );
        }
@@ -2104,7 +2121,8 @@ class Article {
                                'ar_text_id'    => 'rev_text_id',
                                'ar_text'       => '\'\'', // Be explicit to appease
                                'ar_flags'      => '\'\'', // MySQL's "strict mode"...
-                               'ar_len'                => 'rev_len'
+                               'ar_len'                => 'rev_len',
+                               'ar_page_id'    => 'page_id',
                        ), array(
                                'page_id' => $id,
                                'page_id = rev_page'
@@ -2156,60 +2174,52 @@ class Article {
        }
 
        /**
-        * Revert a modification
-        */
-       function rollback() {
-               global $wgUser, $wgOut, $wgRequest, $wgUseRCPatrol;
-
+        * Roll back the most recent consecutive set of edits to a page
+        * from the same user; fails if there are no eligible edits to
+        * roll back to, e.g. user is the sole contributor
+        *
+        * @param string $fromP - Name of the user whose edits to rollback. 
+        * @param string $summary - Custom summary. Set to default summary if empty.
+        * @param string $token - Rollback token.
+        * @param bool $bot - If true, mark all reverted edits as bot.
+        * 
+        * @param array $resultDetails contains result-specific dict of additional values
+        *    ALREADY_ROLLED : 'current' (rev)
+        *    SUCCESS        : 'summary' (str), 'current' (rev), 'target' (rev)
+        * 
+        * @return self::SUCCESS on succes, self::* on failure
+        */
+       public function doRollback( $fromP, $summary, $token, $bot, &$resultDetails ) {
+               global $wgUser, $wgUseRCPatrol;
+               $resultDetails = null;
+               
                if( $wgUser->isAllowed( 'rollback' ) ) {
                        if( $wgUser->isBlocked() ) {
-                               $wgOut->blockedPage();
-                               return;
+                               return self::BLOCKED;
                        }
                } else {
-                       $wgOut->permissionRequired( 'rollback' );
-                       return;
+                       return self::PERM_DENIED;
                }
-
+                       
                if ( wfReadOnly() ) {
-                       $wgOut->readOnlyPage( $this->getContent() );
-                       return;
+                       return self::READONLY;
                }
-               if( !$wgUser->matchEditToken( $wgRequest->getVal( 'token' ),
-                       array( $this->mTitle->getPrefixedText(),
-                               $wgRequest->getVal( 'from' ) )  ) ) {
-                       $wgOut->setPageTitle( wfMsg( 'rollbackfailed' ) );
-                       $wgOut->addWikiText( wfMsg( 'sessionfailure' ) );
-                       return;
-               }
-               $dbw = wfGetDB( DB_MASTER );
+               if( !$wgUser->matchEditToken( $token, array( $this->mTitle->getPrefixedText(), $fromP ) ) )
+                       return self::BAD_TOKEN;
 
-               # Enhanced rollback, marks edits rc_bot=1
-               $bot = $wgRequest->getBool( 'bot' );
-
-               # Replace all this user's current edits with the next one down
+               $dbw = wfGetDB( DB_MASTER );
 
                # Get the last editor
                $current = Revision::newFromTitle( $this->mTitle );
                if( is_null( $current ) ) {
                        # Something wrong... no page?
-                       $wgOut->addHTML( wfMsg( 'notanarticle' ) );
-                       return;
+                       return self::BAD_TITLE;
                }
 
-               $from = str_replace( '_', ' ', $wgRequest->getVal( 'from' ) );
+               $from = str_replace( '_', ' ', $fromP );
                if( $from != $current->getUserText() ) {
-                       $wgOut->setPageTitle( wfMsg('rollbackfailed') );
-                       $wgOut->addWikiText( wfMsg( 'alreadyrolled',
-                               htmlspecialchars( $this->mTitle->getPrefixedText()),
-                               htmlspecialchars( $from ),
-                               htmlspecialchars( $current->getUserText() ) ) );
-                       if( $current->getComment() != '') {
-                               $wgOut->addHTML(
-                                       wfMsg( 'editcomment',
-                                       $wgUser->getSkin()->formatComment( $current->getComment() ) ) );
-                       }
-                       return;
+                       $resultDetails = array( 'current' => $current );
+                       return self::ALREADY_ROLLED;
                }
 
                # Get the last edit not by this guy
@@ -2227,11 +2237,9 @@ class Article {
                        );
                if( $s === false ) {
                        # Something wrong
-                       $wgOut->setPageTitle(wfMsg('rollbackfailed'));
-                       $wgOut->addHTML( wfMsg( 'cantrollback' ) );
-                       return;
+                       return self::ONLY_AUTHOR;
                }
-
+       
                $set = array();
                if ( $bot ) {
                        # Mark all reverted edits as bot
@@ -2244,27 +2252,100 @@ class Article {
 
                if ( $set ) {
                        $dbw->update( 'recentchanges', $set,
-                               array( /* WHERE */
-                                       'rc_cur_id'    => $current->getPage(),
-                                       'rc_user_text' => $current->getUserText(),
-                                       "rc_timestamp > '{$s->rev_timestamp}'",
-                               ), __METHOD__
-                       );
+                                       array( /* WHERE */
+                                               'rc_cur_id' => $current->getPage(),
+                                               'rc_user_text' => $current->getUserText(),
+                                               "rc_timestamp > '{$s->rev_timestamp}'",
+                                       ), __METHOD__
+                               );
                }
 
                # Get the edit summary
                $target = Revision::newFromId( $s->rev_id );
-               $newComment = wfMsgForContent( 'revertpage', $target->getUserText(), $from );
-               $newComment = $wgRequest->getText( 'summary', $newComment );
+               if( empty( $summary ) )
+                       $summary = wfMsgForContent( 'revertpage', $target->getUserText(), $from );
+
+               # Save
+               $flags = EDIT_UPDATE | EDIT_MINOR;
+               if( $bot )
+                       $flags |= EDIT_FORCE_BOT;
+               $this->doEdit( $target->getText(), $summary, $flags );
+
+               $resultDetails = array(
+                       'summary' => $summary,
+                       'current' => $current,
+                       'target' => $target,
+               );
+               return self::SUCCESS;
+       }
 
-               # Save it!
-               $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
-               $wgOut->setRobotpolicy( 'noindex,nofollow' );
-               $wgOut->addHTML( '<h2>' . htmlspecialchars( $newComment ) . "</h2>\n<hr />\n" );
+       /**
+        * User interface for rollback operations
+        */
+       function rollback() {
+               global $wgUser, $wgOut, $wgRequest, $wgUseRCPatrol;
 
-               $this->updateArticle( $target->getText(), $newComment, 1, $this->mTitle->userIsWatching(), $bot );
+               $details = null;
+               $result = $this->doRollback(
+                       $wgRequest->getVal( 'from' ),
+                       $wgRequest->getText( 'summary' ),
+                       $wgRequest->getVal( 'token' ),
+                       $wgRequest->getBool( 'bot' ),
+                       $details
+               );
+
+               switch( $result ) {
+                       case self::BLOCKED:
+                               $wgOut->blockedPage();
+                               break;
+                       case self::PERM_DENIED:
+                               $wgOut->permissionRequired( 'rollback' );
+                               break;
+                       case self::READONLY:
+                               $wgOut->readOnlyPage( $this->getContent() );
+                               break;
+                       case self::BAD_TOKEN:
+                               $wgOut->setPageTitle( wfMsg( 'rollbackfailed' ) );
+                               $wgOut->addWikiText( wfMsg( 'sessionfailure' ) );
+                               break;
+                       case self::BAD_TITLE:
+                               $wgOut->addHtml( wfMsg( 'notanarticle' ) );
+                               break;
+                       case self::ALREADY_ROLLED:
+                               $current = $details['current'];
+                               $wgOut->setPageTitle( wfMsg( 'rollbackfailed' ) );
+                               $wgOut->addWikiText(
+                                       wfMsg( 'alreadyrolled',
+                                               htmlspecialchars( $this->mTitle->getPrefixedText() ),
+                                               htmlspecialchars( $wgRequest->getVal( 'from' ) ),
+                                               htmlspecialchars( $current->getUserText() )
+                                       )
+                               );
+                               if( $current->getComment() != '' ) {
+                                       $wgOut->addHtml( wfMsg( 'editcomment',
+                                               $wgUser->getSkin()->formatComment( $current->getComment() ) ) );
+                               }
+                               break;
+                       case self::ONLY_AUTHOR:
+                               $wgOut->setPageTitle( wfMsg( 'rollbackfailed' ) );
+                               $wgOut->addHtml( wfMsg( 'cantrollback' ) );
+                               break;
+                       case self::SUCCESS:
+                               $current = $details['current'];
+                               $target = $details['target'];
+                               $wgOut->setPageTitle( wfMsg( 'actioncomplete' ) );
+                               $wgOut->setRobotPolicy( 'noindex,nofollow' );
+                               $old = $wgUser->getSkin()->userLink( $current->getUser(), $current->getUserText() )
+                                       . $wgUser->getSkin()->userToolLinks( $current->getUser(), $current->getUserText() );
+                               $new = $wgUser->getSkin()->userLink( $target->getUser(), $target->getUserText() )
+                                       . $wgUser->getSkin()->userToolLinks( $target->getUser(), $target->getUserText() );
+                               $wgOut->addHtml( wfMsgExt( 'rollback-success', array( 'parse', 'replaceafter' ), $old, $new ) );
+                               $wgOut->returnToMain( false, $this->mTitle );
+                               break;
+                       default:
+                               throw new MWException( __METHOD__ . ": Unknown return value `{$retval}`" );
+               }
 
-               $wgOut->returnToMain( false );
        }
 
 
@@ -2730,16 +2811,22 @@ class Article {
                $page = $this->mTitle->getSubjectPage();
 
                $wgOut->setPagetitle( $page->getPrefixedText() );
-               $wgOut->setSubtitle( wfMsg( 'infosubtitle' ));
-
-               # first, see if the page exists at all.
-               $exists = $page->getArticleId() != 0;
-               if( !$exists ) {
-                       if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
-                               $wgOut->addHTML(wfMsgWeirdKey ( $this->mTitle->getText() ) );
+               $wgOut->setPageTitleActionText( wfMsg( 'info_short' ) );
+               $wgOut->setSubtitle( wfMsg( 'infosubtitle' ) );
+
+               if( !$this->mTitle->exists() ) {
+                       $wgOut->addHtml( '<div class="noarticletext">' );
+                       if( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
+                               // This doesn't quite make sense; the user is asking for
+                               // information about the _page_, not the message... -- RC
+                               $wgOut->addHtml( htmlspecialchars( wfMsgWeirdKey( $this->mTitle->getText() ) ) );
                        } else {
-                               $wgOut->addHTML(wfMsg( $wgUser->isLoggedIn() ? 'noarticletext' : 'noarticletextanon' ) );
+                               $msg = $wgUser->isLoggedIn()
+                                       ? 'noarticletext'
+                                       : 'noarticletextanon';
+                               $wgOut->addHtml( wfMsgExt( $msg, 'parse' ) );
                        }
+                       $wgOut->addHtml( '</div>' );
                } else {
                        $dbr = wfGetDB( DB_SLAVE );
                        $wl_clause = array(
@@ -2988,5 +3075,3 @@ class Article {
        }
 
 }
-
-