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, $wgUser;
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( "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 $wgUser, $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->makeKnownLinkObj( $rev->getTitle(),
00232 htmlspecialchars( $wgLang->timeanddate( $ts ) ), 'oldid=' . $rev->getId() );
00233 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
00234 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
00235 }
00236
00237 # Last link
00238 if( !$rev->userCan( Revision::DELETED_TEXT ) )
00239 $last = $this->message['last'];
00240 else if( isset($this->prevId[$row->rev_id]) )
00241 $last = $this->sk->makeKnownLinkObj( $rev->getTitle(), $this->message['last'],
00242 "diff=" . $row->rev_id . "&oldid=" . $this->prevId[$row->rev_id] );
00243
00244 $userLink = $this->sk->revUserTools( $rev );
00245
00246 if(!is_null($size = $row->rev_len)) {
00247 $stxt = $this->sk->formatRevisionSize( $size );
00248 }
00249 $comment = $this->sk->revComment( $rev );
00250
00251 return "<li>$checkBox ($last) $pageLink . . $userLink $stxt $comment</li>";
00252 }
00253
00258 function getPageLink( $row, $titleObj, $ts, $target ) {
00259 global $wgLang;
00260
00261 if( !$this->userCan($row, Revision::DELETED_TEXT) ) {
00262 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
00263 } else {
00264 $link = $this->sk->makeKnownLinkObj( $titleObj,
00265 $wgLang->timeanddate( $ts, true ), "target=$target×tamp=$ts" );
00266 if( $this->isDeleted($row, Revision::DELETED_TEXT) )
00267 $link = '<span class="history-deleted">' . $link . '</span>';
00268 return $link;
00269 }
00270 }
00271
00272 function merge() {
00273 global $wgOut, $wgUser;
00274 # Get the titles directly from the IDs, in case the target page params
00275 # were spoofed. The queries are done based on the IDs, so it's best to
00276 # keep it consistent...
00277 $targetTitle = Title::newFromID( $this->mTargetID );
00278 $destTitle = Title::newFromID( $this->mDestID );
00279 if( is_null($targetTitle) || is_null($destTitle) )
00280 return false;
00281 if( $targetTitle->getArticleId() == $destTitle->getArticleId() )
00282 return false;
00283 # Verify that this timestamp is valid
00284 # Must be older than the destination page
00285 $dbw = wfGetDB( DB_MASTER );
00286 # Get timestamp into DB format
00287 $this->mTimestamp = $this->mTimestamp ? $dbw->timestamp($this->mTimestamp) : '';
00288 # Max timestamp should be min of destination page
00289 $maxtimestamp = $dbw->selectField( 'revision', 'MIN(rev_timestamp)',
00290 array('rev_page' => $this->mDestID ),
00291 __METHOD__ );
00292 # Destination page must exist with revisions
00293 if( !$maxtimestamp ) {
00294 $wgOut->addWikiMsg('mergehistory-fail');
00295 return false;
00296 }
00297 # Get the latest timestamp of the source
00298 $lasttimestamp = $dbw->selectField( array('page','revision'),
00299 'rev_timestamp',
00300 array('page_id' => $this->mTargetID, 'page_latest = rev_id' ),
00301 __METHOD__ );
00302 # $this->mTimestamp must be older than $maxtimestamp
00303 if( $this->mTimestamp >= $maxtimestamp ) {
00304 $wgOut->addWikiMsg('mergehistory-fail');
00305 return false;
00306 }
00307 # Update the revisions
00308 if( $this->mTimestamp ) {
00309 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
00310 $TimestampLimit = wfTimestamp(TS_MW,$this->mTimestamp);
00311 } else {
00312 $timewhere = "rev_timestamp <= {$maxtimestamp}";
00313 $TimestampLimit = wfTimestamp(TS_MW,$lasttimestamp);
00314 }
00315 # Do the moving...
00316 $dbw->update( 'revision',
00317 array( 'rev_page' => $this->mDestID ),
00318 array( 'rev_page' => $this->mTargetID,
00319 $timewhere ),
00320 __METHOD__ );
00321
00322 $count = $dbw->affectedRows();
00323 # Make the source page a redirect if no revisions are left
00324 $haveRevisions = $dbw->selectField( 'revision',
00325 'rev_timestamp',
00326 array( 'rev_page' => $this->mTargetID ),
00327 __METHOD__,
00328 array( 'FOR UPDATE' ) );
00329 if( !$haveRevisions ) {
00330 if( $this->mComment ) {
00331 $comment = wfMsgForContent( 'mergehistory-comment', $targetTitle->getPrefixedText(),
00332 $destTitle->getPrefixedText(), $this->mComment );
00333 } else {
00334 $comment = wfMsgForContent( 'mergehistory-autocomment', $targetTitle->getPrefixedText(),
00335 $destTitle->getPrefixedText() );
00336 }
00337 $mwRedir = MagicWord::get( 'redirect' );
00338 $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $destTitle->getPrefixedText() . "]]\n";
00339 $redirectArticle = new Article( $targetTitle );
00340 $redirectRevision = new Revision( array(
00341 'page' => $this->mTargetID,
00342 'comment' => $comment,
00343 'text' => $redirectText ) );
00344 $redirectRevision->insertOn( $dbw );
00345 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision );
00346
00347 # Now, we record the link from the redirect to the new title.
00348 # It should have no other outgoing links...
00349 $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mDestID ), __METHOD__ );
00350 $dbw->insert( 'pagelinks',
00351 array(
00352 'pl_from' => $this->mDestID,
00353 'pl_namespace' => $destTitle->getNamespace(),
00354 'pl_title' => $destTitle->getDBkey() ),
00355 __METHOD__ );
00356 } else {
00357 $targetTitle->invalidateCache();
00358 }
00359 $destTitle->invalidateCache();
00360 # Check if this did anything
00361 if( !$count ) {
00362 $wgOut->addWikiMsg('mergehistory-fail');
00363 return false;
00364 }
00365 # Update our logs
00366 $log = new LogPage( 'merge' );
00367 $log->addEntry( 'merge', $targetTitle, $this->mComment,
00368 array($destTitle->getPrefixedText(),$TimestampLimit) );
00369
00370 $wgOut->addHTML( wfMsgExt( 'mergehistory-success', array('parseinline'),
00371 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText(), $count ) );
00372
00373 wfRunHooks( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
00374
00375 return true;
00376 }
00377 }
00378
00379 class MergeHistoryPager extends ReverseChronologicalPager {
00380 public $mForm, $mConds;
00381
00382 function __construct( $form, $conds = array(), $source, $dest ) {
00383 $this->mForm = $form;
00384 $this->mConds = $conds;
00385 $this->title = $source;
00386 $this->articleID = $source->getArticleID();
00387
00388 $dbr = wfGetDB( DB_SLAVE );
00389 $maxtimestamp = $dbr->selectField( 'revision', 'MIN(rev_timestamp)',
00390 array('rev_page' => $dest->getArticleID() ),
00391 __METHOD__ );
00392 $this->maxTimestamp = $maxtimestamp;
00393
00394 parent::__construct();
00395 }
00396
00397 function getStartBody() {
00398 wfProfileIn( __METHOD__ );
00399 # Do a link batch query
00400 $this->mResult->seek( 0 );
00401 $batch = new LinkBatch();
00402 # Give some pointers to make (last) links
00403 $this->mForm->prevId = array();
00404 while( $row = $this->mResult->fetchObject() ) {
00405 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->rev_user_text ) );
00406 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->rev_user_text ) );
00407
00408 $rev_id = isset($rev_id) ? $rev_id : $row->rev_id;
00409 if( $rev_id > $row->rev_id )
00410 $this->mForm->prevId[$rev_id] = $row->rev_id;
00411 else if( $rev_id < $row->rev_id )
00412 $this->mForm->prevId[$row->rev_id] = $rev_id;
00413
00414 $rev_id = $row->rev_id;
00415 }
00416
00417 $batch->execute();
00418 $this->mResult->seek( 0 );
00419
00420 wfProfileOut( __METHOD__ );
00421 return '';
00422 }
00423
00424 function formatRow( $row ) {
00425 $block = new Block;
00426 return $this->mForm->formatRevisionRow( $row );
00427 }
00428
00429 function getQueryInfo() {
00430 $conds = $this->mConds;
00431 $conds['rev_page'] = $this->articleID;
00432 $conds[] = 'page_id = rev_page';
00433 $conds[] = "rev_timestamp < {$this->maxTimestamp}";
00434 return array(
00435 'tables' => array('revision','page'),
00436 'fields' => array( 'rev_minor_edit', 'rev_timestamp', 'rev_user', 'rev_user_text', 'rev_comment',
00437 'rev_id', 'rev_page', 'rev_parent_id', 'rev_text_id', 'rev_len', 'rev_deleted' ),
00438 'conds' => $conds
00439 );
00440 }
00441
00442 function getIndexField() {
00443 return 'rev_timestamp';
00444 }
00445 }