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;
64 public function __construct() {
65 parent
::__construct( 'MergeHistory', 'mergehistory' );
71 private function loadRequestParams() {
72 $request = $this->getRequest();
73 $this->mAction
= $request->getVal( 'action' );
74 $this->mTarget
= $request->getVal( 'target' );
75 $this->mDest
= $request->getVal( 'dest' );
76 $this->mSubmitted
= $request->getBool( 'submitted' );
78 $this->mTargetID
= intval( $request->getVal( 'targetID' ) );
79 $this->mDestID
= intval( $request->getVal( 'destID' ) );
80 $this->mTimestamp
= $request->getVal( 'mergepoint' );
81 if ( !preg_match( '/[0-9]{14}/', $this->mTimestamp
) ) {
82 $this->mTimestamp
= '';
84 $this->mComment
= $request->getText( 'wpComment' );
86 $this->mMerge
= $request->wasPosted()
87 && $this->getUser()->matchEditToken( $request->getVal( 'wpEditToken' ) );
90 if ( $this->mSubmitted
) {
91 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
92 $this->mDestObj
= Title
::newFromURL( $this->mDest
);
94 $this->mTargetObj
= null;
95 $this->mDestObj
= null;
97 $this->preCacheMessages();
101 * As we use the same small set of messages in various methods and that
102 * they are called often, we call them once and save them in $this->message
104 function preCacheMessages() {
105 // Precache various messages
106 if ( !isset( $this->message
) ) {
107 $this->message
['last'] = $this->msg( 'last' )->escaped();
111 public function execute( $par ) {
112 $this->checkPermissions();
113 $this->checkReadOnly();
115 $this->loadRequestParams();
118 $this->outputHeader();
120 if ( $this->mTargetID
&& $this->mDestID
&& $this->mAction
== 'submit' && $this->mMerge
) {
126 if ( !$this->mSubmitted
) {
127 $this->showMergeForm();
133 if ( !$this->mTargetObj
instanceof Title
) {
134 $errors[] = $this->msg( 'mergehistory-invalid-source' )->parseAsBlock();
135 } elseif ( !$this->mTargetObj
->exists() ) {
136 $errors[] = $this->msg( 'mergehistory-no-source',
137 wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() )
141 if ( !$this->mDestObj
instanceof Title
) {
142 $errors[] = $this->msg( 'mergehistory-invalid-destination' )->parseAsBlock();
143 } elseif ( !$this->mDestObj
->exists() ) {
144 $errors[] = $this->msg( 'mergehistory-no-destination',
145 wfEscapeWikiText( $this->mDestObj
->getPrefixedText() )
149 if ( $this->mTargetObj
&& $this->mDestObj
&& $this->mTargetObj
->equals( $this->mDestObj
) ) {
150 $errors[] = $this->msg( 'mergehistory-same-destination' )->parseAsBlock();
153 if ( count( $errors ) ) {
154 $this->showMergeForm();
155 $this->getOutput()->addHTML( implode( "\n", $errors ) );
157 $this->showHistory();
161 function showMergeForm() {
162 $this->getOutput()->addWikiMsg( 'mergehistory-header' );
164 $this->getOutput()->addHTML(
165 Xml
::openElement( 'form', array(
167 'action' => wfScript() ) ) .
169 Xml
::element( 'legend', array(),
170 $this->msg( 'mergehistory-box' )->text() ) .
171 Html
::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) .
172 Html
::hidden( 'submitted', '1' ) .
173 Html
::hidden( 'mergepoint', $this->mTimestamp
) .
174 Xml
::openElement( 'table' ) .
176 <td>' . Xml
::label( $this->msg( 'mergehistory-from' )->text(), 'target' ) . '</td>
177 <td>' . Xml
::input( 'target', 30, $this->mTarget
, array( 'id' => 'target' ) ) . '</td>
179 <td>' . Xml
::label( $this->msg( 'mergehistory-into' )->text(), 'dest' ) . '</td>
180 <td>' . Xml
::input( 'dest', 30, $this->mDest
, array( 'id' => 'dest' ) ) . '</td>
182 Xml
::submitButton( $this->msg( 'mergehistory-go' )->text() ) .
184 Xml
::closeElement( 'table' ) .
190 private function showHistory() {
191 $this->showMergeForm();
193 # List all stored revisions
194 $revisions = new MergeHistoryPager(
195 $this, array(), $this->mTargetObj
, $this->mDestObj
197 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
199 $out = $this->getOutput();
200 $titleObj = $this->getPageTitle();
201 $action = $titleObj->getLocalURL( array( 'action' => 'submit' ) );
202 # Start the form here
203 $top = Xml
::openElement(
211 $out->addHTML( $top );
213 if ( $haveRevisions ) {
214 # Format the user-visible controls (comment field, submission button)
215 # in a nice little table
217 Xml
::openElement( 'fieldset' ) .
218 $this->msg( 'mergehistory-merge', $this->mTargetObj
->getPrefixedText(),
219 $this->mDestObj
->getPrefixedText() )->parse() .
220 Xml
::openElement( 'table', array( 'id' => 'mw-mergehistory-table' ) ) .
222 <td class="mw-label">' .
223 Xml
::label( $this->msg( 'mergehistory-reason' )->text(), 'wpComment' ) .
225 <td class="mw-input">' .
226 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
231 <td class="mw-submit">' .
233 $this->msg( 'mergehistory-submit' )->text(),
234 array( 'name' => 'merge', 'id' => 'mw-merge-submit' )
238 Xml
::closeElement( 'table' ) .
239 Xml
::closeElement( 'fieldset' );
241 $out->addHTML( $table );
245 '<h2 id="mw-mergehistory">' .
246 $this->msg( 'mergehistory-list' )->escaped() . "</h2>\n"
249 if ( $haveRevisions ) {
250 $out->addHTML( $revisions->getNavigationBar() );
251 $out->addHTML( '<ul>' );
252 $out->addHTML( $revisions->getBody() );
253 $out->addHTML( '</ul>' );
254 $out->addHTML( $revisions->getNavigationBar() );
256 $out->addWikiMsg( 'mergehistory-empty' );
259 # Show relevant lines from the merge log:
260 $mergeLogPage = new LogPage( 'merge' );
261 $out->addHTML( '<h2>' . $mergeLogPage->getName()->escaped() . "</h2>\n" );
262 LogEventsList
::showLogExtract( $out, 'merge', $this->mTargetObj
);
264 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
265 # Such would happen if a page was renamed after the form loaded, but before submit
266 $misc = Html
::hidden( 'targetID', $this->mTargetObj
->getArticleID() );
267 $misc .= Html
::hidden( 'destID', $this->mDestObj
->getArticleID() );
268 $misc .= Html
::hidden( 'target', $this->mTarget
);
269 $misc .= Html
::hidden( 'dest', $this->mDest
);
270 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
271 $misc .= Xml
::closeElement( 'form' );
272 $out->addHTML( $misc );
277 function formatRevisionRow( $row ) {
278 $rev = new Revision( $row );
281 $last = $this->message
['last'];
283 $ts = wfTimestamp( TS_MW
, $row->rev_timestamp
);
284 $checkBox = Xml
::radio( 'mergepoint', $ts, ( $this->mTimestamp
=== $ts ) );
286 $user = $this->getUser();
288 $pageLink = Linker
::linkKnown(
290 htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) ),
292 array( 'oldid' => $rev->getId() )
294 if ( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
295 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
299 if ( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
300 $last = $this->message
['last'];
301 } elseif ( isset( $this->prevId
[$row->rev_id
] ) ) {
302 $last = Linker
::linkKnown(
304 $this->message
['last'],
307 'diff' => $row->rev_id
,
308 'oldid' => $this->prevId
[$row->rev_id
]
313 $userLink = Linker
::revUserTools( $rev );
315 $size = $row->rev_len
;
316 if ( !is_null( $size ) ) {
317 $stxt = Linker
::formatRevisionSize( $size );
319 $comment = Linker
::revComment( $rev );
321 return Html
::rawElement( 'li', array(),
322 $this->msg( 'mergehistory-revisionrow' )
323 ->rawParams( $checkBox, $last, $pageLink, $userLink, $stxt, $comment )->escaped() );
327 * Actually attempt the history move
329 * @todo if all versions of page A are moved to B and then a user
330 * tries to do a reverse-merge via the "unmerge" log link, then page
331 * A will still be a redirect (as it was after the original merge),
332 * though it will have the old revisions back from before (as expected).
333 * The user may have to "undo" the redirect manually to finish the "unmerge".
334 * Maybe this should delete redirects at the target page of merges?
336 * @return bool Success
339 # Get the titles directly from the IDs, in case the target page params
340 # were spoofed. The queries are done based on the IDs, so it's best to
341 # keep it consistent...
342 $targetTitle = Title
::newFromID( $this->mTargetID
);
343 $destTitle = Title
::newFromID( $this->mDestID
);
344 if ( is_null( $targetTitle ) ||
is_null( $destTitle ) ) {
345 return false; // validate these
347 if ( $targetTitle->getArticleID() == $destTitle->getArticleID() ) {
350 # Verify that this timestamp is valid
351 # Must be older than the destination page
352 $dbw = wfGetDB( DB_MASTER
);
353 # Get timestamp into DB format
354 $this->mTimestamp
= $this->mTimestamp ?
$dbw->timestamp( $this->mTimestamp
) : '';
355 # Max timestamp should be min of destination page
356 $maxtimestamp = $dbw->selectField(
358 'MIN(rev_timestamp)',
359 array( 'rev_page' => $this->mDestID
),
362 # Destination page must exist with revisions
363 if ( !$maxtimestamp ) {
364 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
368 # Get the latest timestamp of the source
369 $lasttimestamp = $dbw->selectField(
370 array( 'page', 'revision' ),
372 array( 'page_id' => $this->mTargetID
, 'page_latest = rev_id' ),
375 # $this->mTimestamp must be older than $maxtimestamp
376 if ( $this->mTimestamp
>= $maxtimestamp ) {
377 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
381 # Get the timestamp pivot condition
382 if ( $this->mTimestamp
) {
383 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
384 $timestampLimit = wfTimestamp( TS_MW
, $this->mTimestamp
);
386 $timewhere = "rev_timestamp <= {$maxtimestamp}";
387 $timestampLimit = wfTimestamp( TS_MW
, $lasttimestamp );
389 # Check that there are not too many revisions to move
390 $limit = 5000; // avoid too much slave lag
391 $count = $dbw->selectRowCount( 'revision', '1',
392 array( 'rev_page' => $this->mTargetID
, $timewhere ),
394 array( 'LIMIT' => $limit +
1 )
396 if ( $count > $limit ) {
397 $this->getOutput()->addWikiMsg( 'mergehistory-fail-toobig' );
404 array( 'rev_page' => $this->mDestID
),
405 array( 'rev_page' => $this->mTargetID
, $timewhere ),
409 $count = $dbw->affectedRows();
410 # Make the source page a redirect if no revisions are left
411 $haveRevisions = $dbw->selectField(
414 array( 'rev_page' => $this->mTargetID
),
416 array( 'FOR UPDATE' )
418 if ( !$haveRevisions ) {
419 if ( $this->mComment
) {
420 $comment = $this->msg(
421 'mergehistory-comment',
422 $targetTitle->getPrefixedText(),
423 $destTitle->getPrefixedText(),
425 )->inContentLanguage()->text();
427 $comment = $this->msg(
428 'mergehistory-autocomment',
429 $targetTitle->getPrefixedText(),
430 $destTitle->getPrefixedText()
431 )->inContentLanguage()->text();
434 $contentHandler = ContentHandler
::getForTitle( $targetTitle );
435 $redirectContent = $contentHandler->makeRedirectContent( $destTitle );
437 if ( $redirectContent ) {
438 $redirectPage = WikiPage
::factory( $targetTitle );
439 $redirectRevision = new Revision( array(
440 'title' => $targetTitle,
441 'page' => $this->mTargetID
,
442 'comment' => $comment,
443 'content' => $redirectContent ) );
444 $redirectRevision->insertOn( $dbw );
445 $redirectPage->updateRevisionOn( $dbw, $redirectRevision );
447 # Now, we record the link from the redirect to the new title.
448 # It should have no other outgoing links...
449 $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mDestID
), __METHOD__
);
450 $dbw->insert( 'pagelinks',
452 'pl_from' => $this->mDestID
,
453 'pl_from_namespace' => $destTitle->getNamespace(),
454 'pl_namespace' => $destTitle->getNamespace(),
455 'pl_title' => $destTitle->getDBkey() ),
459 // would be nice to show a warning if we couldn't create a redirect
462 $targetTitle->invalidateCache(); // update histories
464 $destTitle->invalidateCache(); // update histories
465 # Check if this did anything
467 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
472 $logEntry = new ManualLogEntry( 'merge', 'merge' );
473 $logEntry->setPerformer( $this->getUser() );
474 $logEntry->setComment( $this->mComment
);
475 $logEntry->setTarget( $targetTitle );
476 $logEntry->setParameters( array(
477 '4::dest' => $destTitle->getPrefixedText(),
478 '5::mergepoint' => $timestampLimit
480 $logId = $logEntry->insert();
481 $logEntry->publish( $logId );
483 # @todo message should use redirect=no
484 $this->getOutput()->addWikiText( $this->msg( 'mergehistory-success',
485 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText() )->numParams(
488 Hooks
::run( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
493 protected function getGroupName() {
498 class MergeHistoryPager
extends ReverseChronologicalPager
{
499 /** @var IContextSource */
505 function __construct( $form, $conds, $source, $dest ) {
506 $this->mForm
= $form;
507 $this->mConds
= $conds;
508 $this->title
= $source;
509 $this->articleID
= $source->getArticleID();
511 $dbr = wfGetDB( DB_SLAVE
);
512 $maxtimestamp = $dbr->selectField(
514 'MIN(rev_timestamp)',
515 array( 'rev_page' => $dest->getArticleID() ),
518 $this->maxTimestamp
= $maxtimestamp;
520 parent
::__construct( $form->getContext() );
523 function getStartBody() {
524 # Do a link batch query
525 $this->mResult
->seek( 0 );
526 $batch = new LinkBatch();
527 # Give some pointers to make (last) links
528 $this->mForm
->prevId
= array();
529 foreach ( $this->mResult
as $row ) {
530 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->user_name
) );
531 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->user_name
) );
533 $rev_id = isset( $rev_id ) ?
$rev_id : $row->rev_id
;
534 if ( $rev_id > $row->rev_id
) {
535 $this->mForm
->prevId
[$rev_id] = $row->rev_id
;
536 } elseif ( $rev_id < $row->rev_id
) {
537 $this->mForm
->prevId
[$row->rev_id
] = $rev_id;
540 $rev_id = $row->rev_id
;
544 $this->mResult
->seek( 0 );
549 function formatRow( $row ) {
550 return $this->mForm
->formatRevisionRow( $row );
553 function getQueryInfo() {
554 $conds = $this->mConds
;
555 $conds['rev_page'] = $this->articleID
;
556 $conds[] = "rev_timestamp < " . $this->mDb
->addQuotes( $this->maxTimestamp
);
559 'tables' => array( 'revision', 'page', 'user' ),
560 'fields' => array_merge( Revision
::selectFields(), Revision
::selectUserFields() ),
562 'join_conds' => array(
563 'page' => Revision
::pageJoinCond(),
564 'user' => Revision
::userJoinCond() )
568 function getIndexField() {
569 return 'rev_timestamp';