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 # Get timestamp into DB format
269 $this->mTimestamp
= $this->mTimestamp ?
$dbw->timestamp($this->mTimestamp
) : '';
271 $maxtimestamp = $dbw->selectField( 'revision', 'MIN(rev_timestamp)',
272 array('rev_page' => $this->mDestID
),
274 # Destination page must exist with revisions
275 if( !$maxtimestamp ) {
276 $wgOut->addWikiText( wfMsg('mergehistory-fail') );
279 # Leave the latest version no matter what
280 $lasttime = $dbw->selectField( array('page','revision'),
282 array('page_id' => $this->mTargetID
, 'page_latest = rev_id' ),
284 # Take the most restrictive of the twain
285 $maxtimestamp = ($lasttime < $maxtimestamp) ?
$lasttime : $maxtimestamp;
286 // $this->mTimestamp must be less than $maxtimestamp
287 if( $this->mTimestamp
>= $maxtimestamp ) {
288 $wgOut->addHtml( wfMsg('mergehistory-fail') );
291 # Update the revisions
292 if( $this->mTimestamp
) {
293 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
294 $TimestampLimit = wfTimestamp(TS_MW
,$this->mTimestamp
);
296 $timewhere = "rev_timestamp < {$maxtimestamp}";
297 $TimestampLimit = wfTimestamp(TS_MW
,$maxtimestamp);
300 $dbw->update( 'revision',
301 array( 'rev_page' => $this->mDestID
),
302 array( 'rev_page' => $this->mTargetID
,
305 # Check if this did anything
306 if( !$count = $dbw->affectedRows() ) {
307 $wgOut->addHtml( wfMsg('mergehistory-fail') );
311 $log = new LogPage( 'merge' );
312 $log->addEntry( 'merge', $targetTitle, $this->mComment
,
313 array($destTitle->getPrefixedText(),$TimestampLimit) );
315 $wgOut->addHtml( wfMsgExt( 'mergehistory-success', array('parseinline'),
316 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText(), $count ) );
318 wfRunHooks( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
324 class MergeHistoryPager
extends ReverseChronologicalPager
{
325 public $mForm, $mConds;
327 function __construct( $form, $conds = array(), $title, $title2 ) {
328 $this->mForm
= $form;
329 $this->mConds
= $conds;
330 $this->title
= $title;
331 $this->articleID
= $title->getArticleID();
333 $dbr = wfGetDB( DB_SLAVE
);
334 $maxtimestamp = $dbr->selectField( 'revision', 'MIN(rev_timestamp)',
335 array('rev_page' => $title2->getArticleID() ),
337 $this->maxTimestamp
= $maxtimestamp;
339 parent
::__construct();
342 function getStartBody() {
343 wfProfileIn( __METHOD__
);
344 # Do a link batch query
345 $this->mResult
->seek( 0 );
346 $batch = new LinkBatch();
347 # Give some pointers to make (last) links
348 $this->mForm
->prevId
= array();
349 while( $row = $this->mResult
->fetchObject() ) {
350 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->rev_user_text
) );
351 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->rev_user_text
) );
353 $rev_id = isset($rev_id) ?
$rev_id : $row->rev_id
;
354 if( $rev_id > $row->rev_id
)
355 $this->mForm
->prevId
[$rev_id] = $row->rev_id
;
356 else if( $rev_id < $row->rev_id
)
357 $this->mForm
->prevId
[$row->rev_id
] = $rev_id;
359 $rev_id = $row->rev_id
;
363 $this->mResult
->seek( 0 );
365 wfProfileOut( __METHOD__
);
369 function formatRow( $row ) {
371 return $this->mForm
->formatRevisionRow( $row );
374 function getQueryInfo() {
375 $conds = $this->mConds
;
376 $conds['rev_page'] = $this->articleID
;
377 $conds[] = "rev_timestamp < {$this->maxTimestamp}";
378 # Skip the latest one, as that could cause problems
379 if( $page = $this->title
->getLatestRevID() )
380 $conds[] = "rev_id != {$page}";
383 'tables' => array('revision'),
384 'fields' => array( 'rev_minor_edit', 'rev_timestamp', 'rev_user', 'rev_user_text', 'rev_comment',
385 'rev_id', 'rev_page', 'rev_text_id', 'rev_len', 'rev_deleted' ),
390 function getIndexField() {
391 return 'rev_timestamp';