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;
32 var $mTargetObj, $mDestObj;
34 public function __construct() {
35 parent
::__construct( 'MergeHistory', 'mergehistory' );
38 private function loadRequestParams( $request ) {
40 $this->mAction
= $request->getVal( 'action' );
41 $this->mTarget
= $request->getVal( 'target' );
42 $this->mDest
= $request->getVal( 'dest' );
43 $this->mSubmitted
= $request->getBool( 'submitted' );
45 $this->mTargetID
= intval( $request->getVal( 'targetID' ) );
46 $this->mDestID
= intval( $request->getVal( 'destID' ) );
47 $this->mTimestamp
= $request->getVal( 'mergepoint' );
48 if( !preg_match("/[0-9]{14}/",$this->mTimestamp
) ) {
49 $this->mTimestamp
= '';
51 $this->mComment
= $request->getText( 'wpComment' );
53 $this->mMerge
= $request->wasPosted() && $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
55 if( $this->mSubmitted
) {
56 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
57 $this->mDestObj
= Title
::newFromURL( $this->mDest
);
59 $this->mTargetObj
= null;
60 $this->mDestObj
= null;
62 $this->preCacheMessages();
66 * As we use the same small set of messages in various methods and that
67 * they are called often, we call them once and save them in $this->message
69 function preCacheMessages() {
70 // Precache various messages
71 if( !isset( $this->message
) ) {
72 $this->message
['last'] = wfMsgExt( 'last', array( 'escape' ) );
76 function execute( $par ) {
77 global $wgOut, $wgRequest, $wgUser;
80 $wgOut->readOnlyPage();
84 if( !$this->userCanExecute( $wgUser ) ) {
85 $this->displayRestrictionError();
89 $this->loadRequestParams( $wgRequest );
92 $this->outputHeader();
94 if( $this->mTargetID
&& $this->mDestID
&& $this->mAction
=="submit" && $this->mMerge
) {
95 return $this->merge();
98 if ( !$this->mSubmitted
) {
99 $this->showMergeForm();
104 if ( !$this->mTargetObj
instanceof Title
) {
105 $errors[] = wfMsgExt( 'mergehistory-invalid-source', array( 'parse' ) );
106 } elseif( !$this->mTargetObj
->exists() ) {
107 $errors[] = wfMsgExt( 'mergehistory-no-source', array( 'parse' ),
108 wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() )
112 if ( !$this->mDestObj
instanceof Title
) {
113 $errors[] = wfMsgExt( 'mergehistory-invalid-destination', array( 'parse' ) );
114 } elseif( !$this->mDestObj
->exists() ) {
115 $errors[] = wfMsgExt( 'mergehistory-no-destination', array( 'parse' ),
116 wfEscapeWikiText( $this->mDestObj
->getPrefixedText() )
120 if ( $this->mTargetObj
&& $this->mDestObj
&& $this->mTargetObj
->equals( $this->mDestObj
) ) {
121 $errors[] = wfMsgExt( 'mergehistory-same-destination', array( 'parse' ) );
124 if ( count( $errors ) ) {
125 $this->showMergeForm();
126 $wgOut->addHTML( implode( "\n", $errors ) );
128 $this->showHistory();
133 function showMergeForm() {
134 global $wgOut, $wgScript;
136 $wgOut->addWikiMsg( 'mergehistory-header' );
139 Xml
::openElement( 'form', array(
141 'action' => $wgScript ) ) .
143 Xml
::element( 'legend', array(),
144 wfMsg( 'mergehistory-box' ) ) .
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( wfMsg( 'mergehistory-from' ), 'target' )."</td>
151 <td>".Xml
::input( 'target', 30, $this->mTarget
, array('id'=>'target') )."</td>
153 <td>".Xml
::label( wfMsg( 'mergehistory-into' ), 'dest' )."</td>
154 <td>".Xml
::input( 'dest', 30, $this->mDest
, array('id'=>'dest') )."</td>
156 Xml
::submitButton( wfMsg( 'mergehistory-go' ) ) .
158 Xml
::closeElement( 'table' ) .
163 private function showHistory() {
164 global $wgUser, $wgOut;
166 $this->sk
= $wgUser->getSkin();
168 $wgOut->setPagetitle( wfMsg( "mergehistory" ) );
170 $this->showMergeForm();
172 # List all stored revisions
173 $revisions = new MergeHistoryPager( $this, array(), $this->mTargetObj
, $this->mDestObj
);
174 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
176 $titleObj = $this->getTitle();
177 $action = $titleObj->getLocalURL( array( 'action' => 'submit' ) );
178 # Start the form here
179 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'merge' ) );
180 $wgOut->addHTML( $top );
182 if( $haveRevisions ) {
183 # Format the user-visible controls (comment field, submission button)
184 # in a nice little table
186 Xml
::openElement( 'fieldset' ) .
187 wfMsgExt( 'mergehistory-merge', array('parseinline'),
188 $this->mTargetObj
->getPrefixedText(), $this->mDestObj
->getPrefixedText() ) .
189 Xml
::openElement( 'table', array( 'id' => 'mw-mergehistory-table' ) ) .
191 <td class='mw-label'>" .
192 Xml
::label( wfMsg( 'mergehistory-reason' ), 'wpComment' ) .
194 <td class='mw-input'>" .
195 Xml
::input( 'wpComment', 50, $this->mComment
, array('id' => 'wpComment') ) .
200 <td class='mw-submit'>" .
201 Xml
::submitButton( wfMsg( 'mergehistory-submit' ), array( 'name' => 'merge', 'id' => 'mw-merge-submit' ) ) .
204 Xml
::closeElement( 'table' ) .
205 Xml
::closeElement( 'fieldset' );
207 $wgOut->addHTML( $table );
210 $wgOut->addHTML( "<h2 id=\"mw-mergehistory\">" . wfMsgHtml( "mergehistory-list" ) . "</h2>\n" );
212 if( $haveRevisions ) {
213 $wgOut->addHTML( $revisions->getNavigationBar() );
214 $wgOut->addHTML( "<ul>" );
215 $wgOut->addHTML( $revisions->getBody() );
216 $wgOut->addHTML( "</ul>" );
217 $wgOut->addHTML( $revisions->getNavigationBar() );
219 $wgOut->addWikiMsg( "mergehistory-empty" );
222 # Show relevant lines from the deletion log:
223 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'merge' ) ) . "</h2>\n" );
224 LogEventsList
::showLogExtract( $wgOut, 'merge', $this->mTargetObj
->getPrefixedText() );
226 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
227 # Such would happen if a page was renamed after the form loaded, but before submit
228 $misc = Html
::hidden( 'targetID', $this->mTargetObj
->getArticleID() );
229 $misc .= Html
::hidden( 'destID', $this->mDestObj
->getArticleID() );
230 $misc .= Html
::hidden( 'target', $this->mTarget
);
231 $misc .= Html
::hidden( 'dest', $this->mDest
);
232 $misc .= Html
::hidden( 'wpEditToken', $wgUser->editToken() );
233 $misc .= Xml
::closeElement( 'form' );
234 $wgOut->addHTML( $misc );
239 function formatRevisionRow( $row ) {
242 $rev = new Revision( $row );
245 $last = $this->message
['last'];
247 $ts = wfTimestamp( TS_MW
, $row->rev_timestamp
);
248 $checkBox = Xml
::radio( "mergepoint", $ts, false );
250 $pageLink = $this->sk
->linkKnown(
252 htmlspecialchars( $wgLang->timeanddate( $ts ) ),
254 array( 'oldid' => $rev->getId() )
256 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
257 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
261 if( !$rev->userCan( Revision
::DELETED_TEXT
) )
262 $last = $this->message
['last'];
263 else if( isset($this->prevId
[$row->rev_id
]) )
264 $last = $this->sk
->linkKnown(
266 $this->message
['last'],
269 'diff' => $row->rev_id
,
270 'oldid' => $this->prevId
[$row->rev_id
]
274 $userLink = $this->sk
->revUserTools( $rev );
276 if(!is_null($size = $row->rev_len
)) {
277 $stxt = $this->sk
->formatRevisionSize( $size );
279 $comment = $this->sk
->revComment( $rev );
281 return "<li>$checkBox ($last) $pageLink . . $userLink $stxt $comment</li>";
285 * Fetch revision text link if it's available to all users
288 function getPageLink( $row, $titleObj, $ts, $target ) {
291 if( !$this->userCan($row, Revision
::DELETED_TEXT
) ) {
292 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
294 $link = $this->sk
->linkKnown(
296 $wgLang->timeanddate( $ts, true ),
303 if( $this->isDeleted($row, Revision
::DELETED_TEXT
) )
304 $link = '<span class="history-deleted">' . $link . '</span>';
311 # Get the titles directly from the IDs, in case the target page params
312 # were spoofed. The queries are done based on the IDs, so it's best to
313 # keep it consistent...
314 $targetTitle = Title
::newFromID( $this->mTargetID
);
315 $destTitle = Title
::newFromID( $this->mDestID
);
316 if( is_null($targetTitle) ||
is_null($destTitle) )
317 return false; // validate these
318 if( $targetTitle->getArticleId() == $destTitle->getArticleId() )
320 # Verify that this timestamp is valid
321 # Must be older than the destination page
322 $dbw = wfGetDB( DB_MASTER
);
323 # Get timestamp into DB format
324 $this->mTimestamp
= $this->mTimestamp ?
$dbw->timestamp($this->mTimestamp
) : '';
325 # Max timestamp should be min of destination page
326 $maxtimestamp = $dbw->selectField( 'revision', 'MIN(rev_timestamp)',
327 array('rev_page' => $this->mDestID
),
329 # Destination page must exist with revisions
330 if( !$maxtimestamp ) {
331 $wgOut->addWikiMsg('mergehistory-fail');
334 # Get the latest timestamp of the source
335 $lasttimestamp = $dbw->selectField( array('page','revision'),
337 array('page_id' => $this->mTargetID
, 'page_latest = rev_id' ),
339 # $this->mTimestamp must be older than $maxtimestamp
340 if( $this->mTimestamp
>= $maxtimestamp ) {
341 $wgOut->addWikiMsg('mergehistory-fail');
344 # Update the revisions
345 if( $this->mTimestamp
) {
346 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
347 $TimestampLimit = wfTimestamp(TS_MW
,$this->mTimestamp
);
349 $timewhere = "rev_timestamp <= {$maxtimestamp}";
350 $TimestampLimit = wfTimestamp(TS_MW
,$lasttimestamp);
353 $dbw->update( 'revision',
354 array( 'rev_page' => $this->mDestID
),
355 array( 'rev_page' => $this->mTargetID
,
359 $count = $dbw->affectedRows();
360 # Make the source page a redirect if no revisions are left
361 $haveRevisions = $dbw->selectField( 'revision',
363 array( 'rev_page' => $this->mTargetID
),
365 array( 'FOR UPDATE' ) );
366 if( !$haveRevisions ) {
367 if( $this->mComment
) {
368 $comment = wfMsgForContent( 'mergehistory-comment', $targetTitle->getPrefixedText(),
369 $destTitle->getPrefixedText(), $this->mComment
);
371 $comment = wfMsgForContent( 'mergehistory-autocomment', $targetTitle->getPrefixedText(),
372 $destTitle->getPrefixedText() );
374 $mwRedir = MagicWord
::get( 'redirect' );
375 $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $destTitle->getPrefixedText() . "]]\n";
376 $redirectArticle = new Article( $targetTitle );
377 $redirectRevision = new Revision( array(
378 'page' => $this->mTargetID
,
379 'comment' => $comment,
380 'text' => $redirectText ) );
381 $redirectRevision->insertOn( $dbw );
382 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision );
384 # Now, we record the link from the redirect to the new title.
385 # It should have no other outgoing links...
386 $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mDestID
), __METHOD__
);
387 $dbw->insert( 'pagelinks',
389 'pl_from' => $this->mDestID
,
390 'pl_namespace' => $destTitle->getNamespace(),
391 'pl_title' => $destTitle->getDBkey() ),
394 $targetTitle->invalidateCache(); // update histories
396 $destTitle->invalidateCache(); // update histories
397 # Check if this did anything
399 $wgOut->addWikiMsg('mergehistory-fail');
403 $log = new LogPage( 'merge' );
404 $log->addEntry( 'merge', $targetTitle, $this->mComment
,
405 array($destTitle->getPrefixedText(),$TimestampLimit) );
407 $wgOut->addHTML( wfMsgExt( 'mergehistory-success', array('parseinline'),
408 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText(), $count ) );
410 wfRunHooks( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
416 class MergeHistoryPager
extends ReverseChronologicalPager
{
417 public $mForm, $mConds;
419 function __construct( $form, $conds = array(), $source, $dest ) {
420 $this->mForm
= $form;
421 $this->mConds
= $conds;
422 $this->title
= $source;
423 $this->articleID
= $source->getArticleID();
425 $dbr = wfGetDB( DB_SLAVE
);
426 $maxtimestamp = $dbr->selectField( 'revision', 'MIN(rev_timestamp)',
427 array('rev_page' => $dest->getArticleID() ),
429 $this->maxTimestamp
= $maxtimestamp;
431 parent
::__construct();
434 function getStartBody() {
435 wfProfileIn( __METHOD__
);
436 # Do a link batch query
437 $this->mResult
->seek( 0 );
438 $batch = new LinkBatch();
439 # Give some pointers to make (last) links
440 $this->mForm
->prevId
= array();
441 foreach ( $this->mResult
as $row ) {
442 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->rev_user_text
) );
443 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->rev_user_text
) );
445 $rev_id = isset($rev_id) ?
$rev_id : $row->rev_id
;
446 if( $rev_id > $row->rev_id
)
447 $this->mForm
->prevId
[$rev_id] = $row->rev_id
;
448 else if( $rev_id < $row->rev_id
)
449 $this->mForm
->prevId
[$row->rev_id
] = $rev_id;
451 $rev_id = $row->rev_id
;
455 $this->mResult
->seek( 0 );
457 wfProfileOut( __METHOD__
);
461 function formatRow( $row ) {
462 return $this->mForm
->formatRevisionRow( $row );
465 function getQueryInfo() {
466 $conds = $this->mConds
;
467 $conds['rev_page'] = $this->articleID
;
468 $conds[] = 'page_id = rev_page';
469 $conds[] = "rev_timestamp < {$this->maxTimestamp}";
471 'tables' => array('revision','page'),
472 'fields' => array( 'rev_minor_edit', 'rev_timestamp', 'rev_user', 'rev_user_text', 'rev_comment',
473 'rev_id', 'rev_page', 'rev_parent_id', 'rev_text_id', 'rev_len', 'rev_deleted' ),
478 function getIndexField() {
479 return 'rev_timestamp';