Fix r100138 per Nikas CR: "always use explicit conversion to string with wfMessage"
[lhc/web/wiklou.git] / includes / specials / SpecialMovepage.php
1 <?php
2 /**
3 * Implements Special:Movepage
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 * A special page that allows users to change page titles
26 *
27 * @ingroup SpecialPage
28 */
29 class MovePageForm extends UnlistedSpecialPage {
30
31 /**
32 * @var Title
33 */
34 var $oldTitle, $newTitle; # Objects
35 var $reason; # Text input
36 var $moveTalk, $deleteAndMove, $moveSubpages, $fixRedirects, $leaveRedirect, $moveOverShared; # Checks
37
38 private $watch = false;
39
40 public function __construct() {
41 parent::__construct( 'Movepage' );
42 }
43
44 public function execute( $par ) {
45 # Check for database lock
46 if ( wfReadOnly() ) {
47 throw new ReadOnlyError;
48 }
49
50 $this->setHeaders();
51 $this->outputHeader();
52
53 $request = $this->getRequest();
54 $target = !is_null( $par ) ? $par : $request->getVal( 'target' );
55
56 // Yes, the use of getVal() and getText() is wanted, see bug 20365
57 $oldTitleText = $request->getVal( 'wpOldTitle', $target );
58 $newTitleText = $request->getText( 'wpNewTitle' );
59
60 $this->oldTitle = Title::newFromText( $oldTitleText );
61 $this->newTitle = Title::newFromText( $newTitleText );
62
63 if( is_null( $this->oldTitle ) ) {
64 throw new ErrorPageError( 'notargettitle', 'notargettext' );
65 }
66 if( !$this->oldTitle->exists() ) {
67 throw new ErrorPageError( 'nopagetitle', 'nopagetext' );
68 }
69
70 $user = $this->getUser();
71
72 # Check rights
73 $permErrors = $this->oldTitle->getUserPermissionsErrors( 'move', $user );
74 if( !empty( $permErrors ) ) {
75 // Auto-block user's IP if the account was "hard" blocked
76 $user->spreadAnyEditBlock();
77 $this->getOutput()->showPermissionsErrorPage( $permErrors );
78 return;
79 }
80
81 $def = !$request->wasPosted();
82
83 $this->reason = $request->getText( 'wpReason' );
84 $this->moveTalk = $request->getBool( 'wpMovetalk', $def );
85 $this->fixRedirects = $request->getBool( 'wpFixRedirects', $def );
86 $this->leaveRedirect = $request->getBool( 'wpLeaveRedirect', $def );
87 $this->moveSubpages = $request->getBool( 'wpMovesubpages', false );
88 $this->deleteAndMove = $request->getBool( 'wpDeleteAndMove' ) && $request->getBool( 'wpConfirm' );
89 $this->moveOverShared = $request->getBool( 'wpMoveOverSharedFile', false );
90 $this->watch = $request->getCheck( 'wpWatch' ) && $user->isLoggedIn();
91
92 if ( 'submit' == $request->getVal( 'action' ) && $request->wasPosted()
93 && $user->matchEditToken( $request->getVal( 'wpEditToken' ) ) ) {
94 $this->doSubmit();
95 } else {
96 $this->showForm( '' );
97 }
98 }
99
100 /**
101 * Show the form
102 *
103 * @param $err Mixed: error message. May either be a string message name or
104 * array message name and parameters, like the second argument to
105 * OutputPage::wrapWikiMsg().
106 */
107 function showForm( $err ) {
108 global $wgContLang, $wgFixDoubleRedirects, $wgMaximumMovedPages;
109
110 $this->getSkin()->setRelevantTitle( $this->oldTitle );
111
112 $oldTitleLink = Linker::link( $this->oldTitle );
113
114 $out = $this->getOutput();
115 $out->setPagetitle( wfMsg( 'move-page', $this->oldTitle->getPrefixedText() ) );
116 $out->addModules( 'mediawiki.special.movePage' );
117
118 $newTitle = $this->newTitle;
119
120 if( !$newTitle ) {
121 # Show the current title as a default
122 # when the form is first opened.
123 $newTitle = $this->oldTitle;
124 }
125 else {
126 if( empty($err) ) {
127 # If a title was supplied, probably from the move log revert
128 # link, check for validity. We can then show some diagnostic
129 # information and save a click.
130 $newerr = $this->oldTitle->isValidMoveOperation( $newTitle );
131 if( $newerr ) {
132 $err = $newerr[0];
133 }
134 }
135 }
136
137 $user = $this->getUser();
138
139 if ( !empty($err) && $err[0] == 'articleexists' && $user->isAllowed( 'delete' ) ) {
140 $out->addWikiMsg( 'delete_and_move_text', $newTitle->getPrefixedText() );
141 $movepagebtn = wfMsg( 'delete_and_move' );
142 $submitVar = 'wpDeleteAndMove';
143 $confirm = "
144 <tr>
145 <td></td>
146 <td class='mw-input'>" .
147 Xml::checkLabel( wfMsg( 'delete_and_move_confirm' ), 'wpConfirm', 'wpConfirm' ) .
148 "</td>
149 </tr>";
150 $err = '';
151 } else {
152 if ($this->oldTitle->getNamespace() == NS_USER && !$this->oldTitle->isSubpage() ) {
153 $out->wrapWikiMsg( "<div class=\"error mw-moveuserpage-warning\">\n$1\n</div>", 'moveuserpage-warning' );
154 }
155 $out->addWikiMsg( $wgFixDoubleRedirects ? 'movepagetext' :
156 'movepagetext-noredirectfixer' );
157 $movepagebtn = wfMsg( 'movepagebtn' );
158 $submitVar = 'wpMove';
159 $confirm = false;
160 }
161
162 if ( !empty($err) && $err[0] == 'file-exists-sharedrepo' && $user->isAllowed( 'reupload-shared' ) ) {
163 $out->addWikiMsg( 'move-over-sharedrepo', $newTitle->getPrefixedText() );
164 $submitVar = 'wpMoveOverSharedFile';
165 $err = '';
166 }
167
168 $oldTalk = $this->oldTitle->getTalkPage();
169 $oldTitleSubpages = $this->oldTitle->hasSubpages();
170 $oldTitleTalkSubpages = $this->oldTitle->getTalkPage()->hasSubpages();
171
172 $canMoveSubpage = ( $oldTitleSubpages || $oldTitleTalkSubpages ) &&
173 !count( $this->oldTitle->getUserPermissionsErrors( 'move-subpages', $user ) );
174
175 # We also want to be able to move assoc. subpage talk-pages even if base page
176 # has no associated talk page, so || with $oldTitleTalkSubpages.
177 $considerTalk = !$this->oldTitle->isTalkPage() &&
178 ( $oldTalk->exists()
179 || ( $oldTitleTalkSubpages && $canMoveSubpage ) );
180
181 $dbr = wfGetDB( DB_SLAVE );
182 if ( $wgFixDoubleRedirects ) {
183 $hasRedirects = $dbr->selectField( 'redirect', '1',
184 array(
185 'rd_namespace' => $this->oldTitle->getNamespace(),
186 'rd_title' => $this->oldTitle->getDBkey(),
187 ) , __METHOD__ );
188 } else {
189 $hasRedirects = false;
190 }
191
192 if ( $considerTalk ) {
193 $out->addWikiMsg( 'movepagetalktext' );
194 }
195
196 $token = htmlspecialchars( $user->editToken() );
197
198 if ( !empty($err) ) {
199 $out->setSubtitle( wfMsg( 'formerror' ) );
200 if( $err[0] == 'hookaborted' ) {
201 $hookErr = $err[1];
202 $errMsg = "<p><strong class=\"error\">$hookErr</strong></p>\n";
203 $out->addHTML( $errMsg );
204 } else {
205 $out->wrapWikiMsg( "<p><strong class=\"error\">\n$1\n</strong></p>", $err );
206 }
207 }
208
209 if ( $this->oldTitle->isProtected( 'move' ) ) {
210 # Is the title semi-protected?
211 if ( $this->oldTitle->isSemiProtected( 'move' ) ) {
212 $noticeMsg = 'semiprotectedpagemovewarning';
213 $classes[] = 'mw-textarea-sprotected';
214 } else {
215 # Then it must be protected based on static groups (regular)
216 $noticeMsg = 'protectedpagemovewarning';
217 $classes[] = 'mw-textarea-protected';
218 }
219 $out->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" );
220 $out->addWikiMsg( $noticeMsg );
221 LogEventsList::showLogExtract( $out, 'protect', $this->oldTitle, '', array( 'lim' => 1 ) );
222 $out->addHTML( "</div>\n" );
223 }
224
225 $out->addHTML(
226 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL( 'action=submit' ), 'id' => 'movepage' ) ) .
227 Xml::openElement( 'fieldset' ) .
228 Xml::element( 'legend', null, wfMsg( 'move-page-legend' ) ) .
229 Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-movepage-table' ) ) .
230 "<tr>
231 <td class='mw-label'>" .
232 wfMsgHtml( 'movearticle' ) .
233 "</td>
234 <td class='mw-input'>
235 <strong>{$oldTitleLink}</strong>
236 </td>
237 </tr>
238 <tr>
239 <td class='mw-label'>" .
240 Xml::label( wfMsg( 'newtitle' ), 'wpNewTitle' ) .
241 "</td>
242 <td class='mw-input'>" .
243 Xml::input( 'wpNewTitle', 40, $wgContLang->recodeForEdit( $newTitle->getPrefixedText() ), array( 'type' => 'text', 'id' => 'wpNewTitle' ) ) .
244 Html::hidden( 'wpOldTitle', $this->oldTitle->getPrefixedText() ) .
245 "</td>
246 </tr>
247 <tr>
248 <td class='mw-label'>" .
249 Xml::label( wfMsg( 'movereason' ), 'wpReason' ) .
250 "</td>
251 <td class='mw-input'>" .
252 Html::element( 'textarea', array( 'name' => 'wpReason', 'id' => 'wpReason', 'cols' => 60, 'rows' => 2,
253 'maxlength' => 200 ), $this->reason ) . // maxlength byte limit is enforce in mediawiki.special.movePage.js
254 "</td>
255 </tr>"
256 );
257
258 if( $considerTalk ) {
259 $out->addHTML( "
260 <tr>
261 <td></td>
262 <td class='mw-input'>" .
263 Xml::checkLabel( wfMsg( 'movetalk' ), 'wpMovetalk', 'wpMovetalk', $this->moveTalk ) .
264 "</td>
265 </tr>"
266 );
267 }
268
269 if ( $user->isAllowed( 'suppressredirect' ) ) {
270 $out->addHTML( "
271 <tr>
272 <td></td>
273 <td class='mw-input' >" .
274 Xml::checkLabel( wfMsg( 'move-leave-redirect' ), 'wpLeaveRedirect',
275 'wpLeaveRedirect', $this->leaveRedirect ) .
276 "</td>
277 </tr>"
278 );
279 }
280
281 if ( $hasRedirects ) {
282 $out->addHTML( "
283 <tr>
284 <td></td>
285 <td class='mw-input' >" .
286 Xml::checkLabel( wfMsg( 'fix-double-redirects' ), 'wpFixRedirects',
287 'wpFixRedirects', $this->fixRedirects ) .
288 "</td>
289 </tr>"
290 );
291 }
292
293 if( $canMoveSubpage ) {
294 $out->addHTML( "
295 <tr>
296 <td></td>
297 <td class=\"mw-input\">" .
298 Xml::check(
299 'wpMovesubpages',
300 # Don't check the box if we only have talk subpages to
301 # move and we aren't moving the talk page.
302 $this->moveSubpages && ($this->oldTitle->hasSubpages() || $this->moveTalk),
303 array( 'id' => 'wpMovesubpages' )
304 ) . '&#160;' .
305 Xml::tags( 'label', array( 'for' => 'wpMovesubpages' ),
306 wfMsgExt(
307 ( $this->oldTitle->hasSubpages()
308 ? 'move-subpages'
309 : 'move-talk-subpages' ),
310 array( 'parseinline' ),
311 $this->getLang()->formatNum( $wgMaximumMovedPages ),
312 # $2 to allow use of PLURAL in message.
313 $wgMaximumMovedPages
314 )
315 ) .
316 "</td>
317 </tr>"
318 );
319 }
320
321 $watchChecked = $user->isLoggedIn() && ($this->watch || $user->getBoolOption( 'watchmoves' )
322 || $this->oldTitle->userIsWatching());
323 # Don't allow watching if user is not logged in
324 if( $user->isLoggedIn() ) {
325 $out->addHTML( "
326 <tr>
327 <td></td>
328 <td class='mw-input'>" .
329 Xml::checkLabel( wfMsg( 'move-watch' ), 'wpWatch', 'watch', $watchChecked ) .
330 "</td>
331 </tr>");
332 }
333
334 $out->addHTML( "
335 {$confirm}
336 <tr>
337 <td>&#160;</td>
338 <td class='mw-submit'>" .
339 Xml::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
340 "</td>
341 </tr>" .
342 Xml::closeElement( 'table' ) .
343 Html::hidden( 'wpEditToken', $token ) .
344 Xml::closeElement( 'fieldset' ) .
345 Xml::closeElement( 'form' ) .
346 "\n"
347 );
348
349 $this->showLogFragment( $this->oldTitle );
350 $this->showSubpages( $this->oldTitle );
351
352 }
353
354 function doSubmit() {
355 global $wgMaximumMovedPages, $wgFixDoubleRedirects, $wgDeleteRevisionsLimit;
356
357 $user = $this->getUser();
358
359 if ( $user->pingLimiter( 'move' ) ) {
360 throw new ThrottledError;
361 }
362
363 $ot = $this->oldTitle;
364 $nt = $this->newTitle;
365
366 # Delete to make way if requested
367 if ( $user->isAllowed( 'delete' ) && $this->deleteAndMove ) {
368 $article = new Article( $nt );
369
370 # Disallow deletions of big articles
371 $bigHistory = $article->isBigDeletion();
372 if( $bigHistory && count( $nt->getUserPermissionsErrors( 'bigdelete', $user ) ) ) {
373 $this->showForm( array('delete-toobig', $this->getLang()->formatNum( $wgDeleteRevisionsLimit ) ) );
374 return;
375 }
376
377 // Delete an associated image if there is
378 $file = wfLocalFile( $nt );
379 if( $file->exists() ) {
380 $file->delete( wfMsgForContent( 'delete_and_move_reason' ), false );
381 }
382
383 // This may output an error message and exit
384 $article->doDelete( wfMsgForContent( 'delete_and_move_reason' ) );
385 }
386
387 # don't allow moving to pages with # in
388 if ( !$nt || $nt->getFragment() != '' ) {
389 $this->showForm( 'badtitletext' );
390 return;
391 }
392
393 # Show a warning if the target file exists on a shared repo
394 if ( $nt->getNamespace() == NS_FILE
395 && !( $this->moveOverShared && $user->isAllowed( 'reupload-shared' ) )
396 && !RepoGroup::singleton()->getLocalRepo()->findFile( $nt )
397 && wfFindFile( $nt ) )
398 {
399 $this->showForm( array('file-exists-sharedrepo') );
400 return;
401
402 }
403
404 if ( $user->isAllowed( 'suppressredirect' ) ) {
405 $createRedirect = $this->leaveRedirect;
406 } else {
407 $createRedirect = true;
408 }
409
410 # Do the actual move.
411 $error = $ot->moveTo( $nt, true, $this->reason, $createRedirect );
412 if ( $error !== true ) {
413 # @todo FIXME: Show all the errors in a list, not just the first one
414 $this->showForm( reset( $error ) );
415 return;
416 }
417
418 if ( $wgFixDoubleRedirects && $this->fixRedirects ) {
419 DoubleRedirectJob::fixRedirects( 'move', $ot, $nt );
420 }
421
422 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this, &$ot, &$nt ) );
423
424 $out = $this->getOutput();
425 $out->setPagetitle( wfMsg( 'pagemovedsub' ) );
426
427 $oldLink = Linker::link(
428 $ot,
429 null,
430 array(),
431 array( 'redirect' => 'no' )
432 );
433 $newLink = Linker::linkKnown( $nt );
434 $oldText = $ot->getPrefixedText();
435 $newText = $nt->getPrefixedText();
436
437 $msgName = $createRedirect ? 'movepage-moved-redirect' : 'movepage-moved-noredirect';
438 $out->addHTML( wfMessage( 'movepage-moved' )->rawParams( $oldLink, $newLink, $oldText, $newText )->parseAsBlock() );
439 $out->addWikiMsg( $msgName );
440
441 # Now we move extra pages we've been asked to move: subpages and talk
442 # pages. First, if the old page or the new page is a talk page, we
443 # can't move any talk pages: cancel that.
444 if( $ot->isTalkPage() || $nt->isTalkPage() ) {
445 $this->moveTalk = false;
446 }
447
448 if ( count( $ot->getUserPermissionsErrors( 'move-subpages', $user ) ) ) {
449 $this->moveSubpages = false;
450 }
451
452 # Next make a list of id's. This might be marginally less efficient
453 # than a more direct method, but this is not a highly performance-cri-
454 # tical code path and readable code is more important here.
455 #
456 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
457 # 4 might get confused. If so, consider rewriting as a UNION.
458 #
459 # If the target namespace doesn't allow subpages, moving with subpages
460 # would mean that you couldn't move them back in one operation, which
461 # is bad.
462 # @todo FIXME: A specific error message should be given in this case.
463
464 // @todo FIXME: Use Title::moveSubpages() here
465 $dbr = wfGetDB( DB_MASTER );
466 if( $this->moveSubpages && (
467 MWNamespace::hasSubpages( $nt->getNamespace() ) || (
468 $this->moveTalk &&
469 MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() )
470 )
471 ) ) {
472 $conds = array(
473 'page_title' . $dbr->buildLike( $ot->getDBkey() . '/', $dbr->anyString() )
474 .' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
475 );
476 $conds['page_namespace'] = array();
477 if( MWNamespace::hasSubpages( $nt->getNamespace() ) ) {
478 $conds['page_namespace'] []= $ot->getNamespace();
479 }
480 if( $this->moveTalk && MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() ) ) {
481 $conds['page_namespace'] []= $ot->getTalkPage()->getNamespace();
482 }
483 } elseif( $this->moveTalk ) {
484 $conds = array(
485 'page_namespace' => $ot->getTalkPage()->getNamespace(),
486 'page_title' => $ot->getDBkey()
487 );
488 } else {
489 # Skip the query
490 $conds = null;
491 }
492
493 $extraPages = array();
494 if( !is_null( $conds ) ) {
495 $extraPages = TitleArray::newFromResult(
496 $dbr->select( 'page',
497 array( 'page_id', 'page_namespace', 'page_title' ),
498 $conds,
499 __METHOD__
500 )
501 );
502 }
503
504 $extraOutput = array();
505 $count = 1;
506 foreach( $extraPages as $oldSubpage ) {
507 if( $ot->equals( $oldSubpage ) ) {
508 # Already did this one.
509 continue;
510 }
511
512 $newPageName = preg_replace(
513 '#^'.preg_quote( $ot->getDBkey(), '#' ).'#',
514 StringUtils::escapeRegexReplacement( $nt->getDBkey() ), # bug 21234
515 $oldSubpage->getDBkey()
516 );
517 if( $oldSubpage->isTalkPage() ) {
518 $newNs = $nt->getTalkPage()->getNamespace();
519 } else {
520 $newNs = $nt->getSubjectPage()->getNamespace();
521 }
522 # Bug 14385: we need makeTitleSafe because the new page names may
523 # be longer than 255 characters.
524 $newSubpage = Title::makeTitleSafe( $newNs, $newPageName );
525 if( !$newSubpage ) {
526 $oldLink = Linker::linkKnown( $oldSubpage );
527 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink,
528 htmlspecialchars(Title::makeName( $newNs, $newPageName )));
529 continue;
530 }
531
532 # This was copy-pasted from Renameuser, bleh.
533 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
534 $link = Linker::linkKnown( $newSubpage );
535 $extraOutput []= wfMsgHtml( 'movepage-page-exists', $link );
536 } else {
537 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason, $createRedirect );
538 if( $success === true ) {
539 if ( $this->fixRedirects ) {
540 DoubleRedirectJob::fixRedirects( 'move', $oldSubpage, $newSubpage );
541 }
542 $oldLink = Linker::link(
543 $oldSubpage,
544 null,
545 array(),
546 array( 'redirect' => 'no' )
547 );
548 $newLink = Linker::linkKnown( $newSubpage );
549 $extraOutput []= wfMsgHtml( 'movepage-page-moved', $oldLink, $newLink );
550 ++$count;
551 if( $count >= $wgMaximumMovedPages ) {
552 $extraOutput []= wfMsgExt( 'movepage-max-pages', array( 'parsemag', 'escape' ), $this->getLang()->formatNum( $wgMaximumMovedPages ) );
553 break;
554 }
555 } else {
556 $oldLink = Linker::linkKnown( $oldSubpage );
557 $newLink = Linker::link( $newSubpage );
558 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink, $newLink );
559 }
560 }
561
562 }
563
564 if( $extraOutput !== array() ) {
565 $out->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
566 }
567
568 # Deal with watches (we don't watch subpages)
569 if( $this->watch && $user->isLoggedIn() ) {
570 $user->addWatch( $ot );
571 $user->addWatch( $nt );
572 } else {
573 $user->removeWatch( $ot );
574 $user->removeWatch( $nt );
575 }
576
577 # Re-clear the file redirect cache, which may have been polluted by
578 # parsing in messages above. See CR r56745.
579 # @todo FIXME: Needs a more robust solution inside FileRepo.
580 if( $ot->getNamespace() == NS_FILE ) {
581 RepoGroup::singleton()->getLocalRepo()->invalidateImageRedirect( $ot );
582 }
583 }
584
585 function showLogFragment( $title ) {
586 $out = $this->getOutput();
587 $out->addHTML( Xml::element( 'h2', null, LogPage::logName( 'move' ) ) );
588 LogEventsList::showLogExtract( $out, 'move', $title );
589 }
590
591 function showSubpages( $title ) {
592 if( !MWNamespace::hasSubpages( $title->getNamespace() ) )
593 return;
594
595 $subpages = $title->getSubpages();
596 $count = $subpages instanceof TitleArray ? $subpages->count() : 0;
597
598 $out = $this->getOutput();
599 $out->wrapWikiMsg( '== $1 ==', array( 'movesubpage', $count ) );
600
601 # No subpages.
602 if ( $count == 0 ) {
603 $out->addWikiMsg( 'movenosubpage' );
604 return;
605 }
606
607 $out->addWikiMsg( 'movesubpagetext', $this->getLang()->formatNum( $count ) );
608 $out->addHTML( "<ul>\n" );
609
610 foreach( $subpages as $subpage ) {
611 $link = Linker::link( $subpage );
612 $out->addHTML( "<li>$link</li>\n" );
613 }
614 $out->addHTML( "</ul>\n" );
615 }
616 }
617