3 * Implements Special:Movepage
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.
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.
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
21 * @ingroup SpecialPage
25 * A special page that allows users to change page titles
27 * @ingroup SpecialPage
29 class MovePageForm
extends UnlistedSpecialPage
{
34 var $oldTitle, $newTitle; # Objects
35 var $reason; # Text input
36 var $moveTalk, $deleteAndMove, $moveSubpages, $fixRedirects, $leaveRedirect, $moveOverShared; # Checks
38 private $watch = false;
40 public function __construct() {
41 parent
::__construct( 'Movepage' );
44 public function execute( $par ) {
45 $this->checkReadOnly();
48 $this->outputHeader();
50 $request = $this->getRequest();
51 $target = !is_null( $par ) ?
$par : $request->getVal( 'target' );
53 // Yes, the use of getVal() and getText() is wanted, see bug 20365
55 $oldTitleText = $request->getVal( 'wpOldTitle', $target );
56 $this->oldTitle
= Title
::newFromText( $oldTitleText );
58 if ( is_null( $this->oldTitle
) ) {
59 throw new ErrorPageError( 'notargettitle', 'notargettext' );
61 if ( !$this->oldTitle
->exists() ) {
62 throw new ErrorPageError( 'nopagetitle', 'nopagetext' );
65 $newTitleTextMain = $request->getText( 'wpNewTitleMain' );
66 $newTitleTextNs = $request->getInt( 'wpNewTitleNs', $this->oldTitle
->getNamespace() );
67 // Backwards compatibility for forms submitting here from other sources
68 // which is more common than it should be..
69 $newTitleText_bc = $request->getText( 'wpNewTitle' );
70 $this->newTitle
= strlen( $newTitleText_bc ) > 0
71 ? Title
::newFromText( $newTitleText_bc )
72 : Title
::makeTitleSafe( $newTitleTextNs, $newTitleTextMain );
74 $user = $this->getUser();
77 $permErrors = $this->oldTitle
->getUserPermissionsErrors( 'move', $user );
78 if ( count( $permErrors ) ) {
79 // Auto-block user's IP if the account was "hard" blocked
80 $user->spreadAnyEditBlock();
81 throw new PermissionsError( 'move', $permErrors );
84 $def = !$request->wasPosted();
86 $this->reason
= $request->getText( 'wpReason' );
87 $this->moveTalk
= $request->getBool( 'wpMovetalk', $def );
88 $this->fixRedirects
= $request->getBool( 'wpFixRedirects', $def );
89 $this->leaveRedirect
= $request->getBool( 'wpLeaveRedirect', $def );
90 $this->moveSubpages
= $request->getBool( 'wpMovesubpages', false );
91 $this->deleteAndMove
= $request->getBool( 'wpDeleteAndMove' ) && $request->getBool( 'wpConfirm' );
92 $this->moveOverShared
= $request->getBool( 'wpMoveOverSharedFile', false );
93 $this->watch
= $request->getCheck( 'wpWatch' ) && $user->isLoggedIn();
95 if ( 'submit' == $request->getVal( 'action' ) && $request->wasPosted()
96 && $user->matchEditToken( $request->getVal( 'wpEditToken' ) ) ) {
99 $this->showForm( array() );
106 * @param array $err error messages. Each item is an error message.
107 * It may either be a string message name or array message name and
108 * parameters, like the second argument to OutputPage::wrapWikiMsg().
110 function showForm( $err ) {
111 global $wgContLang, $wgFixDoubleRedirects, $wgMaximumMovedPages;
113 $this->getSkin()->setRelevantTitle( $this->oldTitle
);
115 $oldTitleLink = Linker
::link( $this->oldTitle
);
117 $out = $this->getOutput();
118 $out->setPageTitle( $this->msg( 'move-page', $this->oldTitle
->getPrefixedText() ) );
119 $out->addModules( 'mediawiki.special.movePage' );
121 $newTitle = $this->newTitle
;
124 # Show the current title as a default
125 # when the form is first opened.
126 $newTitle = $this->oldTitle
;
127 } elseif ( !count( $err ) ) {
128 # If a title was supplied, probably from the move log revert
129 # link, check for validity. We can then show some diagnostic
130 # information and save a click.
131 $newerr = $this->oldTitle
->isValidMoveOperation( $newTitle );
132 if ( is_array( $newerr ) ) {
137 $user = $this->getUser();
139 if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'articleexists'
140 && $newTitle->quickUserCan( 'delete', $user )
142 $out->addWikiMsg( 'delete_and_move_text', $newTitle->getPrefixedText() );
143 $movepagebtn = $this->msg( 'delete_and_move' )->text();
144 $submitVar = 'wpDeleteAndMove';
148 <td class='mw-input'>" .
149 Xml
::checkLabel( $this->msg( 'delete_and_move_confirm' )->text(), 'wpConfirm', 'wpConfirm' ) .
154 if ( $this->oldTitle
->getNamespace() == NS_USER
&& !$this->oldTitle
->isSubpage() ) {
155 $out->wrapWikiMsg( "<div class=\"error mw-moveuserpage-warning\">\n$1\n</div>", 'moveuserpage-warning' );
157 $out->addWikiMsg( $wgFixDoubleRedirects ?
'movepagetext' :
158 'movepagetext-noredirectfixer' );
159 $movepagebtn = $this->msg( 'movepagebtn' )->text();
160 $submitVar = 'wpMove';
164 if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'file-exists-sharedrepo'
165 && $user->isAllowed( 'reupload-shared' )
167 $out->addWikiMsg( 'move-over-sharedrepo', $newTitle->getPrefixedText() );
168 $submitVar = 'wpMoveOverSharedFile';
172 $oldTalk = $this->oldTitle
->getTalkPage();
173 $oldTitleSubpages = $this->oldTitle
->hasSubpages();
174 $oldTitleTalkSubpages = $this->oldTitle
->getTalkPage()->hasSubpages();
176 $canMoveSubpage = ( $oldTitleSubpages ||
$oldTitleTalkSubpages ) &&
177 !count( $this->oldTitle
->getUserPermissionsErrors( 'move-subpages', $user ) );
179 # We also want to be able to move assoc. subpage talk-pages even if base page
180 # has no associated talk page, so || with $oldTitleTalkSubpages.
181 $considerTalk = !$this->oldTitle
->isTalkPage() &&
183 ||
( $oldTitleTalkSubpages && $canMoveSubpage ) );
185 $dbr = wfGetDB( DB_SLAVE
);
186 if ( $wgFixDoubleRedirects ) {
187 $hasRedirects = $dbr->selectField( 'redirect', '1',
189 'rd_namespace' => $this->oldTitle
->getNamespace(),
190 'rd_title' => $this->oldTitle
->getDBkey(),
193 $hasRedirects = false;
196 if ( $considerTalk ) {
197 $out->addWikiMsg( 'movepagetalktext' );
200 if ( count( $err ) ) {
201 $out->addHTML( "<div class='error'>\n" );
202 $action_desc = $this->msg( 'action-move' )->plain();
203 $out->addWikiMsg( 'permissionserrorstext-withaction', count( $err ), $action_desc );
205 if ( count( $err ) == 1 ) {
207 $errMsgName = array_shift( $errMsg );
208 if ( $errMsgName == 'hookaborted' ) {
209 $out->addHTML( "<p>{$errMsg[0]}</p>\n" );
211 $out->addWikiMsgArray( $errMsgName, $errMsg );
215 foreach ( $err as $errMsg ) {
216 if ( $errMsg[0] == 'hookaborted' ) {
217 $errStr[] = $errMsg[1];
219 $errMsgName = array_shift( $errMsg );
220 $errStr[] = $this->msg( $errMsgName, $errMsg )->parse();
224 $out->addHTML( '<ul><li>' . implode( "</li>\n<li>", $errStr ) . "</li></ul>\n" );
226 $out->addHTML( "</div>\n" );
229 if ( $this->oldTitle
->isProtected( 'move' ) ) {
230 # Is the title semi-protected?
231 if ( $this->oldTitle
->isSemiProtected( 'move' ) ) {
232 $noticeMsg = 'semiprotectedpagemovewarning';
233 $classes[] = 'mw-textarea-sprotected';
235 # Then it must be protected based on static groups (regular)
236 $noticeMsg = 'protectedpagemovewarning';
237 $classes[] = 'mw-textarea-protected';
239 $out->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" );
240 $out->addWikiMsg( $noticeMsg );
241 LogEventsList
::showLogExtract( $out, 'protect', $this->oldTitle
, '', array( 'lim' => 1 ) );
242 $out->addHTML( "</div>\n" );
245 // Byte limit (not string length limit) for wpReason and wpNewTitleMain
246 // is enforced in the mediawiki.special.movePage module
248 $immovableNamespaces = array();
250 foreach ( array_keys( $this->getLanguage()->getNamespaces() ) as $nsId ) {
251 if ( !MWNamespace
::isMovable( $nsId ) ) {
252 $immovableNamespaces[] = $nsId;
256 $handler = ContentHandler
::getForTitle( $this->oldTitle
);
259 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL( 'action=submit' ), 'id' => 'movepage' ) ) .
260 Xml
::openElement( 'fieldset' ) .
261 Xml
::element( 'legend', null, $this->msg( 'move-page-legend' )->text() ) .
262 Xml
::openElement( 'table', array( 'id' => 'mw-movepage-table' ) ) .
264 <td class='mw-label'>" .
265 $this->msg( 'movearticle' )->escaped() .
267 <td class='mw-input'>
268 <strong>{$oldTitleLink}</strong>
272 <td class='mw-label'>" .
273 Xml
::label( $this->msg( 'newtitle' )->text(), 'wpNewTitleMain' ) .
275 <td class='mw-input'>" .
276 Html
::namespaceSelector(
278 'selected' => $newTitle->getNamespace(),
279 'exclude' => $immovableNamespaces
281 array( 'name' => 'wpNewTitleNs', 'id' => 'wpNewTitleNs' )
283 Xml
::input( 'wpNewTitleMain', 60, $wgContLang->recodeForEdit( $newTitle->getText() ), array(
285 'id' => 'wpNewTitleMain',
288 Html
::hidden( 'wpOldTitle', $this->oldTitle
->getPrefixedText() ) .
292 <td class='mw-label'>" .
293 Xml
::label( $this->msg( 'movereason' )->text(), 'wpReason' ) .
295 <td class='mw-input'>" .
296 Xml
::input( 'wpReason', 60, $this->reason
, array(
305 if ( $considerTalk ) {
309 <td class='mw-input'>" .
310 Xml
::checkLabel( $this->msg( 'movetalk' )->text(), 'wpMovetalk', 'wpMovetalk', $this->moveTalk
) .
316 if ( $user->isAllowed( 'suppressredirect' ) && $handler->supportsRedirects() ) {
320 <td class='mw-input' >" .
321 Xml
::checkLabel( $this->msg( 'move-leave-redirect' )->text(), 'wpLeaveRedirect',
322 'wpLeaveRedirect', $this->leaveRedirect
) .
328 if ( $hasRedirects ) {
332 <td class='mw-input' >" .
333 Xml
::checkLabel( $this->msg( 'fix-double-redirects' )->text(), 'wpFixRedirects',
334 'wpFixRedirects', $this->fixRedirects
) .
340 if ( $canMoveSubpage ) {
344 <td class=\"mw-input\">" .
347 # Don't check the box if we only have talk subpages to
348 # move and we aren't moving the talk page.
349 $this->moveSubpages
&& ( $this->oldTitle
->hasSubpages() ||
$this->moveTalk
),
350 array( 'id' => 'wpMovesubpages' )
352 Xml
::tags( 'label', array( 'for' => 'wpMovesubpages' ),
354 ( $this->oldTitle
->hasSubpages()
356 : 'move-talk-subpages' )
357 )->numParams( $wgMaximumMovedPages )->params( $wgMaximumMovedPages )->parse()
364 $watchChecked = $user->isLoggedIn() && ( $this->watch ||
$user->getBoolOption( 'watchmoves' )
365 ||
$user->isWatched( $this->oldTitle
) );
366 # Don't allow watching if user is not logged in
367 if ( $user->isLoggedIn() ) {
371 <td class='mw-input'>" .
372 Xml
::checkLabel( $this->msg( 'move-watch' )->text(), 'wpWatch', 'watch', $watchChecked ) .
381 <td class='mw-submit'>" .
382 Xml
::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
385 Xml
::closeElement( 'table' ) .
386 Html
::hidden( 'wpEditToken', $user->getEditToken() ) .
387 Xml
::closeElement( 'fieldset' ) .
388 Xml
::closeElement( 'form' ) .
392 $this->showLogFragment( $this->oldTitle
);
393 $this->showSubpages( $this->oldTitle
);
397 function doSubmit() {
398 global $wgMaximumMovedPages, $wgFixDoubleRedirects;
400 $user = $this->getUser();
402 if ( $user->pingLimiter( 'move' ) ) {
403 throw new ThrottledError
;
406 $ot = $this->oldTitle
;
407 $nt = $this->newTitle
;
409 # don't allow moving to pages with # in
410 if ( !$nt ||
$nt->getFragment() != '' ) {
411 $this->showForm( array( array( 'badtitletext' ) ) );
415 # Show a warning if the target file exists on a shared repo
416 if ( $nt->getNamespace() == NS_FILE
417 && !( $this->moveOverShared
&& $user->isAllowed( 'reupload-shared' ) )
418 && !RepoGroup
::singleton()->getLocalRepo()->findFile( $nt )
419 && wfFindFile( $nt ) )
421 $this->showForm( array( array( 'file-exists-sharedrepo' ) ) );
426 # Delete to make way if requested
427 if ( $this->deleteAndMove
) {
428 $permErrors = $nt->getUserPermissionsErrors( 'delete', $user );
429 if ( count( $permErrors ) ) {
430 # Only show the first error
431 $this->showForm( $permErrors );
435 $reason = $this->msg( 'delete_and_move_reason', $ot )->inContentLanguage()->text();
437 // Delete an associated image if there is
438 if ( $nt->getNamespace() == NS_FILE
) {
439 $file = wfLocalFile( $nt );
440 if ( $file->exists() ) {
441 $file->delete( $reason, false );
445 $error = ''; // passed by ref
446 $page = WikiPage
::factory( $nt );
447 $deleteStatus = $page->doDeleteArticleReal( $reason, false, 0, true, $error, $user );
448 if ( !$deleteStatus->isGood() ) {
449 $this->showForm( $deleteStatus->getErrorsArray() );
454 $handler = ContentHandler
::getForTitle( $ot );
456 if ( !$handler->supportsRedirects() ) {
457 $createRedirect = false;
458 } elseif ( $user->isAllowed( 'suppressredirect' ) ) {
459 $createRedirect = $this->leaveRedirect
;
461 $createRedirect = true;
464 # Do the actual move.
465 $error = $ot->moveTo( $nt, true, $this->reason
, $createRedirect );
466 if ( $error !== true ) {
467 $this->showForm( $error );
471 if ( $wgFixDoubleRedirects && $this->fixRedirects
) {
472 DoubleRedirectJob
::fixRedirects( 'move', $ot, $nt );
475 $out = $this->getOutput();
476 $out->setPageTitle( $this->msg( 'pagemovedsub' ) );
478 $oldLink = Linker
::link(
482 array( 'redirect' => 'no' )
484 $newLink = Linker
::linkKnown( $nt );
485 $oldText = $ot->getPrefixedText();
486 $newText = $nt->getPrefixedText();
488 if ( $ot->exists() ) {
489 //NOTE: we assume that if the old title exists, it's because it was re-created as
490 // a redirect to the new title. This is not safe, but what we did before was
491 // even worse: we just determined whether a redirect should have been created,
492 // and reported that it was created if it should have, without any checks.
493 // Also note that isRedirect() is unreliable because of bug 37209.
494 $msgName = 'movepage-moved-redirect';
496 $msgName = 'movepage-moved-noredirect';
499 $out->addHTML( $this->msg( 'movepage-moved' )->rawParams( $oldLink,
500 $newLink )->params( $oldText, $newText )->parseAsBlock() );
501 $out->addWikiMsg( $msgName );
503 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this, &$ot, &$nt ) );
505 # Now we move extra pages we've been asked to move: subpages and talk
506 # pages. First, if the old page or the new page is a talk page, we
507 # can't move any talk pages: cancel that.
508 if ( $ot->isTalkPage() ||
$nt->isTalkPage() ) {
509 $this->moveTalk
= false;
512 if ( count( $ot->getUserPermissionsErrors( 'move-subpages', $user ) ) ) {
513 $this->moveSubpages
= false;
516 # Next make a list of id's. This might be marginally less efficient
517 # than a more direct method, but this is not a highly performance-cri-
518 # tical code path and readable code is more important here.
520 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
521 # 4 might get confused. If so, consider rewriting as a UNION.
523 # If the target namespace doesn't allow subpages, moving with subpages
524 # would mean that you couldn't move them back in one operation, which
526 # @todo FIXME: A specific error message should be given in this case.
528 // @todo FIXME: Use Title::moveSubpages() here
529 $dbr = wfGetDB( DB_MASTER
);
530 if ( $this->moveSubpages
&& (
531 MWNamespace
::hasSubpages( $nt->getNamespace() ) ||
(
533 MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() )
537 'page_title' . $dbr->buildLike( $ot->getDBkey() . '/', $dbr->anyString() )
538 . ' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
540 $conds['page_namespace'] = array();
541 if ( MWNamespace
::hasSubpages( $nt->getNamespace() ) ) {
542 $conds['page_namespace'][] = $ot->getNamespace();
544 if ( $this->moveTalk
&& MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() ) ) {
545 $conds['page_namespace'][] = $ot->getTalkPage()->getNamespace();
547 } elseif ( $this->moveTalk
) {
549 'page_namespace' => $ot->getTalkPage()->getNamespace(),
550 'page_title' => $ot->getDBkey()
557 $extraPages = array();
558 if ( !is_null( $conds ) ) {
559 $extraPages = TitleArray
::newFromResult(
560 $dbr->select( 'page',
561 array( 'page_id', 'page_namespace', 'page_title' ),
568 $extraOutput = array();
570 foreach ( $extraPages as $oldSubpage ) {
571 if ( $ot->equals( $oldSubpage ) ) {
572 # Already did this one.
576 $newPageName = preg_replace(
577 '#^' . preg_quote( $ot->getDBkey(), '#' ) . '#',
578 StringUtils
::escapeRegexReplacement( $nt->getDBkey() ), # bug 21234
579 $oldSubpage->getDBkey()
581 if ( $oldSubpage->isTalkPage() ) {
582 $newNs = $nt->getTalkPage()->getNamespace();
584 $newNs = $nt->getSubjectPage()->getNamespace();
586 # Bug 14385: we need makeTitleSafe because the new page names may
587 # be longer than 255 characters.
588 $newSubpage = Title
::makeTitleSafe( $newNs, $newPageName );
589 if ( !$newSubpage ) {
590 $oldLink = Linker
::linkKnown( $oldSubpage );
591 $extraOutput[] = $this->msg( 'movepage-page-unmoved' )->rawParams( $oldLink
592 )->params( Title
::makeName( $newNs, $newPageName ) )->escaped();
596 # This was copy-pasted from Renameuser, bleh.
597 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
598 $link = Linker
::linkKnown( $newSubpage );
599 $extraOutput[] = $this->msg( 'movepage-page-exists' )->rawParams( $link )->escaped();
601 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason
, $createRedirect );
602 if ( $success === true ) {
603 if ( $this->fixRedirects
) {
604 DoubleRedirectJob
::fixRedirects( 'move', $oldSubpage, $newSubpage );
606 $oldLink = Linker
::link(
610 array( 'redirect' => 'no' )
612 $newLink = Linker
::linkKnown( $newSubpage );
613 $extraOutput[] = $this->msg( 'movepage-page-moved' )->rawParams( $oldLink, $newLink )->escaped();
615 if ( $count >= $wgMaximumMovedPages ) {
616 $extraOutput[] = $this->msg( 'movepage-max-pages' )->numParams( $wgMaximumMovedPages )->escaped();
620 $oldLink = Linker
::linkKnown( $oldSubpage );
621 $newLink = Linker
::link( $newSubpage );
622 $extraOutput[] = $this->msg( 'movepage-page-unmoved' )->rawParams( $oldLink, $newLink )->escaped();
628 if ( $extraOutput !== array() ) {
629 $out->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
632 # Deal with watches (we don't watch subpages)
633 if ( $this->watch
&& $user->isLoggedIn() ) {
634 $user->addWatch( $ot );
635 $user->addWatch( $nt );
637 $user->removeWatch( $ot );
638 $user->removeWatch( $nt );
641 # Re-clear the file redirect cache, which may have been polluted by
642 # parsing in messages above. See CR r56745.
643 # @todo FIXME: Needs a more robust solution inside FileRepo.
644 if ( $ot->getNamespace() == NS_FILE
) {
645 RepoGroup
::singleton()->getLocalRepo()->invalidateImageRedirect( $ot );
649 function showLogFragment( $title ) {
650 $moveLogPage = new LogPage( 'move' );
651 $out = $this->getOutput();
652 $out->addHTML( Xml
::element( 'h2', null, $moveLogPage->getName()->text() ) );
653 LogEventsList
::showLogExtract( $out, 'move', $title );
656 function showSubpages( $title ) {
657 if ( !MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
661 $subpages = $title->getSubpages();
662 $count = $subpages instanceof TitleArray ?
$subpages->count() : 0;
664 $out = $this->getOutput();
665 $out->wrapWikiMsg( '== $1 ==', array( 'movesubpage', $count ) );
669 $out->addWikiMsg( 'movenosubpage' );
673 $out->addWikiMsg( 'movesubpagetext', $this->getLanguage()->formatNum( $count ) );
674 $out->addHTML( "<ul>\n" );
676 foreach ( $subpages as $subpage ) {
677 $link = Linker
::link( $subpage );
678 $out->addHTML( "<li>$link</li>\n" );
680 $out->addHTML( "</ul>\n" );
683 protected function getGroupName() {