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'] = $this->msg( 'last' )->escaped();
83 public function execute( $par ) {
84 $this->checkPermissions();
85 $this->checkReadOnly();
87 $this->loadRequestParams();
90 $this->outputHeader();
92 if ( $this->mTargetID
&& $this->mDestID
&& $this->mAction
== 'submit' && $this->mMerge
) {
98 if ( !$this->mSubmitted
) {
99 $this->showMergeForm();
105 if ( !$this->mTargetObj
instanceof Title
) {
106 $errors[] = $this->msg( 'mergehistory-invalid-source' )->parseAsBlock();
107 } elseif ( !$this->mTargetObj
->exists() ) {
108 $errors[] = $this->msg( 'mergehistory-no-source', array( 'parse' ),
109 wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() )
113 if ( !$this->mDestObj
instanceof Title
) {
114 $errors[] = $this->msg( 'mergehistory-invalid-destination' )->parseAsBlock();
115 } elseif ( !$this->mDestObj
->exists() ) {
116 $errors[] = $this->msg( 'mergehistory-no-destination', array( 'parse' ),
117 wfEscapeWikiText( $this->mDestObj
->getPrefixedText() )
121 if ( $this->mTargetObj
&& $this->mDestObj
&& $this->mTargetObj
->equals( $this->mDestObj
) ) {
122 $errors[] = $this->msg( 'mergehistory-same-destination' )->parseAsBlock();
125 if ( count( $errors ) ) {
126 $this->showMergeForm();
127 $this->getOutput()->addHTML( implode( "\n", $errors ) );
129 $this->showHistory();
133 function showMergeForm() {
136 $this->getOutput()->addWikiMsg( 'mergehistory-header' );
138 $this->getOutput()->addHTML(
139 Xml
::openElement( 'form', array(
141 'action' => $wgScript ) ) .
143 Xml
::element( 'legend', array(),
144 $this->msg( 'mergehistory-box' )->text() ) .
145 Html
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
146 Html
::hidden( 'submitted', '1' ) .
147 Html
::hidden( 'mergepoint', $this->mTimestamp
) .
148 Xml
::openElement( 'table' ) .
150 <td>' . Xml
::label( $this->msg( 'mergehistory-from' )->text(), 'target' ) . '</td>
151 <td>' . Xml
::input( 'target', 30, $this->mTarget
, array( 'id' => 'target' ) ) . '</td>
153 <td>' . Xml
::label( $this->msg( 'mergehistory-into' )->text(), 'dest' ) . '</td>
154 <td>' . Xml
::input( 'dest', 30, $this->mDest
, array( 'id' => 'dest' ) ) . '</td>
156 Xml
::submitButton( $this->msg( 'mergehistory-go' )->text() ) .
158 Xml
::closeElement( 'table' ) .
164 private function showHistory() {
165 $this->showMergeForm();
167 # List all stored revisions
168 $revisions = new MergeHistoryPager(
169 $this, array(), $this->mTargetObj
, $this->mDestObj
171 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
173 $out = $this->getOutput();
174 $titleObj = $this->getTitle();
175 $action = $titleObj->getLocalURL( array( 'action' => 'submit' ) );
176 # Start the form here
177 $top = Xml
::openElement(
185 $out->addHTML( $top );
187 if ( $haveRevisions ) {
188 # Format the user-visible controls (comment field, submission button)
189 # in a nice little table
191 Xml
::openElement( 'fieldset' ) .
192 $this->msg( 'mergehistory-merge', $this->mTargetObj
->getPrefixedText(),
193 $this->mDestObj
->getPrefixedText() )->parse() .
194 Xml
::openElement( 'table', array( 'id' => 'mw-mergehistory-table' ) ) .
196 <td class="mw-label">' .
197 Xml
::label( $this->msg( 'mergehistory-reason' )->text(), 'wpComment' ) .
199 <td class="mw-input">' .
200 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
205 <td class="mw-submit">' .
206 Xml
::submitButton( $this->msg( 'mergehistory-submit' )->text(), array( 'name' => 'merge', 'id' => 'mw-merge-submit' ) ) .
209 Xml
::closeElement( 'table' ) .
210 Xml
::closeElement( 'fieldset' );
212 $out->addHTML( $table );
216 '<h2 id="mw-mergehistory">' .
217 $this->msg( 'mergehistory-list' )->escaped() . "</h2>\n"
220 if ( $haveRevisions ) {
221 $out->addHTML( $revisions->getNavigationBar() );
222 $out->addHTML( '<ul>' );
223 $out->addHTML( $revisions->getBody() );
224 $out->addHTML( '</ul>' );
225 $out->addHTML( $revisions->getNavigationBar() );
227 $out->addWikiMsg( 'mergehistory-empty' );
230 # Show relevant lines from the merge log:
231 $mergeLogPage = new LogPage( 'merge' );
232 $out->addHTML( '<h2>' . $mergeLogPage->getName()->escaped() . "</h2>\n" );
233 LogEventsList
::showLogExtract( $out, 'merge', $this->mTargetObj
);
235 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
236 # Such would happen if a page was renamed after the form loaded, but before submit
237 $misc = Html
::hidden( 'targetID', $this->mTargetObj
->getArticleID() );
238 $misc .= Html
::hidden( 'destID', $this->mDestObj
->getArticleID() );
239 $misc .= Html
::hidden( 'target', $this->mTarget
);
240 $misc .= Html
::hidden( 'dest', $this->mDest
);
241 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
242 $misc .= Xml
::closeElement( 'form' );
243 $out->addHTML( $misc );
248 function formatRevisionRow( $row ) {
249 $rev = new Revision( $row );
252 $last = $this->message
['last'];
254 $ts = wfTimestamp( TS_MW
, $row->rev_timestamp
);
255 $checkBox = Xml
::radio( 'mergepoint', $ts, false );
257 $user = $this->getUser();
259 $pageLink = Linker
::linkKnown(
261 htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) ),
263 array( 'oldid' => $rev->getId() )
265 if ( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
266 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
270 if ( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
271 $last = $this->message
['last'];
272 } elseif ( isset( $this->prevId
[$row->rev_id
] ) ) {
273 $last = Linker
::linkKnown(
275 $this->message
['last'],
278 'diff' => $row->rev_id
,
279 'oldid' => $this->prevId
[$row->rev_id
]
284 $userLink = Linker
::revUserTools( $rev );
286 $size = $row->rev_len
;
287 if ( !is_null( $size ) ) {
288 $stxt = Linker
::formatRevisionSize( $size );
290 $comment = Linker
::revComment( $rev );
292 return Html
::rawElement( 'li', array(),
293 $this->msg( 'mergehistory-revisionrow' )->rawParams( $checkBox, $last, $pageLink, $userLink, $stxt, $comment )->escaped() );
297 # Get the titles directly from the IDs, in case the target page params
298 # were spoofed. The queries are done based on the IDs, so it's best to
299 # keep it consistent...
300 $targetTitle = Title
::newFromID( $this->mTargetID
);
301 $destTitle = Title
::newFromID( $this->mDestID
);
302 if ( is_null( $targetTitle ) ||
is_null( $destTitle ) ) {
303 return false; // validate these
305 if ( $targetTitle->getArticleID() == $destTitle->getArticleID() ) {
308 # Verify that this timestamp is valid
309 # Must be older than the destination page
310 $dbw = wfGetDB( DB_MASTER
);
311 # Get timestamp into DB format
312 $this->mTimestamp
= $this->mTimestamp ?
$dbw->timestamp( $this->mTimestamp
) : '';
313 # Max timestamp should be min of destination page
314 $maxtimestamp = $dbw->selectField(
316 'MIN(rev_timestamp)',
317 array( 'rev_page' => $this->mDestID
),
320 # Destination page must exist with revisions
321 if ( !$maxtimestamp ) {
322 $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' );
339 # Update the revisions
340 if ( $this->mTimestamp
) {
341 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
342 $timestampLimit = wfTimestamp( TS_MW
, $this->mTimestamp
);
344 $timewhere = "rev_timestamp <= {$maxtimestamp}";
345 $timestampLimit = wfTimestamp( TS_MW
, $lasttimestamp );
350 array( 'rev_page' => $this->mDestID
),
351 array( 'rev_page' => $this->mTargetID
, $timewhere ),
355 $count = $dbw->affectedRows();
356 # Make the source page a redirect if no revisions are left
357 $haveRevisions = $dbw->selectField(
360 array( 'rev_page' => $this->mTargetID
),
362 array( 'FOR UPDATE' )
364 if ( !$haveRevisions ) {
365 if ( $this->mComment
) {
366 $comment = $this->msg(
367 'mergehistory-comment',
368 $targetTitle->getPrefixedText(),
369 $destTitle->getPrefixedText(),
371 )->inContentLanguage()->text();
373 $comment = $this->msg(
374 'mergehistory-autocomment',
375 $targetTitle->getPrefixedText(),
376 $destTitle->getPrefixedText()
377 )->inContentLanguage()->text();
380 $contentHandler = ContentHandler
::getForTitle( $targetTitle );
381 $redirectContent = $contentHandler->makeRedirectContent( $destTitle );
383 if ( $redirectContent ) {
384 $redirectPage = WikiPage
::factory( $targetTitle );
385 $redirectRevision = new Revision( array(
386 'title' => $targetTitle,
387 'page' => $this->mTargetID
,
388 'comment' => $comment,
389 'content' => $redirectContent ) );
390 $redirectRevision->insertOn( $dbw );
391 $redirectPage->updateRevisionOn( $dbw, $redirectRevision );
393 # Now, we record the link from the redirect to the new title.
394 # It should have no other outgoing links...
395 $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mDestID
), __METHOD__
);
396 $dbw->insert( 'pagelinks',
398 'pl_from' => $this->mDestID
,
399 'pl_namespace' => $destTitle->getNamespace(),
400 'pl_title' => $destTitle->getDBkey() ),
404 // would be nice to show a warning if we couldn't create a redirect
407 $targetTitle->invalidateCache(); // update histories
409 $destTitle->invalidateCache(); // update histories
410 # Check if this did anything
412 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
417 $log = new LogPage( 'merge' );
419 'merge', $targetTitle, $this->mComment
,
420 array( $destTitle->getPrefixedText(), $timestampLimit )
423 $this->getOutput()->addWikiMsg( 'mergehistory-success',
424 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText(), $count );
426 wfRunHooks( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
431 protected function getGroupName() {
436 class MergeHistoryPager
extends ReverseChronologicalPager
{
437 public $mForm, $mConds;
439 function __construct( $form, $conds = array(), $source, $dest ) {
440 $this->mForm
= $form;
441 $this->mConds
= $conds;
442 $this->title
= $source;
443 $this->articleID
= $source->getArticleID();
445 $dbr = wfGetDB( DB_SLAVE
);
446 $maxtimestamp = $dbr->selectField(
448 'MIN(rev_timestamp)',
449 array( 'rev_page' => $dest->getArticleID() ),
452 $this->maxTimestamp
= $maxtimestamp;
454 parent
::__construct( $form->getContext() );
457 function getStartBody() {
458 wfProfileIn( __METHOD__
);
459 # Do a link batch query
460 $this->mResult
->seek( 0 );
461 $batch = new LinkBatch();
462 # Give some pointers to make (last) links
463 $this->mForm
->prevId
= array();
464 foreach ( $this->mResult
as $row ) {
465 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->user_name
) );
466 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->user_name
) );
468 $rev_id = isset( $rev_id ) ?
$rev_id : $row->rev_id
;
469 if ( $rev_id > $row->rev_id
) {
470 $this->mForm
->prevId
[$rev_id] = $row->rev_id
;
471 } elseif ( $rev_id < $row->rev_id
) {
472 $this->mForm
->prevId
[$row->rev_id
] = $rev_id;
475 $rev_id = $row->rev_id
;
479 $this->mResult
->seek( 0 );
481 wfProfileOut( __METHOD__
);
486 function formatRow( $row ) {
487 return $this->mForm
->formatRevisionRow( $row );
490 function getQueryInfo() {
491 $conds = $this->mConds
;
492 $conds['rev_page'] = $this->articleID
;
493 $conds[] = "rev_timestamp < {$this->maxTimestamp}";
496 'tables' => array( 'revision', 'page', 'user' ),
497 'fields' => array_merge( Revision
::selectFields(), Revision
::selectUserFields() ),
499 'join_conds' => array(
500 'page' => Revision
::pageJoinCond(),
501 'user' => Revision
::userJoinCond() )
505 function getIndexField() {
506 return 'rev_timestamp';