Switch some HTMLForms in special pages to OOUI
[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 string */
32 protected $mAction;
33
34 /** @var string */
35 protected $mTarget;
36
37 /** @var string */
38 protected $mDest;
39
40 /** @var string */
41 protected $mTimestamp;
42
43 /** @var int */
44 protected $mTargetID;
45
46 /** @var int */
47 protected $mDestID;
48
49 /** @var string */
50 protected $mComment;
51
52 /** @var bool Was posted? */
53 protected $mMerge;
54
55 /** @var bool Was submitted? */
56 protected $mSubmitted;
57
58 /** @var Title */
59 protected $mTargetObj;
60
61 /** @var Title */
62 protected $mDestObj;
63
64 public function __construct() {
65 parent::__construct( 'MergeHistory', 'mergehistory' );
66 }
67
68 /**
69 * @return void
70 */
71 private function loadRequestParams() {
72 $request = $this->getRequest();
73 $this->mAction = $request->getVal( 'action' );
74 $this->mTarget = $request->getVal( 'target' );
75 $this->mDest = $request->getVal( 'dest' );
76 $this->mSubmitted = $request->getBool( 'submitted' );
77
78 $this->mTargetID = intval( $request->getVal( 'targetID' ) );
79 $this->mDestID = intval( $request->getVal( 'destID' ) );
80 $this->mTimestamp = $request->getVal( 'mergepoint' );
81 if ( !preg_match( '/[0-9]{14}/', $this->mTimestamp ) ) {
82 $this->mTimestamp = '';
83 }
84 $this->mComment = $request->getText( 'wpComment' );
85
86 $this->mMerge = $request->wasPosted()
87 && $this->getUser()->matchEditToken( $request->getVal( 'wpEditToken' ) );
88
89 // target page
90 if ( $this->mSubmitted ) {
91 $this->mTargetObj = Title::newFromURL( $this->mTarget );
92 $this->mDestObj = Title::newFromURL( $this->mDest );
93 } else {
94 $this->mTargetObj = null;
95 $this->mDestObj = null;
96 }
97 $this->preCacheMessages();
98 }
99
100 /**
101 * As we use the same small set of messages in various methods and that
102 * they are called often, we call them once and save them in $this->message
103 */
104 function preCacheMessages() {
105 // Precache various messages
106 if ( !isset( $this->message ) ) {
107 $this->message['last'] = $this->msg( 'last' )->escaped();
108 }
109 }
110
111 public function execute( $par ) {
112 $this->checkPermissions();
113 $this->checkReadOnly();
114
115 $this->loadRequestParams();
116
117 $this->setHeaders();
118 $this->outputHeader();
119
120 if ( $this->mTargetID && $this->mDestID && $this->mAction == 'submit' && $this->mMerge ) {
121 $this->merge();
122
123 return;
124 }
125
126 if ( !$this->mSubmitted ) {
127 $this->showMergeForm();
128
129 return;
130 }
131
132 $errors = array();
133 if ( !$this->mTargetObj instanceof Title ) {
134 $errors[] = $this->msg( 'mergehistory-invalid-source' )->parseAsBlock();
135 } elseif ( !$this->mTargetObj->exists() ) {
136 $errors[] = $this->msg( 'mergehistory-no-source',
137 wfEscapeWikiText( $this->mTargetObj->getPrefixedText() )
138 )->parseAsBlock();
139 }
140
141 if ( !$this->mDestObj instanceof Title ) {
142 $errors[] = $this->msg( 'mergehistory-invalid-destination' )->parseAsBlock();
143 } elseif ( !$this->mDestObj->exists() ) {
144 $errors[] = $this->msg( 'mergehistory-no-destination',
145 wfEscapeWikiText( $this->mDestObj->getPrefixedText() )
146 )->parseAsBlock();
147 }
148
149 if ( $this->mTargetObj && $this->mDestObj && $this->mTargetObj->equals( $this->mDestObj ) ) {
150 $errors[] = $this->msg( 'mergehistory-same-destination' )->parseAsBlock();
151 }
152
153 if ( count( $errors ) ) {
154 $this->showMergeForm();
155 $this->getOutput()->addHTML( implode( "\n", $errors ) );
156 } else {
157 $this->showHistory();
158 }
159 }
160
161 function showMergeForm() {
162 $out = $this->getOutput();
163 $out->addWikiMsg( 'mergehistory-header' );
164
165 $out->addHTML(
166 Xml::openElement( 'form', array(
167 'method' => 'get',
168 'action' => wfScript() ) ) .
169 '<fieldset>' .
170 Xml::element( 'legend', array(),
171 $this->msg( 'mergehistory-box' )->text() ) .
172 Html::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) .
173 Html::hidden( 'submitted', '1' ) .
174 Html::hidden( 'mergepoint', $this->mTimestamp ) .
175 Xml::openElement( 'table' ) .
176 '<tr>
177 <td>' . Xml::label( $this->msg( 'mergehistory-from' )->text(), 'target' ) . '</td>
178 <td>' . Xml::input( 'target', 30, $this->mTarget, array( 'id' => 'target' ) ) . '</td>
179 </tr><tr>
180 <td>' . Xml::label( $this->msg( 'mergehistory-into' )->text(), 'dest' ) . '</td>
181 <td>' . Xml::input( 'dest', 30, $this->mDest, array( 'id' => 'dest' ) ) . '</td>
182 </tr><tr><td>' .
183 Xml::submitButton( $this->msg( 'mergehistory-go' )->text() ) .
184 '</td></tr>' .
185 Xml::closeElement( 'table' ) .
186 '</fieldset>' .
187 '</form>'
188 );
189
190 $this->addHelpLink( 'Help:Merge history' );
191 }
192
193 private function showHistory() {
194 $this->showMergeForm();
195
196 # List all stored revisions
197 $revisions = new MergeHistoryPager(
198 $this, array(), $this->mTargetObj, $this->mDestObj
199 );
200 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
201
202 $out = $this->getOutput();
203 $titleObj = $this->getPageTitle();
204 $action = $titleObj->getLocalURL( array( 'action' => 'submit' ) );
205 # Start the form here
206 $top = Xml::openElement(
207 'form',
208 array(
209 'method' => 'post',
210 'action' => $action,
211 'id' => 'merge'
212 )
213 );
214 $out->addHTML( $top );
215
216 if ( $haveRevisions ) {
217 # Format the user-visible controls (comment field, submission button)
218 # in a nice little table
219 $table =
220 Xml::openElement( 'fieldset' ) .
221 $this->msg( 'mergehistory-merge', $this->mTargetObj->getPrefixedText(),
222 $this->mDestObj->getPrefixedText() )->parse() .
223 Xml::openElement( 'table', array( 'id' => 'mw-mergehistory-table' ) ) .
224 '<tr>
225 <td class="mw-label">' .
226 Xml::label( $this->msg( 'mergehistory-reason' )->text(), 'wpComment' ) .
227 '</td>
228 <td class="mw-input">' .
229 Xml::input( 'wpComment', 50, $this->mComment, array( 'id' => 'wpComment' ) ) .
230 '</td>
231 </tr>
232 <tr>
233 <td>&#160;</td>
234 <td class="mw-submit">' .
235 Xml::submitButton(
236 $this->msg( 'mergehistory-submit' )->text(),
237 array( 'name' => 'merge', 'id' => 'mw-merge-submit' )
238 ) .
239 '</td>
240 </tr>' .
241 Xml::closeElement( 'table' ) .
242 Xml::closeElement( 'fieldset' );
243
244 $out->addHTML( $table );
245 }
246
247 $out->addHTML(
248 '<h2 id="mw-mergehistory">' .
249 $this->msg( 'mergehistory-list' )->escaped() . "</h2>\n"
250 );
251
252 if ( $haveRevisions ) {
253 $out->addHTML( $revisions->getNavigationBar() );
254 $out->addHTML( '<ul>' );
255 $out->addHTML( $revisions->getBody() );
256 $out->addHTML( '</ul>' );
257 $out->addHTML( $revisions->getNavigationBar() );
258 } else {
259 $out->addWikiMsg( 'mergehistory-empty' );
260 }
261
262 # Show relevant lines from the merge log:
263 $mergeLogPage = new LogPage( 'merge' );
264 $out->addHTML( '<h2>' . $mergeLogPage->getName()->escaped() . "</h2>\n" );
265 LogEventsList::showLogExtract( $out, 'merge', $this->mTargetObj );
266
267 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
268 # Such would happen if a page was renamed after the form loaded, but before submit
269 $misc = Html::hidden( 'targetID', $this->mTargetObj->getArticleID() );
270 $misc .= Html::hidden( 'destID', $this->mDestObj->getArticleID() );
271 $misc .= Html::hidden( 'target', $this->mTarget );
272 $misc .= Html::hidden( 'dest', $this->mDest );
273 $misc .= Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
274 $misc .= Xml::closeElement( 'form' );
275 $out->addHTML( $misc );
276
277 return true;
278 }
279
280 function formatRevisionRow( $row ) {
281 $rev = new Revision( $row );
282
283 $stxt = '';
284 $last = $this->message['last'];
285
286 $ts = wfTimestamp( TS_MW, $row->rev_timestamp );
287 $checkBox = Xml::radio( 'mergepoint', $ts, ( $this->mTimestamp === $ts ) );
288
289 $user = $this->getUser();
290
291 $pageLink = Linker::linkKnown(
292 $rev->getTitle(),
293 htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) ),
294 array(),
295 array( 'oldid' => $rev->getId() )
296 );
297 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
298 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
299 }
300
301 # Last link
302 if ( !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
303 $last = $this->message['last'];
304 } elseif ( isset( $this->prevId[$row->rev_id] ) ) {
305 $last = Linker::linkKnown(
306 $rev->getTitle(),
307 $this->message['last'],
308 array(),
309 array(
310 'diff' => $row->rev_id,
311 'oldid' => $this->prevId[$row->rev_id]
312 )
313 );
314 }
315
316 $userLink = Linker::revUserTools( $rev );
317
318 $size = $row->rev_len;
319 if ( !is_null( $size ) ) {
320 $stxt = Linker::formatRevisionSize( $size );
321 }
322 $comment = Linker::revComment( $rev );
323
324 return Html::rawElement( 'li', array(),
325 $this->msg( 'mergehistory-revisionrow' )
326 ->rawParams( $checkBox, $last, $pageLink, $userLink, $stxt, $comment )->escaped() );
327 }
328
329 /**
330 * Actually attempt the history move
331 *
332 * @todo if all versions of page A are moved to B and then a user
333 * tries to do a reverse-merge via the "unmerge" log link, then page
334 * A will still be a redirect (as it was after the original merge),
335 * though it will have the old revisions back from before (as expected).
336 * The user may have to "undo" the redirect manually to finish the "unmerge".
337 * Maybe this should delete redirects at the target page of merges?
338 *
339 * @return bool Success
340 */
341 function merge() {
342 # Get the titles directly from the IDs, in case the target page params
343 # were spoofed. The queries are done based on the IDs, so it's best to
344 # keep it consistent...
345 $targetTitle = Title::newFromID( $this->mTargetID );
346 $destTitle = Title::newFromID( $this->mDestID );
347 if ( is_null( $targetTitle ) || is_null( $destTitle ) ) {
348 return false; // validate these
349 }
350 if ( $targetTitle->getArticleID() == $destTitle->getArticleID() ) {
351 return false;
352 }
353 # Verify that this timestamp is valid
354 # Must be older than the destination page
355 $dbw = wfGetDB( DB_MASTER );
356 # Get timestamp into DB format
357 $this->mTimestamp = $this->mTimestamp ? $dbw->timestamp( $this->mTimestamp ) : '';
358 # Max timestamp should be min of destination page
359 $maxtimestamp = $dbw->selectField(
360 'revision',
361 'MIN(rev_timestamp)',
362 array( 'rev_page' => $this->mDestID ),
363 __METHOD__
364 );
365 # Destination page must exist with revisions
366 if ( !$maxtimestamp ) {
367 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
368
369 return false;
370 }
371 # Get the latest timestamp of the source
372 $lasttimestamp = $dbw->selectField(
373 array( 'page', 'revision' ),
374 'rev_timestamp',
375 array( 'page_id' => $this->mTargetID, 'page_latest = rev_id' ),
376 __METHOD__
377 );
378 # $this->mTimestamp must be older than $maxtimestamp
379 if ( $this->mTimestamp >= $maxtimestamp ) {
380 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
381
382 return false;
383 }
384 # Get the timestamp pivot condition
385 if ( $this->mTimestamp ) {
386 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
387 $timestampLimit = wfTimestamp( TS_MW, $this->mTimestamp );
388 } else {
389 $timewhere = "rev_timestamp <= {$maxtimestamp}";
390 $timestampLimit = wfTimestamp( TS_MW, $lasttimestamp );
391 }
392 # Check that there are not too many revisions to move
393 $limit = 5000; // avoid too much slave lag
394 $count = $dbw->selectRowCount( 'revision', '1',
395 array( 'rev_page' => $this->mTargetID, $timewhere ),
396 __METHOD__,
397 array( 'LIMIT' => $limit + 1 )
398 );
399 if ( $count > $limit ) {
400 $this->getOutput()->addWikiMsg( 'mergehistory-fail-toobig' );
401
402 return false;
403 }
404 # Do the moving...
405 $dbw->update(
406 'revision',
407 array( 'rev_page' => $this->mDestID ),
408 array( 'rev_page' => $this->mTargetID, $timewhere ),
409 __METHOD__
410 );
411
412 $count = $dbw->affectedRows();
413 # Make the source page a redirect if no revisions are left
414 $haveRevisions = $dbw->selectField(
415 'revision',
416 'rev_timestamp',
417 array( 'rev_page' => $this->mTargetID ),
418 __METHOD__,
419 array( 'FOR UPDATE' )
420 );
421 if ( !$haveRevisions ) {
422 if ( $this->mComment ) {
423 $comment = $this->msg(
424 'mergehistory-comment',
425 $targetTitle->getPrefixedText(),
426 $destTitle->getPrefixedText(),
427 $this->mComment
428 )->inContentLanguage()->text();
429 } else {
430 $comment = $this->msg(
431 'mergehistory-autocomment',
432 $targetTitle->getPrefixedText(),
433 $destTitle->getPrefixedText()
434 )->inContentLanguage()->text();
435 }
436
437 $contentHandler = ContentHandler::getForTitle( $targetTitle );
438 $redirectContent = $contentHandler->makeRedirectContent( $destTitle );
439
440 if ( $redirectContent ) {
441 $redirectPage = WikiPage::factory( $targetTitle );
442 $redirectRevision = new Revision( array(
443 'title' => $targetTitle,
444 'page' => $this->mTargetID,
445 'comment' => $comment,
446 'content' => $redirectContent ) );
447 $redirectRevision->insertOn( $dbw );
448 $redirectPage->updateRevisionOn( $dbw, $redirectRevision );
449
450 # Now, we record the link from the redirect to the new title.
451 # It should have no other outgoing links...
452 $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mDestID ), __METHOD__ );
453 $dbw->insert( 'pagelinks',
454 array(
455 'pl_from' => $this->mDestID,
456 'pl_from_namespace' => $destTitle->getNamespace(),
457 'pl_namespace' => $destTitle->getNamespace(),
458 'pl_title' => $destTitle->getDBkey() ),
459 __METHOD__
460 );
461 } else {
462 // would be nice to show a warning if we couldn't create a redirect
463 }
464 } else {
465 $targetTitle->invalidateCache(); // update histories
466 }
467 $destTitle->invalidateCache(); // update histories
468 # Check if this did anything
469 if ( !$count ) {
470 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
471
472 return false;
473 }
474 # Update our logs
475 $logEntry = new ManualLogEntry( 'merge', 'merge' );
476 $logEntry->setPerformer( $this->getUser() );
477 $logEntry->setComment( $this->mComment );
478 $logEntry->setTarget( $targetTitle );
479 $logEntry->setParameters( array(
480 '4::dest' => $destTitle->getPrefixedText(),
481 '5::mergepoint' => $timestampLimit
482 ) );
483 $logId = $logEntry->insert();
484 $logEntry->publish( $logId );
485
486 # @todo message should use redirect=no
487 $this->getOutput()->addWikiText( $this->msg( 'mergehistory-success',
488 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText() )->numParams(
489 $count )->text() );
490
491 Hooks::run( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
492
493 return true;
494 }
495
496 protected function getGroupName() {
497 return 'pagetools';
498 }
499 }
500
501 class MergeHistoryPager extends ReverseChronologicalPager {
502 /** @var IContextSource */
503 public $mForm;
504
505 /** @var array */
506 public $mConds;
507
508 function __construct( $form, $conds, $source, $dest ) {
509 $this->mForm = $form;
510 $this->mConds = $conds;
511 $this->title = $source;
512 $this->articleID = $source->getArticleID();
513
514 $dbr = wfGetDB( DB_SLAVE );
515 $maxtimestamp = $dbr->selectField(
516 'revision',
517 'MIN(rev_timestamp)',
518 array( 'rev_page' => $dest->getArticleID() ),
519 __METHOD__
520 );
521 $this->maxTimestamp = $maxtimestamp;
522
523 parent::__construct( $form->getContext() );
524 }
525
526 function getStartBody() {
527 # Do a link batch query
528 $this->mResult->seek( 0 );
529 $batch = new LinkBatch();
530 # Give some pointers to make (last) links
531 $this->mForm->prevId = array();
532 foreach ( $this->mResult as $row ) {
533 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
534 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
535
536 $rev_id = isset( $rev_id ) ? $rev_id : $row->rev_id;
537 if ( $rev_id > $row->rev_id ) {
538 $this->mForm->prevId[$rev_id] = $row->rev_id;
539 } elseif ( $rev_id < $row->rev_id ) {
540 $this->mForm->prevId[$row->rev_id] = $rev_id;
541 }
542
543 $rev_id = $row->rev_id;
544 }
545
546 $batch->execute();
547 $this->mResult->seek( 0 );
548
549 return '';
550 }
551
552 function formatRow( $row ) {
553 return $this->mForm->formatRevisionRow( $row );
554 }
555
556 function getQueryInfo() {
557 $conds = $this->mConds;
558 $conds['rev_page'] = $this->articleID;
559 $conds[] = "rev_timestamp < " . $this->mDb->addQuotes( $this->maxTimestamp );
560
561 return array(
562 'tables' => array( 'revision', 'page', 'user' ),
563 'fields' => array_merge( Revision::selectFields(), Revision::selectUserFields() ),
564 'conds' => $conds,
565 'join_conds' => array(
566 'page' => Revision::pageJoinCond(),
567 'user' => Revision::userJoinCond() )
568 );
569 }
570
571 function getIndexField() {
572 return 'rev_timestamp';
573 }
574 }