Revision:getId() not getID()
[lhc/web/wiklou.git] / includes / SpecialMergeHistory.php
1 <?php
2 /**
3 * Special page allowing users with the appropriate permissions to
4 * merge article histories, with some restrictions
5 *
6 * @file
7 * @ingroup SpecialPage
8 */
9
10 /**
11 * Constructor
12 */
13 function wfSpecialMergehistory( $par ) {
14 global $wgRequest;
15
16 $form = new MergehistoryForm( $wgRequest, $par );
17 $form->execute();
18 }
19
20 /**
21 * The HTML form for Special:MergeHistory, which allows users with the appropriate
22 * permissions to view and restore deleted content.
23 * @ingroup SpecialPage
24 */
25 class MergehistoryForm {
26 var $mAction, $mTarget, $mDest, $mTimestamp, $mTargetID, $mDestID, $mComment;
27 var $mTargetObj, $mDestObj;
28
29 function MergehistoryForm( $request, $par = "" ) {
30 global $wgUser;
31
32 $this->mAction = $request->getVal( 'action' );
33 $this->mTarget = $request->getVal( 'target' );
34 $this->mDest = $request->getVal( 'dest' );
35 $this->mSubmitted = $request->getBool( 'submitted' );
36
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 = '';
42 }
43 $this->mComment = $request->getText( 'wpComment' );
44
45 $this->mMerge = $request->wasPosted() && $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
46 // target page
47 if( $this->mSubmitted ) {
48 $this->mTargetObj = Title::newFromURL( $this->mTarget );
49 $this->mDestObj = Title::newFromURL( $this->mDest );
50 } else {
51 $this->mTargetObj = null;
52 $this->mDestObj = null;
53 }
54
55 $this->preCacheMessages();
56 }
57
58 /**
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
61 */
62 function preCacheMessages() {
63 // Precache various messages
64 if( !isset( $this->message ) ) {
65 $this->message['last'] = wfMsgExt( 'last', array( 'escape') );
66 }
67 }
68
69 function execute() {
70 global $wgOut, $wgUser;
71
72 $wgOut->setPagetitle( wfMsgHtml( "mergehistory" ) );
73
74 if( $this->mTargetID && $this->mDestID && $this->mAction=="submit" && $this->mMerge ) {
75 return $this->merge();
76 }
77
78 if ( !$this->mSubmitted ) {
79 $this->showMergeForm();
80 return;
81 }
82
83 $errors = array();
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() )
89 );
90 }
91
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() )
97 );
98 }
99
100 // TODO: warn about target = dest?
101
102 if ( count( $errors ) ) {
103 $this->showMergeForm();
104 $wgOut->addHTML( implode( "\n", $errors ) );
105 } else {
106 $this->showHistory();
107 }
108
109 }
110
111 function showMergeForm() {
112 global $wgOut, $wgScript;
113
114 $wgOut->addWikiMsg( 'mergehistory-header' );
115
116 $wgOut->addHtml(
117 Xml::openElement( 'form', array(
118 'method' => 'get',
119 'action' => $wgScript ) ) .
120 '<fieldset>' .
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' ) .
128 "<tr>
129 <td>".Xml::label( wfMsg( 'mergehistory-from' ), 'target' )."</td>
130 <td>".Xml::input( 'target', 30, $this->mTarget, array('id'=>'target') )."</td>
131 </tr><tr>
132 <td>".Xml::label( wfMsg( 'mergehistory-into' ), 'dest' )."</td>
133 <td>".Xml::input( 'dest', 30, $this->mDest, array('id'=>'dest') )."</td>
134 </tr><tr><td>" .
135 Xml::submitButton( wfMsg( 'mergehistory-go' ) ) .
136 "</td></tr>" .
137 Xml::closeElement( 'table' ) .
138 '</fieldset>' .
139 '</form>' );
140 }
141
142 private function showHistory() {
143 global $wgLang, $wgContLang, $wgUser, $wgOut;
144
145 $this->sk = $wgUser->getSkin();
146
147 $wgOut->setPagetitle( wfMsg( "mergehistory" ) );
148
149 $this->showMergeForm();
150
151 # List all stored revisions
152 $revisions = new MergeHistoryPager( $this, array(), $this->mTargetObj, $this->mDestObj );
153 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
154
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 );
160
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';
165 $table =
166 Xml::openElement( 'fieldset' ) .
167 Xml::openElement( 'table' ) .
168 "<tr>
169 <td colspan='2'>" .
170 wfMsgExt( 'mergehistory-merge', array('parseinline'),
171 $this->mTargetObj->getPrefixedText(), $this->mDestObj->getPrefixedText() ) .
172 "</td>
173 </tr>
174 <tr>
175 <td align='$align'>" .
176 Xml::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
177 "</td>
178 <td>" .
179 Xml::input( 'wpComment', 50, $this->mComment ) .
180 "</td>
181 </tr>
182 <tr>
183 <td>&nbsp;</td>
184 <td>" .
185 Xml::submitButton( wfMsg( 'mergehistory-submit' ), array( 'name' => 'merge', 'id' => 'mw-merge-submit' ) ) .
186 "</td>
187 </tr>" .
188 Xml::closeElement( 'table' ) .
189 Xml::closeElement( 'fieldset' );
190
191 $wgOut->addHtml( $table );
192 }
193
194 $wgOut->addHTML( "<h2 id=\"mw-mergehistory\">" . wfMsgHtml( "mergehistory-list" ) . "</h2>\n" );
195
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() );
202 } else {
203 $wgOut->addWikiMsg( "mergehistory-empty" );
204 }
205
206 # Show relevant lines from the deletion log:
207 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'merge' ) ) . "</h2>\n" );
208 LogEventsList::showLogExtract( $wgOut, 'merge', $this->mTargetObj->getPrefixedText() );
209
210 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
211 # Such would happen if a page was renamed after the form loaded, but before submit
212 $misc = Xml::hidden( 'targetID', $this->mTargetObj->getArticleID() );
213 $misc .= Xml::hidden( 'destID', $this->mDestObj->getArticleID() );
214 $misc .= Xml::hidden( 'target', $this->mTarget );
215 $misc .= Xml::hidden( 'dest', $this->mDest );
216 $misc .= Xml::hidden( 'wpEditToken', $wgUser->editToken() );
217 $misc .= Xml::closeElement( 'form' );
218 $wgOut->addHtml( $misc );
219
220 return true;
221 }
222
223 function formatRevisionRow( $row ) {
224 global $wgUser, $wgLang;
225
226 $rev = new Revision( $row );
227
228 $stxt = '';
229 $last = $this->message['last'];
230
231 $ts = wfTimestamp( TS_MW, $row->rev_timestamp );
232 $checkBox = wfRadio( "mergepoint", $ts, false );
233
234 $pageLink = $this->sk->makeKnownLinkObj( $rev->getTitle(),
235 htmlspecialchars( $wgLang->timeanddate( $ts ) ), 'oldid=' . $rev->getId() );
236 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
237 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
238 }
239
240 # Last link
241 if( !$rev->userCan( Revision::DELETED_TEXT ) )
242 $last = $this->message['last'];
243 else if( isset($this->prevId[$row->rev_id]) )
244 $last = $this->sk->makeKnownLinkObj( $rev->getTitle(), $this->message['last'],
245 "diff=" . $row->rev_id . "&oldid=" . $this->prevId[$row->rev_id] );
246
247 $userLink = $this->sk->revUserTools( $rev );
248
249 if(!is_null($size = $row->rev_len)) {
250 if($size == 0)
251 $stxt = wfMsgHtml('historyempty');
252 else
253 $stxt = wfMsgHtml('historysize', $wgLang->formatNum( $size ) );
254 }
255 $comment = $this->sk->revComment( $rev );
256
257 return "<li>$checkBox ($last) $pageLink . . $userLink $stxt $comment</li>";
258 }
259
260 /**
261 * Fetch revision text link if it's available to all users
262 * @return string
263 */
264 function getPageLink( $row, $titleObj, $ts, $target ) {
265 global $wgLang;
266
267 if( !$this->userCan($row, Revision::DELETED_TEXT) ) {
268 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
269 } else {
270 $link = $this->sk->makeKnownLinkObj( $titleObj,
271 $wgLang->timeanddate( $ts, true ), "target=$target&timestamp=$ts" );
272 if( $this->isDeleted($row, Revision::DELETED_TEXT) )
273 $link = '<span class="history-deleted">' . $link . '</span>';
274 return $link;
275 }
276 }
277
278 function merge() {
279 global $wgOut, $wgUser;
280 # Get the titles directly from the IDs, in case the target page params
281 # were spoofed. The queries are done based on the IDs, so it's best to
282 # keep it consistent...
283 $targetTitle = Title::newFromID( $this->mTargetID );
284 $destTitle = Title::newFromID( $this->mDestID );
285 if( is_null($targetTitle) || is_null($destTitle) )
286 return false; // validate these
287 if( $targetTitle->getArticleId() == $destTitle->getArticleId() )
288 return false;
289 # Verify that this timestamp is valid
290 # Must be older than the destination page
291 $dbw = wfGetDB( DB_MASTER );
292 # Get timestamp into DB format
293 $this->mTimestamp = $this->mTimestamp ? $dbw->timestamp($this->mTimestamp) : '';
294 # Max timestamp should be min of destination page
295 $maxtimestamp = $dbw->selectField( 'revision', 'MIN(rev_timestamp)',
296 array('rev_page' => $this->mDestID ),
297 __METHOD__ );
298 # Destination page must exist with revisions
299 if( !$maxtimestamp ) {
300 $wgOut->addWikiMsg('mergehistory-fail');
301 return false;
302 }
303 # Get the latest timestamp of the source
304 $lasttimestamp = $dbw->selectField( array('page','revision'),
305 'rev_timestamp',
306 array('page_id' => $this->mTargetID, 'page_latest = rev_id' ),
307 __METHOD__ );
308 # $this->mTimestamp must be older than $maxtimestamp
309 if( $this->mTimestamp >= $maxtimestamp ) {
310 $wgOut->addWikiMsg('mergehistory-fail');
311 return false;
312 }
313 # Update the revisions
314 if( $this->mTimestamp ) {
315 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
316 $TimestampLimit = wfTimestamp(TS_MW,$this->mTimestamp);
317 } else {
318 $timewhere = "rev_timestamp <= {$maxtimestamp}";
319 $TimestampLimit = wfTimestamp(TS_MW,$lasttimestamp);
320 }
321 # Do the moving...
322 $dbw->update( 'revision',
323 array( 'rev_page' => $this->mDestID ),
324 array( 'rev_page' => $this->mTargetID,
325 $timewhere ),
326 __METHOD__ );
327
328 $count = $dbw->affectedRows();
329 # Make the source page a redirect if no revisions are left
330 $haveRevisions = $dbw->selectField( 'revision',
331 'rev_timestamp',
332 array( 'rev_page' => $this->mTargetID ),
333 __METHOD__,
334 array( 'FOR UPDATE' ) );
335 if( !$haveRevisions ) {
336 if( $this->mComment ) {
337 $comment = wfMsgForContent( 'mergehistory-comment', $targetTitle->getPrefixedText(),
338 $destTitle->getPrefixedText(), $this->mComment );
339 } else {
340 $comment = wfMsgForContent( 'mergehistory-autocomment', $targetTitle->getPrefixedText(),
341 $destTitle->getPrefixedText() );
342 }
343 $mwRedir = MagicWord::get( 'redirect' );
344 $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $destTitle->getPrefixedText() . "]]\n";
345 $redirectArticle = new Article( $targetTitle );
346 $redirectRevision = new Revision( array(
347 'page' => $this->mTargetID,
348 'comment' => $comment,
349 'text' => $redirectText ) );
350 $redirectRevision->insertOn( $dbw );
351 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision );
352
353 # Now, we record the link from the redirect to the new title.
354 # It should have no other outgoing links...
355 $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mDestID ), __METHOD__ );
356 $dbw->insert( 'pagelinks',
357 array(
358 'pl_from' => $this->mDestID,
359 'pl_namespace' => $destTitle->getNamespace(),
360 'pl_title' => $destTitle->getDBkey() ),
361 __METHOD__ );
362 } else {
363 $targetTitle->invalidateCache(); // update histories
364 }
365 $destTitle->invalidateCache(); // update histories
366 # Check if this did anything
367 if( !$count ) {
368 $wgOut->addWikiMsg('mergehistory-fail');
369 return false;
370 }
371 # Update our logs
372 $log = new LogPage( 'merge' );
373 $log->addEntry( 'merge', $targetTitle, $this->mComment,
374 array($destTitle->getPrefixedText(),$TimestampLimit) );
375
376 $wgOut->addHtml( wfMsgExt( 'mergehistory-success', array('parseinline'),
377 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText(), $count ) );
378
379 wfRunHooks( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
380
381 return true;
382 }
383 }
384
385 class MergeHistoryPager extends ReverseChronologicalPager {
386 public $mForm, $mConds;
387
388 function __construct( $form, $conds = array(), $source, $dest ) {
389 $this->mForm = $form;
390 $this->mConds = $conds;
391 $this->title = $source;
392 $this->articleID = $source->getArticleID();
393
394 $dbr = wfGetDB( DB_SLAVE );
395 $maxtimestamp = $dbr->selectField( 'revision', 'MIN(rev_timestamp)',
396 array('rev_page' => $dest->getArticleID() ),
397 __METHOD__ );
398 $this->maxTimestamp = $maxtimestamp;
399
400 parent::__construct();
401 }
402
403 function getStartBody() {
404 wfProfileIn( __METHOD__ );
405 # Do a link batch query
406 $this->mResult->seek( 0 );
407 $batch = new LinkBatch();
408 # Give some pointers to make (last) links
409 $this->mForm->prevId = array();
410 while( $row = $this->mResult->fetchObject() ) {
411 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->rev_user_text ) );
412 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->rev_user_text ) );
413
414 $rev_id = isset($rev_id) ? $rev_id : $row->rev_id;
415 if( $rev_id > $row->rev_id )
416 $this->mForm->prevId[$rev_id] = $row->rev_id;
417 else if( $rev_id < $row->rev_id )
418 $this->mForm->prevId[$row->rev_id] = $rev_id;
419
420 $rev_id = $row->rev_id;
421 }
422
423 $batch->execute();
424 $this->mResult->seek( 0 );
425
426 wfProfileOut( __METHOD__ );
427 return '';
428 }
429
430 function formatRow( $row ) {
431 $block = new Block;
432 return $this->mForm->formatRevisionRow( $row );
433 }
434
435 function getQueryInfo() {
436 $conds = $this->mConds;
437 $conds['rev_page'] = $this->articleID;
438 $conds[] = "rev_timestamp < {$this->maxTimestamp}";
439
440 return array(
441 'tables' => array('revision'),
442 'fields' => array( 'rev_minor_edit', 'rev_timestamp', 'rev_user', 'rev_user_text', 'rev_comment',
443 'rev_id', 'rev_page', 'rev_text_id', 'rev_len', 'rev_deleted' ),
444 'conds' => $conds
445 );
446 }
447
448 function getIndexField() {
449 return 'rev_timestamp';
450 }
451 }