* Add form to RCLinked and add to sp:specialpages
[lhc/web/wiklou.git] / includes / SpecialMergeHistory.php
index 5a2233d..e0de5cc 100644 (file)
@@ -32,25 +32,25 @@ class MergehistoryForm {
                $this->mAction = $request->getVal( 'action' );
                $this->mTarget = $request->getVal( 'target' );
                $this->mDest = $request->getVal( 'dest' );
+               $this->mSubmitted = $request->getBool( 'submitted' );
                
                $this->mTargetID = intval( $request->getVal( 'targetID' ) );
                $this->mDestID = intval( $request->getVal( 'destID' ) );
                $this->mTimestamp = $request->getVal( 'mergepoint' );
+               if( !preg_match("/[0-9]{14}/",$this->mTimestamp) ) {
+                       $this->mTimestamp = '';
+               }
                $this->mComment = $request->getText( 'wpComment' );
                
                $this->mMerge = $request->wasPosted() && $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
                // target page
-               if( $this->mTarget !== "" ) {
+               if( $this->mSubmitted ) {
                        $this->mTargetObj = Title::newFromURL( $this->mTarget );
-               } else {
-                       $this->mTargetObj = NULL;
-               }
-               # Destination
-               if( $this->mDest !== "" ) {
                        $this->mDestObj = Title::newFromURL( $this->mDest );
                } else {
-                       $this->mDestObj = NULL;
-               }               
+                       $this->mTargetObj = null;
+                       $this->mDestObj = null;
+               }
                
                $this->preCacheMessages();
        }
