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 $this->showMergeForm();
172 # List all stored revisions
173 $revisions = new MergeHistoryPager(
174 $this, array(), $this->mTargetObj
, $this->mDestObj
176 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
178 $out = $this->getOutput();
179 $titleObj = $this->getTitle();
180 $action = $titleObj->getLocalURL( array( 'action' => 'submit' ) );
181 # Start the form here
182 $top = Xml
::openElement(
190 $out->addHTML( $top );
192 if( $haveRevisions ) {
193 # Format the user-visible controls (comment field, submission button)
194 # in a nice little table
196 Xml
::openElement( 'fieldset' ) .
197 wfMsgExt( 'mergehistory-merge', array( 'parseinline' ),
198 $this->mTargetObj
->getPrefixedText(), $this->mDestObj
->getPrefixedText() ) .
199 Xml
::openElement( 'table', array( 'id' => 'mw-mergehistory-table' ) ) .
201 <td class="mw-label">' .
202 Xml
::label( wfMsg( 'mergehistory-reason' ), 'wpComment' ) .
204 <td class="mw-input">' .
205 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
210 <td class="mw-submit">' .
211 Xml
::submitButton( wfMsg( 'mergehistory-submit' ), array( 'name' => 'merge', 'id' => 'mw-merge-submit' ) ) .
214 Xml
::closeElement( 'table' ) .
215 Xml
::closeElement( 'fieldset' );
217 $out->addHTML( $table );
221 '<h2 id="mw-mergehistory">' .
222 wfMsgHtml( 'mergehistory-list' ) . "</h2>\n"
225 if( $haveRevisions ) {
226 $out->addHTML( $revisions->getNavigationBar() );
227 $out->addHTML( '<ul>' );
228 $out->addHTML( $revisions->getBody() );
229 $out->addHTML( '</ul>' );
230 $out->addHTML( $revisions->getNavigationBar() );
232 $out->addWikiMsg( 'mergehistory-empty' );
235 # Show relevant lines from the deletion log:
236 $out->addHTML( '<h2>' . htmlspecialchars( LogPage
::logName( 'merge' ) ) . "</h2>\n" );
237 LogEventsList
::showLogExtract( $out, 'merge', $this->mTargetObj
);
239 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
240 # Such would happen if a page was renamed after the form loaded, but before submit
241 $misc = Html
::hidden( 'targetID', $this->mTargetObj
->getArticleID() );
242 $misc .= Html
::hidden( 'destID', $this->mDestObj
->getArticleID() );
243 $misc .= Html
::hidden( 'target', $this->mTarget
);
244 $misc .= Html
::hidden( 'dest', $this->mDest
);
245 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->editToken() );
246 $misc .= Xml
::closeElement( 'form' );
247 $out->addHTML( $misc );
252 function formatRevisionRow( $row ) {
253 $rev = new Revision( $row );
256 $last = $this->message
['last'];
258 $ts = wfTimestamp( TS_MW
, $row->rev_timestamp
);
259 $checkBox = Xml
::radio( 'mergepoint', $ts, false );
261 $pageLink = Linker
::linkKnown(
263 htmlspecialchars( $this->getLang()->timeanddate( $ts ) ),
265 array( 'oldid' => $rev->getId() )
267 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
268 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
272 if( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
273 $last = $this->message
['last'];
274 } elseif( isset( $this->prevId
[$row->rev_id
] ) ) {
275 $last = Linker
::linkKnown(
277 $this->message
['last'],
280 'diff' => $row->rev_id
,
281 'oldid' => $this->prevId
[$row->rev_id
]
286 $userLink = Linker
::revUserTools( $rev );
288 $size = $row->rev_len
;
289 if( !is_null( $size ) ) {
290 $stxt = Linker
::formatRevisionSize( $size );
292 $comment = Linker
::revComment( $rev );
294 return "<li>$checkBox ($last) $pageLink . . $userLink $stxt $comment</li>";
298 # Get the titles directly from the IDs, in case the target page params
299 # were spoofed. The queries are done based on the IDs, so it's best to
300 # keep it consistent...
301 $targetTitle = Title
::newFromID( $this->mTargetID
);
302 $destTitle = Title
::newFromID( $this->mDestID
);
303 if( is_null( $targetTitle ) ||
is_null( $destTitle ) ) {
304 return false; // validate these
306 if( $targetTitle->getArticleId() == $destTitle->getArticleId() ) {
309 # Verify that this timestamp is valid
310 # Must be older than the destination page
311 $dbw = wfGetDB( DB_MASTER
);
312 # Get timestamp into DB format
313 $this->mTimestamp
= $this->mTimestamp ?
$dbw->timestamp( $this->mTimestamp
) : '';
314 # Max timestamp should be min of destination page
315 $maxtimestamp = $dbw->selectField(
317 'MIN(rev_timestamp)',
318 array( 'rev_page' => $this->mDestID
),
321 # Destination page must exist with revisions
322 if( !$maxtimestamp ) {
323 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
326 # Get the latest timestamp of the source
327 $lasttimestamp = $dbw->selectField(
328 array( 'page', 'revision' ),
330 array( 'page_id' => $this->mTargetID
, 'page_latest = rev_id' ),
333 # $this->mTimestamp must be older than $maxtimestamp
334 if( $this->mTimestamp
>= $maxtimestamp ) {
335 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
338 # Update the revisions
339 if( $this->mTimestamp
) {
340 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
341 $timestampLimit = wfTimestamp( TS_MW
, $this->mTimestamp
);
343 $timewhere = "rev_timestamp <= {$maxtimestamp}";
344 $timestampLimit = wfTimestamp( TS_MW
, $lasttimestamp );
349 array( 'rev_page' => $this->mDestID
),
350 array( 'rev_page' => $this->mTargetID
, $timewhere ),
354 $count = $dbw->affectedRows();
355 # Make the source page a redirect if no revisions are left
356 $haveRevisions = $dbw->selectField(
359 array( 'rev_page' => $this->mTargetID
),
361 array( 'FOR UPDATE' )
363 if( !$haveRevisions ) {
364 if( $this->mComment
) {
365 $comment = wfMsgForContent(
366 'mergehistory-comment',
367 $targetTitle->getPrefixedText(),
368 $destTitle->getPrefixedText(),
372 $comment = wfMsgForContent(
373 'mergehistory-autocomment',
374 $targetTitle->getPrefixedText(),
375 $destTitle->getPrefixedText()
378 $mwRedir = MagicWord
::get( 'redirect' );
379 $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $destTitle->getPrefixedText() . "]]\n";
380 $redirectArticle = new Article( $targetTitle );
381 $redirectRevision = new Revision( array(
382 'page' => $this->mTargetID
,
383 'comment' => $comment,
384 'text' => $redirectText ) );
385 $redirectRevision->insertOn( $dbw );
386 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision );
388 # Now, we record the link from the redirect to the new title.
389 # It should have no other outgoing links...
390 $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mDestID
), __METHOD__
);
391 $dbw->insert( 'pagelinks',
393 'pl_from' => $this->mDestID
,
394 'pl_namespace' => $destTitle->getNamespace(),
395 'pl_title' => $destTitle->getDBkey() ),
399 $targetTitle->invalidateCache(); // update histories
401 $destTitle->invalidateCache(); // update histories
402 # Check if this did anything
404 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
408 $log = new LogPage( 'merge' );
410 'merge', $targetTitle, $this->mComment
,
411 array( $destTitle->getPrefixedText(), $timestampLimit )
414 $this->getOutput()->addHTML(
415 wfMsgExt( 'mergehistory-success', array('parseinline'),
416 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText(), $count ) );
418 wfRunHooks( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
424 class MergeHistoryPager
extends ReverseChronologicalPager
{
425 public $mForm, $mConds;
427 function __construct( $form, $conds = array(), $source, $dest ) {
428 $this->mForm
= $form;
429 $this->mConds
= $conds;
430 $this->title
= $source;
431 $this->articleID
= $source->getArticleID();
433 $dbr = wfGetDB( DB_SLAVE
);
434 $maxtimestamp = $dbr->selectField(
436 'MIN(rev_timestamp)',
437 array( 'rev_page' => $dest->getArticleID() ),
440 $this->maxTimestamp
= $maxtimestamp;
442 parent
::__construct( $form->getContext() );
445 function getStartBody() {
446 wfProfileIn( __METHOD__
);
447 # Do a link batch query
448 $this->mResult
->seek( 0 );
449 $batch = new LinkBatch();
450 # Give some pointers to make (last) links
451 $this->mForm
->prevId
= array();
452 foreach ( $this->mResult
as $row ) {
453 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->user_name
) );
454 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->user_name
) );
456 $rev_id = isset( $rev_id ) ?
$rev_id : $row->rev_id
;
457 if( $rev_id > $row->rev_id
) {
458 $this->mForm
->prevId
[$rev_id] = $row->rev_id
;
459 } elseif( $rev_id < $row->rev_id
) {
460 $this->mForm
->prevId
[$row->rev_id
] = $rev_id;
463 $rev_id = $row->rev_id
;
467 $this->mResult
->seek( 0 );
469 wfProfileOut( __METHOD__
);
473 function formatRow( $row ) {
474 return $this->mForm
->formatRevisionRow( $row );
477 function getQueryInfo() {
478 $conds = $this->mConds
;
479 $conds['rev_page'] = $this->articleID
;
480 $conds[] = "rev_timestamp < {$this->maxTimestamp}";
482 'tables' => array( 'revision', 'page', 'user' ),
483 'fields' => array_merge( Revision
::selectFields(), Revision
::selectUserFields() ),
485 'join_conds' => array(
486 'page' => array( 'INNER JOIN', 'rev_page = page_id' ),
487 'user' => array( 'LEFT JOIN', 'user_id = rev_user' ) )
491 function getIndexField() {
492 return 'rev_timestamp';