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' );
36 $this->mTargetID
= intval( $request->getVal( 'targetID' ) );
37 $this->mDestID
= intval( $request->getVal( 'destID' ) );
38 $this->mTimestamp
= $request->getVal( 'mergepoint' );
39 $this->mComment
= $request->getText( 'wpComment' );
41 $this->mMerge
= $request->wasPosted() && $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
43 if( $this->mTarget
!== "" ) {
44 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
46 $this->mTargetObj
= NULL;
49 if( $this->mDest
!== "" ) {
50 $this->mDestObj
= Title
::newFromURL( $this->mDest
);
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( is_object($this->mTargetObj
) && is_object($this->mDestObj
) ) {
79 return $this->showHistory();
82 return $this->showMergeForm();
85 function showMergeForm() {
86 global $wgOut, $wgScript;
88 $wgOut->addWikiText( wfMsg( 'mergehistory-header' ) );
91 Xml
::openElement( 'form', array(
93 'action' => $wgScript ) ) .
95 Xml
::element( 'legend', array(),
96 wfMsg( 'mergehistory-box' ) ) .
98 SpecialPage
::getTitleFor( 'Mergehistory' )->getPrefixedDbKey() ) .
99 Xml
::openElement( 'table' ) .
101 <td>".Xml
::Label( wfMsg( 'mergehistory-from' ), 'target' )."</td>
102 <td>".Xml
::input( 'target', 30, $this->mTarget
, array('id'=>'target') )."</td>
104 <td>".Xml
::Label( wfMsg( 'mergehistory-into' ), 'dest' )."</td>
105 <td>".Xml
::input( 'dest', 30, $this->mDest
, array('id'=>'dest') )."</td>
107 Xml
::submitButton( wfMsg( 'mergehistory-go' ) ) .
109 Xml
::closeElement( 'table' ) .
114 private function showHistory() {
115 global $wgLang, $wgContLang, $wgUser, $wgOut;
117 $this->sk
= $wgUser->getSkin();
119 $wgOut->setPagetitle( wfMsg( "mergehistory" ) );
121 $this->showMergeForm();
123 # List all stored revisions
124 $revisions = new MergeHistoryPager( $this, array(), $this->mTargetObj
, $this->mDestObj
);
125 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
127 $titleObj = SpecialPage
::getTitleFor( "Mergehistory" );
128 $action = $titleObj->getLocalURL( "action=submit" );
129 # Start the form here
130 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'merge' ) );
131 $wgOut->addHtml( $top );
133 if( $haveRevisions ) {
134 # Format the user-visible controls (comment field, submission button)
135 # in a nice little table
136 $align = $wgContLang->isRtl() ?
'left' : 'right';
138 Xml
::openElement( 'fieldset' ) .
139 Xml
::openElement( 'table' ) .
142 wfMsgExt( 'mergehistory-merge', array('parseinline'),
143 $this->mTargetObj
->getPrefixedText(), $this->mDestObj
->getPrefixedText() ) .
147 <td align='$align'>" .
148 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
151 Xml
::input( 'wpComment', 50, $this->mComment
) .
157 Xml
::submitButton( wfMsg( 'mergehistory-submit' ), array( 'name' => 'merge', 'id' => 'mw-merge-submit' ) ) .
160 Xml
::closeElement( 'table' ) .
161 Xml
::closeElement( 'fieldset' );
163 $wgOut->addHtml( $table );
166 $wgOut->addHTML( "<h2 id=\"mw-mergehistory\">" . wfMsgHtml( "mergehistory-list" ) . "</h2>\n" );
168 if( $haveRevisions ) {
169 $wgOut->addHTML( $revisions->getNavigationBar() );
170 $wgOut->addHTML( "<ul>" );
171 $wgOut->addHTML( $revisions->getBody() );
172 $wgOut->addHTML( "</ul>" );
173 $wgOut->addHTML( $revisions->getNavigationBar() );
175 $wgOut->addWikiText( wfMsg( "mergehistory-empty" ) );
178 # Show relevant lines from the deletion log:
179 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'merge' ) ) . "</h2>\n" );
180 $logViewer = new LogViewer(
183 array( 'page' => $this->mTargetObj
->getPrefixedText(),
184 'type' => 'merge' ) ) ) );
185 $logViewer->showList( $wgOut );
187 # Slip in the hidden controls here
188 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
189 # Such would happen if a page was renamed after the form loaded, but before submit
190 $misc = Xml
::hidden( 'targetID', $this->mTargetObj
->getArticleID() );
191 $misc .= Xml
::hidden( 'destID', $this->mDestObj
->getArticleID() );
192 $misc .= Xml
::hidden( 'target', $this->mTarget
);
193 $misc .= Xml
::hidden( 'dest', $this->mDest
);
194 $misc .= Xml
::hidden( 'wpEditToken', $wgUser->editToken() );
195 $misc .= Xml
::closeElement( 'form' );
196 $wgOut->addHtml( $misc );
201 function formatRevisionRow( $row ) {
202 global $wgUser, $wgLang;
204 $rev = new Revision( $row );
207 $last = $this->message
['last'];
209 $ts = wfTimestamp( TS_MW
, $row->rev_timestamp
);
210 $checkBox = wfRadio( "mergepoint", $ts, false );
212 $pageLink = $this->sk
->makeKnownLinkObj( $rev->getTitle(),
213 $wgLang->timeanddate( $ts ), 'oldid=' . $rev->getID() );
214 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
215 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
219 if( !$rev->userCan( Revision
::DELETED_TEXT
) )
220 $last = $this->message
['last'];
221 else if( isset($this->prevId
[$row->rev_id
]) )
222 $last = $this->sk
->makeKnownLinkObj( $rev->getTitle(), $this->message
['last'],
223 "&diff=" . $row->rev_id
. "&oldid=" . $this->prevId
[$row->rev_id
] );
225 $userLink = $this->sk
->revUserTools( $rev );
227 if(!is_null($size = $row->rev_len
)) {
229 $stxt = wfMsgHtml('historyempty');
231 $stxt = wfMsgHtml('historysize', $wgLang->formatNum( $size ) );
233 $comment = $this->sk
->revComment( $rev );
235 return "<li>$checkBox ($last) $pageLink . . $userLink $stxt $comment</li>";
239 * Fetch revision text link if it's available to all users
242 function getPageLink( $row, $titleObj, $ts, $target ) {
245 if( !$this->userCan($row, Revision
::DELETED_TEXT
) ) {
246 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
248 $link = $this->sk
->makeKnownLinkObj( $titleObj,
249 $wgLang->timeanddate( $ts, true ), "target=$target×tamp=$ts" );
250 if( $this->isDeleted($row, Revision
::DELETED_TEXT
) )
251 $link = '<span class="history-deleted">' . $link . '</span>';
257 global $wgOut, $wgUser;
258 # Get the titles directly from the IDs, in case the target page params
259 # were spoofed. The queries are done based on the IDs, so it's best to
260 # keep it consistent...
261 $targetTitle = Title
::newFromID( $this->mTargetID
);
262 $destTitle = Title
::newFromID( $this->mDestID
);
263 if( is_null($targetTitle) ||
is_null($destTitle) )
264 return false; // validate these
265 # Verify that this timestamp is valid
266 # Must be older than the destination page
267 $dbw = wfGetDB( DB_MASTER
);
268 $maxtimestamp = $dbw->selectField( 'revision', 'MIN(rev_timestamp)',
269 array('rev_page' => $this->mDestID
),
271 # Destination page must exist with revisions
272 if( !$maxtimestamp ) {
273 $wgOut->addWikiText( wfMsg('mergehistory-fail') );
276 # Leave the latest version no matter what
277 $lasttime = $dbw->selectField( array('page','revision'),
279 array('page_id' => $this->mTargetID
, 'page_latest = rev_id' ),
281 # Take the most restrictive of the twain
282 $maxtimestamp = ($lasttime < $maxtimestamp) ?
$lasttime : $maxtimestamp;
283 if( $this->mTimestamp
>= $maxtimestamp ) {
284 $wgOut->addHtml( wfMsg('mergehistory-fail') );
287 # Update the revisions
288 if( $this->mTimestamp
)
289 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
291 $timewhere = '1 = 1';
293 $dbw->update( 'revision',
294 array( 'rev_page' => $this->mDestID
),
295 array( 'rev_page' => $this->mTargetID
,
296 "rev_timestamp < {$maxtimestamp}",
299 # Check if this did anything
300 $count = $dbw->affectedRows();
302 $wgOut->addHtml( wfMsg('mergehistory-fail') );
306 $log = new LogPage( 'merge' );
307 $log->addEntry( 'merge', $targetTitle, $this->mComment
,
308 array($destTitle->getPrefixedText(),$this->mTimestamp
) );
310 $wgOut->addHtml( wfMsgExt( 'mergehistory-success', array('parseinline'),
311 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText(), $count ) );
313 wfRunHooks( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
319 class MergeHistoryPager
extends ReverseChronologicalPager
{
320 public $mForm, $mConds;
322 function __construct( $form, $conds = array(), $title, $title2 ) {
323 $this->mForm
= $form;
324 $this->mConds
= $conds;
325 $this->title
= $title;
326 $this->articleID
= $title->getArticleID();
328 $dbr = wfGetDB( DB_SLAVE
);
329 $maxtimestamp = $dbr->selectField( 'revision', 'MIN(rev_timestamp)',
330 array('rev_page' => $title2->getArticleID() ),
332 $this->maxTimestamp
= $maxtimestamp;
334 parent
::__construct();
337 function getStartBody() {
338 wfProfileIn( __METHOD__
);
339 # Do a link batch query
340 $this->mResult
->seek( 0 );
341 $batch = new LinkBatch();
342 # Give some pointers to make (last) links
343 $this->mForm
->prevId
= array();
344 while( $row = $this->mResult
->fetchObject() ) {
345 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->rev_user_text
) );
346 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->rev_user_text
) );
348 $rev_id = isset($rev_id) ?
$rev_id : $row->rev_id
;
349 if( $rev_id > $row->rev_id
)
350 $this->mForm
->prevId
[$rev_id] = $row->rev_id
;
351 else if( $rev_id < $row->rev_id
)
352 $this->mForm
->prevId
[$row->rev_id
] = $rev_id;
354 $rev_id = $row->rev_id
;
358 $this->mResult
->seek( 0 );
360 wfProfileOut( __METHOD__
);
364 function formatRow( $row ) {
366 return $this->mForm
->formatRevisionRow( $row );
369 function getQueryInfo() {
370 $conds = $this->mConds
;
371 $conds['rev_page'] = $this->articleID
;
372 $conds[] = "rev_timestamp < {$this->maxTimestamp}";
373 # Skip the latest one, as that could cause problems
374 if( $page = $this->title
->getLatestRevID() )
375 $conds[] = "rev_id != {$page}";
378 'tables' => array('revision'),
379 'fields' => array( 'rev_minor_edit', 'rev_timestamp', 'rev_user', 'rev_user_text', 'rev_comment',
380 'rev_id', 'rev_page', 'rev_text_id', 'rev_len', 'rev_deleted' ),
385 function getIndexField() {
386 return 'rev_timestamp';