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 $out->addHTML( implode( "\n", $errors ) );
157 $this->showHistory();
161 function showMergeForm() {
162 $out = $this->getOutput();
163 $out->addWikiMsg( 'mergehistory-header' );
166 Xml
::openElement( 'form', array(
168 'action' => wfScript() ) ) .
170 Xml
::element( 'legend', array(),
171 $this->msg( 'mergehistory-box' )->text() ) .
172 Html
::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) .
173 Html
::hidden( 'submitted', '1' ) .
174 Html
::hidden( 'mergepoint', $this->mTimestamp
) .
175 Xml
::openElement( 'table' ) .
177 <td>' . Xml
::label( $this->msg( 'mergehistory-from' )->text(), 'target' ) . '</td>
178 <td>' . Xml
::input( 'target', 30, $this->mTarget
, array( 'id' => 'target' ) ) . '</td>
180 <td>' . Xml
::label( $this->msg( 'mergehistory-into' )->text(), 'dest' ) . '</td>
181 <td>' . Xml
::input( 'dest', 30, $this->mDest
, array( 'id' => 'dest' ) ) . '</td>
183 Xml
::submitButton( $this->msg( 'mergehistory-go' )->text() ) .
185 Xml
::closeElement( 'table' ) .
190 $out->addHelpLink( 'Help:Merge history' );
193 private function showHistory() {
194 $this->showMergeForm();
196 # List all stored revisions
197 $revisions = new MergeHistoryPager(
198 $this, array(), $this->mTargetObj
, $this->mDestObj
200 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
202 $out = $this->getOutput();
203 $titleObj = $this->getPageTitle();
204 $action = $titleObj->getLocalURL( array( 'action' => 'submit' ) );
205 # Start the form here
206 $top = Xml
::openElement(
214 $out->addHTML( $top );
216 if ( $haveRevisions ) {
217 # Format the user-visible controls (comment field, submission button)
218 # in a nice little table
220 Xml
::openElement( 'fieldset' ) .
221 $this->msg( 'mergehistory-merge', $this->mTargetObj
->getPrefixedText(),
222 $this->mDestObj
->getPrefixedText() )->parse() .
223 Xml
::openElement( 'table', array( 'id' => 'mw-mergehistory-table' ) ) .
225 <td class="mw-label">' .
226 Xml
::label( $this->msg( 'mergehistory-reason' )->text(), 'wpComment' ) .
228 <td class="mw-input">' .
229 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
234 <td class="mw-submit">' .
236 $this->msg( 'mergehistory-submit' )->text(),
237 array( 'name' => 'merge', 'id' => 'mw-merge-submit' )
241 Xml
::closeElement( 'table' ) .
242 Xml
::closeElement( 'fieldset' );
244 $out->addHTML( $table );
248 '<h2 id="mw-mergehistory">' .
249 $this->msg( 'mergehistory-list' )->escaped() . "</h2>\n"
252 if ( $haveRevisions ) {
253 $out->addHTML( $revisions->getNavigationBar() );
254 $out->addHTML( '<ul>' );
255 $out->addHTML( $revisions->getBody() );
256 $out->addHTML( '</ul>' );
257 $out->addHTML( $revisions->getNavigationBar() );
259 $out->addWikiMsg( 'mergehistory-empty' );
262 # Show relevant lines from the merge log:
263 $mergeLogPage = new LogPage( 'merge' );
264 $out->addHTML( '<h2>' . $mergeLogPage->getName()->escaped() . "</h2>\n" );
265 LogEventsList
::showLogExtract( $out, 'merge', $this->mTargetObj
);
267 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
268 # Such would happen if a page was renamed after the form loaded, but before submit
269 $misc = Html
::hidden( 'targetID', $this->mTargetObj
->getArticleID() );
270 $misc .= Html
::hidden( 'destID', $this->mDestObj
->getArticleID() );
271 $misc .= Html
::hidden( 'target', $this->mTarget
);
272 $misc .= Html
::hidden( 'dest', $this->mDest
);
273 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
274 $misc .= Xml
::closeElement( 'form' );
275 $out->addHTML( $misc );
280 function formatRevisionRow( $row ) {
281 $rev = new Revision( $row );
284 $last = $this->message
['last'];
286 $ts = wfTimestamp( TS_MW
, $row->rev_timestamp
);
287 $checkBox = Xml
::radio( 'mergepoint', $ts, ( $this->mTimestamp
=== $ts ) );
289 $user = $this->getUser();
291 $pageLink = Linker
::linkKnown(
293 htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) ),
295 array( 'oldid' => $rev->getId() )
297 if ( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
298 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
302 if ( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
303 $last = $this->message
['last'];
304 } elseif ( isset( $this->prevId
[$row->rev_id
] ) ) {
305 $last = Linker
::linkKnown(
307 $this->message
['last'],
310 'diff' => $row->rev_id
,
311 'oldid' => $this->prevId
[$row->rev_id
]
316 $userLink = Linker
::revUserTools( $rev );
318 $size = $row->rev_len
;
319 if ( !is_null( $size ) ) {
320 $stxt = Linker
::formatRevisionSize( $size );
322 $comment = Linker
::revComment( $rev );
324 return Html
::rawElement( 'li', array(),
325 $this->msg( 'mergehistory-revisionrow' )
326 ->rawParams( $checkBox, $last, $pageLink, $userLink, $stxt, $comment )->escaped() );
330 * Actually attempt the history move
332 * @todo if all versions of page A are moved to B and then a user
333 * tries to do a reverse-merge via the "unmerge" log link, then page
334 * A will still be a redirect (as it was after the original merge),
335 * though it will have the old revisions back from before (as expected).
336 * The user may have to "undo" the redirect manually to finish the "unmerge".
337 * Maybe this should delete redirects at the target page of merges?
339 * @return bool Success
342 # Get the titles directly from the IDs, in case the target page params
343 # were spoofed. The queries are done based on the IDs, so it's best to
344 # keep it consistent...
345 $targetTitle = Title
::newFromID( $this->mTargetID
);
346 $destTitle = Title
::newFromID( $this->mDestID
);
347 if ( is_null( $targetTitle ) ||
is_null( $destTitle ) ) {
348 return false; // validate these
350 if ( $targetTitle->getArticleID() == $destTitle->getArticleID() ) {
353 # Verify that this timestamp is valid
354 # Must be older than the destination page
355 $dbw = wfGetDB( DB_MASTER
);
356 # Get timestamp into DB format
357 $this->mTimestamp
= $this->mTimestamp ?
$dbw->timestamp( $this->mTimestamp
) : '';
358 # Max timestamp should be min of destination page
359 $maxtimestamp = $dbw->selectField(
361 'MIN(rev_timestamp)',
362 array( 'rev_page' => $this->mDestID
),
365 # Destination page must exist with revisions
366 if ( !$maxtimestamp ) {
367 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
371 # Get the latest timestamp of the source
372 $lasttimestamp = $dbw->selectField(
373 array( 'page', 'revision' ),
375 array( 'page_id' => $this->mTargetID
, 'page_latest = rev_id' ),
378 # $this->mTimestamp must be older than $maxtimestamp
379 if ( $this->mTimestamp
>= $maxtimestamp ) {
380 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
384 # Get the timestamp pivot condition
385 if ( $this->mTimestamp
) {
386 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
387 $timestampLimit = wfTimestamp( TS_MW
, $this->mTimestamp
);
389 $timewhere = "rev_timestamp <= {$maxtimestamp}";
390 $timestampLimit = wfTimestamp( TS_MW
, $lasttimestamp );
392 # Check that there are not too many revisions to move
393 $limit = 5000; // avoid too much slave lag
394 $count = $dbw->selectRowCount( 'revision', '1',
395 array( 'rev_page' => $this->mTargetID
, $timewhere ),
397 array( 'LIMIT' => $limit +
1 )
399 if ( $count > $limit ) {
400 $this->getOutput()->addWikiMsg( 'mergehistory-fail-toobig' );
407 array( 'rev_page' => $this->mDestID
),
408 array( 'rev_page' => $this->mTargetID
, $timewhere ),
412 $count = $dbw->affectedRows();
413 # Make the source page a redirect if no revisions are left
414 $haveRevisions = $dbw->selectField(
417 array( 'rev_page' => $this->mTargetID
),
419 array( 'FOR UPDATE' )
421 if ( !$haveRevisions ) {
422 if ( $this->mComment
) {
423 $comment = $this->msg(
424 'mergehistory-comment',
425 $targetTitle->getPrefixedText(),
426 $destTitle->getPrefixedText(),
428 )->inContentLanguage()->text();
430 $comment = $this->msg(
431 'mergehistory-autocomment',
432 $targetTitle->getPrefixedText(),
433 $destTitle->getPrefixedText()
434 )->inContentLanguage()->text();
437 $contentHandler = ContentHandler
::getForTitle( $targetTitle );
438 $redirectContent = $contentHandler->makeRedirectContent( $destTitle );
440 if ( $redirectContent ) {
441 $redirectPage = WikiPage
::factory( $targetTitle );
442 $redirectRevision = new Revision( array(
443 'title' => $targetTitle,
444 'page' => $this->mTargetID
,
445 'comment' => $comment,
446 'content' => $redirectContent ) );
447 $redirectRevision->insertOn( $dbw );
448 $redirectPage->updateRevisionOn( $dbw, $redirectRevision );
450 # Now, we record the link from the redirect to the new title.
451 # It should have no other outgoing links...
452 $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mDestID
), __METHOD__
);
453 $dbw->insert( 'pagelinks',
455 'pl_from' => $this->mDestID
,
456 'pl_from_namespace' => $destTitle->getNamespace(),
457 'pl_namespace' => $destTitle->getNamespace(),
458 'pl_title' => $destTitle->getDBkey() ),
462 // would be nice to show a warning if we couldn't create a redirect
465 $targetTitle->invalidateCache(); // update histories
467 $destTitle->invalidateCache(); // update histories
468 # Check if this did anything
470 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
475 $logEntry = new ManualLogEntry( 'merge', 'merge' );
476 $logEntry->setPerformer( $this->getUser() );
477 $logEntry->setComment( $this->mComment
);
478 $logEntry->setTarget( $targetTitle );
479 $logEntry->setParameters( array(
480 '4::dest' => $destTitle->getPrefixedText(),
481 '5::mergepoint' => $timestampLimit
483 $logId = $logEntry->insert();
484 $logEntry->publish( $logId );
486 # @todo message should use redirect=no
487 $this->getOutput()->addWikiText( $this->msg( 'mergehistory-success',
488 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText() )->numParams(
491 Hooks
::run( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
496 protected function getGroupName() {
501 class MergeHistoryPager
extends ReverseChronologicalPager
{
502 /** @var IContextSource */
508 function __construct( $form, $conds, $source, $dest ) {
509 $this->mForm
= $form;
510 $this->mConds
= $conds;
511 $this->title
= $source;
512 $this->articleID
= $source->getArticleID();
514 $dbr = wfGetDB( DB_SLAVE
);
515 $maxtimestamp = $dbr->selectField(
517 'MIN(rev_timestamp)',
518 array( 'rev_page' => $dest->getArticleID() ),
521 $this->maxTimestamp
= $maxtimestamp;
523 parent
::__construct( $form->getContext() );
526 function getStartBody() {
527 # Do a link batch query
528 $this->mResult
->seek( 0 );
529 $batch = new LinkBatch();
530 # Give some pointers to make (last) links
531 $this->mForm
->prevId
= array();
532 foreach ( $this->mResult
as $row ) {
533 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->user_name
) );
534 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->user_name
) );
536 $rev_id = isset( $rev_id ) ?
$rev_id : $row->rev_id
;
537 if ( $rev_id > $row->rev_id
) {
538 $this->mForm
->prevId
[$rev_id] = $row->rev_id
;
539 } elseif ( $rev_id < $row->rev_id
) {
540 $this->mForm
->prevId
[$row->rev_id
] = $rev_id;
543 $rev_id = $row->rev_id
;
547 $this->mResult
->seek( 0 );
552 function formatRow( $row ) {
553 return $this->mForm
->formatRevisionRow( $row );
556 function getQueryInfo() {
557 $conds = $this->mConds
;
558 $conds['rev_page'] = $this->articleID
;
559 $conds[] = "rev_timestamp < " . $this->mDb
->addQuotes( $this->maxTimestamp
);
562 'tables' => array( 'revision', 'page', 'user' ),
563 'fields' => array_merge( Revision
::selectFields(), Revision
::selectUserFields() ),
565 'join_conds' => array(
566 'page' => Revision
::pageJoinCond(),
567 'user' => Revision
::userJoinCond() )
571 function getIndexField() {
572 return 'rev_timestamp';