4 * Special page allowing users with the appropriate permissions to
5 * merge article histories, with some restrictions
7 * @addtogroup SpecialPage
13 function wfSpecialMergehistory( $par ) {
16 $form = new MergehistoryForm( $wgRequest, $par );
21 * The HTML form for Special:MergeHistory, which allows users with the appropriate
22 * permissions to view and restore deleted content.
23 * @addtogroup SpecialPage
25 class MergehistoryForm
{
26 var $mAction, $mTarget, $mDest, $mTimestamp, $mTargetID, $mDestID, $mComment;
27 var $mTargetObj, $mDestObj;
29 function MergehistoryForm( $request, $par = "" ) {
32 $this->mAction
= $request->getVal( 'action' );
33 $this->mTarget
= $request->getVal( 'target' );
34 $this->mDest
= $request->getVal( 'dest' );
35 $this->mSubmitted
= $request->getBool( 'submitted' );
37 $this->mTargetID
= intval( $request->getVal( 'targetID' ) );
38 $this->mDestID
= intval( $request->getVal( 'destID' ) );
39 $this->mTimestamp
= $request->getVal( 'mergepoint' );
40 if( !preg_match("/[0-9]{14}/",$this->mTimestamp
) ) {
41 $this->mTimestamp
= '';
43 $this->mComment
= $request->getText( 'wpComment' );
45 $this->mMerge
= $request->wasPosted() && $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
47 if( $this->mSubmitted
) {
48 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
49 $this->mDestObj
= Title
::newFromURL( $this->mDest
);
51 $this->mTargetObj
= null;
52 $this->mDestObj
= null;
55 $this->preCacheMessages();
59 * As we use the same small set of messages in various methods and that
60 * they are called often, we call them once and save them in $this->message
62 function preCacheMessages() {
63 // Precache various messages
64 if( !isset( $this->message
) ) {
65 $this->message
['last'] = wfMsgExt( 'last', array( 'escape') );
70 global $wgOut, $wgUser;
72 $wgOut->setPagetitle( wfMsgHtml( "mergehistory" ) );
74 if( $this->mTargetID
&& $this->mDestID
&& $this->mAction
=="submit" && $this->mMerge
) {
75 return $this->merge();
78 if ( !$this->mSubmitted
) {
79 $this->showMergeForm();
84 if ( !$this->mTargetObj
instanceof Title
) {
85 $errors[] = wfMsgExt( 'mergehistory-invalid-source', array( 'parse' ) );
86 } elseif( !$this->mTargetObj
->exists() ) {
87 $errors[] = wfMsgExt( 'mergehistory-no-source', array( 'parse' ),
88 wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() )
92 if ( !$this->mDestObj
instanceof Title
) {
93 $errors[] = wfMsgExt( 'mergehistory-invalid-destination', array( 'parse' ) );
94 } elseif( !$this->mDestObj
->exists() ) {
95 $errors[] = wfMsgExt( 'mergehistory-no-destination', array( 'parse' ),
96 wfEscapeWikiText( $this->mDestObj
->getPrefixedText() )
100 // TODO: warn about target = dest?
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',
124 SpecialPage
::getTitleFor( 'Mergehistory' )->getPrefixedDbKey() ) .
125 Xml
::hidden( 'submitted', '1' ) .
126 Xml
::hidden( 'mergepoint', $this->mTimestamp
) .
127 Xml
::openElement( 'table' ) .
129 <td>".Xml
::label( wfMsg( 'mergehistory-from' ), 'target' )."</td>
130 <td>".Xml
::input( 'target', 30, $this->mTarget
, array('id'=>'target') )."</td>
132 <td>".Xml
::label( wfMsg( 'mergehistory-into' ), 'dest' )."</td>
133 <td>".Xml
::input( 'dest', 30, $this->mDest
, array('id'=>'dest') )."</td>
135 Xml
::submitButton( wfMsg( 'mergehistory-go' ) ) .
137 Xml
::closeElement( 'table' ) .
142 private function showHistory() {
143 global $wgLang, $wgContLang, $wgUser, $wgOut;
145 $this->sk
= $wgUser->getSkin();
147 $wgOut->setPagetitle( wfMsg( "mergehistory" ) );
149 $this->showMergeForm();
151 # List all stored revisions
152 $revisions = new MergeHistoryPager( $this, array(), $this->mTargetObj
, $this->mDestObj
);
153 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
155 $titleObj = SpecialPage
::getTitleFor( "Mergehistory" );
156 $action = $titleObj->getLocalURL( "action=submit" );
157 # Start the form here
158 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'merge' ) );
159 $wgOut->addHtml( $top );
161 if( $haveRevisions ) {
162 # Format the user-visible controls (comment field, submission button)
163 # in a nice little table
164 $align = $wgContLang->isRtl() ?
'left' : 'right';
166 Xml
::openElement( 'fieldset' ) .
167 Xml
::openElement( 'table' ) .
170 wfMsgExt( 'mergehistory-merge', array('parseinline'),
171 $this->mTargetObj
->getPrefixedText(), $this->mDestObj
->getPrefixedText() ) .
175 <td align='$align'>" .
176 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
179 Xml
::input( 'wpComment', 50, $this->mComment
) .
185 Xml
::submitButton( wfMsg( 'mergehistory-submit' ), array( 'name' => 'merge', 'id' => 'mw-merge-submit' ) ) .
188 Xml
::closeElement( 'table' ) .
189 Xml
::closeElement( 'fieldset' );
191 $wgOut->addHtml( $table );
194 $wgOut->addHTML( "<h2 id=\"mw-mergehistory\">" . wfMsgHtml( "mergehistory-list" ) . "</h2>\n" );
196 if( $haveRevisions ) {
197 $wgOut->addHTML( $revisions->getNavigationBar() );
198 $wgOut->addHTML( "<ul>" );
199 $wgOut->addHTML( $revisions->getBody() );
200 $wgOut->addHTML( "</ul>" );
201 $wgOut->addHTML( $revisions->getNavigationBar() );
203 $wgOut->addWikiMsg( "mergehistory-empty" );
206 # Show relevant lines from the deletion log:
207 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'merge' ) ) . "</h2>\n" );
208 $logViewer = new LogViewer(
211 array( 'page' => $this->mTargetObj
->getPrefixedText(),
212 'type' => 'merge' ) ) ) );
213 $logViewer->showList( $wgOut );
215 # Slip in the hidden controls here
216 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
217 # Such would happen if a page was renamed after the form loaded, but before submit
218 $misc = Xml
::hidden( 'targetID', $this->mTargetObj
->getArticleID() );
219 $misc .= Xml
::hidden( 'destID', $this->mDestObj
->getArticleID() );
220 $misc .= Xml
::hidden( 'target', $this->mTarget
);
221 $misc .= Xml
::hidden( 'dest', $this->mDest
);
222 $misc .= Xml
::hidden( 'wpEditToken', $wgUser->editToken() );
223 $misc .= Xml
::closeElement( 'form' );
224 $wgOut->addHtml( $misc );
229 function formatRevisionRow( $row ) {
230 global $wgUser, $wgLang;
232 $rev = new Revision( $row );
235 $last = $this->message
['last'];
237 $ts = wfTimestamp( TS_MW
, $row->rev_timestamp
);
238 $checkBox = wfRadio( "mergepoint", $ts, false );
240 $pageLink = $this->sk
->makeKnownLinkObj( $rev->getTitle(),
241 htmlspecialchars( $wgLang->timeanddate( $ts ) ), 'oldid=' . $rev->getID() );
242 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
243 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
247 if( !$rev->userCan( Revision
::DELETED_TEXT
) )
248 $last = $this->message
['last'];
249 else if( isset($this->prevId
[$row->rev_id
]) )
250 $last = $this->sk
->makeKnownLinkObj( $rev->getTitle(), $this->message
['last'],
251 "diff=" . $row->rev_id
. "&oldid=" . $this->prevId
[$row->rev_id
] );
253 $userLink = $this->sk
->revUserTools( $rev );
255 if(!is_null($size = $row->rev_len
)) {
257 $stxt = wfMsgHtml('historyempty');
259 $stxt = wfMsgHtml('historysize', $wgLang->formatNum( $size ) );
261 $comment = $this->sk
->revComment( $rev );
263 return "<li>$checkBox ($last) $pageLink . . $userLink $stxt $comment</li>";
267 * Fetch revision text link if it's available to all users
270 function getPageLink( $row, $titleObj, $ts, $target ) {
273 if( !$this->userCan($row, Revision
::DELETED_TEXT
) ) {
274 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
276 $link = $this->sk
->makeKnownLinkObj( $titleObj,
277 $wgLang->timeanddate( $ts, true ), "target=$target×tamp=$ts" );
278 if( $this->isDeleted($row, Revision
::DELETED_TEXT
) )
279 $link = '<span class="history-deleted">' . $link . '</span>';
285 global $wgOut, $wgUser;
286 # Get the titles directly from the IDs, in case the target page params
287 # were spoofed. The queries are done based on the IDs, so it's best to
288 # keep it consistent...
289 $targetTitle = Title
::newFromID( $this->mTargetID
);
290 $destTitle = Title
::newFromID( $this->mDestID
);
291 if( is_null($targetTitle) ||
is_null($destTitle) )
292 return false; // validate these
293 if( $targetTitle->getArticleId() == $destTitle->getArticleId() )
295 # Verify that this timestamp is valid
296 # Must be older than the destination page
297 $dbw = wfGetDB( DB_MASTER
);
298 # Get timestamp into DB format
299 $this->mTimestamp
= $this->mTimestamp ?
$dbw->timestamp($this->mTimestamp
) : '';
300 # Max timestamp should be min of destination page
301 $maxtimestamp = $dbw->selectField( 'revision', 'MIN(rev_timestamp)',
302 array('rev_page' => $this->mDestID
),
304 # Destination page must exist with revisions
305 if( !$maxtimestamp ) {
306 $wgOut->addWikiMsg('mergehistory-fail');
309 # Get the latest timestamp of the source
310 $lasttimestamp = $dbw->selectField( array('page','revision'),
312 array('page_id' => $this->mTargetID
, 'page_latest = rev_id' ),
314 # $this->mTimestamp must be older than $maxtimestamp
315 if( $this->mTimestamp
>= $maxtimestamp ) {
316 $wgOut->addWikiMsg('mergehistory-fail');
319 # Update the revisions
320 if( $this->mTimestamp
) {
321 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
322 $TimestampLimit = wfTimestamp(TS_MW
,$this->mTimestamp
);
324 $timewhere = "rev_timestamp <= {$maxtimestamp}";
325 $TimestampLimit = wfTimestamp(TS_MW
,$lasttimestamp);
328 $dbw->update( 'revision',
329 array( 'rev_page' => $this->mDestID
),
330 array( 'rev_page' => $this->mTargetID
,
334 $count = $dbw->affectedRows();
335 # Make the source page a redirect if no revisions are left
336 $haveRevisions = $dbw->selectField( 'revision',
338 array( 'rev_page' => $this->mTargetID
),
340 array( 'FOR UPDATE' ) );
341 if( !$haveRevisions ) {
342 if( $this->mComment
) {
343 $comment = wfMsgForContent( 'mergehistory-comment', $targetTitle->getPrefixedText(),
344 $destTitle->getPrefixedText(), $this->mComment
);
346 $comment = wfMsgForContent( 'mergehistory-autocomment', $targetTitle->getPrefixedText(),
347 $destTitle->getPrefixedText() );
349 $mwRedir = MagicWord
::get( 'redirect' );
350 $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $destTitle->getPrefixedText() . "]]\n";
351 $redirectArticle = new Article( $targetTitle );
352 $redirectRevision = new Revision( array(
353 'page' => $this->mTargetID
,
354 'comment' => $comment,
355 'text' => $redirectText ) );
356 $redirectRevision->insertOn( $dbw );
357 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision );
359 # Now, we record the link from the redirect to the new title.
360 # It should have no other outgoing links...
361 $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mDestID
), __METHOD__
);
362 $dbw->insert( 'pagelinks',
364 'pl_from' => $this->mDestID
,
365 'pl_namespace' => $destTitle->getNamespace(),
366 'pl_title' => $destTitle->getDBkey() ),
369 $targetTitle->invalidateCache(); // update histories
371 $destTitle->invalidateCache(); // update histories
372 # Check if this did anything
374 $wgOut->addWikiMsg('mergehistory-fail');
378 $log = new LogPage( 'merge' );
379 $log->addEntry( 'merge', $targetTitle, $this->mComment
,
380 array($destTitle->getPrefixedText(),$TimestampLimit) );
382 $wgOut->addHtml( wfMsgExt( 'mergehistory-success', array('parseinline'),
383 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText(), $count ) );
385 wfRunHooks( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
391 class MergeHistoryPager
extends ReverseChronologicalPager
{
392 public $mForm, $mConds;
394 function __construct( $form, $conds = array(), $source, $dest ) {
395 $this->mForm
= $form;
396 $this->mConds
= $conds;
397 $this->title
= $source;
398 $this->articleID
= $source->getArticleID();
400 $dbr = wfGetDB( DB_SLAVE
);
401 $maxtimestamp = $dbr->selectField( 'revision', 'MIN(rev_timestamp)',
402 array('rev_page' => $dest->getArticleID() ),
404 $this->maxTimestamp
= $maxtimestamp;
406 parent
::__construct();
409 function getStartBody() {
410 wfProfileIn( __METHOD__
);
411 # Do a link batch query
412 $this->mResult
->seek( 0 );
413 $batch = new LinkBatch();
414 # Give some pointers to make (last) links
415 $this->mForm
->prevId
= array();
416 while( $row = $this->mResult
->fetchObject() ) {
417 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->rev_user_text
) );
418 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->rev_user_text
) );
420 $rev_id = isset($rev_id) ?
$rev_id : $row->rev_id
;
421 if( $rev_id > $row->rev_id
)
422 $this->mForm
->prevId
[$rev_id] = $row->rev_id
;
423 else if( $rev_id < $row->rev_id
)
424 $this->mForm
->prevId
[$row->rev_id
] = $rev_id;
426 $rev_id = $row->rev_id
;
430 $this->mResult
->seek( 0 );
432 wfProfileOut( __METHOD__
);
436 function formatRow( $row ) {
438 return $this->mForm
->formatRevisionRow( $row );
441 function getQueryInfo() {
442 $conds = $this->mConds
;
443 $conds['rev_page'] = $this->articleID
;
444 $conds[] = "rev_timestamp < {$this->maxTimestamp}";
447 'tables' => array('revision'),
448 'fields' => array( 'rev_minor_edit', 'rev_timestamp', 'rev_user', 'rev_user_text', 'rev_comment',
449 'rev_id', 'rev_page', 'rev_text_id', 'rev_len', 'rev_deleted' ),
454 function getIndexField() {
455 return 'rev_timestamp';