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' );
39 * @param $request WebRequest
42 private function loadRequestParams( $request ) {
45 $this->mAction
= $request->getVal( 'action' );
46 $this->mTarget
= $request->getVal( 'target' );
47 $this->mDest
= $request->getVal( 'dest' );
48 $this->mSubmitted
= $request->getBool( 'submitted' );
50 $this->mTargetID
= intval( $request->getVal( 'targetID' ) );
51 $this->mDestID
= intval( $request->getVal( 'destID' ) );
52 $this->mTimestamp
= $request->getVal( 'mergepoint' );
53 if( !preg_match( '/[0-9]{14}/', $this->mTimestamp
) ) {
54 $this->mTimestamp
= '';
56 $this->mComment
= $request->getText( 'wpComment' );
58 $this->mMerge
= $request->wasPosted() && $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
60 if( $this->mSubmitted
) {
61 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
62 $this->mDestObj
= Title
::newFromURL( $this->mDest
);
64 $this->mTargetObj
= null;
65 $this->mDestObj
= null;
67 $this->preCacheMessages();
71 * As we use the same small set of messages in various methods and that
72 * they are called often, we call them once and save them in $this->message
74 function preCacheMessages() {
75 // Precache various messages
76 if( !isset( $this->message
) ) {
77 $this->message
['last'] = wfMsgExt( 'last', array( 'escape' ) );
81 public function execute( $par ) {
82 global $wgOut, $wgRequest, $wgUser;
85 $wgOut->readOnlyPage();
89 if( !$this->userCanExecute( $wgUser ) ) {
90 $this->displayRestrictionError();
94 $this->loadRequestParams( $wgRequest );
97 $this->outputHeader();
99 if( $this->mTargetID
&& $this->mDestID
&& $this->mAction
== 'submit' && $this->mMerge
) {
100 return $this->merge();
103 if ( !$this->mSubmitted
) {
104 $this->showMergeForm();
109 if ( !$this->mTargetObj
instanceof Title
) {
110 $errors[] = wfMsgExt( 'mergehistory-invalid-source', array( 'parse' ) );
111 } elseif( !$this->mTargetObj
->exists() ) {
112 $errors[] = wfMsgExt( 'mergehistory-no-source', array( 'parse' ),
113 wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() )
117 if ( !$this->mDestObj
instanceof Title
) {
118 $errors[] = wfMsgExt( 'mergehistory-invalid-destination', array( 'parse' ) );
119 } elseif( !$this->mDestObj
->exists() ) {
120 $errors[] = wfMsgExt( 'mergehistory-no-destination', array( 'parse' ),
121 wfEscapeWikiText( $this->mDestObj
->getPrefixedText() )
125 if ( $this->mTargetObj
&& $this->mDestObj
&& $this->mTargetObj
->equals( $this->mDestObj
) ) {
126 $errors[] = wfMsgExt( 'mergehistory-same-destination', array( 'parse' ) );
129 if ( count( $errors ) ) {
130 $this->showMergeForm();
131 $wgOut->addHTML( implode( "\n", $errors ) );
133 $this->showHistory();
138 function showMergeForm() {
139 global $wgOut, $wgScript;
141 $wgOut->addWikiMsg( 'mergehistory-header' );
144 Xml
::openElement( 'form', array(
146 'action' => $wgScript ) ) .
148 Xml
::element( 'legend', array(),
149 wfMsg( 'mergehistory-box' ) ) .
150 Html
::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
151 Html
::hidden( 'submitted', '1' ) .
152 Html
::hidden( 'mergepoint', $this->mTimestamp
) .
153 Xml
::openElement( 'table' ) .
155 <td>' . Xml
::label( wfMsg( 'mergehistory-from' ), 'target' ) . '</td>
156 <td>' . Xml
::input( 'target', 30, $this->mTarget
, array( 'id' => 'target' ) ) . '</td>
158 <td>' . Xml
::label( wfMsg( 'mergehistory-into' ), 'dest' ) . '</td>
159 <td>' . Xml
::input( 'dest', 30, $this->mDest
, array( 'id' => 'dest' ) ) . '</td>
161 Xml
::submitButton( wfMsg( 'mergehistory-go' ) ) .
163 Xml
::closeElement( 'table' ) .
169 private function showHistory() {
170 global $wgUser, $wgOut;
172 $this->sk
= $wgUser->getSkin();
174 $wgOut->setPageTitle( wfMsg( 'mergehistory' ) );
176 $this->showMergeForm();
178 # List all stored revisions
179 $revisions = new MergeHistoryPager(
180 $this, array(), $this->mTargetObj
, $this->mDestObj
182 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
184 $titleObj = $this->getTitle();
185 $action = $titleObj->getLocalURL( array( 'action' => 'submit' ) );
186 # Start the form here
187 $top = Xml
::openElement(
195 $wgOut->addHTML( $top );
197 if( $haveRevisions ) {
198 # Format the user-visible controls (comment field, submission button)
199 # in a nice little table
201 Xml
::openElement( 'fieldset' ) .
202 wfMsgExt( 'mergehistory-merge', array( 'parseinline' ),
203 $this->mTargetObj
->getPrefixedText(), $this->mDestObj
->getPrefixedText() ) .
204 Xml
::openElement( 'table', array( 'id' => 'mw-mergehistory-table' ) ) .
206 <td class="mw-label">' .
207 Xml
::label( wfMsg( 'mergehistory-reason' ), 'wpComment' ) .
209 <td class="mw-input">' .
210 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
215 <td class="mw-submit">' .
216 Xml
::submitButton( wfMsg( 'mergehistory-submit' ), array( 'name' => 'merge', 'id' => 'mw-merge-submit' ) ) .
219 Xml
::closeElement( 'table' ) .
220 Xml
::closeElement( 'fieldset' );
222 $wgOut->addHTML( $table );
226 '<h2 id="mw-mergehistory">' .
227 wfMsgHtml( 'mergehistory-list' ) . "</h2>\n"
230 if( $haveRevisions ) {
231 $wgOut->addHTML( $revisions->getNavigationBar() );
232 $wgOut->addHTML( '<ul>' );
233 $wgOut->addHTML( $revisions->getBody() );
234 $wgOut->addHTML( '</ul>' );
235 $wgOut->addHTML( $revisions->getNavigationBar() );
237 $wgOut->addWikiMsg( 'mergehistory-empty' );
240 # Show relevant lines from the deletion log:
241 $wgOut->addHTML( '<h2>' . htmlspecialchars( LogPage
::logName( 'merge' ) ) . "</h2>\n" );
242 LogEventsList
::showLogExtract( $wgOut, 'merge', $this->mTargetObj
->getPrefixedText() );
244 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
245 # Such would happen if a page was renamed after the form loaded, but before submit
246 $misc = Html
::hidden( 'targetID', $this->mTargetObj
->getArticleID() );
247 $misc .= Html
::hidden( 'destID', $this->mDestObj
->getArticleID() );
248 $misc .= Html
::hidden( 'target', $this->mTarget
);
249 $misc .= Html
::hidden( 'dest', $this->mDest
);
250 $misc .= Html
::hidden( 'wpEditToken', $wgUser->editToken() );
251 $misc .= Xml
::closeElement( 'form' );
252 $wgOut->addHTML( $misc );
257 function formatRevisionRow( $row ) {
260 $rev = new Revision( $row );
263 $last = $this->message
['last'];
265 $ts = wfTimestamp( TS_MW
, $row->rev_timestamp
);
266 $checkBox = Xml
::radio( 'mergepoint', $ts, false );
268 $pageLink = $this->sk
->linkKnown(
270 htmlspecialchars( $wgLang->timeanddate( $ts ) ),
272 array( 'oldid' => $rev->getId() )
274 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
275 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
279 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
280 $last = $this->message
['last'];
281 } elseif( isset( $this->prevId
[$row->rev_id
] ) ) {
282 $last = $this->sk
->linkKnown(
284 $this->message
['last'],
287 'diff' => $row->rev_id
,
288 'oldid' => $this->prevId
[$row->rev_id
]
293 $userLink = $this->sk
->revUserTools( $rev );
295 $size = $row->rev_len
;
296 if( !is_null( $size ) ) {
297 $stxt = $this->sk
->formatRevisionSize( $size );
299 $comment = $this->sk
->revComment( $rev );
301 return "<li>$checkBox ($last) $pageLink . . $userLink $stxt $comment</li>";
305 * Fetch revision text link if it's available to all users
308 function getPageLink( $row, $titleObj, $ts, $target ) {
311 if( !$this->userCan( $row, Revision
::DELETED_TEXT
) ) {
312 return '<span class="history-deleted">' .
313 $wgLang->timeanddate( $ts, true ) . '</span>';
315 $link = $this->sk
->linkKnown(
317 $wgLang->timeanddate( $ts, true ),
324 if( $this->isDeleted( $row, Revision
::DELETED_TEXT
) ) {
325 $link = '<span class="history-deleted">' . $link . '</span>';
333 # Get the titles directly from the IDs, in case the target page params
334 # were spoofed. The queries are done based on the IDs, so it's best to
335 # keep it consistent...
336 $targetTitle = Title
::newFromID( $this->mTargetID
);
337 $destTitle = Title
::newFromID( $this->mDestID
);
338 if( is_null( $targetTitle ) ||
is_null( $destTitle ) ) {
339 return false; // validate these
341 if( $targetTitle->getArticleId() == $destTitle->getArticleId() ) {
344 # Verify that this timestamp is valid
345 # Must be older than the destination page
346 $dbw = wfGetDB( DB_MASTER
);
347 # Get timestamp into DB format
348 $this->mTimestamp
= $this->mTimestamp ?
$dbw->timestamp( $this->mTimestamp
) : '';
349 # Max timestamp should be min of destination page
350 $maxtimestamp = $dbw->selectField(
352 'MIN(rev_timestamp)',
353 array( 'rev_page' => $this->mDestID
),
356 # Destination page must exist with revisions
357 if( !$maxtimestamp ) {
358 $wgOut->addWikiMsg( 'mergehistory-fail' );
361 # Get the latest timestamp of the source
362 $lasttimestamp = $dbw->selectField(
363 array( 'page', 'revision' ),
365 array( 'page_id' => $this->mTargetID
, 'page_latest = rev_id' ),
368 # $this->mTimestamp must be older than $maxtimestamp
369 if( $this->mTimestamp
>= $maxtimestamp ) {
370 $wgOut->addWikiMsg( 'mergehistory-fail' );
373 # Update the revisions
374 if( $this->mTimestamp
) {
375 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
376 $timestampLimit = wfTimestamp( TS_MW
, $this->mTimestamp
);
378 $timewhere = "rev_timestamp <= {$maxtimestamp}";
379 $timestampLimit = wfTimestamp( TS_MW
, $lasttimestamp );
384 array( 'rev_page' => $this->mDestID
),
385 array( 'rev_page' => $this->mTargetID
, $timewhere ),
389 $count = $dbw->affectedRows();
390 # Make the source page a redirect if no revisions are left
391 $haveRevisions = $dbw->selectField(
394 array( 'rev_page' => $this->mTargetID
),
396 array( 'FOR UPDATE' )
398 if( !$haveRevisions ) {
399 if( $this->mComment
) {
400 $comment = wfMsgForContent(
401 'mergehistory-comment',
402 $targetTitle->getPrefixedText(),
403 $destTitle->getPrefixedText(),
407 $comment = wfMsgForContent(
408 'mergehistory-autocomment',
409 $targetTitle->getPrefixedText(),
410 $destTitle->getPrefixedText()
413 $mwRedir = MagicWord
::get( 'redirect' );
414 $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $destTitle->getPrefixedText() . "]]\n";
415 $redirectArticle = new Article( $targetTitle );
416 $redirectRevision = new Revision( array(
417 'page' => $this->mTargetID
,
418 'comment' => $comment,
419 'text' => $redirectText ) );
420 $redirectRevision->insertOn( $dbw );
421 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision );
423 # Now, we record the link from the redirect to the new title.
424 # It should have no other outgoing links...
425 $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mDestID
), __METHOD__
);
426 $dbw->insert( 'pagelinks',
428 'pl_from' => $this->mDestID
,
429 'pl_namespace' => $destTitle->getNamespace(),
430 'pl_title' => $destTitle->getDBkey() ),
434 $targetTitle->invalidateCache(); // update histories
436 $destTitle->invalidateCache(); // update histories
437 # Check if this did anything
439 $wgOut->addWikiMsg( 'mergehistory-fail' );
443 $log = new LogPage( 'merge' );
445 'merge', $targetTitle, $this->mComment
,
446 array( $destTitle->getPrefixedText(), $timestampLimit )
450 wfMsgExt( 'mergehistory-success', array('parseinline'),
451 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText(), $count ) );
453 wfRunHooks( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
459 class MergeHistoryPager
extends ReverseChronologicalPager
{
460 public $mForm, $mConds;
462 function __construct( $form, $conds = array(), $source, $dest ) {
463 $this->mForm
= $form;
464 $this->mConds
= $conds;
465 $this->title
= $source;
466 $this->articleID
= $source->getArticleID();
468 $dbr = wfGetDB( DB_SLAVE
);
469 $maxtimestamp = $dbr->selectField(
471 'MIN(rev_timestamp)',
472 array( 'rev_page' => $dest->getArticleID() ),
475 $this->maxTimestamp
= $maxtimestamp;
477 parent
::__construct();
480 function getStartBody() {
481 wfProfileIn( __METHOD__
);
482 # Do a link batch query
483 $this->mResult
->seek( 0 );
484 $batch = new LinkBatch();
485 # Give some pointers to make (last) links
486 $this->mForm
->prevId
= array();
487 foreach ( $this->mResult
as $row ) {
488 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->rev_user_text
) );
489 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->rev_user_text
) );
491 $rev_id = isset( $rev_id ) ?
$rev_id : $row->rev_id
;
492 if( $rev_id > $row->rev_id
) {
493 $this->mForm
->prevId
[$rev_id] = $row->rev_id
;
494 } elseif( $rev_id < $row->rev_id
) {
495 $this->mForm
->prevId
[$row->rev_id
] = $rev_id;
498 $rev_id = $row->rev_id
;
502 $this->mResult
->seek( 0 );
504 wfProfileOut( __METHOD__
);
508 function formatRow( $row ) {
509 return $this->mForm
->formatRevisionRow( $row );
512 function getQueryInfo() {
513 $conds = $this->mConds
;
514 $conds['rev_page'] = $this->articleID
;
515 $conds[] = 'page_id = rev_page';
516 $conds[] = "rev_timestamp < {$this->maxTimestamp}";
518 'tables' => array( 'revision', 'page' ),
520 'rev_minor_edit', 'rev_timestamp', 'rev_user', 'rev_user_text',
521 'rev_comment', 'rev_id', 'rev_page', 'rev_parent_id',
522 'rev_text_id', 'rev_len', 'rev_deleted'
528 function getIndexField() {
529 return 'rev_timestamp';