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;
57 if( !$wgUser->isAllowed( 'mergehistory' ) ) {
58 $wgOut->permissionRequired( 'mergehistory' );
62 $this->loadRequestParams( $wgRequest );
64 $wgOut->setPagetitle( wfMsgHtml( "mergehistory" ) );
66 if( $this->mTargetID
&& $this->mDestID
&& $this->mAction
=="submit" && $this->mMerge
) {
67 return $this->merge();
70 if ( !$this->mSubmitted
) {
71 $this->showMergeForm();
76 if ( !$this->mTargetObj
instanceof Title
) {
77 $errors[] = wfMsgExt( 'mergehistory-invalid-source', array( 'parse' ) );
78 } elseif( !$this->mTargetObj
->exists() ) {
79 $errors[] = wfMsgExt( 'mergehistory-no-source', array( 'parse' ),
80 wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() )
84 if ( !$this->mDestObj
instanceof Title
) {
85 $errors[] = wfMsgExt( 'mergehistory-invalid-destination', array( 'parse' ) );
86 } elseif( !$this->mDestObj
->exists() ) {
87 $errors[] = wfMsgExt( 'mergehistory-no-destination', array( 'parse' ),
88 wfEscapeWikiText( $this->mDestObj
->getPrefixedText() )
92 if ( $this->mTargetObj
&& $this->mDestObj
&& $this->mTargetObj
->equals( $this->mDestObj
) ) {
93 $errors[] = wfMsgExt( 'mergehistory-same-destination', array( 'parse' ) );
96 if ( count( $errors ) ) {
97 $this->showMergeForm();
98 $wgOut->addHTML( implode( "\n", $errors ) );
100 $this->showHistory();
105 function showMergeForm() {
106 global $wgOut, $wgScript;
108 $wgOut->addWikiMsg( 'mergehistory-header' );
111 Xml
::openElement( 'form', array(
113 'action' => $wgScript ) ) .
115 Xml
::element( 'legend', array(),
116 wfMsg( 'mergehistory-box' ) ) .
117 Xml
::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
118 Xml
::hidden( 'submitted', '1' ) .
119 Xml
::hidden( 'mergepoint', $this->mTimestamp
) .
120 Xml
::openElement( 'table' ) .
122 <td>".Xml
::label( wfMsg( 'mergehistory-from' ), 'target' )."</td>
123 <td>".Xml
::input( 'target', 30, $this->mTarget
, array('id'=>'target') )."</td>
125 <td>".Xml
::label( wfMsg( 'mergehistory-into' ), 'dest' )."</td>
126 <td>".Xml
::input( 'dest', 30, $this->mDest
, array('id'=>'dest') )."</td>
128 Xml
::submitButton( wfMsg( 'mergehistory-go' ) ) .
130 Xml
::closeElement( 'table' ) .
135 private function showHistory() {
136 global $wgLang, $wgUser, $wgOut;
138 $this->sk
= $wgUser->getSkin();
140 $wgOut->setPagetitle( wfMsg( "mergehistory" ) );
142 $this->showMergeForm();
144 # List all stored revisions
145 $revisions = new MergeHistoryPager( $this, array(), $this->mTargetObj
, $this->mDestObj
);
146 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
148 $titleObj = $this->getTitle();
149 $action = $titleObj->getLocalURL( array( 'action' => 'submit' ) );
150 # Start the form here
151 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'merge' ) );
152 $wgOut->addHTML( $top );
154 if( $haveRevisions ) {
155 # Format the user-visible controls (comment field, submission button)
156 # in a nice little table
158 Xml
::openElement( 'fieldset' ) .
159 wfMsgExt( 'mergehistory-merge', array('parseinline'),
160 $this->mTargetObj
->getPrefixedText(), $this->mDestObj
->getPrefixedText() ) .
161 Xml
::openElement( 'table', array( 'id' => 'mw-mergehistory-table' ) ) .
163 <td class='mw-label'>" .
164 Xml
::label( wfMsg( 'mergehistory-reason' ), 'wpComment' ) .
166 <td class='mw-input'>" .
167 Xml
::input( 'wpComment', 50, $this->mComment
, array('id' => 'wpComment') ) .
172 <td class='mw-submit'>" .
173 Xml
::submitButton( wfMsg( 'mergehistory-submit' ), array( 'name' => 'merge', 'id' => 'mw-merge-submit' ) ) .
176 Xml
::closeElement( 'table' ) .
177 Xml
::closeElement( 'fieldset' );
179 $wgOut->addHTML( $table );
182 $wgOut->addHTML( "<h2 id=\"mw-mergehistory\">" . wfMsgHtml( "mergehistory-list" ) . "</h2>\n" );
184 if( $haveRevisions ) {
185 $wgOut->addHTML( $revisions->getNavigationBar() );
186 $wgOut->addHTML( "<ul>" );
187 $wgOut->addHTML( $revisions->getBody() );
188 $wgOut->addHTML( "</ul>" );
189 $wgOut->addHTML( $revisions->getNavigationBar() );
191 $wgOut->addWikiMsg( "mergehistory-empty" );
194 # Show relevant lines from the deletion log:
195 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'merge' ) ) . "</h2>\n" );
196 LogEventsList
::showLogExtract( $wgOut, 'merge', $this->mTargetObj
->getPrefixedText() );
198 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
199 # Such would happen if a page was renamed after the form loaded, but before submit
200 $misc = Xml
::hidden( 'targetID', $this->mTargetObj
->getArticleID() );
201 $misc .= Xml
::hidden( 'destID', $this->mDestObj
->getArticleID() );
202 $misc .= Xml
::hidden( 'target', $this->mTarget
);
203 $misc .= Xml
::hidden( 'dest', $this->mDest
);
204 $misc .= Xml
::hidden( 'wpEditToken', $wgUser->editToken() );
205 $misc .= Xml
::closeElement( 'form' );
206 $wgOut->addHTML( $misc );
211 function formatRevisionRow( $row ) {
214 $rev = new Revision( $row );
217 $last = $this->message
['last'];
219 $ts = wfTimestamp( TS_MW
, $row->rev_timestamp
);
220 $checkBox = Xml
::radio( "mergepoint", $ts, false );
222 $pageLink = $this->sk
->linkKnown(
224 htmlspecialchars( $wgLang->timeanddate( $ts ) ),
226 array( 'oldid' => $rev->getId() )
228 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
229 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
233 if( !$rev->userCan( Revision
::DELETED_TEXT
) )
234 $last = $this->message
['last'];
235 else if( isset($this->prevId
[$row->rev_id
]) )
236 $last = $this->sk
->linkKnown(
238 $this->message
['last'],
241 'diff' => $row->rev_id
,
242 'oldid' => $this->prevId
[$row->rev_id
]
246 $userLink = $this->sk
->revUserTools( $rev );
248 if(!is_null($size = $row->rev_len
)) {
249 $stxt = $this->sk
->formatRevisionSize( $size );
251 $comment = $this->sk
->revComment( $rev );
253 return "<li>$checkBox ($last) $pageLink . . $userLink $stxt $comment</li>";
257 * Fetch revision text link if it's available to all users
260 function getPageLink( $row, $titleObj, $ts, $target ) {
263 if( !$this->userCan($row, Revision
::DELETED_TEXT
) ) {
264 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
266 $link = $this->sk
->linkKnown(
268 $wgLang->timeanddate( $ts, true ),
275 if( $this->isDeleted($row, Revision
::DELETED_TEXT
) )
276 $link = '<span class="history-deleted">' . $link . '</span>';
283 # Get the titles directly from the IDs, in case the target page params
284 # were spoofed. The queries are done based on the IDs, so it's best to
285 # keep it consistent...
286 $targetTitle = Title
::newFromID( $this->mTargetID
);
287 $destTitle = Title
::newFromID( $this->mDestID
);
288 if( is_null($targetTitle) ||
is_null($destTitle) )
289 return false; // validate these
290 if( $targetTitle->getArticleId() == $destTitle->getArticleId() )
292 # Verify that this timestamp is valid
293 # Must be older than the destination page
294 $dbw = wfGetDB( DB_MASTER
);
295 # Get timestamp into DB format
296 $this->mTimestamp
= $this->mTimestamp ?
$dbw->timestamp($this->mTimestamp
) : '';
297 # Max timestamp should be min of destination page
298 $maxtimestamp = $dbw->selectField( 'revision', 'MIN(rev_timestamp)',
299 array('rev_page' => $this->mDestID
),
301 # Destination page must exist with revisions
302 if( !$maxtimestamp ) {
303 $wgOut->addWikiMsg('mergehistory-fail');
306 # Get the latest timestamp of the source
307 $lasttimestamp = $dbw->selectField( array('page','revision'),
309 array('page_id' => $this->mTargetID
, 'page_latest = rev_id' ),
311 # $this->mTimestamp must be older than $maxtimestamp
312 if( $this->mTimestamp
>= $maxtimestamp ) {
313 $wgOut->addWikiMsg('mergehistory-fail');
316 # Update the revisions
317 if( $this->mTimestamp
) {
318 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
319 $TimestampLimit = wfTimestamp(TS_MW
,$this->mTimestamp
);
321 $timewhere = "rev_timestamp <= {$maxtimestamp}";
322 $TimestampLimit = wfTimestamp(TS_MW
,$lasttimestamp);
325 $dbw->update( 'revision',
326 array( 'rev_page' => $this->mDestID
),
327 array( 'rev_page' => $this->mTargetID
,
331 $count = $dbw->affectedRows();
332 # Make the source page a redirect if no revisions are left
333 $haveRevisions = $dbw->selectField( 'revision',
335 array( 'rev_page' => $this->mTargetID
),
337 array( 'FOR UPDATE' ) );
338 if( !$haveRevisions ) {
339 if( $this->mComment
) {
340 $comment = wfMsgForContent( 'mergehistory-comment', $targetTitle->getPrefixedText(),
341 $destTitle->getPrefixedText(), $this->mComment
);
343 $comment = wfMsgForContent( 'mergehistory-autocomment', $targetTitle->getPrefixedText(),
344 $destTitle->getPrefixedText() );
346 $mwRedir = MagicWord
::get( 'redirect' );
347 $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $destTitle->getPrefixedText() . "]]\n";
348 $redirectArticle = new Article( $targetTitle );
349 $redirectRevision = new Revision( array(
350 'page' => $this->mTargetID
,
351 'comment' => $comment,
352 'text' => $redirectText ) );
353 $redirectRevision->insertOn( $dbw );
354 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision );
356 # Now, we record the link from the redirect to the new title.
357 # It should have no other outgoing links...
358 $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mDestID
), __METHOD__
);
359 $dbw->insert( 'pagelinks',
361 'pl_from' => $this->mDestID
,
362 'pl_namespace' => $destTitle->getNamespace(),
363 'pl_title' => $destTitle->getDBkey() ),
366 $targetTitle->invalidateCache(); // update histories
368 $destTitle->invalidateCache(); // update histories
369 # Check if this did anything
371 $wgOut->addWikiMsg('mergehistory-fail');
375 $log = new LogPage( 'merge' );
376 $log->addEntry( 'merge', $targetTitle, $this->mComment
,
377 array($destTitle->getPrefixedText(),$TimestampLimit) );
379 $wgOut->addHTML( wfMsgExt( 'mergehistory-success', array('parseinline'),
380 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText(), $count ) );
382 wfRunHooks( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
388 class MergeHistoryPager
extends ReverseChronologicalPager
{
389 public $mForm, $mConds;
391 function __construct( $form, $conds = array(), $source, $dest ) {
392 $this->mForm
= $form;
393 $this->mConds
= $conds;
394 $this->title
= $source;
395 $this->articleID
= $source->getArticleID();
397 $dbr = wfGetDB( DB_SLAVE
);
398 $maxtimestamp = $dbr->selectField( 'revision', 'MIN(rev_timestamp)',
399 array('rev_page' => $dest->getArticleID() ),
401 $this->maxTimestamp
= $maxtimestamp;
403 parent
::__construct();
406 function getStartBody() {
407 wfProfileIn( __METHOD__
);
408 # Do a link batch query
409 $this->mResult
->seek( 0 );
410 $batch = new LinkBatch();
411 # Give some pointers to make (last) links
412 $this->mForm
->prevId
= array();
413 while( $row = $this->mResult
->fetchObject() ) {
414 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->rev_user_text
) );
415 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->rev_user_text
) );
417 $rev_id = isset($rev_id) ?
$rev_id : $row->rev_id
;
418 if( $rev_id > $row->rev_id
)
419 $this->mForm
->prevId
[$rev_id] = $row->rev_id
;
420 else if( $rev_id < $row->rev_id
)
421 $this->mForm
->prevId
[$row->rev_id
] = $rev_id;
423 $rev_id = $row->rev_id
;
427 $this->mResult
->seek( 0 );
429 wfProfileOut( __METHOD__
);
433 function formatRow( $row ) {
435 return $this->mForm
->formatRevisionRow( $row );
438 function getQueryInfo() {
439 $conds = $this->mConds
;
440 $conds['rev_page'] = $this->articleID
;
441 $conds[] = 'page_id = rev_page';
442 $conds[] = "rev_timestamp < {$this->maxTimestamp}";
444 'tables' => array('revision','page'),
445 'fields' => array( 'rev_minor_edit', 'rev_timestamp', 'rev_user', 'rev_user_text', 'rev_comment',
446 'rev_id', 'rev_page', 'rev_parent_id', 'rev_text_id', 'rev_len', 'rev_deleted' ),
451 function getIndexField() {
452 return 'rev_timestamp';