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