3 * Special page allowing users with the appropriate permissions to
4 * merge article histories, with some restrictions
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 * @ingroup 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 if ( $this->mTargetObj
&& $this->mDestObj
&& $this->mTargetObj
->equals( $this->mDestObj
) ) {
101 $errors[] = wfMsgExt( 'mergehistory-same-destination', array( 'parse' ) );
104 if ( count( $errors ) ) {
105 $this->showMergeForm();
106 $wgOut->addHTML( implode( "\n", $errors ) );
108 $this->showHistory();
113 function showMergeForm() {
114 global $wgOut, $wgScript;
116 $wgOut->addWikiMsg( 'mergehistory-header' );
119 Xml
::openElement( 'form', array(
121 'action' => $wgScript ) ) .
123 Xml
::element( 'legend', array(),
124 wfMsg( 'mergehistory-box' ) ) .
125 Xml
::hidden( 'title',
126 SpecialPage
::getTitleFor( 'Mergehistory' )->getPrefixedDbKey() ) .
127 Xml
::hidden( 'submitted', '1' ) .
128 Xml
::hidden( 'mergepoint', $this->mTimestamp
) .
129 Xml
::openElement( 'table' ) .
131 <td>".Xml
::label( wfMsg( 'mergehistory-from' ), 'target' )."</td>
132 <td>".Xml
::input( 'target', 30, $this->mTarget
, array('id'=>'target') )."</td>
134 <td>".Xml
::label( wfMsg( 'mergehistory-into' ), 'dest' )."</td>
135 <td>".Xml
::input( 'dest', 30, $this->mDest
, array('id'=>'dest') )."</td>
137 Xml
::submitButton( wfMsg( 'mergehistory-go' ) ) .
139 Xml
::closeElement( 'table' ) .
144 private function showHistory() {
145 global $wgLang, $wgUser, $wgOut;
147 $this->sk
= $wgUser->getSkin();
149 $wgOut->setPagetitle( wfMsg( "mergehistory" ) );
151 $this->showMergeForm();
153 # List all stored revisions
154 $revisions = new MergeHistoryPager( $this, array(), $this->mTargetObj
, $this->mDestObj
);
155 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
157 $titleObj = SpecialPage
::getTitleFor( "Mergehistory" );
158 $action = $titleObj->getLocalURL( "action=submit" );
159 # Start the form here
160 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'merge' ) );
161 $wgOut->addHTML( $top );
163 if( $haveRevisions ) {
164 # Format the user-visible controls (comment field, submission button)
165 # in a nice little table
167 Xml
::openElement( 'fieldset' ) .
168 wfMsgExt( 'mergehistory-merge', array('parseinline'),
169 $this->mTargetObj
->getPrefixedText(), $this->mDestObj
->getPrefixedText() ) .
170 Xml
::openElement( 'table', array( 'id' => 'mw-mergehistory-table' ) ) .
172 <td class='mw-label'>" .
173 Xml
::label( wfMsg( 'mergehistory-reason' ), 'wpComment' ) .
175 <td class='mw-input'>" .
176 Xml
::input( 'wpComment', 50, $this->mComment
, array('id' => 'wpComment') ) .
181 <td class='mw-submit'>" .
182 Xml
::submitButton( wfMsg( 'mergehistory-submit' ), array( 'name' => 'merge', 'id' => 'mw-merge-submit' ) ) .
185 Xml
::closeElement( 'table' ) .
186 Xml
::closeElement( 'fieldset' );
188 $wgOut->addHTML( $table );
191 $wgOut->addHTML( "<h2 id=\"mw-mergehistory\">" . wfMsgHtml( "mergehistory-list" ) . "</h2>\n" );
193 if( $haveRevisions ) {
194 $wgOut->addHTML( $revisions->getNavigationBar() );
195 $wgOut->addHTML( "<ul>" );
196 $wgOut->addHTML( $revisions->getBody() );
197 $wgOut->addHTML( "</ul>" );
198 $wgOut->addHTML( $revisions->getNavigationBar() );
200 $wgOut->addWikiMsg( "mergehistory-empty" );
203 # Show relevant lines from the deletion log:
204 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'merge' ) ) . "</h2>\n" );
205 LogEventsList
::showLogExtract( $wgOut, 'merge', $this->mTargetObj
->getPrefixedText() );
207 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
208 # Such would happen if a page was renamed after the form loaded, but before submit
209 $misc = Xml
::hidden( 'targetID', $this->mTargetObj
->getArticleID() );
210 $misc .= Xml
::hidden( 'destID', $this->mDestObj
->getArticleID() );
211 $misc .= Xml
::hidden( 'target', $this->mTarget
);
212 $misc .= Xml
::hidden( 'dest', $this->mDest
);
213 $misc .= Xml
::hidden( 'wpEditToken', $wgUser->editToken() );
214 $misc .= Xml
::closeElement( 'form' );
215 $wgOut->addHTML( $misc );
220 function formatRevisionRow( $row ) {
221 global $wgUser, $wgLang;
223 $rev = new Revision( $row );
226 $last = $this->message
['last'];
228 $ts = wfTimestamp( TS_MW
, $row->rev_timestamp
);
229 $checkBox = Xml
::radio( "mergepoint", $ts, false );
231 $pageLink = $this->sk
->makeKnownLinkObj( $rev->getTitle(),
232 htmlspecialchars( $wgLang->timeanddate( $ts ) ), 'oldid=' . $rev->getId() );
233 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
234 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
238 if( !$rev->userCan( Revision
::DELETED_TEXT
) )
239 $last = $this->message
['last'];
240 else if( isset($this->prevId
[$row->rev_id
]) )
241 $last = $this->sk
->makeKnownLinkObj( $rev->getTitle(), $this->message
['last'],
242 "diff=" . $row->rev_id
. "&oldid=" . $this->prevId
[$row->rev_id
] );
244 $userLink = $this->sk
->revUserTools( $rev );
246 if(!is_null($size = $row->rev_len
)) {
247 $stxt = $this->sk
->formatRevisionSize( $size );
249 $comment = $this->sk
->revComment( $rev );
251 return "<li>$checkBox ($last) $pageLink . . $userLink $stxt $comment</li>";
255 * Fetch revision text link if it's available to all users
258 function getPageLink( $row, $titleObj, $ts, $target ) {
261 if( !$this->userCan($row, Revision
::DELETED_TEXT
) ) {
262 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
264 $link = $this->sk
->makeKnownLinkObj( $titleObj,
265 $wgLang->timeanddate( $ts, true ), "target=$target×tamp=$ts" );
266 if( $this->isDeleted($row, Revision
::DELETED_TEXT
) )
267 $link = '<span class="history-deleted">' . $link . '</span>';
273 global $wgOut, $wgUser;
274 # Get the titles directly from the IDs, in case the target page params
275 # were spoofed. The queries are done based on the IDs, so it's best to
276 # keep it consistent...
277 $targetTitle = Title
::newFromID( $this->mTargetID
);
278 $destTitle = Title
::newFromID( $this->mDestID
);
279 if( is_null($targetTitle) ||
is_null($destTitle) )
280 return false; // validate these
281 if( $targetTitle->getArticleId() == $destTitle->getArticleId() )
283 # Verify that this timestamp is valid
284 # Must be older than the destination page
285 $dbw = wfGetDB( DB_MASTER
);
286 # Get timestamp into DB format
287 $this->mTimestamp
= $this->mTimestamp ?
$dbw->timestamp($this->mTimestamp
) : '';
288 # Max timestamp should be min of destination page
289 $maxtimestamp = $dbw->selectField( 'revision', 'MIN(rev_timestamp)',
290 array('rev_page' => $this->mDestID
),
292 # Destination page must exist with revisions
293 if( !$maxtimestamp ) {
294 $wgOut->addWikiMsg('mergehistory-fail');
297 # Get the latest timestamp of the source
298 $lasttimestamp = $dbw->selectField( array('page','revision'),
300 array('page_id' => $this->mTargetID
, 'page_latest = rev_id' ),
302 # $this->mTimestamp must be older than $maxtimestamp
303 if( $this->mTimestamp
>= $maxtimestamp ) {
304 $wgOut->addWikiMsg('mergehistory-fail');
307 # Update the revisions
308 if( $this->mTimestamp
) {
309 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
310 $TimestampLimit = wfTimestamp(TS_MW
,$this->mTimestamp
);
312 $timewhere = "rev_timestamp <= {$maxtimestamp}";
313 $TimestampLimit = wfTimestamp(TS_MW
,$lasttimestamp);
316 $dbw->update( 'revision',
317 array( 'rev_page' => $this->mDestID
),
318 array( 'rev_page' => $this->mTargetID
,
322 $count = $dbw->affectedRows();
323 # Make the source page a redirect if no revisions are left
324 $haveRevisions = $dbw->selectField( 'revision',
326 array( 'rev_page' => $this->mTargetID
),
328 array( 'FOR UPDATE' ) );
329 if( !$haveRevisions ) {
330 if( $this->mComment
) {
331 $comment = wfMsgForContent( 'mergehistory-comment', $targetTitle->getPrefixedText(),
332 $destTitle->getPrefixedText(), $this->mComment
);
334 $comment = wfMsgForContent( 'mergehistory-autocomment', $targetTitle->getPrefixedText(),
335 $destTitle->getPrefixedText() );
337 $mwRedir = MagicWord
::get( 'redirect' );
338 $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $destTitle->getPrefixedText() . "]]\n";
339 $redirectArticle = new Article( $targetTitle );
340 $redirectRevision = new Revision( array(
341 'page' => $this->mTargetID
,
342 'comment' => $comment,
343 'text' => $redirectText ) );
344 $redirectRevision->insertOn( $dbw );
345 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision );
347 # Now, we record the link from the redirect to the new title.
348 # It should have no other outgoing links...
349 $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mDestID
), __METHOD__
);
350 $dbw->insert( 'pagelinks',
352 'pl_from' => $this->mDestID
,
353 'pl_namespace' => $destTitle->getNamespace(),
354 'pl_title' => $destTitle->getDBkey() ),
357 $targetTitle->invalidateCache(); // update histories
359 $destTitle->invalidateCache(); // update histories
360 # Check if this did anything
362 $wgOut->addWikiMsg('mergehistory-fail');
366 $log = new LogPage( 'merge' );
367 $log->addEntry( 'merge', $targetTitle, $this->mComment
,
368 array($destTitle->getPrefixedText(),$TimestampLimit) );
370 $wgOut->addHTML( wfMsgExt( 'mergehistory-success', array('parseinline'),
371 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText(), $count ) );
373 wfRunHooks( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
379 class MergeHistoryPager
extends ReverseChronologicalPager
{
380 public $mForm, $mConds;
382 function __construct( $form, $conds = array(), $source, $dest ) {
383 $this->mForm
= $form;
384 $this->mConds
= $conds;
385 $this->title
= $source;
386 $this->articleID
= $source->getArticleID();
388 $dbr = wfGetDB( DB_SLAVE
);
389 $maxtimestamp = $dbr->selectField( 'revision', 'MIN(rev_timestamp)',
390 array('rev_page' => $dest->getArticleID() ),
392 $this->maxTimestamp
= $maxtimestamp;
394 parent
::__construct();
397 function getStartBody() {
398 wfProfileIn( __METHOD__
);
399 # Do a link batch query
400 $this->mResult
->seek( 0 );
401 $batch = new LinkBatch();
402 # Give some pointers to make (last) links
403 $this->mForm
->prevId
= array();
404 while( $row = $this->mResult
->fetchObject() ) {
405 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->rev_user_text
) );
406 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->rev_user_text
) );
408 $rev_id = isset($rev_id) ?
$rev_id : $row->rev_id
;
409 if( $rev_id > $row->rev_id
)
410 $this->mForm
->prevId
[$rev_id] = $row->rev_id
;
411 else if( $rev_id < $row->rev_id
)
412 $this->mForm
->prevId
[$row->rev_id
] = $rev_id;
414 $rev_id = $row->rev_id
;
418 $this->mResult
->seek( 0 );
420 wfProfileOut( __METHOD__
);
424 function formatRow( $row ) {
426 return $this->mForm
->formatRevisionRow( $row );
429 function getQueryInfo() {
430 $conds = $this->mConds
;
431 $conds['rev_page'] = $this->articleID
;
432 $conds[] = 'page_id = rev_page';
433 $conds[] = "rev_timestamp < {$this->maxTimestamp}";
435 'tables' => array('revision','page'),
436 'fields' => array( 'rev_minor_edit', 'rev_timestamp', 'rev_user', 'rev_user_text', 'rev_comment',
437 'rev_id', 'rev_page', 'rev_parent_id', 'rev_text_id', 'rev_len', 'rev_deleted' ),
442 function getIndexField() {
443 return 'rev_timestamp';