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