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', array( 'parse' ),
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', array( 'parse' ),
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() {
164 $this->getOutput()->addWikiMsg( 'mergehistory-header' );
166 $this->getOutput()->addHTML(
167 Xml
::openElement( 'form', array(
169 'action' => $wgScript ) ) .
171 Xml
::element( 'legend', array(),
172 $this->msg( 'mergehistory-box' )->text() ) .
173 Html
::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) .
174 Html
::hidden( 'submitted', '1' ) .
175 Html
::hidden( 'mergepoint', $this->mTimestamp
) .
176 Xml
::openElement( 'table' ) .
178 <td>' . Xml
::label( $this->msg( 'mergehistory-from' )->text(), 'target' ) . '</td>
179 <td>' . Xml
::input( 'target', 30, $this->mTarget
, array( 'id' => 'target' ) ) . '</td>
181 <td>' . Xml
::label( $this->msg( 'mergehistory-into' )->text(), 'dest' ) . '</td>
182 <td>' . Xml
::input( 'dest', 30, $this->mDest
, array( 'id' => 'dest' ) ) . '</td>
184 Xml
::submitButton( $this->msg( 'mergehistory-go' )->text() ) .
186 Xml
::closeElement( 'table' ) .
192 private function showHistory() {
193 $this->showMergeForm();
195 # List all stored revisions
196 $revisions = new MergeHistoryPager(
197 $this, array(), $this->mTargetObj
, $this->mDestObj
199 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
201 $out = $this->getOutput();
202 $titleObj = $this->getPageTitle();
203 $action = $titleObj->getLocalURL( array( 'action' => 'submit' ) );
204 # Start the form here
205 $top = Xml
::openElement(
213 $out->addHTML( $top );
215 if ( $haveRevisions ) {
216 # Format the user-visible controls (comment field, submission button)
217 # in a nice little table
219 Xml
::openElement( 'fieldset' ) .
220 $this->msg( 'mergehistory-merge', $this->mTargetObj
->getPrefixedText(),
221 $this->mDestObj
->getPrefixedText() )->parse() .
222 Xml
::openElement( 'table', array( 'id' => 'mw-mergehistory-table' ) ) .
224 <td class="mw-label">' .
225 Xml
::label( $this->msg( 'mergehistory-reason' )->text(), 'wpComment' ) .
227 <td class="mw-input">' .
228 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
233 <td class="mw-submit">' .
235 $this->msg( 'mergehistory-submit' )->text(),
236 array( 'name' => 'merge', 'id' => 'mw-merge-submit' )
240 Xml
::closeElement( 'table' ) .
241 Xml
::closeElement( 'fieldset' );
243 $out->addHTML( $table );
247 '<h2 id="mw-mergehistory">' .
248 $this->msg( 'mergehistory-list' )->escaped() . "</h2>\n"
251 if ( $haveRevisions ) {
252 $out->addHTML( $revisions->getNavigationBar() );
253 $out->addHTML( '<ul>' );
254 $out->addHTML( $revisions->getBody() );
255 $out->addHTML( '</ul>' );
256 $out->addHTML( $revisions->getNavigationBar() );
258 $out->addWikiMsg( 'mergehistory-empty' );
261 # Show relevant lines from the merge log:
262 $mergeLogPage = new LogPage( 'merge' );
263 $out->addHTML( '<h2>' . $mergeLogPage->getName()->escaped() . "</h2>\n" );
264 LogEventsList
::showLogExtract( $out, 'merge', $this->mTargetObj
);
266 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
267 # Such would happen if a page was renamed after the form loaded, but before submit
268 $misc = Html
::hidden( 'targetID', $this->mTargetObj
->getArticleID() );
269 $misc .= Html
::hidden( 'destID', $this->mDestObj
->getArticleID() );
270 $misc .= Html
::hidden( 'target', $this->mTarget
);
271 $misc .= Html
::hidden( 'dest', $this->mDest
);
272 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
273 $misc .= Xml
::closeElement( 'form' );
274 $out->addHTML( $misc );
279 function formatRevisionRow( $row ) {
280 $rev = new Revision( $row );
283 $last = $this->message
['last'];
285 $ts = wfTimestamp( TS_MW
, $row->rev_timestamp
);
286 $checkBox = Xml
::radio( 'mergepoint', $ts, false );
288 $user = $this->getUser();
290 $pageLink = Linker
::linkKnown(
292 htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) ),
294 array( 'oldid' => $rev->getId() )
296 if ( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
297 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
301 if ( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
302 $last = $this->message
['last'];
303 } elseif ( isset( $this->prevId
[$row->rev_id
] ) ) {
304 $last = Linker
::linkKnown(
306 $this->message
['last'],
309 'diff' => $row->rev_id
,
310 'oldid' => $this->prevId
[$row->rev_id
]
315 $userLink = Linker
::revUserTools( $rev );
317 $size = $row->rev_len
;
318 if ( !is_null( $size ) ) {
319 $stxt = Linker
::formatRevisionSize( $size );
321 $comment = Linker
::revComment( $rev );
323 return Html
::rawElement( 'li', array(),
324 $this->msg( 'mergehistory-revisionrow' )
325 ->rawParams( $checkBox, $last, $pageLink, $userLink, $stxt, $comment )->escaped() );
329 # Get the titles directly from the IDs, in case the target page params
330 # were spoofed. The queries are done based on the IDs, so it's best to
331 # keep it consistent...
332 $targetTitle = Title
::newFromID( $this->mTargetID
);
333 $destTitle = Title
::newFromID( $this->mDestID
);
334 if ( is_null( $targetTitle ) ||
is_null( $destTitle ) ) {
335 return false; // validate these
337 if ( $targetTitle->getArticleID() == $destTitle->getArticleID() ) {
340 # Verify that this timestamp is valid
341 # Must be older than the destination page
342 $dbw = wfGetDB( DB_MASTER
);
343 # Get timestamp into DB format
344 $this->mTimestamp
= $this->mTimestamp ?
$dbw->timestamp( $this->mTimestamp
) : '';
345 # Max timestamp should be min of destination page
346 $maxtimestamp = $dbw->selectField(
348 'MIN(rev_timestamp)',
349 array( 'rev_page' => $this->mDestID
),
352 # Destination page must exist with revisions
353 if ( !$maxtimestamp ) {
354 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
358 # Get the latest timestamp of the source
359 $lasttimestamp = $dbw->selectField(
360 array( 'page', 'revision' ),
362 array( 'page_id' => $this->mTargetID
, 'page_latest = rev_id' ),
365 # $this->mTimestamp must be older than $maxtimestamp
366 if ( $this->mTimestamp
>= $maxtimestamp ) {
367 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
371 # Update the revisions
372 if ( $this->mTimestamp
) {
373 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
374 $timestampLimit = wfTimestamp( TS_MW
, $this->mTimestamp
);
376 $timewhere = "rev_timestamp <= {$maxtimestamp}";
377 $timestampLimit = wfTimestamp( TS_MW
, $lasttimestamp );
382 array( 'rev_page' => $this->mDestID
),
383 array( 'rev_page' => $this->mTargetID
, $timewhere ),
387 $count = $dbw->affectedRows();
388 # Make the source page a redirect if no revisions are left
389 $haveRevisions = $dbw->selectField(
392 array( 'rev_page' => $this->mTargetID
),
394 array( 'FOR UPDATE' )
396 if ( !$haveRevisions ) {
397 if ( $this->mComment
) {
398 $comment = $this->msg(
399 'mergehistory-comment',
400 $targetTitle->getPrefixedText(),
401 $destTitle->getPrefixedText(),
403 )->inContentLanguage()->text();
405 $comment = $this->msg(
406 'mergehistory-autocomment',
407 $targetTitle->getPrefixedText(),
408 $destTitle->getPrefixedText()
409 )->inContentLanguage()->text();
412 $contentHandler = ContentHandler
::getForTitle( $targetTitle );
413 $redirectContent = $contentHandler->makeRedirectContent( $destTitle );
415 if ( $redirectContent ) {
416 $redirectPage = WikiPage
::factory( $targetTitle );
417 $redirectRevision = new Revision( array(
418 'title' => $targetTitle,
419 'page' => $this->mTargetID
,
420 'comment' => $comment,
421 'content' => $redirectContent ) );
422 $redirectRevision->insertOn( $dbw );
423 $redirectPage->updateRevisionOn( $dbw, $redirectRevision );
425 # Now, we record the link from the redirect to the new title.
426 # It should have no other outgoing links...
427 $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mDestID
), __METHOD__
);
428 $dbw->insert( 'pagelinks',
430 'pl_from' => $this->mDestID
,
431 'pl_namespace' => $destTitle->getNamespace(),
432 'pl_title' => $destTitle->getDBkey() ),
436 // would be nice to show a warning if we couldn't create a redirect
439 $targetTitle->invalidateCache(); // update histories
441 $destTitle->invalidateCache(); // update histories
442 # Check if this did anything
444 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
449 $log = new LogPage( 'merge' );
451 'merge', $targetTitle, $this->mComment
,
452 array( $destTitle->getPrefixedText(), $timestampLimit ), $this->getUser()
455 $this->getOutput()->addWikiMsg( 'mergehistory-success',
456 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText(), $count );
458 wfRunHooks( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
463 protected function getGroupName() {
468 class MergeHistoryPager
extends ReverseChronologicalPager
{
469 /** @var IContextSource */
475 function __construct( $form, $conds = array(), $source, $dest ) {
476 $this->mForm
= $form;
477 $this->mConds
= $conds;
478 $this->title
= $source;
479 $this->articleID
= $source->getArticleID();
481 $dbr = wfGetDB( DB_SLAVE
);
482 $maxtimestamp = $dbr->selectField(
484 'MIN(rev_timestamp)',
485 array( 'rev_page' => $dest->getArticleID() ),
488 $this->maxTimestamp
= $maxtimestamp;
490 parent
::__construct( $form->getContext() );
493 function getStartBody() {
494 wfProfileIn( __METHOD__
);
495 # Do a link batch query
496 $this->mResult
->seek( 0 );
497 $batch = new LinkBatch();
498 # Give some pointers to make (last) links
499 $this->mForm
->prevId
= array();
500 foreach ( $this->mResult
as $row ) {
501 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->user_name
) );
502 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->user_name
) );
504 $rev_id = isset( $rev_id ) ?
$rev_id : $row->rev_id
;
505 if ( $rev_id > $row->rev_id
) {
506 $this->mForm
->prevId
[$rev_id] = $row->rev_id
;
507 } elseif ( $rev_id < $row->rev_id
) {
508 $this->mForm
->prevId
[$row->rev_id
] = $rev_id;
511 $rev_id = $row->rev_id
;
515 $this->mResult
->seek( 0 );
517 wfProfileOut( __METHOD__
);
522 function formatRow( $row ) {
523 return $this->mForm
->formatRevisionRow( $row );
526 function getQueryInfo() {
527 $conds = $this->mConds
;
528 $conds['rev_page'] = $this->articleID
;
529 $conds[] = "rev_timestamp < {$this->maxTimestamp}";
532 'tables' => array( 'revision', 'page', 'user' ),
533 'fields' => array_merge( Revision
::selectFields(), Revision
::selectUserFields() ),
535 'join_conds' => array(
536 'page' => Revision
::pageJoinCond(),
537 'user' => Revision
::userJoinCond() )
541 function getIndexField() {
542 return 'rev_timestamp';