3 * Implements Special:MergeHistory
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
25 * Special page allowing users with the appropriate permissions to
26 * merge article histories, with some restrictions
28 * @ingroup SpecialPage
30 class SpecialMergeHistory
extends SpecialPage
{
31 var $mAction, $mTarget, $mDest, $mTimestamp, $mTargetID, $mDestID, $mComment;
36 var $mTargetObj, $mDestObj;
38 public function __construct() {
39 parent
::__construct( 'MergeHistory', 'mergehistory' );
43 * @param $request WebRequest
46 private function loadRequestParams( $request ) {
49 $this->mAction
= $request->getVal( 'action' );
50 $this->mTarget
= $request->getVal( 'target' );
51 $this->mDest
= $request->getVal( 'dest' );
52 $this->mSubmitted
= $request->getBool( 'submitted' );
54 $this->mTargetID
= intval( $request->getVal( 'targetID' ) );
55 $this->mDestID
= intval( $request->getVal( 'destID' ) );
56 $this->mTimestamp
= $request->getVal( 'mergepoint' );
57 if( !preg_match( '/[0-9]{14}/', $this->mTimestamp
) ) {
58 $this->mTimestamp
= '';
60 $this->mComment
= $request->getText( 'wpComment' );
62 $this->mMerge
= $request->wasPosted() && $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
64 if( $this->mSubmitted
) {
65 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
66 $this->mDestObj
= Title
::newFromURL( $this->mDest
);
68 $this->mTargetObj
= null;
69 $this->mDestObj
= null;
71 $this->preCacheMessages();
75 * As we use the same small set of messages in various methods and that
76 * they are called often, we call them once and save them in $this->message
78 function preCacheMessages() {
79 // Precache various messages
80 if( !isset( $this->message
) ) {
81 $this->message
['last'] = wfMsgExt( 'last', array( 'escape' ) );
85 public function execute( $par ) {
86 global $wgOut, $wgRequest, $wgUser;
89 $wgOut->readOnlyPage();
93 if( !$this->userCanExecute( $wgUser ) ) {
94 $this->displayRestrictionError();
98 $this->loadRequestParams( $wgRequest );
101 $this->outputHeader();
103 if( $this->mTargetID
&& $this->mDestID
&& $this->mAction
== 'submit' && $this->mMerge
) {
104 return $this->merge();
107 if ( !$this->mSubmitted
) {
108 $this->showMergeForm();
113 if ( !$this->mTargetObj
instanceof Title
) {
114 $errors[] = wfMsgExt( 'mergehistory-invalid-source', array( 'parse' ) );
115 } elseif( !$this->mTargetObj
->exists() ) {
116 $errors[] = wfMsgExt( 'mergehistory-no-source', array( 'parse' ),
117 wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() )
121 if ( !$this->mDestObj
instanceof Title
) {
122 $errors[] = wfMsgExt( 'mergehistory-invalid-destination', array( 'parse' ) );
123 } elseif( !$this->mDestObj
->exists() ) {
124 $errors[] = wfMsgExt( 'mergehistory-no-destination', array( 'parse' ),
125 wfEscapeWikiText( $this->mDestObj
->getPrefixedText() )
129 if ( $this->mTargetObj
&& $this->mDestObj
&& $this->mTargetObj
->equals( $this->mDestObj
) ) {
130 $errors[] = wfMsgExt( 'mergehistory-same-destination', array( 'parse' ) );
133 if ( count( $errors ) ) {
134 $this->showMergeForm();
135 $wgOut->addHTML( implode( "\n", $errors ) );
137 $this->showHistory();
142 function showMergeForm() {
143 global $wgOut, $wgScript;
145 $wgOut->addWikiMsg( 'mergehistory-header' );
148 Xml
::openElement( 'form', array(
150 'action' => $wgScript ) ) .
152 Xml
::element( 'legend', array(),
153 wfMsg( 'mergehistory-box' ) ) .
154 Html
::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
155 Html
::hidden( 'submitted', '1' ) .
156 Html
::hidden( 'mergepoint', $this->mTimestamp
) .
157 Xml
::openElement( 'table' ) .
159 <td>' . Xml
::label( wfMsg( 'mergehistory-from' ), 'target' ) . '</td>
160 <td>' . Xml
::input( 'target', 30, $this->mTarget
, array( 'id' => 'target' ) ) . '</td>
162 <td>' . Xml
::label( wfMsg( 'mergehistory-into' ), 'dest' ) . '</td>
163 <td>' . Xml
::input( 'dest', 30, $this->mDest
, array( 'id' => 'dest' ) ) . '</td>
165 Xml
::submitButton( wfMsg( 'mergehistory-go' ) ) .
167 Xml
::closeElement( 'table' ) .
173 private function showHistory() {
174 global $wgUser, $wgOut;
176 $this->sk
= $this->getSkin();
178 $wgOut->setPageTitle( wfMsg( 'mergehistory' ) );
180 $this->showMergeForm();
182 # List all stored revisions
183 $revisions = new MergeHistoryPager(
184 $this, array(), $this->mTargetObj
, $this->mDestObj
186 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
188 $titleObj = $this->getTitle();
189 $action = $titleObj->getLocalURL( array( 'action' => 'submit' ) );
190 # Start the form here
191 $top = Xml
::openElement(
199 $wgOut->addHTML( $top );
201 if( $haveRevisions ) {
202 # Format the user-visible controls (comment field, submission button)
203 # in a nice little table
205 Xml
::openElement( 'fieldset' ) .
206 wfMsgExt( 'mergehistory-merge', array( 'parseinline' ),
207 $this->mTargetObj
->getPrefixedText(), $this->mDestObj
->getPrefixedText() ) .
208 Xml
::openElement( 'table', array( 'id' => 'mw-mergehistory-table' ) ) .
210 <td class="mw-label">' .
211 Xml
::label( wfMsg( 'mergehistory-reason' ), 'wpComment' ) .
213 <td class="mw-input">' .
214 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
219 <td class="mw-submit">' .
220 Xml
::submitButton( wfMsg( 'mergehistory-submit' ), array( 'name' => 'merge', 'id' => 'mw-merge-submit' ) ) .
223 Xml
::closeElement( 'table' ) .
224 Xml
::closeElement( 'fieldset' );
226 $wgOut->addHTML( $table );
230 '<h2 id="mw-mergehistory">' .
231 wfMsgHtml( 'mergehistory-list' ) . "</h2>\n"
234 if( $haveRevisions ) {
235 $wgOut->addHTML( $revisions->getNavigationBar() );
236 $wgOut->addHTML( '<ul>' );
237 $wgOut->addHTML( $revisions->getBody() );
238 $wgOut->addHTML( '</ul>' );
239 $wgOut->addHTML( $revisions->getNavigationBar() );
241 $wgOut->addWikiMsg( 'mergehistory-empty' );
244 # Show relevant lines from the deletion log:
245 $wgOut->addHTML( '<h2>' . htmlspecialchars( LogPage
::logName( 'merge' ) ) . "</h2>\n" );
246 LogEventsList
::showLogExtract( $wgOut, 'merge', $this->mTargetObj
->getPrefixedText() );
248 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
249 # Such would happen if a page was renamed after the form loaded, but before submit
250 $misc = Html
::hidden( 'targetID', $this->mTargetObj
->getArticleID() );
251 $misc .= Html
::hidden( 'destID', $this->mDestObj
->getArticleID() );
252 $misc .= Html
::hidden( 'target', $this->mTarget
);
253 $misc .= Html
::hidden( 'dest', $this->mDest
);
254 $misc .= Html
::hidden( 'wpEditToken', $wgUser->editToken() );
255 $misc .= Xml
::closeElement( 'form' );
256 $wgOut->addHTML( $misc );
261 function formatRevisionRow( $row ) {
264 $rev = new Revision( $row );
267 $last = $this->message
['last'];
269 $ts = wfTimestamp( TS_MW
, $row->rev_timestamp
);
270 $checkBox = Xml
::radio( 'mergepoint', $ts, false );
272 $pageLink = $this->sk
->linkKnown(
274 htmlspecialchars( $wgLang->timeanddate( $ts ) ),
276 array( 'oldid' => $rev->getId() )
278 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
279 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
283 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
284 $last = $this->message
['last'];
285 } elseif( isset( $this->prevId
[$row->rev_id
] ) ) {
286 $last = $this->sk
->linkKnown(
288 $this->message
['last'],
291 'diff' => $row->rev_id
,
292 'oldid' => $this->prevId
[$row->rev_id
]
297 $userLink = $this->sk
->revUserTools( $rev );
299 $size = $row->rev_len
;
300 if( !is_null( $size ) ) {
301 $stxt = $this->sk
->formatRevisionSize( $size );
303 $comment = $this->sk
->revComment( $rev );
305 return "<li>$checkBox ($last) $pageLink . . $userLink $stxt $comment</li>";
309 * Fetch revision text link if it's available to all users
312 function getPageLink( $row, $titleObj, $ts, $target ) {
315 if( !$this->userCan( $row, Revision
::DELETED_TEXT
) ) {
316 return '<span class="history-deleted">' .
317 $wgLang->timeanddate( $ts, true ) . '</span>';
319 $link = $this->sk
->linkKnown(
321 $wgLang->timeanddate( $ts, true ),
328 if( $this->isDeleted( $row, Revision
::DELETED_TEXT
) ) {
329 $link = '<span class="history-deleted">' . $link . '</span>';
337 # Get the titles directly from the IDs, in case the target page params
338 # were spoofed. The queries are done based on the IDs, so it's best to
339 # keep it consistent...
340 $targetTitle = Title
::newFromID( $this->mTargetID
);
341 $destTitle = Title
::newFromID( $this->mDestID
);
342 if( is_null( $targetTitle ) ||
is_null( $destTitle ) ) {
343 return false; // validate these
345 if( $targetTitle->getArticleId() == $destTitle->getArticleId() ) {
348 # Verify that this timestamp is valid
349 # Must be older than the destination page
350 $dbw = wfGetDB( DB_MASTER
);
351 # Get timestamp into DB format
352 $this->mTimestamp
= $this->mTimestamp ?
$dbw->timestamp( $this->mTimestamp
) : '';
353 # Max timestamp should be min of destination page
354 $maxtimestamp = $dbw->selectField(
356 'MIN(rev_timestamp)',
357 array( 'rev_page' => $this->mDestID
),
360 # Destination page must exist with revisions
361 if( !$maxtimestamp ) {
362 $wgOut->addWikiMsg( 'mergehistory-fail' );
365 # Get the latest timestamp of the source
366 $lasttimestamp = $dbw->selectField(
367 array( 'page', 'revision' ),
369 array( 'page_id' => $this->mTargetID
, 'page_latest = rev_id' ),
372 # $this->mTimestamp must be older than $maxtimestamp
373 if( $this->mTimestamp
>= $maxtimestamp ) {
374 $wgOut->addWikiMsg( 'mergehistory-fail' );
377 # Update the revisions
378 if( $this->mTimestamp
) {
379 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
380 $timestampLimit = wfTimestamp( TS_MW
, $this->mTimestamp
);
382 $timewhere = "rev_timestamp <= {$maxtimestamp}";
383 $timestampLimit = wfTimestamp( TS_MW
, $lasttimestamp );
388 array( 'rev_page' => $this->mDestID
),
389 array( 'rev_page' => $this->mTargetID
, $timewhere ),
393 $count = $dbw->affectedRows();
394 # Make the source page a redirect if no revisions are left
395 $haveRevisions = $dbw->selectField(
398 array( 'rev_page' => $this->mTargetID
),
400 array( 'FOR UPDATE' )
402 if( !$haveRevisions ) {
403 if( $this->mComment
) {
404 $comment = wfMsgForContent(
405 'mergehistory-comment',
406 $targetTitle->getPrefixedText(),
407 $destTitle->getPrefixedText(),
411 $comment = wfMsgForContent(
412 'mergehistory-autocomment',
413 $targetTitle->getPrefixedText(),
414 $destTitle->getPrefixedText()
417 $mwRedir = MagicWord
::get( 'redirect' );
418 $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $destTitle->getPrefixedText() . "]]\n";
419 $redirectArticle = new Article( $targetTitle );
420 $redirectRevision = new Revision( array(
421 'page' => $this->mTargetID
,
422 'comment' => $comment,
423 'text' => $redirectText ) );
424 $redirectRevision->insertOn( $dbw );
425 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision );
427 # Now, we record the link from the redirect to the new title.
428 # It should have no other outgoing links...
429 $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mDestID
), __METHOD__
);
430 $dbw->insert( 'pagelinks',
432 'pl_from' => $this->mDestID
,
433 'pl_namespace' => $destTitle->getNamespace(),
434 'pl_title' => $destTitle->getDBkey() ),
438 $targetTitle->invalidateCache(); // update histories
440 $destTitle->invalidateCache(); // update histories
441 # Check if this did anything
443 $wgOut->addWikiMsg( 'mergehistory-fail' );
447 $log = new LogPage( 'merge' );
449 'merge', $targetTitle, $this->mComment
,
450 array( $destTitle->getPrefixedText(), $timestampLimit )
454 wfMsgExt( 'mergehistory-success', array('parseinline'),
455 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText(), $count ) );
457 wfRunHooks( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
463 class MergeHistoryPager
extends ReverseChronologicalPager
{
464 public $mForm, $mConds;
466 function __construct( $form, $conds = array(), $source, $dest ) {
467 $this->mForm
= $form;
468 $this->mConds
= $conds;
469 $this->title
= $source;
470 $this->articleID
= $source->getArticleID();
472 $dbr = wfGetDB( DB_SLAVE
);
473 $maxtimestamp = $dbr->selectField(
475 'MIN(rev_timestamp)',
476 array( 'rev_page' => $dest->getArticleID() ),
479 $this->maxTimestamp
= $maxtimestamp;
481 parent
::__construct();
484 function getTitle() {
485 return SpecialPage
::getTitleFor( 'Contributions' );
488 function getStartBody() {
489 wfProfileIn( __METHOD__
);
490 # Do a link batch query
491 $this->mResult
->seek( 0 );
492 $batch = new LinkBatch();
493 # Give some pointers to make (last) links
494 $this->mForm
->prevId
= array();
495 foreach ( $this->mResult
as $row ) {
496 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->rev_user_text
) );
497 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->rev_user_text
) );
499 $rev_id = isset( $rev_id ) ?
$rev_id : $row->rev_id
;
500 if( $rev_id > $row->rev_id
) {
501 $this->mForm
->prevId
[$rev_id] = $row->rev_id
;
502 } elseif( $rev_id < $row->rev_id
) {
503 $this->mForm
->prevId
[$row->rev_id
] = $rev_id;
506 $rev_id = $row->rev_id
;
510 $this->mResult
->seek( 0 );
512 wfProfileOut( __METHOD__
);
516 function formatRow( $row ) {
517 return $this->mForm
->formatRevisionRow( $row );
520 function getQueryInfo() {
521 $conds = $this->mConds
;
522 $conds['rev_page'] = $this->articleID
;
523 $conds[] = 'page_id = rev_page';
524 $conds[] = "rev_timestamp < {$this->maxTimestamp}";
526 'tables' => array( 'revision', 'page' ),
528 'rev_minor_edit', 'rev_timestamp', 'rev_user', 'rev_user_text',
529 'rev_comment', 'rev_id', 'rev_page', 'rev_parent_id',
530 'rev_text_id', 'rev_len', 'rev_deleted'
536 function getIndexField() {
537 return 'rev_timestamp';