00001 <?php
00013 function wfSpecialMergehistory( $par ) {
00014 global $wgRequest;
00015
00016 $form = new MergehistoryForm( $wgRequest, $par );
00017 $form->execute();
00018 }
00019
00025 class MergehistoryForm {
00026 var $mAction, $mTarget, $mDest, $mTimestamp, $mTargetID, $mDestID, $mComment;
00027 var $mTargetObj, $mDestObj;
00028
00029 function MergehistoryForm( $request, $par = "" ) {
00030 global $wgUser;
00031
00032 $this->mAction = $request->getVal( 'action' );
00033 $this->mTarget = $request->getVal( 'target' );
00034 $this->mDest = $request->getVal( 'dest' );
00035 $this->mSubmitted = $request->getBool( 'submitted' );
00036
00037 $this->mTargetID = intval( $request->getVal( 'targetID' ) );
00038 $this->mDestID = intval( $request->getVal( 'destID' ) );
00039 $this->mTimestamp = $request->getVal( 'mergepoint' );
00040 if( !preg_match("/[0-9]{14}/",$this->mTimestamp) ) {
00041 $this->mTimestamp = '';
00042 }
00043 $this->mComment = $request->getText( 'wpComment' );
00044
00045 $this->mMerge = $request->wasPosted() && $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
00046
00047 if( $this->mSubmitted ) {
00048 $this->mTargetObj = Title::newFromURL( $this->mTarget );
00049 $this->mDestObj = Title::newFromURL( $this->mDest );
00050 } else {
00051 $this->mTargetObj = null;
00052 $this->mDestObj = null;
00053 }
00054
00055 $this->preCacheMessages();
00056 }
00057
00062 function preCacheMessages() {
00063
00064 if( !isset( $this->message ) ) {
00065 $this->message['last'] = wfMsgExt( 'last', array( 'escape') );
00066 }
00067 }
00068
00069 function execute() {
00070 global $wgOut;
00071
00072 $wgOut->setPagetitle( wfMsgHtml( "mergehistory" ) );
00073
00074 if( $this->mTargetID && $this->mDestID && $this->mAction=="submit" && $this->mMerge ) {
00075 return $this->merge();
00076 }
00077
00078 if ( !$this->mSubmitted ) {
00079 $this->showMergeForm();
00080 return;
00081 }
00082
00083 $errors = array();
00084 if ( !$this->mTargetObj instanceof Title ) {
00085 $errors[] = wfMsgExt( 'mergehistory-invalid-source', array( 'parse' ) );
00086 } elseif( !$this->mTargetObj->exists() ) {
00087 $errors[] = wfMsgExt( 'mergehistory-no-source', array( 'parse' ),
00088 wfEscapeWikiText( $this->mTargetObj->getPrefixedText() )
00089 );
00090 }
00091
00092 if ( !$this->mDestObj instanceof Title) {
00093 $errors[] = wfMsgExt( 'mergehistory-invalid-destination', array( 'parse' ) );
00094 } elseif( !$this->mDestObj->exists() ) {
00095 $errors[] = wfMsgExt( 'mergehistory-no-destination', array( 'parse' ),
00096 wfEscapeWikiText( $this->mDestObj->getPrefixedText() )
00097 );
00098 }
00099
00100 if ( $this->mTargetObj && $this->mDestObj && $this->mTargetObj->equals( $this->mDestObj ) ) {
00101 $errors[] = wfMsgExt( 'mergehistory-same-destination', array( 'parse' ) );
00102 }
00103
00104 if ( count( $errors ) ) {
00105 $this->showMergeForm();
00106 $wgOut->addHTML( implode( "\n", $errors ) );
00107 } else {
00108 $this->showHistory();
00109 }
00110
00111 }
00112
00113 function showMergeForm() {
00114 global $wgOut, $wgScript;
00115
00116 $wgOut->addWikiMsg( 'mergehistory-header' );
00117
00118 $wgOut->addHTML(
00119 Xml::openElement( 'form', array(
00120 'method' => 'get',
00121 'action' => $wgScript ) ) .
00122 '<fieldset>' .
00123 Xml::element( 'legend', array(),
00124 wfMsg( 'mergehistory-box' ) ) .
00125 Xml::hidden( 'title',
00126 SpecialPage::getTitleFor( 'Mergehistory' )->getPrefixedDbKey() ) .
00127 Xml::hidden( 'submitted', '1' ) .
00128 Xml::hidden( 'mergepoint', $this->mTimestamp ) .
00129 Xml::openElement( 'table' ) .
00130 "<tr>
00131 <td>".Xml::label( wfMsg( 'mergehistory-from' ), 'target' )."</td>
00132 <td>".Xml::input( 'target', 30, $this->mTarget, array('id'=>'target') )."</td>
00133 </tr><tr>
00134 <td>".Xml::label( wfMsg( 'mergehistory-into' ), 'dest' )."</td>
00135 <td>".Xml::input( 'dest', 30, $this->mDest, array('id'=>'dest') )."</td>
00136 </tr><tr><td>" .
00137 Xml::submitButton( wfMsg( 'mergehistory-go' ) ) .
00138 "</td></tr>" .
00139 Xml::closeElement( 'table' ) .
00140 '</fieldset>' .
00141 '</form>' );
00142 }
00143
00144 private function showHistory() {
00145 global $wgLang, $wgUser, $wgOut;
00146
00147 $this->sk = $wgUser->getSkin();
00148
00149 $wgOut->setPagetitle( wfMsg( "mergehistory" ) );
00150
00151 $this->showMergeForm();
00152
00153 # List all stored revisions
00154 $revisions = new MergeHistoryPager( $this, array(), $this->mTargetObj, $this->mDestObj );
00155 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
00156
00157 $titleObj = SpecialPage::getTitleFor( "Mergehistory" );
00158 $action = $titleObj->getLocalURL( array( 'action' => 'submit' ) );
00159 # Start the form here
00160 $top = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'merge' ) );
00161 $wgOut->addHTML( $top );
00162
00163 if( $haveRevisions ) {
00164 # Format the user-visible controls (comment field, submission button)
00165 # in a nice little table
00166 $table =
00167 Xml::openElement( 'fieldset' ) .
00168 wfMsgExt( 'mergehistory-merge', array('parseinline'),
00169 $this->mTargetObj->getPrefixedText(), $this->mDestObj->getPrefixedText() ) .
00170 Xml::openElement( 'table', array( 'id' => 'mw-mergehistory-table' ) ) .
00171 "<tr>
00172 <td class='mw-label'>" .
00173 Xml::label( wfMsg( 'mergehistory-reason' ), 'wpComment' ) .
00174 "</td>
00175 <td class='mw-input'>" .
00176 Xml::input( 'wpComment', 50, $this->mComment, array('id' => 'wpComment') ) .
00177 "</td>
00178 </tr>
00179 <tr>
00180 <td> </td>
00181 <td class='mw-submit'>" .
00182 Xml::submitButton( wfMsg( 'mergehistory-submit' ), array( 'name' => 'merge', 'id' => 'mw-merge-submit' ) ) .
00183 "</td>
00184 </tr>" .
00185 Xml::closeElement( 'table' ) .
00186 Xml::closeElement( 'fieldset' );
00187
00188 $wgOut->addHTML( $table );
00189 }
00190
00191 $wgOut->addHTML( "<h2 id=\"mw-mergehistory\">" . wfMsgHtml( "mergehistory-list" ) . "</h2>\n" );
00192
00193 if( $haveRevisions ) {
00194 $wgOut->addHTML( $revisions->getNavigationBar() );
00195 $wgOut->addHTML( "<ul>" );
00196 $wgOut->addHTML( $revisions->getBody() );
00197 $wgOut->addHTML( "</ul>" );
00198 $wgOut->addHTML( $revisions->getNavigationBar() );
00199 } else {
00200 $wgOut->addWikiMsg( "mergehistory-empty" );
00201 }
00202
00203 # Show relevant lines from the deletion log:
00204 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'merge' ) ) . "</h2>\n" );
00205 LogEventsList::showLogExtract( $wgOut, 'merge', $this->mTargetObj->getPrefixedText() );
00206
00207 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
00208 # Such would happen if a page was renamed after the form loaded, but before submit
00209 $misc = Xml::hidden( 'targetID', $this->mTargetObj->getArticleID() );
00210 $misc .= Xml::hidden( 'destID', $this->mDestObj->getArticleID() );
00211 $misc .= Xml::hidden( 'target', $this->mTarget );
00212 $misc .= Xml::hidden( 'dest', $this->mDest );
00213 $misc .= Xml::hidden( 'wpEditToken', $wgUser->editToken() );
00214 $misc .= Xml::closeElement( 'form' );
00215 $wgOut->addHTML( $misc );
00216
00217 return true;
00218 }
00219
00220 function formatRevisionRow( $row ) {
00221 global $wgLang;
00222
00223 $rev = new Revision( $row );
00224
00225 $stxt = '';
00226 $last = $this->message['last'];
00227
00228 $ts = wfTimestamp( TS_MW, $row->rev_timestamp );
00229 $checkBox = Xml::radio( "mergepoint", $ts, false );
00230
00231 $pageLink = $this->sk->linkKnown(
00232 $rev->getTitle(),
00233 htmlspecialchars( $wgLang->timeanddate( $ts ) ),
00234 array(),
00235 array( 'oldid' => $rev->getId() )
00236 );
00237 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
00238 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
00239 }
00240
00241 # Last link
00242 if( !$rev->userCan( Revision::DELETED_TEXT ) )
00243 $last = $this->message['last'];
00244 else if( isset($this->prevId[$row->rev_id]) )
00245 $last = $this->sk->linkKnown(
00246 $rev->getTitle(),
00247 $this->message['last'],
00248 array(),
00249 array(
00250 'diff' => $row->rev_id,
00251 'oldid' => $this->prevId[$row->rev_id]
00252 )
00253 );
00254
00255 $userLink = $this->sk->revUserTools( $rev );
00256
00257 if(!is_null($size = $row->rev_len)) {
00258 $stxt = $this->sk->formatRevisionSize( $size );
00259 }
00260 $comment = $this->sk->revComment( $rev );
00261
00262 return "<li>$checkBox ($last) $pageLink . . $userLink $stxt $comment</li>";
00263 }
00264
00269 function getPageLink( $row, $titleObj, $ts, $target ) {
00270 global $wgLang;
00271
00272 if( !$this->userCan($row, Revision::DELETED_TEXT) ) {
00273 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
00274 } else {
00275 $link = $this->sk->linkKnown(
00276 $titleObj,
00277 $wgLang->timeanddate( $ts, true ),
00278 array(),
00279 array(
00280 'target' => $target,
00281 'timestamp' => $ts
00282 )
00283 );
00284 if( $this->isDeleted($row, Revision::DELETED_TEXT) )
00285 $link = '<span class="history-deleted">' . $link . '</span>';
00286 return $link;
00287 }
00288 }
00289
00290 function merge() {
00291 global $wgOut;
00292 # Get the titles directly from the IDs, in case the target page params
00293 # were spoofed. The queries are done based on the IDs, so it's best to
00294 # keep it consistent...
00295 $targetTitle = Title::newFromID( $this->mTargetID );
00296 $destTitle = Title::newFromID( $this->mDestID );
00297 if( is_null($targetTitle) || is_null($destTitle) )
00298 return false;
00299 if( $targetTitle->getArticleId() == $destTitle->getArticleId() )
00300 return false;
00301 # Verify that this timestamp is valid
00302 # Must be older than the destination page
00303 $dbw = wfGetDB( DB_MASTER );
00304 # Get timestamp into DB format
00305 $this->mTimestamp = $this->mTimestamp ? $dbw->timestamp($this->mTimestamp) : '';
00306 # Max timestamp should be min of destination page
00307 $maxtimestamp = $dbw->selectField( 'revision', 'MIN(rev_timestamp)',
00308 array('rev_page' => $this->mDestID ),
00309 __METHOD__ );
00310 # Destination page must exist with revisions
00311 if( !$maxtimestamp ) {
00312 $wgOut->addWikiMsg('mergehistory-fail');
00313 return false;
00314 }
00315 # Get the latest timestamp of the source
00316 $lasttimestamp = $dbw->selectField( array('page','revision'),
00317 'rev_timestamp',
00318 array('page_id' => $this->mTargetID, 'page_latest = rev_id' ),
00319 __METHOD__ );
00320 # $this->mTimestamp must be older than $maxtimestamp
00321 if( $this->mTimestamp >= $maxtimestamp ) {
00322 $wgOut->addWikiMsg('mergehistory-fail');
00323 return false;
00324 }
00325 # Update the revisions
00326 if( $this->mTimestamp ) {
00327 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
00328 $TimestampLimit = wfTimestamp(TS_MW,$this->mTimestamp);
00329 } else {
00330 $timewhere = "rev_timestamp <= {$maxtimestamp}";
00331 $TimestampLimit = wfTimestamp(TS_MW,$lasttimestamp);
00332 }
00333 # Do the moving...
00334 $dbw->update( 'revision',
00335 array( 'rev_page' => $this->mDestID ),
00336 array( 'rev_page' => $this->mTargetID,
00337 $timewhere ),
00338 __METHOD__ );
00339
00340 $count = $dbw->affectedRows();
00341 # Make the source page a redirect if no revisions are left
00342 $haveRevisions = $dbw->selectField( 'revision',
00343 'rev_timestamp',
00344 array( 'rev_page' => $this->mTargetID ),
00345 __METHOD__,
00346 array( 'FOR UPDATE' ) );
00347 if( !$haveRevisions ) {
00348 if( $this->mComment ) {
00349 $comment = wfMsgForContent( 'mergehistory-comment', $targetTitle->getPrefixedText(),
00350 $destTitle->getPrefixedText(), $this->mComment );
00351 } else {
00352 $comment = wfMsgForContent( 'mergehistory-autocomment', $targetTitle->getPrefixedText(),
00353 $destTitle->getPrefixedText() );
00354 }
00355 $mwRedir = MagicWord::get( 'redirect' );
00356 $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $destTitle->getPrefixedText() . "]]\n";
00357 $redirectArticle = new Article( $targetTitle );
00358 $redirectRevision = new Revision( array(
00359 'page' => $this->mTargetID,
00360 'comment' => $comment,
00361 'text' => $redirectText ) );
00362 $redirectRevision->insertOn( $dbw );
00363 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision );
00364
00365 # Now, we record the link from the redirect to the new title.
00366 # It should have no other outgoing links...
00367 $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mDestID ), __METHOD__ );
00368 $dbw->insert( 'pagelinks',
00369 array(
00370 'pl_from' => $this->mDestID,
00371 'pl_namespace' => $destTitle->getNamespace(),
00372 'pl_title' => $destTitle->getDBkey() ),
00373 __METHOD__ );
00374 } else {
00375 $targetTitle->invalidateCache();
00376 }
00377 $destTitle->invalidateCache();
00378 # Check if this did anything
00379 if( !$count ) {
00380 $wgOut->addWikiMsg('mergehistory-fail');
00381 return false;
00382 }
00383 # Update our logs
00384 $log = new LogPage( 'merge' );
00385 $log->addEntry( 'merge', $targetTitle, $this->mComment,
00386 array($destTitle->getPrefixedText(),$TimestampLimit) );
00387
00388 $wgOut->addHTML( wfMsgExt( 'mergehistory-success', array('parseinline'),
00389 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText(), $count ) );
00390
00391 wfRunHooks( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
00392
00393 return true;
00394 }
00395 }
00396
00397 class MergeHistoryPager extends ReverseChronologicalPager {
00398 public $mForm, $mConds;
00399
00400 function __construct( $form, $conds = array(), $source, $dest ) {
00401 $this->mForm = $form;
00402 $this->mConds = $conds;
00403 $this->title = $source;
00404 $this->articleID = $source->getArticleID();
00405
00406 $dbr = wfGetDB( DB_SLAVE );
00407 $maxtimestamp = $dbr->selectField( 'revision', 'MIN(rev_timestamp)',
00408 array('rev_page' => $dest->getArticleID() ),
00409 __METHOD__ );
00410 $this->maxTimestamp = $maxtimestamp;
00411
00412 parent::__construct();
00413 }
00414
00415 function getStartBody() {
00416 wfProfileIn( __METHOD__ );
00417 # Do a link batch query
00418 $this->mResult->seek( 0 );
00419 $batch = new LinkBatch();
00420 # Give some pointers to make (last) links
00421 $this->mForm->prevId = array();
00422 while( $row = $this->mResult->fetchObject() ) {
00423 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->rev_user_text ) );
00424 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->rev_user_text ) );
00425
00426 $rev_id = isset($rev_id) ? $rev_id : $row->rev_id;
00427 if( $rev_id > $row->rev_id )
00428 $this->mForm->prevId[$rev_id] = $row->rev_id;
00429 else if( $rev_id < $row->rev_id )
00430 $this->mForm->prevId[$row->rev_id] = $rev_id;
00431
00432 $rev_id = $row->rev_id;
00433 }
00434
00435 $batch->execute();
00436 $this->mResult->seek( 0 );
00437
00438 wfProfileOut( __METHOD__ );
00439 return '';
00440 }
00441
00442 function formatRow( $row ) {
00443 $block = new Block;
00444 return $this->mForm->formatRevisionRow( $row );
00445 }
00446
00447 function getQueryInfo() {
00448 $conds = $this->mConds;
00449 $conds['rev_page'] = $this->articleID;
00450 $conds[] = 'page_id = rev_page';
00451 $conds[] = "rev_timestamp < {$this->maxTimestamp}";
00452 return array(
00453 'tables' => array('revision','page'),
00454 'fields' => array( 'rev_minor_edit', 'rev_timestamp', 'rev_user', 'rev_user_text', 'rev_comment',
00455 'rev_id', 'rev_page', 'rev_parent_id', 'rev_text_id', 'rev_len', 'rev_deleted' ),
00456 'conds' => $conds
00457 );
00458 }
00459
00460 function getIndexField() {
00461 return 'rev_timestamp';
00462 }
00463 }