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
{
41 protected $mTimestamp;
52 /** @var bool Was posted? */
55 /** @var bool Was submitted? */
56 protected $mSubmitted;
59 protected $mTargetObj;
67 public function __construct() {
68 parent
::__construct( 'MergeHistory', 'mergehistory' );
74 private function loadRequestParams() {
75 $request = $this->getRequest();
76 $this->mAction
= $request->getVal( 'action' );
77 $this->mTarget
= $request->getVal( 'target' );
78 $this->mDest
= $request->getVal( 'dest' );
79 $this->mSubmitted
= $request->getBool( 'submitted' );
81 $this->mTargetID
= intval( $request->getVal( 'targetID' ) );
82 $this->mDestID
= intval( $request->getVal( 'destID' ) );
83 $this->mTimestamp
= $request->getVal( 'mergepoint' );
84 if ( !preg_match( '/[0-9]{14}/', $this->mTimestamp
) ) {
85 $this->mTimestamp
= '';
87 $this->mComment
= $request->getText( 'wpComment' );
89 $this->mMerge
= $request->wasPosted()
90 && $this->getUser()->matchEditToken( $request->getVal( 'wpEditToken' ) );
93 if ( $this->mSubmitted
) {
94 $this->mTargetObj
= Title
::newFromText( $this->mTarget
);
95 $this->mDestObj
= Title
::newFromText( $this->mDest
);
97 $this->mTargetObj
= null;
98 $this->mDestObj
= null;
102 public function execute( $par ) {
103 $this->useTransactionalTimeLimit();
105 $this->checkPermissions();
106 $this->checkReadOnly();
108 $this->loadRequestParams();
111 $this->outputHeader();
113 if ( $this->mTargetID
&& $this->mDestID
&& $this->mAction
== 'submit' && $this->mMerge
) {
119 if ( !$this->mSubmitted
) {
120 $this->showMergeForm();
126 if ( !$this->mTargetObj
instanceof Title
) {
127 $errors[] = $this->msg( 'mergehistory-invalid-source' )->parseAsBlock();
128 } elseif ( !$this->mTargetObj
->exists() ) {
129 $errors[] = $this->msg( 'mergehistory-no-source',
130 wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() )
134 if ( !$this->mDestObj
instanceof Title
) {
135 $errors[] = $this->msg( 'mergehistory-invalid-destination' )->parseAsBlock();
136 } elseif ( !$this->mDestObj
->exists() ) {
137 $errors[] = $this->msg( 'mergehistory-no-destination',
138 wfEscapeWikiText( $this->mDestObj
->getPrefixedText() )
142 if ( $this->mTargetObj
&& $this->mDestObj
&& $this->mTargetObj
->equals( $this->mDestObj
) ) {
143 $errors[] = $this->msg( 'mergehistory-same-destination' )->parseAsBlock();
146 if ( count( $errors ) ) {
147 $this->showMergeForm();
148 $this->getOutput()->addHTML( implode( "\n", $errors ) );
150 $this->showHistory();
154 function showMergeForm() {
155 $out = $this->getOutput();
156 $out->addWikiMsg( 'mergehistory-header' );
159 Xml
::openElement( 'form', array(
161 'action' => wfScript() ) ) .
163 Xml
::element( 'legend', array(),
164 $this->msg( 'mergehistory-box' )->text() ) .
165 Html
::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) .
166 Html
::hidden( 'submitted', '1' ) .
167 Html
::hidden( 'mergepoint', $this->mTimestamp
) .
168 Xml
::openElement( 'table' ) .
170 <td>' . Xml
::label( $this->msg( 'mergehistory-from' )->text(), 'target' ) . '</td>
171 <td>' . Xml
::input( 'target', 30, $this->mTarget
, array( 'id' => 'target' ) ) . '</td>
173 <td>' . Xml
::label( $this->msg( 'mergehistory-into' )->text(), 'dest' ) . '</td>
174 <td>' . Xml
::input( 'dest', 30, $this->mDest
, array( 'id' => 'dest' ) ) . '</td>
176 Xml
::submitButton( $this->msg( 'mergehistory-go' )->text() ) .
178 Xml
::closeElement( 'table' ) .
183 $this->addHelpLink( 'Help:Merge history' );
186 private function showHistory() {
187 $this->showMergeForm();
189 # List all stored revisions
190 $revisions = new MergeHistoryPager(
191 $this, array(), $this->mTargetObj
, $this->mDestObj
193 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
195 $out = $this->getOutput();
196 $titleObj = $this->getPageTitle();
197 $action = $titleObj->getLocalURL( array( 'action' => 'submit' ) );
198 # Start the form here
199 $top = Xml
::openElement(
207 $out->addHTML( $top );
209 if ( $haveRevisions ) {
210 # Format the user-visible controls (comment field, submission button)
211 # in a nice little table
213 Xml
::openElement( 'fieldset' ) .
214 $this->msg( 'mergehistory-merge', $this->mTargetObj
->getPrefixedText(),
215 $this->mDestObj
->getPrefixedText() )->parse() .
216 Xml
::openElement( 'table', array( 'id' => 'mw-mergehistory-table' ) ) .
218 <td class="mw-label">' .
219 Xml
::label( $this->msg( 'mergehistory-reason' )->text(), 'wpComment' ) .
221 <td class="mw-input">' .
222 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
227 <td class="mw-submit">' .
229 $this->msg( 'mergehistory-submit' )->text(),
230 array( 'name' => 'merge', 'id' => 'mw-merge-submit' )
234 Xml
::closeElement( 'table' ) .
235 Xml
::closeElement( 'fieldset' );
237 $out->addHTML( $table );
241 '<h2 id="mw-mergehistory">' .
242 $this->msg( 'mergehistory-list' )->escaped() . "</h2>\n"
245 if ( $haveRevisions ) {
246 $out->addHTML( $revisions->getNavigationBar() );
247 $out->addHTML( '<ul>' );
248 $out->addHTML( $revisions->getBody() );
249 $out->addHTML( '</ul>' );
250 $out->addHTML( $revisions->getNavigationBar() );
252 $out->addWikiMsg( 'mergehistory-empty' );
255 # Show relevant lines from the merge log:
256 $mergeLogPage = new LogPage( 'merge' );
257 $out->addHTML( '<h2>' . $mergeLogPage->getName()->escaped() . "</h2>\n" );
258 LogEventsList
::showLogExtract( $out, 'merge', $this->mTargetObj
);
260 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
261 # Such would happen if a page was renamed after the form loaded, but before submit
262 $misc = Html
::hidden( 'targetID', $this->mTargetObj
->getArticleID() );
263 $misc .= Html
::hidden( 'destID', $this->mDestObj
->getArticleID() );
264 $misc .= Html
::hidden( 'target', $this->mTarget
);
265 $misc .= Html
::hidden( 'dest', $this->mDest
);
266 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
267 $misc .= Xml
::closeElement( 'form' );
268 $out->addHTML( $misc );
273 function formatRevisionRow( $row ) {
274 $rev = new Revision( $row );
277 $last = $this->msg( 'last' )->escaped();
279 $ts = wfTimestamp( TS_MW
, $row->rev_timestamp
);
280 $checkBox = Xml
::radio( 'mergepoint', $ts, ( $this->mTimestamp
=== $ts ) );
282 $user = $this->getUser();
284 $pageLink = Linker
::linkKnown(
286 htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) ),
288 array( 'oldid' => $rev->getId() )
290 if ( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
291 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
295 if ( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
296 $last = $this->msg( 'last' )->escaped();
297 } elseif ( isset( $this->prevId
[$row->rev_id
] ) ) {
298 $last = Linker
::linkKnown(
300 $this->msg( 'last' )->escaped(),
303 'diff' => $row->rev_id
,
304 'oldid' => $this->prevId
[$row->rev_id
]
309 $userLink = Linker
::revUserTools( $rev );
311 $size = $row->rev_len
;
312 if ( !is_null( $size ) ) {
313 $stxt = Linker
::formatRevisionSize( $size );
315 $comment = Linker
::revComment( $rev );
317 return Html
::rawElement( 'li', array(),
318 $this->msg( 'mergehistory-revisionrow' )
319 ->rawParams( $checkBox, $last, $pageLink, $userLink, $stxt, $comment )->escaped() );
323 * Actually attempt the history move
325 * @todo if all versions of page A are moved to B and then a user
326 * tries to do a reverse-merge via the "unmerge" log link, then page
327 * A will still be a redirect (as it was after the original merge),
328 * though it will have the old revisions back from before (as expected).
329 * The user may have to "undo" the redirect manually to finish the "unmerge".
330 * Maybe this should delete redirects at the target page of merges?
332 * @return bool Success
335 # Get the titles directly from the IDs, in case the target page params
336 # were spoofed. The queries are done based on the IDs, so it's best to
337 # keep it consistent...
338 $targetTitle = Title
::newFromID( $this->mTargetID
);
339 $destTitle = Title
::newFromID( $this->mDestID
);
340 if ( is_null( $targetTitle ) ||
is_null( $destTitle ) ) {
341 return false; // validate these
343 if ( $targetTitle->getArticleID() == $destTitle->getArticleID() ) {
346 # Verify that this timestamp is valid
347 # Must be older than the destination page
348 $dbw = wfGetDB( DB_MASTER
);
349 # Get timestamp into DB format
350 $this->mTimestamp
= $this->mTimestamp ?
$dbw->timestamp( $this->mTimestamp
) : '';
351 # Max timestamp should be min of destination page
352 $maxtimestamp = $dbw->selectField(
354 'MIN(rev_timestamp)',
355 array( 'rev_page' => $this->mDestID
),
358 # Destination page must exist with revisions
359 if ( !$maxtimestamp ) {
360 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
364 # Get the latest timestamp of the source
365 $lasttimestamp = $dbw->selectField(
366 array( 'page', 'revision' ),
368 array( 'page_id' => $this->mTargetID
, 'page_latest = rev_id' ),
371 # $this->mTimestamp must be older than $maxtimestamp
372 if ( $this->mTimestamp
>= $maxtimestamp ) {
373 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
377 # Get the timestamp pivot condition
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 );
385 # Check that there are not too many revisions to move
386 $limit = 5000; // avoid too much slave lag
387 $count = $dbw->selectRowCount( 'revision', '1',
388 array( 'rev_page' => $this->mTargetID
, $timewhere ),
390 array( 'LIMIT' => $limit +
1 )
392 if ( $count > $limit ) {
393 $this->getOutput()->addWikiMsg( 'mergehistory-fail-toobig' );
400 array( 'rev_page' => $this->mDestID
),
401 array( 'rev_page' => $this->mTargetID
, $timewhere ),
405 $count = $dbw->affectedRows();
406 # Make the source page a redirect if no revisions are left
407 $haveRevisions = $dbw->selectField(
410 array( 'rev_page' => $this->mTargetID
),
412 array( 'FOR UPDATE' )
414 if ( !$haveRevisions ) {
415 if ( $this->mComment
) {
416 $comment = $this->msg(
417 'mergehistory-comment',
418 $targetTitle->getPrefixedText(),
419 $destTitle->getPrefixedText(),
421 )->inContentLanguage()->text();
423 $comment = $this->msg(
424 'mergehistory-autocomment',
425 $targetTitle->getPrefixedText(),
426 $destTitle->getPrefixedText()
427 )->inContentLanguage()->text();
430 $contentHandler = ContentHandler
::getForTitle( $targetTitle );
431 $redirectContent = $contentHandler->makeRedirectContent( $destTitle );
433 if ( $redirectContent ) {
434 $redirectPage = WikiPage
::factory( $targetTitle );
435 $redirectRevision = new Revision( array(
436 'title' => $targetTitle,
437 'page' => $this->mTargetID
,
438 'comment' => $comment,
439 'content' => $redirectContent ) );
440 $redirectRevision->insertOn( $dbw );
441 $redirectPage->updateRevisionOn( $dbw, $redirectRevision );
443 # Now, we record the link from the redirect to the new title.
444 # It should have no other outgoing links...
445 $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mDestID
), __METHOD__
);
446 $dbw->insert( 'pagelinks',
448 'pl_from' => $this->mDestID
,
449 'pl_from_namespace' => $destTitle->getNamespace(),
450 'pl_namespace' => $destTitle->getNamespace(),
451 'pl_title' => $destTitle->getDBkey() ),
455 // would be nice to show a warning if we couldn't create a redirect
458 $targetTitle->invalidateCache(); // update histories
460 $destTitle->invalidateCache(); // update histories
461 # Check if this did anything
463 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
468 $logEntry = new ManualLogEntry( 'merge', 'merge' );
469 $logEntry->setPerformer( $this->getUser() );
470 $logEntry->setComment( $this->mComment
);
471 $logEntry->setTarget( $targetTitle );
472 $logEntry->setParameters( array(
473 '4::dest' => $destTitle->getPrefixedText(),
474 '5::mergepoint' => $timestampLimit
476 $logId = $logEntry->insert();
477 $logEntry->publish( $logId );
479 $targetLink = Linker
::link(
481 $targetTitle->getPrefixedText(),
483 array( 'redirect' => 'no' )
486 $this->getOutput()->addWikiMsg( $this->msg( 'mergehistory-done' )
487 ->rawParams( $targetLink )
488 ->params( $destTitle->getPrefixedText() )
489 ->numParams( $count )
492 Hooks
::run( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
497 protected function getGroupName() {
502 class MergeHistoryPager
extends ReverseChronologicalPager
{
503 /** @var SpecialMergeHistory */
509 function __construct( SpecialMergeHistory
$form, $conds, Title
$source, Title
$dest ) {
510 $this->mForm
= $form;
511 $this->mConds
= $conds;
512 $this->title
= $source;
513 $this->articleID
= $source->getArticleID();
515 $dbr = wfGetDB( DB_SLAVE
);
516 $maxtimestamp = $dbr->selectField(
518 'MIN(rev_timestamp)',
519 array( 'rev_page' => $dest->getArticleID() ),
522 $this->maxTimestamp
= $maxtimestamp;
524 parent
::__construct( $form->getContext() );
527 function getStartBody() {
528 # Do a link batch query
529 $this->mResult
->seek( 0 );
530 $batch = new LinkBatch();
531 # Give some pointers to make (last) links
532 $this->mForm
->prevId
= array();
533 foreach ( $this->mResult
as $row ) {
534 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->user_name
) );
535 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->user_name
) );
537 $rev_id = isset( $rev_id ) ?
$rev_id : $row->rev_id
;
538 if ( $rev_id > $row->rev_id
) {
539 $this->mForm
->prevId
[$rev_id] = $row->rev_id
;
540 } elseif ( $rev_id < $row->rev_id
) {
541 $this->mForm
->prevId
[$row->rev_id
] = $rev_id;
544 $rev_id = $row->rev_id
;
548 $this->mResult
->seek( 0 );
553 function formatRow( $row ) {
554 return $this->mForm
->formatRevisionRow( $row );
557 function getQueryInfo() {
558 $conds = $this->mConds
;
559 $conds['rev_page'] = $this->articleID
;
560 $conds[] = "rev_timestamp < " . $this->mDb
->addQuotes( $this->maxTimestamp
);
563 'tables' => array( 'revision', 'page', 'user' ),
564 'fields' => array_merge( Revision
::selectFields(), Revision
::selectUserFields() ),
566 'join_conds' => array(
567 'page' => Revision
::pageJoinCond(),
568 'user' => Revision
::userJoinCond() )
572 function getIndexField() {
573 return 'rev_timestamp';