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
{
37 /** @var string Text input */
46 protected $deleteAndMove;
49 protected $moveSubpages;
52 protected $fixRedirects;
55 protected $leaveRedirect;
58 protected $moveOverShared;
60 private $watch = false;
62 public function __construct() {
63 parent
::__construct( 'Movepage' );
66 public function execute( $par ) {
67 $this->checkReadOnly();
70 $this->outputHeader();
72 $request = $this->getRequest();
73 $target = !is_null( $par ) ?
$par : $request->getVal( 'target' );
75 // Yes, the use of getVal() and getText() is wanted, see bug 20365
77 $oldTitleText = $request->getVal( 'wpOldTitle', $target );
78 $this->oldTitle
= Title
::newFromText( $oldTitleText );
80 if ( is_null( $this->oldTitle
) ) {
81 throw new ErrorPageError( 'notargettitle', 'notargettext' );
83 if ( !$this->oldTitle
->exists() ) {
84 throw new ErrorPageError( 'nopagetitle', 'nopagetext' );
87 $newTitleTextMain = $request->getText( 'wpNewTitleMain' );
88 $newTitleTextNs = $request->getInt( 'wpNewTitleNs', $this->oldTitle
->getNamespace() );
89 // Backwards compatibility for forms submitting here from other sources
90 // which is more common than it should be..
91 $newTitleText_bc = $request->getText( 'wpNewTitle' );
92 $this->newTitle
= strlen( $newTitleText_bc ) > 0
93 ? Title
::newFromText( $newTitleText_bc )
94 : Title
::makeTitleSafe( $newTitleTextNs, $newTitleTextMain );
96 $user = $this->getUser();
99 $permErrors = $this->oldTitle
->getUserPermissionsErrors( 'move', $user );
100 if ( count( $permErrors ) ) {
101 // Auto-block user's IP if the account was "hard" blocked
102 $user->spreadAnyEditBlock();
103 throw new PermissionsError( 'move', $permErrors );
106 $def = !$request->wasPosted();
108 $this->reason
= $request->getText( 'wpReason' );
109 $this->moveTalk
= $request->getBool( 'wpMovetalk', $def );
110 $this->fixRedirects
= $request->getBool( 'wpFixRedirects', $def );
111 $this->leaveRedirect
= $request->getBool( 'wpLeaveRedirect', $def );
112 $this->moveSubpages
= $request->getBool( 'wpMovesubpages', false );
113 $this->deleteAndMove
= $request->getBool( 'wpDeleteAndMove' ) && $request->getBool( 'wpConfirm' );
114 $this->moveOverShared
= $request->getBool( 'wpMoveOverSharedFile', false );
115 $this->watch
= $request->getCheck( 'wpWatch' ) && $user->isLoggedIn();
117 if ( 'submit' == $request->getVal( 'action' ) && $request->wasPosted()
118 && $user->matchEditToken( $request->getVal( 'wpEditToken' ) )
122 $this->showForm( array() );
129 * @param array $err Error messages. Each item is an error message.
130 * It may either be a string message name or array message name and
131 * parameters, like the second argument to OutputPage::wrapWikiMsg().
133 function showForm( $err ) {
136 $this->getSkin()->setRelevantTitle( $this->oldTitle
);
138 $oldTitleLink = Linker
::link( $this->oldTitle
);
140 $out = $this->getOutput();
141 $out->setPageTitle( $this->msg( 'move-page', $this->oldTitle
->getPrefixedText() ) );
142 $out->addModules( 'mediawiki.special.movePage' );
144 $newTitle = $this->newTitle
;
147 # Show the current title as a default
148 # when the form is first opened.
149 $newTitle = $this->oldTitle
;
150 } elseif ( !count( $err ) ) {
151 # If a title was supplied, probably from the move log revert
152 # link, check for validity. We can then show some diagnostic
153 # information and save a click.
154 $newerr = $this->oldTitle
->isValidMoveOperation( $newTitle );
155 if ( is_array( $newerr ) ) {
160 $user = $this->getUser();
162 if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'articleexists'
163 && $newTitle->quickUserCan( 'delete', $user )
165 $out->addWikiMsg( 'delete_and_move_text', $newTitle->getPrefixedText() );
166 $movepagebtn = $this->msg( 'delete_and_move' )->text();
167 $submitVar = 'wpDeleteAndMove';
171 <td class='mw-input'>" .
173 $this->msg( 'delete_and_move_confirm' )->text(),
181 if ( $this->oldTitle
->getNamespace() == NS_USER
&& !$this->oldTitle
->isSubpage() ) {
183 "<div class=\"error mw-moveuserpage-warning\">\n$1\n</div>",
184 'moveuserpage-warning'
186 } elseif ( $this->oldTitle
->getNamespace() == NS_CATEGORY
) {
188 "<div class=\"error mw-movecategorypage-warning\">\n$1\n</div>",
189 'movecategorypage-warning'
193 $out->addWikiMsg( $this->getConfig()->get( 'FixDoubleRedirects' ) ?
195 'movepagetext-noredirectfixer'
197 $movepagebtn = $this->msg( 'movepagebtn' )->text();
198 $submitVar = 'wpMove';
202 if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'file-exists-sharedrepo'
203 && $user->isAllowed( 'reupload-shared' )
205 $out->addWikiMsg( 'move-over-sharedrepo', $newTitle->getPrefixedText() );
206 $submitVar = 'wpMoveOverSharedFile';
210 $oldTalk = $this->oldTitle
->getTalkPage();
211 $oldTitleSubpages = $this->oldTitle
->hasSubpages();
212 $oldTitleTalkSubpages = $this->oldTitle
->getTalkPage()->hasSubpages();
214 $canMoveSubpage = ( $oldTitleSubpages ||
$oldTitleTalkSubpages ) &&
215 !count( $this->oldTitle
->getUserPermissionsErrors( 'move-subpages', $user ) );
217 # We also want to be able to move assoc. subpage talk-pages even if base page
218 # has no associated talk page, so || with $oldTitleTalkSubpages.
219 $considerTalk = !$this->oldTitle
->isTalkPage() &&
221 ||
( $oldTitleTalkSubpages && $canMoveSubpage ) );
223 $dbr = wfGetDB( DB_SLAVE
);
224 if ( $this->getConfig()->get( 'FixDoubleRedirects' ) ) {
225 $hasRedirects = $dbr->selectField( 'redirect', '1',
227 'rd_namespace' => $this->oldTitle
->getNamespace(),
228 'rd_title' => $this->oldTitle
->getDBkey(),
231 $hasRedirects = false;
234 if ( $considerTalk ) {
235 $out->addWikiMsg( 'movepagetalktext' );
238 if ( count( $err ) ) {
239 $out->addHTML( "<div class='error'>\n" );
240 $action_desc = $this->msg( 'action-move' )->plain();
241 $out->addWikiMsg( 'permissionserrorstext-withaction', count( $err ), $action_desc );
243 if ( count( $err ) == 1 ) {
245 $errMsgName = array_shift( $errMsg );
247 if ( $errMsgName == 'hookaborted' ) {
248 $out->addHTML( "<p>{$errMsg[0]}</p>\n" );
250 $out->addWikiMsgArray( $errMsgName, $errMsg );
255 foreach ( $err as $errMsg ) {
256 if ( $errMsg[0] == 'hookaborted' ) {
257 $errStr[] = $errMsg[1];
259 $errMsgName = array_shift( $errMsg );
260 $errStr[] = $this->msg( $errMsgName, $errMsg )->parse();
264 $out->addHTML( '<ul><li>' . implode( "</li>\n<li>", $errStr ) . "</li></ul>\n" );
266 $out->addHTML( "</div>\n" );
269 if ( $this->oldTitle
->isProtected( 'move' ) ) {
270 # Is the title semi-protected?
271 if ( $this->oldTitle
->isSemiProtected( 'move' ) ) {
272 $noticeMsg = 'semiprotectedpagemovewarning';
273 $classes[] = 'mw-textarea-sprotected';
275 # Then it must be protected based on static groups (regular)
276 $noticeMsg = 'protectedpagemovewarning';
277 $classes[] = 'mw-textarea-protected';
279 $out->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" );
280 $out->addWikiMsg( $noticeMsg );
281 LogEventsList
::showLogExtract(
288 $out->addHTML( "</div>\n" );
291 // Byte limit (not string length limit) for wpReason and wpNewTitleMain
292 // is enforced in the mediawiki.special.movePage module
294 $immovableNamespaces = array();
296 foreach ( array_keys( $this->getLanguage()->getNamespaces() ) as $nsId ) {
297 if ( !MWNamespace
::isMovable( $nsId ) ) {
298 $immovableNamespaces[] = $nsId;
302 $handler = ContentHandler
::getForTitle( $this->oldTitle
);
309 'action' => $this->getPageTitle()->getLocalURL( 'action=submit' ),
313 Xml
::openElement( 'fieldset' ) .
314 Xml
::element( 'legend', null, $this->msg( 'move-page-legend' )->text() ) .
315 Xml
::openElement( 'table', array( 'id' => 'mw-movepage-table' ) ) .
317 <td class='mw-label'>" .
318 $this->msg( 'movearticle' )->escaped() .
320 <td class='mw-input'>
321 <strong>{$oldTitleLink}</strong>
325 <td class='mw-label'>" .
326 Xml
::label( $this->msg( 'newtitle' )->text(), 'wpNewTitleMain' ) .
328 <td class='mw-input'>" .
329 Html
::namespaceSelector(
331 'selected' => $newTitle->getNamespace(),
332 'exclude' => $immovableNamespaces
334 array( 'name' => 'wpNewTitleNs', 'id' => 'wpNewTitleNs' )
339 $wgContLang->recodeForEdit( $newTitle->getText() ),
342 'id' => 'wpNewTitleMain',
346 Html
::hidden( 'wpOldTitle', $this->oldTitle
->getPrefixedText() ) .
350 <td class='mw-label'>" .
351 Xml
::label( $this->msg( 'movereason' )->text(), 'wpReason' ) .
353 <td class='mw-input'>" .
354 Xml
::input( 'wpReason', 60, $this->reason
, array(
363 if ( $considerTalk ) {
367 <td class='mw-input'>" .
369 $this->msg( 'movetalk' )->text(),
379 if ( $user->isAllowed( 'suppressredirect' ) ) {
380 if ( $handler->supportsRedirects() ) {
381 $isChecked = $this->leaveRedirect
;
386 'disabled' => 'disabled'
392 <td class='mw-input' >" .
394 $this->msg( 'move-leave-redirect' )->text(),
405 if ( $hasRedirects ) {
409 <td class='mw-input' >" .
411 $this->msg( 'fix-double-redirects' )->text(),
421 if ( $canMoveSubpage ) {
422 $maximumMovedPages = $this->getConfig()->get( 'MaximumMovedPages' );
426 <td class=\"mw-input\">" .
429 # Don't check the box if we only have talk subpages to
430 # move and we aren't moving the talk page.
431 $this->moveSubpages
&& ( $this->oldTitle
->hasSubpages() ||
$this->moveTalk
),
432 array( 'id' => 'wpMovesubpages' )
434 Xml
::tags( 'label', array( 'for' => 'wpMovesubpages' ),
436 ( $this->oldTitle
->hasSubpages()
438 : 'move-talk-subpages' )
439 )->numParams( $maximumMovedPages )->params( $maximumMovedPages )->parse()
446 $watchChecked = $user->isLoggedIn() && ( $this->watch ||
$user->getBoolOption( 'watchmoves' )
447 ||
$user->isWatched( $this->oldTitle
) );
448 # Don't allow watching if user is not logged in
449 if ( $user->isLoggedIn() ) {
453 <td class='mw-input'>" .
455 $this->msg( 'move-watch' )->text(),
468 <td class='mw-submit'>" .
469 Xml
::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
472 Xml
::closeElement( 'table' ) .
473 Html
::hidden( 'wpEditToken', $user->getEditToken() ) .
474 Xml
::closeElement( 'fieldset' ) .
475 Xml
::closeElement( 'form' ) .
479 $this->showLogFragment( $this->oldTitle
);
480 $this->showSubpages( $this->oldTitle
);
483 function doSubmit() {
484 $user = $this->getUser();
486 if ( $user->pingLimiter( 'move' ) ) {
487 throw new ThrottledError
;
490 $ot = $this->oldTitle
;
491 $nt = $this->newTitle
;
493 # don't allow moving to pages with # in
494 if ( !$nt ||
$nt->hasFragment() ) {
495 $this->showForm( array( array( 'badtitletext' ) ) );
500 # Show a warning if the target file exists on a shared repo
501 if ( $nt->getNamespace() == NS_FILE
502 && !( $this->moveOverShared
&& $user->isAllowed( 'reupload-shared' ) )
503 && !RepoGroup
::singleton()->getLocalRepo()->findFile( $nt )
506 $this->showForm( array( array( 'file-exists-sharedrepo' ) ) );
511 # Delete to make way if requested
512 if ( $this->deleteAndMove
) {
513 $permErrors = $nt->getUserPermissionsErrors( 'delete', $user );
514 if ( count( $permErrors ) ) {
515 # Only show the first error
516 $this->showForm( $permErrors );
521 $reason = $this->msg( 'delete_and_move_reason', $ot )->inContentLanguage()->text();
523 // Delete an associated image if there is
524 if ( $nt->getNamespace() == NS_FILE
) {
525 $file = wfLocalFile( $nt );
526 if ( $file->exists() ) {
527 $file->delete( $reason, false );
531 $error = ''; // passed by ref
532 $page = WikiPage
::factory( $nt );
533 $deleteStatus = $page->doDeleteArticleReal( $reason, false, 0, true, $error, $user );
534 if ( !$deleteStatus->isGood() ) {
535 $this->showForm( $deleteStatus->getErrorsArray() );
541 $handler = ContentHandler
::getForTitle( $ot );
543 if ( !$handler->supportsRedirects() ) {
544 $createRedirect = false;
545 } elseif ( $user->isAllowed( 'suppressredirect' ) ) {
546 $createRedirect = $this->leaveRedirect
;
548 $createRedirect = true;
551 # Do the actual move.
552 $mp = new MovePage( $ot, $nt );
553 $valid = $mp->isValidMove();
554 if ( !$valid->isOK() ) {
555 $this->showForm( $valid->getErrorsArray() );
559 $permStatus = $mp->checkPermissions( $user, $this->reason
);
560 if ( !$permStatus->isOK() ) {
561 $this->showForm( $permStatus->getErrorsArray() );
565 $status = $mp->move( $user, $this->reason
, $createRedirect );
566 if ( !$status->isOK() ) {
567 $this->showForm( $status->getErrorsArray() );
571 if ( $this->getConfig()->get( 'FixDoubleRedirects' ) && $this->fixRedirects
) {
572 DoubleRedirectJob
::fixRedirects( 'move', $ot, $nt );
575 $out = $this->getOutput();
576 $out->setPageTitle( $this->msg( 'pagemovedsub' ) );
578 $oldLink = Linker
::link(
581 array( 'id' => 'movepage-oldlink' ),
582 array( 'redirect' => 'no' )
584 $newLink = Linker
::linkKnown(
587 array( 'id' => 'movepage-newlink' )
589 $oldText = $ot->getPrefixedText();
590 $newText = $nt->getPrefixedText();
592 if ( $ot->exists() ) {
593 //NOTE: we assume that if the old title exists, it's because it was re-created as
594 // a redirect to the new title. This is not safe, but what we did before was
595 // even worse: we just determined whether a redirect should have been created,
596 // and reported that it was created if it should have, without any checks.
597 // Also note that isRedirect() is unreliable because of bug 37209.
598 $msgName = 'movepage-moved-redirect';
600 $msgName = 'movepage-moved-noredirect';
603 $out->addHTML( $this->msg( 'movepage-moved' )->rawParams( $oldLink,
604 $newLink )->params( $oldText, $newText )->parseAsBlock() );
605 $out->addWikiMsg( $msgName );
607 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this, &$ot, &$nt ) );
609 # Now we move extra pages we've been asked to move: subpages and talk
610 # pages. First, if the old page or the new page is a talk page, we
611 # can't move any talk pages: cancel that.
612 if ( $ot->isTalkPage() ||
$nt->isTalkPage() ) {
613 $this->moveTalk
= false;
616 if ( count( $ot->getUserPermissionsErrors( 'move-subpages', $user ) ) ) {
617 $this->moveSubpages
= false;
620 # Next make a list of id's. This might be marginally less efficient
621 # than a more direct method, but this is not a highly performance-cri-
622 # tical code path and readable code is more important here.
624 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
625 # 4 might get confused. If so, consider rewriting as a UNION.
627 # If the target namespace doesn't allow subpages, moving with subpages
628 # would mean that you couldn't move them back in one operation, which
630 # @todo FIXME: A specific error message should be given in this case.
632 // @todo FIXME: Use Title::moveSubpages() here
633 $dbr = wfGetDB( DB_MASTER
);
634 if ( $this->moveSubpages
&& (
635 MWNamespace
::hasSubpages( $nt->getNamespace() ) ||
(
637 && MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() )
641 'page_title' . $dbr->buildLike( $ot->getDBkey() . '/', $dbr->anyString() )
642 . ' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
644 $conds['page_namespace'] = array();
645 if ( MWNamespace
::hasSubpages( $nt->getNamespace() ) ) {
646 $conds['page_namespace'][] = $ot->getNamespace();
648 if ( $this->moveTalk
&&
649 MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() )
651 $conds['page_namespace'][] = $ot->getTalkPage()->getNamespace();
653 } elseif ( $this->moveTalk
) {
655 'page_namespace' => $ot->getTalkPage()->getNamespace(),
656 'page_title' => $ot->getDBkey()
663 $extraPages = array();
664 if ( !is_null( $conds ) ) {
665 $extraPages = TitleArray
::newFromResult(
666 $dbr->select( 'page',
667 array( 'page_id', 'page_namespace', 'page_title' ),
674 $extraOutput = array();
676 foreach ( $extraPages as $oldSubpage ) {
677 if ( $ot->equals( $oldSubpage ) ||
$nt->equals( $oldSubpage ) ) {
678 # Already did this one.
682 $newPageName = preg_replace(
683 '#^' . preg_quote( $ot->getDBkey(), '#' ) . '#',
684 StringUtils
::escapeRegexReplacement( $nt->getDBkey() ), # bug 21234
685 $oldSubpage->getDBkey()
688 if ( $oldSubpage->isTalkPage() ) {
689 $newNs = $nt->getTalkPage()->getNamespace();
691 $newNs = $nt->getSubjectPage()->getNamespace();
694 # Bug 14385: we need makeTitleSafe because the new page names may
695 # be longer than 255 characters.
696 $newSubpage = Title
::makeTitleSafe( $newNs, $newPageName );
697 if ( !$newSubpage ) {
698 $oldLink = Linker
::linkKnown( $oldSubpage );
699 $extraOutput[] = $this->msg( 'movepage-page-unmoved' )->rawParams( $oldLink )
700 ->params( Title
::makeName( $newNs, $newPageName ) )->escaped();
704 # This was copy-pasted from Renameuser, bleh.
705 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
706 $link = Linker
::linkKnown( $newSubpage );
707 $extraOutput[] = $this->msg( 'movepage-page-exists' )->rawParams( $link )->escaped();
709 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason
, $createRedirect );
711 if ( $success === true ) {
712 if ( $this->fixRedirects
) {
713 DoubleRedirectJob
::fixRedirects( 'move', $oldSubpage, $newSubpage );
715 $oldLink = Linker
::link(
719 array( 'redirect' => 'no' )
722 $newLink = Linker
::linkKnown( $newSubpage );
723 $extraOutput[] = $this->msg( 'movepage-page-moved' )
724 ->rawParams( $oldLink, $newLink )->escaped();
727 $maximumMovedPages = $this->getConfig()->get( 'MaximumMovedPages' );
728 if ( $count >= $maximumMovedPages ) {
729 $extraOutput[] = $this->msg( 'movepage-max-pages' )
730 ->numParams( $maximumMovedPages )->escaped();
734 $oldLink = Linker
::linkKnown( $oldSubpage );
735 $newLink = Linker
::link( $newSubpage );
736 $extraOutput[] = $this->msg( 'movepage-page-unmoved' )
737 ->rawParams( $oldLink, $newLink )->escaped();
742 if ( $extraOutput !== array() ) {
743 $out->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
746 # Deal with watches (we don't watch subpages)
747 WatchAction
::doWatchOrUnwatch( $this->watch
, $ot, $user );
748 WatchAction
::doWatchOrUnwatch( $this->watch
, $nt, $user );
751 function showLogFragment( $title ) {
752 $moveLogPage = new LogPage( 'move' );
753 $out = $this->getOutput();
754 $out->addHTML( Xml
::element( 'h2', null, $moveLogPage->getName()->text() ) );
755 LogEventsList
::showLogExtract( $out, 'move', $title );
758 function showSubpages( $title ) {
759 if ( !MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
763 $subpages = $title->getSubpages();
764 $count = $subpages instanceof TitleArray ?
$subpages->count() : 0;
766 $out = $this->getOutput();
767 $out->wrapWikiMsg( '== $1 ==', array( 'movesubpage', $count ) );
771 $out->addWikiMsg( 'movenosubpage' );
776 $out->addWikiMsg( 'movesubpagetext', $this->getLanguage()->formatNum( $count ) );
777 $out->addHTML( "<ul>\n" );
779 foreach ( $subpages as $subpage ) {
780 $link = Linker
::link( $subpage );
781 $out->addHTML( "<li>$link</li>\n" );
783 $out->addHTML( "</ul>\n" );
786 protected function getGroupName() {