3 * Special page allowing users with the appropriate permissions to
4 * merge article histories, with some restrictions
8 class SpecialMergeHistory
extends SpecialPage
{
9 var $mAction, $mTarget, $mDest, $mTimestamp, $mTargetID, $mDestID, $mComment;
10 var $mTargetObj, $mDestObj;
12 public function __construct() {
13 parent
::__construct( 'MergeHistory', 'mergehistory' );
16 private function loadRequestParams( $request ) {
18 $this->mAction
= $request->getVal( 'action' );
19 $this->mTarget
= $request->getVal( 'target' );
20 $this->mDest
= $request->getVal( 'dest' );
21 $this->mSubmitted
= $request->getBool( 'submitted' );
23 $this->mTargetID
= intval( $request->getVal( 'targetID' ) );
24 $this->mDestID
= intval( $request->getVal( 'destID' ) );
25 $this->mTimestamp
= $request->getVal( 'mergepoint' );
26 if( !preg_match("/[0-9]{14}/",$this->mTimestamp
) ) {
27 $this->mTimestamp
= '';
29 $this->mComment
= $request->getText( 'wpComment' );
31 $this->mMerge
= $request->wasPosted() && $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
33 if( $this->mSubmitted
) {
34 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
35 $this->mDestObj
= Title
::newFromURL( $this->mDest
);
37 $this->mTargetObj
= null;
38 $this->mDestObj
= null;
40 $this->preCacheMessages();
44 * As we use the same small set of messages in various methods and that
45 * they are called often, we call them once and save them in $this->message
47 function preCacheMessages() {
48 // Precache various messages
49 if( !isset( $this->message
) ) {
50 $this->message
['last'] = wfMsgExt( 'last', array( 'escape' ) );
54 function execute( $par ) {
55 global $wgOut, $wgRequest, $wgUser;
58 $wgOut->readOnlyPage();
62 if( !$this->userCanExecute( $wgUser ) ) {
63 $this->displayRestrictionError();
67 $this->loadRequestParams( $wgRequest );
70 $this->outputHeader();
72 if( $this->mTargetID
&& $this->mDestID
&& $this->mAction
=="submit" && $this->mMerge
) {
73 return $this->merge();
76 if ( !$this->mSubmitted
) {
77 $this->showMergeForm();
82 if ( !$this->mTargetObj
instanceof Title
) {
83 $errors[] = wfMsgExt( 'mergehistory-invalid-source', array( 'parse' ) );
84 } elseif( !$this->mTargetObj
->exists() ) {
85 $errors[] = wfMsgExt( 'mergehistory-no-source', array( 'parse' ),
86 wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() )
90 if ( !$this->mDestObj
instanceof Title
) {
91 $errors[] = wfMsgExt( 'mergehistory-invalid-destination', array( 'parse' ) );
92 } elseif( !$this->mDestObj
->exists() ) {
93 $errors[] = wfMsgExt( 'mergehistory-no-destination', array( 'parse' ),
94 wfEscapeWikiText( $this->mDestObj
->getPrefixedText() )
98 if ( $this->mTargetObj
&& $this->mDestObj
&& $this->mTargetObj
->equals( $this->mDestObj
) ) {
99 $errors[] = wfMsgExt( 'mergehistory-same-destination', array( 'parse' ) );
102 if ( count( $errors ) ) {
103 $this->showMergeForm();
104 $wgOut->addHTML( implode( "\n", $errors ) );
106 $this->showHistory();
111 function showMergeForm() {
112 global $wgOut, $wgScript;
114 $wgOut->addWikiMsg( 'mergehistory-header' );
117 Xml
::openElement( 'form', array(
119 'action' => $wgScript ) ) .
121 Xml
::element( 'legend', array(),
122 wfMsg( 'mergehistory-box' ) ) .
123 Xml
::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
124 Xml
::hidden( 'submitted', '1' ) .
125 Xml
::hidden( 'mergepoint', $this->mTimestamp
) .
126 Xml
::openElement( 'table' ) .
128 <td>".Xml
::label( wfMsg( 'mergehistory-from' ), 'target' )."</td>
129 <td>".Xml
::input( 'target', 30, $this->mTarget
, array('id'=>'target') )."</td>
131 <td>".Xml
::label( wfMsg( 'mergehistory-into' ), 'dest' )."</td>
132 <td>".Xml
::input( 'dest', 30, $this->mDest
, array('id'=>'dest') )."</td>
134 Xml
::submitButton( wfMsg( 'mergehistory-go' ) ) .
136 Xml
::closeElement( 'table' ) .
141 private function showHistory() {
142 global $wgLang, $wgUser, $wgOut;
144 $this->sk
= $wgUser->getSkin();
146 $wgOut->setPagetitle( wfMsg( "mergehistory" ) );
148 $this->showMergeForm();
150 # List all stored revisions
151 $revisions = new MergeHistoryPager( $this, array(), $this->mTargetObj
, $this->mDestObj
);
152 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
154 $titleObj = $this->getTitle();
155 $action = $titleObj->getLocalURL( array( 'action' => 'submit' ) );
156 # Start the form here
157 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'merge' ) );
158 $wgOut->addHTML( $top );
160 if( $haveRevisions ) {
161 # Format the user-visible controls (comment field, submission button)
162 # in a nice little table
164 Xml
::openElement( 'fieldset' ) .
165 wfMsgExt( 'mergehistory-merge', array('parseinline'),
166 $this->mTargetObj
->getPrefixedText(), $this->mDestObj
->getPrefixedText() ) .
167 Xml
::openElement( 'table', array( 'id' => 'mw-mergehistory-table' ) ) .
169 <td class='mw-label'>" .
170 Xml
::label( wfMsg( 'mergehistory-reason' ), 'wpComment' ) .
172 <td class='mw-input'>" .
173 Xml
::input( 'wpComment', 50, $this->mComment
, array('id' => 'wpComment') ) .
178 <td class='mw-submit'>" .
179 Xml
::submitButton( wfMsg( 'mergehistory-submit' ), array( 'name' => 'merge', 'id' => 'mw-merge-submit' ) ) .
182 Xml
::closeElement( 'table' ) .
183 Xml
::closeElement( 'fieldset' );
185 $wgOut->addHTML( $table );
188 $wgOut->addHTML( "<h2 id=\"mw-mergehistory\">" . wfMsgHtml( "mergehistory-list" ) . "</h2>\n" );
190 if( $haveRevisions ) {
191 $wgOut->addHTML( $revisions->getNavigationBar() );
192 $wgOut->addHTML( "<ul>" );
193 $wgOut->addHTML( $revisions->getBody() );
194 $wgOut->addHTML( "</ul>" );
195 $wgOut->addHTML( $revisions->getNavigationBar() );
197 $wgOut->addWikiMsg( "mergehistory-empty" );
200 # Show relevant lines from the deletion log:
201 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'merge' ) ) . "</h2>\n" );
202 LogEventsList
::showLogExtract( $wgOut, 'merge', $this->mTargetObj
->getPrefixedText() );
204 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
205 # Such would happen if a page was renamed after the form loaded, but before submit
206 $misc = Xml
::hidden( 'targetID', $this->mTargetObj
->getArticleID() );
207 $misc .= Xml
::hidden( 'destID', $this->mDestObj
->getArticleID() );
208 $misc .= Xml
::hidden( 'target', $this->mTarget
);
209 $misc .= Xml
::hidden( 'dest', $this->mDest
);
210 $misc .= Xml
::hidden( 'wpEditToken', $wgUser->editToken() );
211 $misc .= Xml
::closeElement( 'form' );
212 $wgOut->addHTML( $misc );
217 function formatRevisionRow( $row ) {
220 $rev = new Revision( $row );
223 $last = $this->message
['last'];
225 $ts = wfTimestamp( TS_MW
, $row->rev_timestamp
);
226 $checkBox = Xml
::radio( "mergepoint", $ts, false );
228 $pageLink = $this->sk
->linkKnown(
230 htmlspecialchars( $wgLang->timeanddate( $ts ) ),
232 array( 'oldid' => $rev->getId() )
234 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
235 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
239 if( !$rev->userCan( Revision
::DELETED_TEXT
) )
240 $last = $this->message
['last'];
241 else if( isset($this->prevId
[$row->rev_id
]) )
242 $last = $this->sk
->linkKnown(
244 $this->message
['last'],
247 'diff' => $row->rev_id
,
248 'oldid' => $this->prevId
[$row->rev_id
]
252 $userLink = $this->sk
->revUserTools( $rev );
254 if(!is_null($size = $row->rev_len
)) {
255 $stxt = $this->sk
->formatRevisionSize( $size );
257 $comment = $this->sk
->revComment( $rev );
259 return "<li>$checkBox ($last) $pageLink . . $userLink $stxt $comment</li>";
263 * Fetch revision text link if it's available to all users
266 function getPageLink( $row, $titleObj, $ts, $target ) {
269 if( !$this->userCan($row, Revision
::DELETED_TEXT
) ) {
270 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
272 $link = $this->sk
->linkKnown(
274 $wgLang->timeanddate( $ts, true ),
281 if( $this->isDeleted($row, Revision
::DELETED_TEXT
) )
282 $link = '<span class="history-deleted">' . $link . '</span>';
289 # Get the titles directly from the IDs, in case the target page params
290 # were spoofed. The queries are done based on the IDs, so it's best to
291 # keep it consistent...
292 $targetTitle = Title
::newFromID( $this->mTargetID
);
293 $destTitle = Title
::newFromID( $this->mDestID
);
294 if( is_null($targetTitle) ||
is_null($destTitle) )
295 return false; // validate these
296 if( $targetTitle->getArticleId() == $destTitle->getArticleId() )
298 # Verify that this timestamp is valid
299 # Must be older than the destination page
300 $dbw = wfGetDB( DB_MASTER
);
301 # Get timestamp into DB format
302 $this->mTimestamp
= $this->mTimestamp ?
$dbw->timestamp($this->mTimestamp
) : '';
303 # Max timestamp should be min of destination page
304 $maxtimestamp = $dbw->selectField( 'revision', 'MIN(rev_timestamp)',
305 array('rev_page' => $this->mDestID
),
307 # Destination page must exist with revisions
308 if( !$maxtimestamp ) {
309 $wgOut->addWikiMsg('mergehistory-fail');
312 # Get the latest timestamp of the source
313 $lasttimestamp = $dbw->selectField( array('page','revision'),
315 array('page_id' => $this->mTargetID
, 'page_latest = rev_id' ),
317 # $this->mTimestamp must be older than $maxtimestamp
318 if( $this->mTimestamp
>= $maxtimestamp ) {
319 $wgOut->addWikiMsg('mergehistory-fail');
322 # Update the revisions
323 if( $this->mTimestamp
) {
324 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
325 $TimestampLimit = wfTimestamp(TS_MW
,$this->mTimestamp
);
327 $timewhere = "rev_timestamp <= {$maxtimestamp}";
328 $TimestampLimit = wfTimestamp(TS_MW
,$lasttimestamp);
331 $dbw->update( 'revision',
332 array( 'rev_page' => $this->mDestID
),
333 array( 'rev_page' => $this->mTargetID
,
337 $count = $dbw->affectedRows();
338 # Make the source page a redirect if no revisions are left
339 $haveRevisions = $dbw->selectField( 'revision',
341 array( 'rev_page' => $this->mTargetID
),
343 array( 'FOR UPDATE' ) );
344 if( !$haveRevisions ) {
345 if( $this->mComment
) {
346 $comment = wfMsgForContent( 'mergehistory-comment', $targetTitle->getPrefixedText(),
347 $destTitle->getPrefixedText(), $this->mComment
);
349 $comment = wfMsgForContent( 'mergehistory-autocomment', $targetTitle->getPrefixedText(),
350 $destTitle->getPrefixedText() );
352 $mwRedir = MagicWord
::get( 'redirect' );
353 $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $destTitle->getPrefixedText() . "]]\n";
354 $redirectArticle = new Article( $targetTitle );
355 $redirectRevision = new Revision( array(
356 'page' => $this->mTargetID
,
357 'comment' => $comment,
358 'text' => $redirectText ) );
359 $redirectRevision->insertOn( $dbw );
360 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision );
362 # Now, we record the link from the redirect to the new title.
363 # It should have no other outgoing links...
364 $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mDestID
), __METHOD__
);
365 $dbw->insert( 'pagelinks',
367 'pl_from' => $this->mDestID
,
368 'pl_namespace' => $destTitle->getNamespace(),
369 'pl_title' => $destTitle->getDBkey() ),
372 $targetTitle->invalidateCache(); // update histories
374 $destTitle->invalidateCache(); // update histories
375 # Check if this did anything
377 $wgOut->addWikiMsg('mergehistory-fail');
381 $log = new LogPage( 'merge' );
382 $log->addEntry( 'merge', $targetTitle, $this->mComment
,
383 array($destTitle->getPrefixedText(),$TimestampLimit) );
385 $wgOut->addHTML( wfMsgExt( 'mergehistory-success', array('parseinline'),
386 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText(), $count ) );
388 wfRunHooks( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
394 class MergeHistoryPager
extends ReverseChronologicalPager
{
395 public $mForm, $mConds;
397 function __construct( $form, $conds = array(), $source, $dest ) {
398 $this->mForm
= $form;
399 $this->mConds
= $conds;
400 $this->title
= $source;
401 $this->articleID
= $source->getArticleID();
403 $dbr = wfGetDB( DB_SLAVE
);
404 $maxtimestamp = $dbr->selectField( 'revision', 'MIN(rev_timestamp)',
405 array('rev_page' => $dest->getArticleID() ),
407 $this->maxTimestamp
= $maxtimestamp;
409 parent
::__construct();
412 function getStartBody() {
413 wfProfileIn( __METHOD__
);
414 # Do a link batch query
415 $this->mResult
->seek( 0 );
416 $batch = new LinkBatch();
417 # Give some pointers to make (last) links
418 $this->mForm
->prevId
= array();
419 while( $row = $this->mResult
->fetchObject() ) {
420 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->rev_user_text
) );
421 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->rev_user_text
) );
423 $rev_id = isset($rev_id) ?
$rev_id : $row->rev_id
;
424 if( $rev_id > $row->rev_id
)
425 $this->mForm
->prevId
[$rev_id] = $row->rev_id
;
426 else if( $rev_id < $row->rev_id
)
427 $this->mForm
->prevId
[$row->rev_id
] = $rev_id;
429 $rev_id = $row->rev_id
;
433 $this->mResult
->seek( 0 );
435 wfProfileOut( __METHOD__
);
439 function formatRow( $row ) {
441 return $this->mForm
->formatRevisionRow( $row );
444 function getQueryInfo() {
445 $conds = $this->mConds
;
446 $conds['rev_page'] = $this->articleID
;
447 $conds[] = 'page_id = rev_page';
448 $conds[] = "rev_timestamp < {$this->maxTimestamp}";
450 'tables' => array('revision','page'),
451 'fields' => array( 'rev_minor_edit', 'rev_timestamp', 'rev_user', 'rev_user_text', 'rev_comment',
452 'rev_id', 'rev_page', 'rev_parent_id', 'rev_text_id', 'rev_len', 'rev_deleted' ),
457 function getIndexField() {
458 return 'rev_timestamp';