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