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
) {
97 if ( !$this->mSubmitted
) {
98 $this->showMergeForm();
103 if ( !$this->mTargetObj
instanceof Title
) {
104 $errors[] = $this->msg( 'mergehistory-invalid-source' )->parseAsBlock();
105 } elseif ( !$this->mTargetObj
->exists() ) {
106 $errors[] = $this->msg( 'mergehistory-no-source', array( 'parse' ),
107 wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() )
111 if ( !$this->mDestObj
instanceof Title
) {
112 $errors[] = $this->msg( 'mergehistory-invalid-destination' )->parseAsBlock();
113 } elseif ( !$this->mDestObj
->exists() ) {
114 $errors[] = $this->msg( 'mergehistory-no-destination', array( 'parse' ),
115 wfEscapeWikiText( $this->mDestObj
->getPrefixedText() )
119 if ( $this->mTargetObj
&& $this->mDestObj
&& $this->mTargetObj
->equals( $this->mDestObj
) ) {
120 $errors[] = $this->msg( 'mergehistory-same-destination' )->parseAsBlock();
123 if ( count( $errors ) ) {
124 $this->showMergeForm();
125 $this->getOutput()->addHTML( implode( "\n", $errors ) );
127 $this->showHistory();
132 function showMergeForm() {
135 $this->getOutput()->addWikiMsg( 'mergehistory-header' );
137 $this->getOutput()->addHTML(
138 Xml
::openElement( 'form', array(
140 'action' => $wgScript ) ) .
142 Xml
::element( 'legend', array(),
143 $this->msg( 'mergehistory-box' )->text() ) .
144 Html
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
145 Html
::hidden( 'submitted', '1' ) .
146 Html
::hidden( 'mergepoint', $this->mTimestamp
) .
147 Xml
::openElement( 'table' ) .
149 <td>' . Xml
::label( $this->msg( 'mergehistory-from' )->text(), 'target' ) . '</td>
150 <td>' . Xml
::input( 'target', 30, $this->mTarget
, array( 'id' => 'target' ) ) . '</td>
152 <td>' . Xml
::label( $this->msg( 'mergehistory-into' )->text(), 'dest' ) . '</td>
153 <td>' . Xml
::input( 'dest', 30, $this->mDest
, array( 'id' => 'dest' ) ) . '</td>
155 Xml
::submitButton( $this->msg( 'mergehistory-go' )->text() ) .
157 Xml
::closeElement( 'table' ) .
163 private function showHistory() {
164 $this->showMergeForm();
166 # List all stored revisions
167 $revisions = new MergeHistoryPager(
168 $this, array(), $this->mTargetObj
, $this->mDestObj
170 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
172 $out = $this->getOutput();
173 $titleObj = $this->getTitle();
174 $action = $titleObj->getLocalURL( array( 'action' => 'submit' ) );
175 # Start the form here
176 $top = Xml
::openElement(
184 $out->addHTML( $top );
186 if ( $haveRevisions ) {
187 # Format the user-visible controls (comment field, submission button)
188 # in a nice little table
190 Xml
::openElement( 'fieldset' ) .
191 $this->msg( 'mergehistory-merge', $this->mTargetObj
->getPrefixedText(),
192 $this->mDestObj
->getPrefixedText() )->parse() .
193 Xml
::openElement( 'table', array( 'id' => 'mw-mergehistory-table' ) ) .
195 <td class="mw-label">' .
196 Xml
::label( $this->msg( 'mergehistory-reason' )->text(), 'wpComment' ) .
198 <td class="mw-input">' .
199 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
204 <td class="mw-submit">' .
205 Xml
::submitButton( $this->msg( 'mergehistory-submit' )->text(), array( 'name' => 'merge', 'id' => 'mw-merge-submit' ) ) .
208 Xml
::closeElement( 'table' ) .
209 Xml
::closeElement( 'fieldset' );
211 $out->addHTML( $table );
215 '<h2 id="mw-mergehistory">' .
216 $this->msg( 'mergehistory-list' )->escaped() . "</h2>\n"
219 if ( $haveRevisions ) {
220 $out->addHTML( $revisions->getNavigationBar() );
221 $out->addHTML( '<ul>' );
222 $out->addHTML( $revisions->getBody() );
223 $out->addHTML( '</ul>' );
224 $out->addHTML( $revisions->getNavigationBar() );
226 $out->addWikiMsg( 'mergehistory-empty' );
229 # Show relevant lines from the merge log:
230 $mergeLogPage = new LogPage( 'merge' );
231 $out->addHTML( '<h2>' . $mergeLogPage->getName()->escaped() . "</h2>\n" );
232 LogEventsList
::showLogExtract( $out, 'merge', $this->mTargetObj
);
234 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
235 # Such would happen if a page was renamed after the form loaded, but before submit
236 $misc = Html
::hidden( 'targetID', $this->mTargetObj
->getArticleID() );
237 $misc .= Html
::hidden( 'destID', $this->mDestObj
->getArticleID() );
238 $misc .= Html
::hidden( 'target', $this->mTarget
);
239 $misc .= Html
::hidden( 'dest', $this->mDest
);
240 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
241 $misc .= Xml
::closeElement( 'form' );
242 $out->addHTML( $misc );
247 function formatRevisionRow( $row ) {
248 $rev = new Revision( $row );
251 $last = $this->message
['last'];
253 $ts = wfTimestamp( TS_MW
, $row->rev_timestamp
);
254 $checkBox = Xml
::radio( 'mergepoint', $ts, false );
256 $user = $this->getUser();
258 $pageLink = Linker
::linkKnown(
260 htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) ),
262 array( 'oldid' => $rev->getId() )
264 if ( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
265 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
269 if ( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
270 $last = $this->message
['last'];
271 } elseif ( isset( $this->prevId
[$row->rev_id
] ) ) {
272 $last = Linker
::linkKnown(
274 $this->message
['last'],
277 'diff' => $row->rev_id
,
278 'oldid' => $this->prevId
[$row->rev_id
]
283 $userLink = Linker
::revUserTools( $rev );
285 $size = $row->rev_len
;
286 if ( !is_null( $size ) ) {
287 $stxt = Linker
::formatRevisionSize( $size );
289 $comment = Linker
::revComment( $rev );
291 return Html
::rawElement( 'li', array(),
292 $this->msg( 'mergehistory-revisionrow' )->rawParams( $checkBox, $last, $pageLink, $userLink, $stxt, $comment )->escaped() );
296 # Get the titles directly from the IDs, in case the target page params
297 # were spoofed. The queries are done based on the IDs, so it's best to
298 # keep it consistent...
299 $targetTitle = Title
::newFromID( $this->mTargetID
);
300 $destTitle = Title
::newFromID( $this->mDestID
);
301 if ( is_null( $targetTitle ) ||
is_null( $destTitle ) ) {
302 return false; // validate these
304 if ( $targetTitle->getArticleID() == $destTitle->getArticleID() ) {
307 # Verify that this timestamp is valid
308 # Must be older than the destination page
309 $dbw = wfGetDB( DB_MASTER
);
310 # Get timestamp into DB format
311 $this->mTimestamp
= $this->mTimestamp ?
$dbw->timestamp( $this->mTimestamp
) : '';
312 # Max timestamp should be min of destination page
313 $maxtimestamp = $dbw->selectField(
315 'MIN(rev_timestamp)',
316 array( 'rev_page' => $this->mDestID
),
319 # Destination page must exist with revisions
320 if ( !$maxtimestamp ) {
321 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
324 # Get the latest timestamp of the source
325 $lasttimestamp = $dbw->selectField(
326 array( 'page', 'revision' ),
328 array( 'page_id' => $this->mTargetID
, 'page_latest = rev_id' ),
331 # $this->mTimestamp must be older than $maxtimestamp
332 if ( $this->mTimestamp
>= $maxtimestamp ) {
333 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
336 # Update the revisions
337 if ( $this->mTimestamp
) {
338 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
339 $timestampLimit = wfTimestamp( TS_MW
, $this->mTimestamp
);
341 $timewhere = "rev_timestamp <= {$maxtimestamp}";
342 $timestampLimit = wfTimestamp( TS_MW
, $lasttimestamp );
347 array( 'rev_page' => $this->mDestID
),
348 array( 'rev_page' => $this->mTargetID
, $timewhere ),
352 $count = $dbw->affectedRows();
353 # Make the source page a redirect if no revisions are left
354 $haveRevisions = $dbw->selectField(
357 array( 'rev_page' => $this->mTargetID
),
359 array( 'FOR UPDATE' )
361 if ( !$haveRevisions ) {
362 if ( $this->mComment
) {
363 $comment = $this->msg(
364 'mergehistory-comment',
365 $targetTitle->getPrefixedText(),
366 $destTitle->getPrefixedText(),
368 )->inContentLanguage()->text();
370 $comment = $this->msg(
371 'mergehistory-autocomment',
372 $targetTitle->getPrefixedText(),
373 $destTitle->getPrefixedText()
374 )->inContentLanguage()->text();
377 $contentHandler = ContentHandler
::getForTitle( $targetTitle );
378 $redirectContent = $contentHandler->makeRedirectContent( $destTitle );
380 if ( $redirectContent ) {
381 $redirectPage = WikiPage
::factory( $targetTitle );
382 $redirectRevision = new Revision( array(
383 'title' => $targetTitle,
384 'page' => $this->mTargetID
,
385 'comment' => $comment,
386 'content' => $redirectContent ) );
387 $redirectRevision->insertOn( $dbw );
388 $redirectPage->updateRevisionOn( $dbw, $redirectRevision );
390 # Now, we record the link from the redirect to the new title.
391 # It should have no other outgoing links...
392 $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mDestID
), __METHOD__
);
393 $dbw->insert( 'pagelinks',
395 'pl_from' => $this->mDestID
,
396 'pl_namespace' => $destTitle->getNamespace(),
397 'pl_title' => $destTitle->getDBkey() ),
401 // would be nice to show a warning if we couldn't create a redirect
404 $targetTitle->invalidateCache(); // update histories
406 $destTitle->invalidateCache(); // update histories
407 # Check if this did anything
409 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
413 $log = new LogPage( 'merge' );
415 'merge', $targetTitle, $this->mComment
,
416 array( $destTitle->getPrefixedText(), $timestampLimit )
419 $this->getOutput()->addWikiMsg( 'mergehistory-success',
420 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText(), $count );
422 wfRunHooks( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
427 protected function getGroupName() {
432 class MergeHistoryPager
extends ReverseChronologicalPager
{
433 public $mForm, $mConds;
435 function __construct( $form, $conds = array(), $source, $dest ) {
436 $this->mForm
= $form;
437 $this->mConds
= $conds;
438 $this->title
= $source;
439 $this->articleID
= $source->getArticleID();
441 $dbr = wfGetDB( DB_SLAVE
);
442 $maxtimestamp = $dbr->selectField(
444 'MIN(rev_timestamp)',
445 array( 'rev_page' => $dest->getArticleID() ),
448 $this->maxTimestamp
= $maxtimestamp;
450 parent
::__construct( $form->getContext() );
453 function getStartBody() {
454 wfProfileIn( __METHOD__
);
455 # Do a link batch query
456 $this->mResult
->seek( 0 );
457 $batch = new LinkBatch();
458 # Give some pointers to make (last) links
459 $this->mForm
->prevId
= array();
460 foreach ( $this->mResult
as $row ) {
461 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->user_name
) );
462 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->user_name
) );
464 $rev_id = isset( $rev_id ) ?
$rev_id : $row->rev_id
;
465 if ( $rev_id > $row->rev_id
) {
466 $this->mForm
->prevId
[$rev_id] = $row->rev_id
;
467 } elseif ( $rev_id < $row->rev_id
) {
468 $this->mForm
->prevId
[$row->rev_id
] = $rev_id;
471 $rev_id = $row->rev_id
;
475 $this->mResult
->seek( 0 );
477 wfProfileOut( __METHOD__
);
481 function formatRow( $row ) {
482 return $this->mForm
->formatRevisionRow( $row );
485 function getQueryInfo() {
486 $conds = $this->mConds
;
487 $conds['rev_page'] = $this->articleID
;
488 $conds[] = "rev_timestamp < {$this->maxTimestamp}";
490 'tables' => array( 'revision', 'page', 'user' ),
491 'fields' => array_merge( Revision
::selectFields(), Revision
::selectUserFields() ),
493 'join_conds' => array(
494 'page' => Revision
::pageJoinCond(),
495 'user' => Revision
::userJoinCond() )
499 function getIndexField() {
500 return 'rev_timestamp';