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;
32 var $mTargetObj, $mDestObj;
34 public function __construct() {
35 parent
::__construct( 'MergeHistory', 'mergehistory' );
38 private function loadRequestParams( $request ) {
41 $this->mAction
= $request->getVal( 'action' );
42 $this->mTarget
= $request->getVal( 'target' );
43 $this->mDest
= $request->getVal( 'dest' );
44 $this->mSubmitted
= $request->getBool( 'submitted' );
46 $this->mTargetID
= intval( $request->getVal( 'targetID' ) );
47 $this->mDestID
= intval( $request->getVal( 'destID' ) );
48 $this->mTimestamp
= $request->getVal( 'mergepoint' );
49 if( !preg_match( '/[0-9]{14}/', $this->mTimestamp
) ) {
50 $this->mTimestamp
= '';
52 $this->mComment
= $request->getText( 'wpComment' );
54 $this->mMerge
= $request->wasPosted() && $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
56 if( $this->mSubmitted
) {
57 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
58 $this->mDestObj
= Title
::newFromURL( $this->mDest
);
60 $this->mTargetObj
= null;
61 $this->mDestObj
= null;
63 $this->preCacheMessages();
67 * As we use the same small set of messages in various methods and that
68 * they are called often, we call them once and save them in $this->message
70 function preCacheMessages() {
71 // Precache various messages
72 if( !isset( $this->message
) ) {
73 $this->message
['last'] = wfMsgExt( 'last', array( 'escape' ) );
77 public function execute( $par ) {
78 global $wgOut, $wgRequest, $wgUser;
81 $wgOut->readOnlyPage();
85 if( !$this->userCanExecute( $wgUser ) ) {
86 $this->displayRestrictionError();
90 $this->loadRequestParams( $wgRequest );
93 $this->outputHeader();
95 if( $this->mTargetID
&& $this->mDestID
&& $this->mAction
== 'submit' && $this->mMerge
) {
96 return $this->merge();
99 if ( !$this->mSubmitted
) {
100 $this->showMergeForm();
105 if ( !$this->mTargetObj
instanceof Title
) {
106 $errors[] = wfMsgExt( 'mergehistory-invalid-source', array( 'parse' ) );
107 } elseif( !$this->mTargetObj
->exists() ) {
108 $errors[] = wfMsgExt( 'mergehistory-no-source', array( 'parse' ),
109 wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() )
113 if ( !$this->mDestObj
instanceof Title
) {
114 $errors[] = wfMsgExt( 'mergehistory-invalid-destination', array( 'parse' ) );
115 } elseif( !$this->mDestObj
->exists() ) {
116 $errors[] = wfMsgExt( 'mergehistory-no-destination', array( 'parse' ),
117 wfEscapeWikiText( $this->mDestObj
->getPrefixedText() )
121 if ( $this->mTargetObj
&& $this->mDestObj
&& $this->mTargetObj
->equals( $this->mDestObj
) ) {
122 $errors[] = wfMsgExt( 'mergehistory-same-destination', array( 'parse' ) );
125 if ( count( $errors ) ) {
126 $this->showMergeForm();
127 $wgOut->addHTML( implode( "\n", $errors ) );
129 $this->showHistory();
134 function showMergeForm() {
135 global $wgOut, $wgScript;
137 $wgOut->addWikiMsg( 'mergehistory-header' );
140 Xml
::openElement( 'form', array(
142 'action' => $wgScript ) ) .
144 Xml
::element( 'legend', array(),
145 wfMsg( 'mergehistory-box' ) ) .
146 Html
::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
147 Html
::hidden( 'submitted', '1' ) .
148 Html
::hidden( 'mergepoint', $this->mTimestamp
) .
149 Xml
::openElement( 'table' ) .
151 <td>' . Xml
::label( wfMsg( 'mergehistory-from' ), 'target' ) . '</td>
152 <td>' . Xml
::input( 'target', 30, $this->mTarget
, array( 'id' => 'target' ) ) . '</td>
154 <td>' . Xml
::label( wfMsg( 'mergehistory-into' ), 'dest' ) . '</td>
155 <td>' . Xml
::input( 'dest', 30, $this->mDest
, array( 'id' => 'dest' ) ) . '</td>
157 Xml
::submitButton( wfMsg( 'mergehistory-go' ) ) .
159 Xml
::closeElement( 'table' ) .
165 private function showHistory() {
166 global $wgUser, $wgOut;
168 $this->sk
= $wgUser->getSkin();
170 $wgOut->setPageTitle( wfMsg( 'mergehistory' ) );
172 $this->showMergeForm();
174 # List all stored revisions
175 $revisions = new MergeHistoryPager(
176 $this, array(), $this->mTargetObj
, $this->mDestObj
178 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
180 $titleObj = $this->getTitle();
181 $action = $titleObj->getLocalURL( array( 'action' => 'submit' ) );
182 # Start the form here
183 $top = Xml
::openElement(
191 $wgOut->addHTML( $top );
193 if( $haveRevisions ) {
194 # Format the user-visible controls (comment field, submission button)
195 # in a nice little table
197 Xml
::openElement( 'fieldset' ) .
198 wfMsgExt( 'mergehistory-merge', array( 'parseinline' ),
199 $this->mTargetObj
->getPrefixedText(), $this->mDestObj
->getPrefixedText() ) .
200 Xml
::openElement( 'table', array( 'id' => 'mw-mergehistory-table' ) ) .
202 <td class="mw-label">' .
203 Xml
::label( wfMsg( 'mergehistory-reason' ), 'wpComment' ) .
205 <td class="mw-input">' .
206 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
211 <td class="mw-submit">' .
212 Xml
::submitButton( wfMsg( 'mergehistory-submit' ), array( 'name' => 'merge', 'id' => 'mw-merge-submit' ) ) .
215 Xml
::closeElement( 'table' ) .
216 Xml
::closeElement( 'fieldset' );
218 $wgOut->addHTML( $table );
222 '<h2 id="mw-mergehistory">' .
223 wfMsgHtml( 'mergehistory-list' ) . "</h2>\n"
226 if( $haveRevisions ) {
227 $wgOut->addHTML( $revisions->getNavigationBar() );
228 $wgOut->addHTML( '<ul>' );
229 $wgOut->addHTML( $revisions->getBody() );
230 $wgOut->addHTML( '</ul>' );
231 $wgOut->addHTML( $revisions->getNavigationBar() );
233 $wgOut->addWikiMsg( 'mergehistory-empty' );
236 # Show relevant lines from the deletion log:
237 $wgOut->addHTML( '<h2>' . htmlspecialchars( LogPage
::logName( 'merge' ) ) . "</h2>\n" );
238 LogEventsList
::showLogExtract( $wgOut, 'merge', $this->mTargetObj
->getPrefixedText() );
240 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
241 # Such would happen if a page was renamed after the form loaded, but before submit
242 $misc = Html
::hidden( 'targetID', $this->mTargetObj
->getArticleID() );
243 $misc .= Html
::hidden( 'destID', $this->mDestObj
->getArticleID() );
244 $misc .= Html
::hidden( 'target', $this->mTarget
);
245 $misc .= Html
::hidden( 'dest', $this->mDest
);
246 $misc .= Html
::hidden( 'wpEditToken', $wgUser->editToken() );
247 $misc .= Xml
::closeElement( 'form' );
248 $wgOut->addHTML( $misc );
253 function formatRevisionRow( $row ) {
256 $rev = new Revision( $row );
259 $last = $this->message
['last'];
261 $ts = wfTimestamp( TS_MW
, $row->rev_timestamp
);
262 $checkBox = Xml
::radio( 'mergepoint', $ts, false );
264 $pageLink = $this->sk
->linkKnown(
266 htmlspecialchars( $wgLang->timeanddate( $ts ) ),
268 array( 'oldid' => $rev->getId() )
270 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
271 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
275 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
276 $last = $this->message
['last'];
277 } elseif( isset( $this->prevId
[$row->rev_id
] ) ) {
278 $last = $this->sk
->linkKnown(
280 $this->message
['last'],
283 'diff' => $row->rev_id
,
284 'oldid' => $this->prevId
[$row->rev_id
]
289 $userLink = $this->sk
->revUserTools( $rev );
291 $size = $row->rev_len
;
292 if( !is_null( $size ) ) {
293 $stxt = $this->sk
->formatRevisionSize( $size );
295 $comment = $this->sk
->revComment( $rev );
297 return "<li>$checkBox ($last) $pageLink . . $userLink $stxt $comment</li>";
301 * Fetch revision text link if it's available to all users
304 function getPageLink( $row, $titleObj, $ts, $target ) {
307 if( !$this->userCan( $row, Revision
::DELETED_TEXT
) ) {
308 return '<span class="history-deleted">' .
309 $wgLang->timeanddate( $ts, true ) . '</span>';
311 $link = $this->sk
->linkKnown(
313 $wgLang->timeanddate( $ts, true ),
320 if( $this->isDeleted( $row, Revision
::DELETED_TEXT
) ) {
321 $link = '<span class="history-deleted">' . $link . '</span>';
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 $wgOut->addWikiMsg( 'mergehistory-fail' );
357 # Get the latest timestamp of the source
358 $lasttimestamp = $dbw->selectField(
359 array( 'page', 'revision' ),
361 array( 'page_id' => $this->mTargetID
, 'page_latest = rev_id' ),
364 # $this->mTimestamp must be older than $maxtimestamp
365 if( $this->mTimestamp
>= $maxtimestamp ) {
366 $wgOut->addWikiMsg( 'mergehistory-fail' );
369 # Update the revisions
370 if( $this->mTimestamp
) {
371 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
372 $timestampLimit = wfTimestamp( TS_MW
, $this->mTimestamp
);
374 $timewhere = "rev_timestamp <= {$maxtimestamp}";
375 $timestampLimit = wfTimestamp( TS_MW
, $lasttimestamp );
380 array( 'rev_page' => $this->mDestID
),
381 array( 'rev_page' => $this->mTargetID
, $timewhere ),
385 $count = $dbw->affectedRows();
386 # Make the source page a redirect if no revisions are left
387 $haveRevisions = $dbw->selectField(
390 array( 'rev_page' => $this->mTargetID
),
392 array( 'FOR UPDATE' )
394 if( !$haveRevisions ) {
395 if( $this->mComment
) {
396 $comment = wfMsgForContent(
397 'mergehistory-comment',
398 $targetTitle->getPrefixedText(),
399 $destTitle->getPrefixedText(),
403 $comment = wfMsgForContent(
404 'mergehistory-autocomment',
405 $targetTitle->getPrefixedText(),
406 $destTitle->getPrefixedText()
409 $mwRedir = MagicWord
::get( 'redirect' );
410 $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $destTitle->getPrefixedText() . "]]\n";
411 $redirectArticle = new Article( $targetTitle );
412 $redirectRevision = new Revision( array(
413 'page' => $this->mTargetID
,
414 'comment' => $comment,
415 'text' => $redirectText ) );
416 $redirectRevision->insertOn( $dbw );
417 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision );
419 # Now, we record the link from the redirect to the new title.
420 # It should have no other outgoing links...
421 $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mDestID
), __METHOD__
);
422 $dbw->insert( 'pagelinks',
424 'pl_from' => $this->mDestID
,
425 'pl_namespace' => $destTitle->getNamespace(),
426 'pl_title' => $destTitle->getDBkey() ),
430 $targetTitle->invalidateCache(); // update histories
432 $destTitle->invalidateCache(); // update histories
433 # Check if this did anything
435 $wgOut->addWikiMsg( 'mergehistory-fail' );
439 $log = new LogPage( 'merge' );
441 'merge', $targetTitle, $this->mComment
,
442 array( $destTitle->getPrefixedText(), $timestampLimit )
446 wfMsgExt( 'mergehistory-success', array('parseinline'),
447 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText(), $count ) );
449 wfRunHooks( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
455 class MergeHistoryPager
extends ReverseChronologicalPager
{
456 public $mForm, $mConds;
458 function __construct( $form, $conds = array(), $source, $dest ) {
459 $this->mForm
= $form;
460 $this->mConds
= $conds;
461 $this->title
= $source;
462 $this->articleID
= $source->getArticleID();
464 $dbr = wfGetDB( DB_SLAVE
);
465 $maxtimestamp = $dbr->selectField(
467 'MIN(rev_timestamp)',
468 array( 'rev_page' => $dest->getArticleID() ),
471 $this->maxTimestamp
= $maxtimestamp;
473 parent
::__construct();
476 function getStartBody() {
477 wfProfileIn( __METHOD__
);
478 # Do a link batch query
479 $this->mResult
->seek( 0 );
480 $batch = new LinkBatch();
481 # Give some pointers to make (last) links
482 $this->mForm
->prevId
= array();
483 foreach ( $this->mResult
as $row ) {
484 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->rev_user_text
) );
485 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->rev_user_text
) );
487 $rev_id = isset( $rev_id ) ?
$rev_id : $row->rev_id
;
488 if( $rev_id > $row->rev_id
) {
489 $this->mForm
->prevId
[$rev_id] = $row->rev_id
;
490 } elseif( $rev_id < $row->rev_id
) {
491 $this->mForm
->prevId
[$row->rev_id
] = $rev_id;
494 $rev_id = $row->rev_id
;
498 $this->mResult
->seek( 0 );
500 wfProfileOut( __METHOD__
);
504 function formatRow( $row ) {
505 return $this->mForm
->formatRevisionRow( $row );
508 function getQueryInfo() {
509 $conds = $this->mConds
;
510 $conds['rev_page'] = $this->articleID
;
511 $conds[] = 'page_id = rev_page';
512 $conds[] = "rev_timestamp < {$this->maxTimestamp}";
514 'tables' => array( 'revision', 'page' ),
516 'rev_minor_edit', 'rev_timestamp', 'rev_user', 'rev_user_text',
517 'rev_comment', 'rev_id', 'rev_page', 'rev_parent_id',
518 'rev_text_id', 'rev_len', 'rev_deleted'
524 function getIndexField() {
525 return 'rev_timestamp';