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