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