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;
36 var $mTargetObj, $mDestObj;
38 public function __construct() {
39 parent
::__construct( 'MergeHistory', 'mergehistory' );
45 private function loadRequestParams() {
46 $request = $this->getRequest();
47 $this->mAction
= $request->getVal( 'action' );
48 $this->mTarget
= $request->getVal( 'target' );
49 $this->mDest
= $request->getVal( 'dest' );
50 $this->mSubmitted
= $request->getBool( 'submitted' );
52 $this->mTargetID
= intval( $request->getVal( 'targetID' ) );
53 $this->mDestID
= intval( $request->getVal( 'destID' ) );
54 $this->mTimestamp
= $request->getVal( 'mergepoint' );
55 if( !preg_match( '/[0-9]{14}/', $this->mTimestamp
) ) {
56 $this->mTimestamp
= '';
58 $this->mComment
= $request->getText( 'wpComment' );
60 $this->mMerge
= $request->wasPosted() && $this->getUser()->matchEditToken( $request->getVal( 'wpEditToken' ) );
62 if( $this->mSubmitted
) {
63 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
64 $this->mDestObj
= Title
::newFromURL( $this->mDest
);
66 $this->mTargetObj
= null;
67 $this->mDestObj
= null;
69 $this->preCacheMessages();
73 * As we use the same small set of messages in various methods and that
74 * they are called often, we call them once and save them in $this->message
76 function preCacheMessages() {
77 // Precache various messages
78 if( !isset( $this->message
) ) {
79 $this->message
['last'] = wfMsgExt( 'last', array( 'escape' ) );
83 public function execute( $par ) {
84 $user = $this->getUser();
85 if( !$this->userCanExecute( $user ) ) {
86 $this->displayRestrictionError();
91 throw new ReadOnlyError
;
94 $this->loadRequestParams();
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 $this->getOutput()->addHTML( implode( "\n", $errors ) );
133 $this->showHistory();
138 function showMergeForm() {
141 $this->getOutput()->addWikiMsg( 'mergehistory-header' );
143 $this->getOutput()->addHTML(
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 $out = $this->getOutput();
171 $out->setPageTitle( wfMsg( 'mergehistory' ) );
173 $this->showMergeForm();
175 # List all stored revisions
176 $revisions = new MergeHistoryPager(
177 $this, array(), $this->mTargetObj
, $this->mDestObj
179 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
181 $titleObj = $this->getTitle();
182 $action = $titleObj->getLocalURL( array( 'action' => 'submit' ) );
183 # Start the form here
184 $top = Xml
::openElement(
192 $out->addHTML( $top );
194 if( $haveRevisions ) {
195 # Format the user-visible controls (comment field, submission button)
196 # in a nice little table
198 Xml
::openElement( 'fieldset' ) .
199 wfMsgExt( 'mergehistory-merge', array( 'parseinline' ),
200 $this->mTargetObj
->getPrefixedText(), $this->mDestObj
->getPrefixedText() ) .
201 Xml
::openElement( 'table', array( 'id' => 'mw-mergehistory-table' ) ) .
203 <td class="mw-label">' .
204 Xml
::label( wfMsg( 'mergehistory-reason' ), 'wpComment' ) .
206 <td class="mw-input">' .
207 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
212 <td class="mw-submit">' .
213 Xml
::submitButton( wfMsg( 'mergehistory-submit' ), array( 'name' => 'merge', 'id' => 'mw-merge-submit' ) ) .
216 Xml
::closeElement( 'table' ) .
217 Xml
::closeElement( 'fieldset' );
219 $out->addHTML( $table );
223 '<h2 id="mw-mergehistory">' .
224 wfMsgHtml( 'mergehistory-list' ) . "</h2>\n"
227 if( $haveRevisions ) {
228 $out->addHTML( $revisions->getNavigationBar() );
229 $out->addHTML( '<ul>' );
230 $out->addHTML( $revisions->getBody() );
231 $out->addHTML( '</ul>' );
232 $out->addHTML( $revisions->getNavigationBar() );
234 $out->addWikiMsg( 'mergehistory-empty' );
237 # Show relevant lines from the deletion log:
238 $out->addHTML( '<h2>' . htmlspecialchars( LogPage
::logName( 'merge' ) ) . "</h2>\n" );
239 LogEventsList
::showLogExtract( $out, 'merge', $this->mTargetObj
);
241 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
242 # Such would happen if a page was renamed after the form loaded, but before submit
243 $misc = Html
::hidden( 'targetID', $this->mTargetObj
->getArticleID() );
244 $misc .= Html
::hidden( 'destID', $this->mDestObj
->getArticleID() );
245 $misc .= Html
::hidden( 'target', $this->mTarget
);
246 $misc .= Html
::hidden( 'dest', $this->mDest
);
247 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->editToken() );
248 $misc .= Xml
::closeElement( 'form' );
249 $out->addHTML( $misc );
254 function formatRevisionRow( $row ) {
255 $rev = new Revision( $row );
258 $last = $this->message
['last'];
260 $ts = wfTimestamp( TS_MW
, $row->rev_timestamp
);
261 $checkBox = Xml
::radio( 'mergepoint', $ts, false );
263 $pageLink = Linker
::linkKnown(
265 htmlspecialchars( $this->getLang()->timeanddate( $ts ) ),
267 array( 'oldid' => $rev->getId() )
269 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
270 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
274 if( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
275 $last = $this->message
['last'];
276 } elseif( isset( $this->prevId
[$row->rev_id
] ) ) {
277 $last = Linker
::linkKnown(
279 $this->message
['last'],
282 'diff' => $row->rev_id
,
283 'oldid' => $this->prevId
[$row->rev_id
]
288 $userLink = Linker
::revUserTools( $rev );
290 $size = $row->rev_len
;
291 if( !is_null( $size ) ) {
292 $stxt = Linker
::formatRevisionSize( $size );
294 $comment = Linker
::revComment( $rev );
296 return "<li>$checkBox ($last) $pageLink . . $userLink $stxt $comment</li>";
300 * Fetch revision text link if it's available to all users
303 function getPageLink( $row, $titleObj, $ts, $target ) {
304 if( !$this->userCan( $row, Revision
::DELETED_TEXT
) ) {
305 return '<span class="history-deleted">' .
306 $this->getLang()->timeanddate( $ts, true ) . '</span>';
308 $link = Linker
::linkKnown(
310 $this->getLang()->timeanddate( $ts, true ),
317 if( $this->isDeleted( $row, Revision
::DELETED_TEXT
) ) {
318 $link = '<span class="history-deleted">' . $link . '</span>';
325 # Get the titles directly from the IDs, in case the target page params
326 # were spoofed. The queries are done based on the IDs, so it's best to
327 # keep it consistent...
328 $targetTitle = Title
::newFromID( $this->mTargetID
);
329 $destTitle = Title
::newFromID( $this->mDestID
);
330 if( is_null( $targetTitle ) ||
is_null( $destTitle ) ) {
331 return false; // validate these
333 if( $targetTitle->getArticleId() == $destTitle->getArticleId() ) {
336 # Verify that this timestamp is valid
337 # Must be older than the destination page
338 $dbw = wfGetDB( DB_MASTER
);
339 # Get timestamp into DB format
340 $this->mTimestamp
= $this->mTimestamp ?
$dbw->timestamp( $this->mTimestamp
) : '';
341 # Max timestamp should be min of destination page
342 $maxtimestamp = $dbw->selectField(
344 'MIN(rev_timestamp)',
345 array( 'rev_page' => $this->mDestID
),
348 # Destination page must exist with revisions
349 if( !$maxtimestamp ) {
350 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
353 # Get the latest timestamp of the source
354 $lasttimestamp = $dbw->selectField(
355 array( 'page', 'revision' ),
357 array( 'page_id' => $this->mTargetID
, 'page_latest = rev_id' ),
360 # $this->mTimestamp must be older than $maxtimestamp
361 if( $this->mTimestamp
>= $maxtimestamp ) {
362 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
365 # Update the revisions
366 if( $this->mTimestamp
) {
367 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
368 $timestampLimit = wfTimestamp( TS_MW
, $this->mTimestamp
);
370 $timewhere = "rev_timestamp <= {$maxtimestamp}";
371 $timestampLimit = wfTimestamp( TS_MW
, $lasttimestamp );
376 array( 'rev_page' => $this->mDestID
),
377 array( 'rev_page' => $this->mTargetID
, $timewhere ),
381 $count = $dbw->affectedRows();
382 # Make the source page a redirect if no revisions are left
383 $haveRevisions = $dbw->selectField(
386 array( 'rev_page' => $this->mTargetID
),
388 array( 'FOR UPDATE' )
390 if( !$haveRevisions ) {
391 if( $this->mComment
) {
392 $comment = wfMsgForContent(
393 'mergehistory-comment',
394 $targetTitle->getPrefixedText(),
395 $destTitle->getPrefixedText(),
399 $comment = wfMsgForContent(
400 'mergehistory-autocomment',
401 $targetTitle->getPrefixedText(),
402 $destTitle->getPrefixedText()
405 $mwRedir = MagicWord
::get( 'redirect' );
406 $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $destTitle->getPrefixedText() . "]]\n";
407 $redirectArticle = new Article( $targetTitle );
408 $redirectRevision = new Revision( array(
409 'page' => $this->mTargetID
,
410 'comment' => $comment,
411 'text' => $redirectText ) );
412 $redirectRevision->insertOn( $dbw );
413 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision );
415 # Now, we record the link from the redirect to the new title.
416 # It should have no other outgoing links...
417 $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mDestID
), __METHOD__
);
418 $dbw->insert( 'pagelinks',
420 'pl_from' => $this->mDestID
,
421 'pl_namespace' => $destTitle->getNamespace(),
422 'pl_title' => $destTitle->getDBkey() ),
426 $targetTitle->invalidateCache(); // update histories
428 $destTitle->invalidateCache(); // update histories
429 # Check if this did anything
431 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
435 $log = new LogPage( 'merge' );
437 'merge', $targetTitle, $this->mComment
,
438 array( $destTitle->getPrefixedText(), $timestampLimit )
441 $this->getOutput()->addHTML(
442 wfMsgExt( 'mergehistory-success', array('parseinline'),
443 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText(), $count ) );
445 wfRunHooks( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
451 class MergeHistoryPager
extends ReverseChronologicalPager
{
452 public $mForm, $mConds;
454 function __construct( $form, $conds = array(), $source, $dest ) {
455 $this->mForm
= $form;
456 $this->mConds
= $conds;
457 $this->title
= $source;
458 $this->articleID
= $source->getArticleID();
460 $dbr = wfGetDB( DB_SLAVE
);
461 $maxtimestamp = $dbr->selectField(
463 'MIN(rev_timestamp)',
464 array( 'rev_page' => $dest->getArticleID() ),
467 $this->maxTimestamp
= $maxtimestamp;
469 parent
::__construct( $form->getContext() );
472 function getStartBody() {
473 wfProfileIn( __METHOD__
);
474 # Do a link batch query
475 $this->mResult
->seek( 0 );
476 $batch = new LinkBatch();
477 # Give some pointers to make (last) links
478 $this->mForm
->prevId
= array();
479 foreach ( $this->mResult
as $row ) {
480 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->rev_user_text
) );
481 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->rev_user_text
) );
483 $rev_id = isset( $rev_id ) ?
$rev_id : $row->rev_id
;
484 if( $rev_id > $row->rev_id
) {
485 $this->mForm
->prevId
[$rev_id] = $row->rev_id
;
486 } elseif( $rev_id < $row->rev_id
) {
487 $this->mForm
->prevId
[$row->rev_id
] = $rev_id;
490 $rev_id = $row->rev_id
;
494 $this->mResult
->seek( 0 );
496 wfProfileOut( __METHOD__
);
500 function formatRow( $row ) {
501 return $this->mForm
->formatRevisionRow( $row );
504 function getQueryInfo() {
505 $conds = $this->mConds
;
506 $conds['rev_page'] = $this->articleID
;
507 $conds[] = 'page_id = rev_page';
508 $conds[] = "rev_timestamp < {$this->maxTimestamp}";
510 'tables' => array( 'revision', 'page' ),
512 'rev_minor_edit', 'rev_timestamp', 'rev_user', 'rev_user_text',
513 'rev_comment', 'rev_id', 'rev_page', 'rev_parent_id',
514 'rev_text_id', 'rev_len', 'rev_deleted'
520 function getIndexField() {
521 return 'rev_timestamp';