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 # Get the titles directly from the IDs, in case the target page params
301 # were spoofed. The queries are done based on the IDs, so it's best to
302 # keep it consistent...
303 $targetTitle = Title
::newFromID( $this->mTargetID
);
304 $destTitle = Title
::newFromID( $this->mDestID
);
305 if( is_null( $targetTitle ) ||
is_null( $destTitle ) ) {
306 return false; // validate these
308 if( $targetTitle->getArticleId() == $destTitle->getArticleId() ) {
311 # Verify that this timestamp is valid
312 # Must be older than the destination page
313 $dbw = wfGetDB( DB_MASTER
);
314 # Get timestamp into DB format
315 $this->mTimestamp
= $this->mTimestamp ?
$dbw->timestamp( $this->mTimestamp
) : '';
316 # Max timestamp should be min of destination page
317 $maxtimestamp = $dbw->selectField(
319 'MIN(rev_timestamp)',
320 array( 'rev_page' => $this->mDestID
),
323 # Destination page must exist with revisions
324 if( !$maxtimestamp ) {
325 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
328 # Get the latest timestamp of the source
329 $lasttimestamp = $dbw->selectField(
330 array( 'page', 'revision' ),
332 array( 'page_id' => $this->mTargetID
, 'page_latest = rev_id' ),
335 # $this->mTimestamp must be older than $maxtimestamp
336 if( $this->mTimestamp
>= $maxtimestamp ) {
337 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
340 # Update the revisions
341 if( $this->mTimestamp
) {
342 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
343 $timestampLimit = wfTimestamp( TS_MW
, $this->mTimestamp
);
345 $timewhere = "rev_timestamp <= {$maxtimestamp}";
346 $timestampLimit = wfTimestamp( TS_MW
, $lasttimestamp );
351 array( 'rev_page' => $this->mDestID
),
352 array( 'rev_page' => $this->mTargetID
, $timewhere ),
356 $count = $dbw->affectedRows();
357 # Make the source page a redirect if no revisions are left
358 $haveRevisions = $dbw->selectField(
361 array( 'rev_page' => $this->mTargetID
),
363 array( 'FOR UPDATE' )
365 if( !$haveRevisions ) {
366 if( $this->mComment
) {
367 $comment = wfMsgForContent(
368 'mergehistory-comment',
369 $targetTitle->getPrefixedText(),
370 $destTitle->getPrefixedText(),
374 $comment = wfMsgForContent(
375 'mergehistory-autocomment',
376 $targetTitle->getPrefixedText(),
377 $destTitle->getPrefixedText()
380 $mwRedir = MagicWord
::get( 'redirect' );
381 $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $destTitle->getPrefixedText() . "]]\n";
382 $redirectArticle = new Article( $targetTitle );
383 $redirectRevision = new Revision( array(
384 'page' => $this->mTargetID
,
385 'comment' => $comment,
386 'text' => $redirectText ) );
387 $redirectRevision->insertOn( $dbw );
388 $redirectArticle->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 $targetTitle->invalidateCache(); // update histories
403 $destTitle->invalidateCache(); // update histories
404 # Check if this did anything
406 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
410 $log = new LogPage( 'merge' );
412 'merge', $targetTitle, $this->mComment
,
413 array( $destTitle->getPrefixedText(), $timestampLimit )
416 $this->getOutput()->addHTML(
417 wfMsgExt( 'mergehistory-success', array('parseinline'),
418 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText(), $count ) );
420 wfRunHooks( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
426 class MergeHistoryPager
extends ReverseChronologicalPager
{
427 public $mForm, $mConds;
429 function __construct( $form, $conds = array(), $source, $dest ) {
430 $this->mForm
= $form;
431 $this->mConds
= $conds;
432 $this->title
= $source;
433 $this->articleID
= $source->getArticleID();
435 $dbr = wfGetDB( DB_SLAVE
);
436 $maxtimestamp = $dbr->selectField(
438 'MIN(rev_timestamp)',
439 array( 'rev_page' => $dest->getArticleID() ),
442 $this->maxTimestamp
= $maxtimestamp;
444 parent
::__construct( $form->getContext() );
447 function getStartBody() {
448 wfProfileIn( __METHOD__
);
449 # Do a link batch query
450 $this->mResult
->seek( 0 );
451 $batch = new LinkBatch();
452 # Give some pointers to make (last) links
453 $this->mForm
->prevId
= array();
454 foreach ( $this->mResult
as $row ) {
455 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->rev_user_text
) );
456 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->rev_user_text
) );
458 $rev_id = isset( $rev_id ) ?
$rev_id : $row->rev_id
;
459 if( $rev_id > $row->rev_id
) {
460 $this->mForm
->prevId
[$rev_id] = $row->rev_id
;
461 } elseif( $rev_id < $row->rev_id
) {
462 $this->mForm
->prevId
[$row->rev_id
] = $rev_id;
465 $rev_id = $row->rev_id
;
469 $this->mResult
->seek( 0 );
471 wfProfileOut( __METHOD__
);
475 function formatRow( $row ) {
476 return $this->mForm
->formatRevisionRow( $row );
479 function getQueryInfo() {
480 $conds = $this->mConds
;
481 $conds['rev_page'] = $this->articleID
;
482 $conds[] = 'page_id = rev_page';
483 $conds[] = "rev_timestamp < {$this->maxTimestamp}";
485 'tables' => array( 'revision', 'page' ),
487 'rev_minor_edit', 'rev_timestamp', 'rev_user', 'rev_user_text',
488 'rev_comment', 'rev_id', 'rev_page', 'rev_parent_id',
489 'rev_text_id', 'rev_len', 'rev_deleted'
495 function getIndexField() {
496 return 'rev_timestamp';