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 deletion log:
230 $out->addHTML( '<h2>' . htmlspecialchars( LogPage
::logName( 'merge' ) ) . "</h2>\n" );
231 LogEventsList
::showLogExtract( $out, 'merge', $this->mTargetObj
);
233 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
234 # Such would happen if a page was renamed after the form loaded, but before submit
235 $misc = Html
::hidden( 'targetID', $this->mTargetObj
->getArticleID() );
236 $misc .= Html
::hidden( 'destID', $this->mDestObj
->getArticleID() );
237 $misc .= Html
::hidden( 'target', $this->mTarget
);
238 $misc .= Html
::hidden( 'dest', $this->mDest
);
239 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
240 $misc .= Xml
::closeElement( 'form' );
241 $out->addHTML( $misc );
246 function formatRevisionRow( $row ) {
247 $rev = new Revision( $row );
250 $last = $this->message
['last'];
252 $ts = wfTimestamp( TS_MW
, $row->rev_timestamp
);
253 $checkBox = Xml
::radio( 'mergepoint', $ts, false );
255 $pageLink = Linker
::linkKnown(
257 htmlspecialchars( $this->getLanguage()->timeanddate( $ts ) ),
259 array( 'oldid' => $rev->getId() )
261 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
262 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
266 if( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
267 $last = $this->message
['last'];
268 } elseif( isset( $this->prevId
[$row->rev_id
] ) ) {
269 $last = Linker
::linkKnown(
271 $this->message
['last'],
274 'diff' => $row->rev_id
,
275 'oldid' => $this->prevId
[$row->rev_id
]
280 $userLink = Linker
::revUserTools( $rev );
282 $size = $row->rev_len
;
283 if( !is_null( $size ) ) {
284 $stxt = Linker
::formatRevisionSize( $size );
286 $comment = Linker
::revComment( $rev );
288 $revisionRow = $this->msg( 'mergehistory-revisionrow' )->rawParams( $checkBox, $last, $pageLink, $userLink, $stxt, $comment)->escaped();
289 $revisionRow .= "<li>" . $line . "</li>";
295 # Get the titles directly from the IDs, in case the target page params
296 # were spoofed. The queries are done based on the IDs, so it's best to
297 # keep it consistent...
298 $targetTitle = Title
::newFromID( $this->mTargetID
);
299 $destTitle = Title
::newFromID( $this->mDestID
);
300 if( is_null( $targetTitle ) ||
is_null( $destTitle ) ) {
301 return false; // validate these
303 if( $targetTitle->getArticleID() == $destTitle->getArticleID() ) {
306 # Verify that this timestamp is valid
307 # Must be older than the destination page
308 $dbw = wfGetDB( DB_MASTER
);
309 # Get timestamp into DB format
310 $this->mTimestamp
= $this->mTimestamp ?
$dbw->timestamp( $this->mTimestamp
) : '';
311 # Max timestamp should be min of destination page
312 $maxtimestamp = $dbw->selectField(
314 'MIN(rev_timestamp)',
315 array( 'rev_page' => $this->mDestID
),
318 # Destination page must exist with revisions
319 if( !$maxtimestamp ) {
320 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
323 # Get the latest timestamp of the source
324 $lasttimestamp = $dbw->selectField(
325 array( 'page', 'revision' ),
327 array( 'page_id' => $this->mTargetID
, 'page_latest = rev_id' ),
330 # $this->mTimestamp must be older than $maxtimestamp
331 if( $this->mTimestamp
>= $maxtimestamp ) {
332 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
335 # Update the revisions
336 if( $this->mTimestamp
) {
337 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
338 $timestampLimit = wfTimestamp( TS_MW
, $this->mTimestamp
);
340 $timewhere = "rev_timestamp <= {$maxtimestamp}";
341 $timestampLimit = wfTimestamp( TS_MW
, $lasttimestamp );
346 array( 'rev_page' => $this->mDestID
),
347 array( 'rev_page' => $this->mTargetID
, $timewhere ),
351 $count = $dbw->affectedRows();
352 # Make the source page a redirect if no revisions are left
353 $haveRevisions = $dbw->selectField(
356 array( 'rev_page' => $this->mTargetID
),
358 array( 'FOR UPDATE' )
360 if( !$haveRevisions ) {
361 if( $this->mComment
) {
362 $comment = $this->msg(
363 'mergehistory-comment',
364 $targetTitle->getPrefixedText(),
365 $destTitle->getPrefixedText(),
367 )->inContentLanguage()->text();
369 $comment = $this->msg(
370 'mergehistory-autocomment',
371 $targetTitle->getPrefixedText(),
372 $destTitle->getPrefixedText()
373 )->inContentLanguage()->text();
375 $mwRedir = MagicWord
::get( 'redirect' );
376 $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $destTitle->getPrefixedText() . "]]\n";
377 $redirectPage = WikiPage
::factory( $targetTitle );
378 $redirectRevision = new Revision( array(
379 'page' => $this->mTargetID
,
380 'comment' => $comment,
381 'text' => $redirectText ) );
382 $redirectRevision->insertOn( $dbw );
383 $redirectPage->updateRevisionOn( $dbw, $redirectRevision );
385 # Now, we record the link from the redirect to the new title.
386 # It should have no other outgoing links...
387 $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mDestID
), __METHOD__
);
388 $dbw->insert( 'pagelinks',
390 'pl_from' => $this->mDestID
,
391 'pl_namespace' => $destTitle->getNamespace(),
392 'pl_title' => $destTitle->getDBkey() ),
396 $targetTitle->invalidateCache(); // update histories
398 $destTitle->invalidateCache(); // update histories
399 # Check if this did anything
401 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
405 $log = new LogPage( 'merge' );
407 'merge', $targetTitle, $this->mComment
,
408 array( $destTitle->getPrefixedText(), $timestampLimit )
411 $this->getOutput()->addWikiMsg( 'mergehistory-success',
412 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText(), $count );
414 wfRunHooks( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
420 class MergeHistoryPager
extends ReverseChronologicalPager
{
421 public $mForm, $mConds;
423 function __construct( $form, $conds = array(), $source, $dest ) {
424 $this->mForm
= $form;
425 $this->mConds
= $conds;
426 $this->title
= $source;
427 $this->articleID
= $source->getArticleID();
429 $dbr = wfGetDB( DB_SLAVE
);
430 $maxtimestamp = $dbr->selectField(
432 'MIN(rev_timestamp)',
433 array( 'rev_page' => $dest->getArticleID() ),
436 $this->maxTimestamp
= $maxtimestamp;
438 parent
::__construct( $form->getContext() );
441 function getStartBody() {
442 wfProfileIn( __METHOD__
);
443 # Do a link batch query
444 $this->mResult
->seek( 0 );
445 $batch = new LinkBatch();
446 # Give some pointers to make (last) links
447 $this->mForm
->prevId
= array();
448 foreach ( $this->mResult
as $row ) {
449 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->user_name
) );
450 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->user_name
) );
452 $rev_id = isset( $rev_id ) ?
$rev_id : $row->rev_id
;
453 if( $rev_id > $row->rev_id
) {
454 $this->mForm
->prevId
[$rev_id] = $row->rev_id
;
455 } elseif( $rev_id < $row->rev_id
) {
456 $this->mForm
->prevId
[$row->rev_id
] = $rev_id;
459 $rev_id = $row->rev_id
;
463 $this->mResult
->seek( 0 );
465 wfProfileOut( __METHOD__
);
469 function formatRow( $row ) {
470 return $this->mForm
->formatRevisionRow( $row );
473 function getQueryInfo() {
474 $conds = $this->mConds
;
475 $conds['rev_page'] = $this->articleID
;
476 $conds[] = "rev_timestamp < {$this->maxTimestamp}";
478 'tables' => array( 'revision', 'page', 'user' ),
479 'fields' => array_merge( Revision
::selectFields(), Revision
::selectUserFields() ),
481 'join_conds' => array(
482 'page' => Revision
::pageJoinCond(),
483 'user' => Revision
::userJoinCond() )
487 function getIndexField() {
488 return 'rev_timestamp';