@@ -74,18 +74,44 @@ class MergehistoryForm {
                if( $this->mTargetID && $this->mDestID && $this->mAction=="submit" && $this->mMerge ) {
                        return $this->merge();
                }
-               
-               if( is_object($this->mTargetObj) && is_object($this->mDestObj) ) {
-                       return $this->showHistory();
+
+               if ( !$this->mSubmitted ) {
+                       $this->showMergeForm();
+                       return;
                }
-               
-               return $this->showMergeForm();
+
+               $errors = array();
+               if ( !$this->mTargetObj instanceof Title ) {
+                       $errors[] = wfMsgExt( 'mergehistory-invalid-source', array( 'parse' ) );
+               } elseif( !$this->mTargetObj->exists() ) {
+                       $errors[] = wfMsgExt( 'mergehistory-no-source', array( 'parse' ),
+                               wfEscapeWikiText( $this->mTargetObj->getPrefixedText() )
+                       );
+               }
+
+               if ( !$this->mDestObj instanceof Title) {
+                       $errors[] = wfMsgExt( 'mergehistory-invalid-destination', array( 'parse' ) );
+               } elseif( !$this->mDestObj->exists() ) {
+                       $errors[] = wfMsgExt( 'mergehistory-no-destination', array( 'parse' ),
+                               wfEscapeWikiText( $this->mDestObj->getPrefixedText() )
+                       );
+               }
+
+               // TODO: warn about target = dest?
+
+               if ( count( $errors ) ) {
+                       $this->showMergeForm();
+                       $wgOut->addHTML( implode( "\n", $errors ) );
+               } else {
+                       $this->showHistory();
+               }
+
        }
 
        function showMergeForm() {
                global $wgOut, $wgScript;
                
-               $wgOut->addWikiText( wfMsg( 'mergehistory-header' ) );
+               $wgOut->addWikiMsg( 'mergehistory-header' );
                
                $wgOut->addHtml(
                        Xml::openElement( 'form', array(
@@ -96,12 +122,14 @@ class MergehistoryForm {
                                wfMsg( 'mergehistory-box' ) ) .
                        Xml::hidden( 'title',
                                SpecialPage::getTitleFor( 'Mergehistory' )->getPrefixedDbKey() ) .
+                       Xml::hidden( 'submitted', '1' ) . 
+                       Xml::hidden( 'mergepoint', $this->mTimestamp ) . 
                        Xml::openElement( 'table' ) .
                        "<tr>
-                               <td>".Xml::Label( wfMsg( 'mergehistory-from' ), 'target' )."</td>
+                               <td>".Xml::label( wfMsg( 'mergehistory-from' ), 'target' )."</td>
                                <td>".Xml::input( 'target', 30, $this->mTarget, array('id'=>'target') )."</td>
                        </tr><tr>
-                               <td>".Xml::Label( wfMsg( 'mergehistory-into' ), 'dest' )."</td>
+                               <td>".Xml::label( wfMsg( 'mergehistory-into' ), 'dest' )."</td>
                                <td>".Xml::input( 'dest', 30, $this->mDest, array('id'=>'dest') )."</td>
                        </tr><tr><td>" .
                        Xml::submitButton( wfMsg( 'mergehistory-go' ) ) .
@@ -117,9 +145,9 @@ class MergehistoryForm {
                $this->sk = $wgUser->getSkin();
                
                $wgOut->setPagetitle( wfMsg( "mergehistory" ) );
-               
+
                $this->showMergeForm();
-               
+
                # List all stored revisions
                $revisions = new MergeHistoryPager( $this, array(), $this->mTargetObj, $this->mDestObj );
                $haveRevisions = $revisions && $revisions->getNumRows() > 0;
@@ -172,19 +200,13 @@ class MergehistoryForm {
                        $wgOut->addHTML( "</ul>" );
                        $wgOut->addHTML( $revisions->getNavigationBar() );
                } else {
-                       $wgOut->addWikiText( wfMsg( "mergehistory-empty" ) );
+                       $wgOut->addWikiMsg( "mergehistory-empty" );
                }
 
                # Show relevant lines from the deletion log:
                $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'merge' ) ) . "</h2>\n" );
-               $logViewer = new LogViewer(
-                       new LogReader(
-                               new FauxRequest(
-                                       array( 'page' => $this->mTargetObj->getPrefixedText(),
-                                                  'type' => 'merge' ) ) ) );
-               $logViewer->showList( $wgOut );
+               LogEventsList::showLogExtract( $wgOut, 'merge', $this->mTargetObj->getPrefixedText() );
                
-               # Slip in the hidden controls here
                # When we submit, go by page ID to avoid some nasty but unlikely collisions.
                # Such would happen if a page was renamed after the form loaded, but before submit
                $misc = Xml::hidden( 'targetID', $this->mTargetObj->getArticleID() );
@@ -210,7 +232,7 @@ class MergehistoryForm {
                $checkBox = wfRadio( "mergepoint", $ts, false );
                
                $pageLink = $this->sk->makeKnownLinkObj( $rev->getTitle(), 
-                       $wgLang->timeanddate( $ts ), 'oldid=' . $rev->getID() );
+                       htmlspecialchars( $wgLang->timeanddate( $ts ) ), 'oldid=' . $rev->getID() );
                if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
                        $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
                }
@@ -220,7 +242,7 @@ class MergehistoryForm {
                        $last = $this->message['last'];
                else if( isset($this->prevId[$row->rev_id]) )
                        $last = $this->sk->makeKnownLinkObj( $rev->getTitle(), $this->message['last'], 
-                               "&diff=" . $row->rev_id . "&oldid=" . $this->prevId[$row->rev_id] );
+                               "diff=" . $row->rev_id . "&oldid=" . $this->prevId[$row->rev_id] );
                
                $userLink = $this->sk->revUserTools( $rev );
                
@@ -262,30 +284,30 @@ class MergehistoryForm {
                $destTitle = Title::newFromID( $this->mDestID );
                if( is_null($targetTitle) || is_null($destTitle) )
                        return false; // validate these
+               if( $targetTitle->getArticleId() == $destTitle->getArticleId() )
+                       return false;
                # Verify that this timestamp is valid
                # Must be older than the destination page
                $dbw = wfGetDB( DB_MASTER );
                # Get timestamp into DB format
                $this->mTimestamp = $this->mTimestamp ? $dbw->timestamp($this->mTimestamp) : '';
-               
+               # Max timestamp should be min of destination page
                $maxtimestamp = $dbw->selectField( 'revision', 'MIN(rev_timestamp)',
                        array('rev_page' => $this->mDestID ),
                        __METHOD__ );
                # Destination page must exist with revisions
                if( !$maxtimestamp ) {
-                       $wgOut->addWikiText( wfMsg('mergehistory-fail') );
+                       $wgOut->addWikiMsg('mergehistory-fail');
                        return false;
                }
-               # Leave the latest version no matter what
-               $lasttime = $dbw->selectField( array('page','revision'), 
+               # Get the latest timestamp of the source
+               $lasttimestamp = $dbw->selectField( array('page','revision'), 
                        'rev_timestamp',
                        array('page_id' => $this->mTargetID, 'page_latest = rev_id' ),
                        __METHOD__ );
-               # Take the most restrictive of the twain
-               $maxtimestamp = ($lasttime < $maxtimestamp) ? $lasttime : $maxtimestamp;
-               // $this->mTimestamp must be less than $maxtimestamp
+               # $this->mTimestamp must be older than $maxtimestamp
                if( $this->mTimestamp >= $maxtimestamp ) {
-                       $wgOut->addHtml( wfMsg('mergehistory-fail') );
+                       $wgOut->addWikiMsg('mergehistory-fail');
                        return false;
                }
                # Update the revisions
@@ -293,18 +315,57 @@ class MergehistoryForm {
                        $timewhere = "rev_timestamp <= {$this->mTimestamp}";
                        $TimestampLimit = wfTimestamp(TS_MW,$this->mTimestamp);
                } else {
-                       $timewhere = "rev_timestamp < {$maxtimestamp}";
-                       $TimestampLimit = wfTimestamp(TS_MW,$maxtimestamp);
+                       $timewhere = "rev_timestamp <= {$maxtimestamp}";
+                       $TimestampLimit = wfTimestamp(TS_MW,$lasttimestamp);
                }
-               
+               # Do the moving...
                $dbw->update( 'revision',
                        array( 'rev_page' => $this->mDestID ),
                        array( 'rev_page' => $this->mTargetID,
                                $timewhere ),
                        __METHOD__ );
+               
+               $count = $dbw->affectedRows();
+               # Make the source page a redirect if no revisions are left
+               $haveRevisions = $dbw->selectField( 'revision', 
+                       'rev_timestamp',
+                       array( 'rev_page' => $this->mTargetID  ),
+                       __METHOD__,
+                       array( 'FOR UPDATE' ) );
+               if( !$haveRevisions ) {
+                       if( $this->mComment ) {
+                               $comment = wfMsgForContent( 'mergehistory-comment', $targetTitle->getPrefixedText(), 
+                                       $destTitle->getPrefixedText(), $this->mComment );
+                       } else {
+                               $comment = wfMsgForContent( 'mergehistory-autocomment', $targetTitle->getPrefixedText(), 
+                                       $destTitle->getPrefixedText() );
+                       }
+                       $mwRedir = MagicWord::get( 'redirect' );
+                       $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $destTitle->getPrefixedText() . "]]\n";
+                       $redirectArticle = new Article( $targetTitle );
+                       $redirectRevision = new Revision( array(
+                               'page'    => $this->mTargetID,
+                               'comment' => $comment,
+                               'text'    => $redirectText ) );
+                       $redirectRevision->insertOn( $dbw );
+                       $redirectArticle->updateRevisionOn( $dbw, $redirectRevision );
+
+                       # Now, we record the link from the redirect to the new title.
+                       # It should have no other outgoing links...
+                       $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mDestID ), __METHOD__ );
+                       $dbw->insert( 'pagelinks',
+                               array(
+                                       'pl_from'      => $this->mDestID,
+                                       'pl_namespace' => $destTitle->getNamespace(),
+                                       'pl_title'     => $destTitle->getDBkey() ),
+                               __METHOD__ );
+               } else {
+                       $targetTitle->invalidateCache(); // update histories
+               }
+               $destTitle->invalidateCache(); // update histories
                # Check if this did anything
-               if( !$count = $dbw->affectedRows() ) {
-                       $wgOut->addHtml( wfMsg('mergehistory-fail') );
+               if( !$count ) {
+                       $wgOut->addWikiMsg('mergehistory-fail');
                        return false;
                }
                # Update our logs
@@ -324,15 +385,15 @@ class MergehistoryForm {
 class MergeHistoryPager extends ReverseChronologicalPager {
        public $mForm, $mConds;
 
-       function __construct( $form, $conds = array(), $title, $title2 ) {
+       function __construct( $form, $conds = array(), $source, $dest ) {
                $this->mForm = $form;
                $this->mConds = $conds;
-               $this->title = $title;
-               $this->articleID = $title->getArticleID();
+               $this->title = $source;
+               $this->articleID = $source->getArticleID();
                
                $dbr = wfGetDB( DB_SLAVE );
                $maxtimestamp = $dbr->selectField( 'revision', 'MIN(rev_timestamp)',
-                       array('rev_page' => $title2->getArticleID() ),
+                       array('rev_page' => $dest->getArticleID() ),
                        __METHOD__ );
                $this->maxTimestamp = $maxtimestamp; 
                
@@ -375,9 +436,6 @@ class MergeHistoryPager extends ReverseChronologicalPager {
                $conds = $this->mConds;
                $conds['rev_page'] = $this->articleID;
                $conds[] = "rev_timestamp < {$this->maxTimestamp}";
-               # Skip the latest one, as that could cause problems
-               if( $page = $this->title->getLatestRevID() )
-                       $conds[] = "rev_id != {$page}";
                
                return array(
                        'tables' => array('revision'),