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
{
31 protected $oldTitle = null;
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->useTransactionalTimeLimit();
69 $this->checkReadOnly();
72 $this->outputHeader();
74 $request = $this->getRequest();
75 $target = !is_null( $par ) ?
$par : $request->getVal( 'target' );
77 // Yes, the use of getVal() and getText() is wanted, see bug 20365
79 $oldTitleText = $request->getVal( 'wpOldTitle', $target );
80 if ( is_string( $oldTitleText ) ) {
81 $this->oldTitle
= Title
::newFromText( $oldTitleText );
84 if ( $this->oldTitle
=== null ) {
85 // Either oldTitle wasn't passed, or newFromText returned null
86 throw new ErrorPageError( 'notargettitle', 'notargettext' );
88 if ( !$this->oldTitle
->exists() ) {
89 throw new ErrorPageError( 'nopagetitle', 'nopagetext' );
92 $newTitleTextMain = $request->getText( 'wpNewTitleMain' );
93 $newTitleTextNs = $request->getInt( 'wpNewTitleNs', $this->oldTitle
->getNamespace() );
94 // Backwards compatibility for forms submitting here from other sources
95 // which is more common than it should be..
96 $newTitleText_bc = $request->getText( 'wpNewTitle' );
97 $this->newTitle
= strlen( $newTitleText_bc ) > 0
98 ? Title
::newFromText( $newTitleText_bc )
99 : Title
::makeTitleSafe( $newTitleTextNs, $newTitleTextMain );
101 $user = $this->getUser();
104 $permErrors = $this->oldTitle
->getUserPermissionsErrors( 'move', $user );
105 if ( count( $permErrors ) ) {
106 // Auto-block user's IP if the account was "hard" blocked
107 $user->spreadAnyEditBlock();
108 throw new PermissionsError( 'move', $permErrors );
111 $def = !$request->wasPosted();
113 $this->reason
= $request->getText( 'wpReason' );
114 $this->moveTalk
= $request->getBool( 'wpMovetalk', $def );
115 $this->fixRedirects
= $request->getBool( 'wpFixRedirects', $def );
116 $this->leaveRedirect
= $request->getBool( 'wpLeaveRedirect', $def );
117 $this->moveSubpages
= $request->getBool( 'wpMovesubpages', false );
118 $this->deleteAndMove
= $request->getBool( 'wpDeleteAndMove' ) && $request->getBool( 'wpConfirm' );
119 $this->moveOverShared
= $request->getBool( 'wpMoveOverSharedFile', false );
120 $this->watch
= $request->getCheck( 'wpWatch' ) && $user->isLoggedIn();
122 if ( 'submit' == $request->getVal( 'action' ) && $request->wasPosted()
123 && $user->matchEditToken( $request->getVal( 'wpEditToken' ) )
127 $this->showForm( array() );
134 * @param array $err Error messages. Each item is an error message.
135 * It may either be a string message name or array message name and
136 * parameters, like the second argument to OutputPage::wrapWikiMsg().
138 function showForm( $err ) {
141 $this->getSkin()->setRelevantTitle( $this->oldTitle
);
143 $oldTitleLink = Linker
::link( $this->oldTitle
);
145 $out = $this->getOutput();
146 $out->setPageTitle( $this->msg( 'move-page', $this->oldTitle
->getPrefixedText() ) );
147 $out->addModules( 'mediawiki.special.movePage' );
148 $this->addHelpLink( 'Help:Moving a page' );
150 $newTitle = $this->newTitle
;
153 # Show the current title as a default
154 # when the form is first opened.
155 $newTitle = $this->oldTitle
;
156 } elseif ( !count( $err ) ) {
157 # If a title was supplied, probably from the move log revert
158 # link, check for validity. We can then show some diagnostic
159 # information and save a click.
160 $newerr = $this->oldTitle
->isValidMoveOperation( $newTitle );
161 if ( is_array( $newerr ) ) {
166 $user = $this->getUser();
168 if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'articleexists'
169 && $newTitle->quickUserCan( 'delete', $user )
171 $out->addWikiMsg( 'delete_and_move_text', $newTitle->getPrefixedText() );
172 $movepagebtn = $this->msg( 'delete_and_move' )->text();
173 $submitVar = 'wpDeleteAndMove';
177 if ( $this->oldTitle
->getNamespace() == NS_USER
&& !$this->oldTitle
->isSubpage() ) {
179 "<div class=\"error mw-moveuserpage-warning\">\n$1\n</div>",
180 'moveuserpage-warning'
182 } elseif ( $this->oldTitle
->getNamespace() == NS_CATEGORY
) {
184 "<div class=\"error mw-movecategorypage-warning\">\n$1\n</div>",
185 'movecategorypage-warning'
189 $out->addWikiMsg( $this->getConfig()->get( 'FixDoubleRedirects' ) ?
191 'movepagetext-noredirectfixer'
193 $movepagebtn = $this->msg( 'movepagebtn' )->text();
194 $submitVar = 'wpMove';
198 if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'file-exists-sharedrepo'
199 && $user->isAllowed( 'reupload-shared' )
201 $out->addWikiMsg( 'move-over-sharedrepo', $newTitle->getPrefixedText() );
202 $submitVar = 'wpMoveOverSharedFile';
206 $oldTalk = $this->oldTitle
->getTalkPage();
207 $oldTitleSubpages = $this->oldTitle
->hasSubpages();
208 $oldTitleTalkSubpages = $this->oldTitle
->getTalkPage()->hasSubpages();
210 $canMoveSubpage = ( $oldTitleSubpages ||
$oldTitleTalkSubpages ) &&
211 !count( $this->oldTitle
->getUserPermissionsErrors( 'move-subpages', $user ) );
213 # We also want to be able to move assoc. subpage talk-pages even if base page
214 # has no associated talk page, so || with $oldTitleTalkSubpages.
215 $considerTalk = !$this->oldTitle
->isTalkPage() &&
217 ||
( $oldTitleTalkSubpages && $canMoveSubpage ) );
219 $dbr = wfGetDB( DB_SLAVE
);
220 if ( $this->getConfig()->get( 'FixDoubleRedirects' ) ) {
221 $hasRedirects = $dbr->selectField( 'redirect', '1',
223 'rd_namespace' => $this->oldTitle
->getNamespace(),
224 'rd_title' => $this->oldTitle
->getDBkey(),
227 $hasRedirects = false;
230 if ( $considerTalk ) {
231 $out->addWikiMsg( 'movepagetalktext' );
234 if ( count( $err ) ) {
235 $out->addHTML( "<div class='error'>\n" );
236 $action_desc = $this->msg( 'action-move' )->plain();
237 $out->addWikiMsg( 'permissionserrorstext-withaction', count( $err ), $action_desc );
239 if ( count( $err ) == 1 ) {
241 $errMsgName = array_shift( $errMsg );
243 if ( $errMsgName == 'hookaborted' ) {
244 $out->addHTML( "<p>{$errMsg[0]}</p>\n" );
246 $out->addWikiMsgArray( $errMsgName, $errMsg );
251 foreach ( $err as $errMsg ) {
252 if ( $errMsg[0] == 'hookaborted' ) {
253 $errStr[] = $errMsg[1];
255 $errMsgName = array_shift( $errMsg );
256 $errStr[] = $this->msg( $errMsgName, $errMsg )->parse();
260 $out->addHTML( '<ul><li>' . implode( "</li>\n<li>", $errStr ) . "</li></ul>\n" );
262 $out->addHTML( "</div>\n" );
265 if ( $this->oldTitle
->isProtected( 'move' ) ) {
266 # Is the title semi-protected?
267 if ( $this->oldTitle
->isSemiProtected( 'move' ) ) {
268 $noticeMsg = 'semiprotectedpagemovewarning';
269 $classes[] = 'mw-textarea-sprotected';
271 # Then it must be protected based on static groups (regular)
272 $noticeMsg = 'protectedpagemovewarning';
273 $classes[] = 'mw-textarea-protected';
275 $out->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" );
276 $out->addWikiMsg( $noticeMsg );
277 LogEventsList
::showLogExtract(
284 $out->addHTML( "</div>\n" );
287 // Byte limit (not string length limit) for wpReason and wpNewTitleMain
288 // is enforced in the mediawiki.special.movePage module
290 $immovableNamespaces = array();
292 foreach ( array_keys( $this->getLanguage()->getNamespaces() ) as $nsId ) {
293 if ( !MWNamespace
::isMovable( $nsId ) ) {
294 $immovableNamespaces[] = $nsId;
298 $handler = ContentHandler
::getForTitle( $this->oldTitle
);
305 'action' => $this->getPageTitle()->getLocalURL( 'action=submit' ),
309 Xml
::openElement( 'fieldset' ) .
310 Xml
::element( 'legend', null, $this->msg( 'move-page-legend' )->text() ) .
311 Xml
::openElement( 'table', array( 'id' => 'mw-movepage-table' ) )
316 <td class='mw-label'>" .
317 $this->msg( 'movearticle' )->escaped() .
319 <td class='mw-input'>
320 <strong>{$oldTitleLink}</strong>
324 <td class='mw-label'>" .
325 Xml
::label( $this->msg( 'newtitle' )->text(), 'wpNewTitleMain' ) .
327 <td class='mw-input'>" .
328 Html
::namespaceSelector(
330 'selected' => $newTitle->getNamespace(),
331 'exclude' => $immovableNamespaces
333 array( 'name' => 'wpNewTitleNs', 'id' => 'wpNewTitleNs' )
338 $wgContLang->recodeForEdit( $newTitle->getText() ),
341 'id' => 'wpNewTitleMain',
345 Html
::hidden( 'wpOldTitle', $this->oldTitle
->getPrefixedText() ) .
349 <td class='mw-label'>" .
350 Xml
::label( $this->msg( 'movereason' )->text(), 'wpReason' ) .
352 <td class='mw-input'>" .
353 Xml
::input( 'wpReason', 60, $this->reason
, array(
362 if ( $considerTalk ) {
366 <td class='mw-input'>" .
368 $this->msg( 'movetalk' )->text(),
378 if ( $user->isAllowed( 'suppressredirect' ) ) {
379 if ( $handler->supportsRedirects() ) {
380 $isChecked = $this->leaveRedirect
;
385 'disabled' => 'disabled'
391 <td class='mw-input'>" .
393 $this->msg( 'move-leave-redirect' )->text(),
404 if ( $hasRedirects ) {
408 <td class='mw-input'>" .
410 $this->msg( 'fix-double-redirects' )->text(),
420 if ( $canMoveSubpage ) {
421 $maximumMovedPages = $this->getConfig()->get( 'MaximumMovedPages' );
425 <td class='mw-input'>" .
428 # Don't check the box if we only have talk subpages to
429 # move and we aren't moving the talk page.
430 $this->moveSubpages
&& ( $this->oldTitle
->hasSubpages() ||
$this->moveTalk
),
431 array( 'id' => 'wpMovesubpages' )
435 array( 'for' => 'wpMovesubpages' ),
437 ( $this->oldTitle
->hasSubpages()
439 : 'move-talk-subpages' )
440 )->numParams( $maximumMovedPages )->params( $maximumMovedPages )->parse()
447 $watchChecked = $user->isLoggedIn() && ( $this->watch ||
$user->getBoolOption( 'watchmoves' )
448 ||
$user->isWatched( $this->oldTitle
) );
449 # Don't allow watching if user is not logged in
450 if ( $user->isLoggedIn() ) {
454 <td class='mw-input'>" .
456 $this->msg( 'move-watch' )->text(),
470 <td class='mw-input'>" .
472 $this->msg( 'delete_and_move_confirm' )->text(),
484 <td class='mw-submit'>" .
485 Xml
::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
491 Xml
::closeElement( 'table' ) .
492 Html
::hidden( 'wpEditToken', $user->getEditToken() ) .
493 Xml
::closeElement( 'fieldset' ) .
494 Xml
::closeElement( 'form' ) .
498 $this->showLogFragment( $this->oldTitle
);
499 $this->showSubpages( $this->oldTitle
);
502 function doSubmit() {
503 $user = $this->getUser();
505 if ( $user->pingLimiter( 'move' ) ) {
506 throw new ThrottledError
;
509 $ot = $this->oldTitle
;
510 $nt = $this->newTitle
;
512 # don't allow moving to pages with # in
513 if ( !$nt ||
$nt->hasFragment() ) {
514 $this->showForm( array( array( 'badtitletext' ) ) );
519 # Show a warning if the target file exists on a shared repo
520 if ( $nt->getNamespace() == NS_FILE
521 && !( $this->moveOverShared
&& $user->isAllowed( 'reupload-shared' ) )
522 && !RepoGroup
::singleton()->getLocalRepo()->findFile( $nt )
525 $this->showForm( array( array( 'file-exists-sharedrepo' ) ) );
530 # Delete to make way if requested
531 if ( $this->deleteAndMove
) {
532 $permErrors = $nt->getUserPermissionsErrors( 'delete', $user );
533 if ( count( $permErrors ) ) {
534 # Only show the first error
535 $this->showForm( $permErrors );
540 $reason = $this->msg( 'delete_and_move_reason', $ot )->inContentLanguage()->text();
542 // Delete an associated image if there is
543 if ( $nt->getNamespace() == NS_FILE
) {
544 $file = wfLocalFile( $nt );
545 $file->load( File
::READ_LATEST
);
546 if ( $file->exists() ) {
547 $file->delete( $reason, false, $user );
551 $error = ''; // passed by ref
552 $page = WikiPage
::factory( $nt );
553 $deleteStatus = $page->doDeleteArticleReal( $reason, false, 0, true, $error, $user );
554 if ( !$deleteStatus->isGood() ) {
555 $this->showForm( $deleteStatus->getErrorsArray() );
561 $handler = ContentHandler
::getForTitle( $ot );
563 if ( !$handler->supportsRedirects() ) {
564 $createRedirect = false;
565 } elseif ( $user->isAllowed( 'suppressredirect' ) ) {
566 $createRedirect = $this->leaveRedirect
;
568 $createRedirect = true;
571 # Do the actual move.
572 $mp = new MovePage( $ot, $nt );
573 $valid = $mp->isValidMove();
574 if ( !$valid->isOK() ) {
575 $this->showForm( $valid->getErrorsArray() );
579 $permStatus = $mp->checkPermissions( $user, $this->reason
);
580 if ( !$permStatus->isOK() ) {
581 $this->showForm( $permStatus->getErrorsArray() );
585 $status = $mp->move( $user, $this->reason
, $createRedirect );
586 if ( !$status->isOK() ) {
587 $this->showForm( $status->getErrorsArray() );
591 if ( $this->getConfig()->get( 'FixDoubleRedirects' ) && $this->fixRedirects
) {
592 DoubleRedirectJob
::fixRedirects( 'move', $ot, $nt );
595 $out = $this->getOutput();
596 $out->setPageTitle( $this->msg( 'pagemovedsub' ) );
598 $oldLink = Linker
::link(
601 array( 'id' => 'movepage-oldlink' ),
602 array( 'redirect' => 'no' )
604 $newLink = Linker
::linkKnown(
607 array( 'id' => 'movepage-newlink' )
609 $oldText = $ot->getPrefixedText();
610 $newText = $nt->getPrefixedText();
612 if ( $ot->exists() ) {
613 //NOTE: we assume that if the old title exists, it's because it was re-created as
614 // a redirect to the new title. This is not safe, but what we did before was
615 // even worse: we just determined whether a redirect should have been created,
616 // and reported that it was created if it should have, without any checks.
617 // Also note that isRedirect() is unreliable because of bug 37209.
618 $msgName = 'movepage-moved-redirect';
620 $msgName = 'movepage-moved-noredirect';
623 $out->addHTML( $this->msg( 'movepage-moved' )->rawParams( $oldLink,
624 $newLink )->params( $oldText, $newText )->parseAsBlock() );
625 $out->addWikiMsg( $msgName );
627 Hooks
::run( 'SpecialMovepageAfterMove', array( &$this, &$ot, &$nt ) );
629 # Now we move extra pages we've been asked to move: subpages and talk
630 # pages. First, if the old page or the new page is a talk page, we
631 # can't move any talk pages: cancel that.
632 if ( $ot->isTalkPage() ||
$nt->isTalkPage() ) {
633 $this->moveTalk
= false;
636 if ( count( $ot->getUserPermissionsErrors( 'move-subpages', $user ) ) ) {
637 $this->moveSubpages
= false;
640 # Next make a list of id's. This might be marginally less efficient
641 # than a more direct method, but this is not a highly performance-cri-
642 # tical code path and readable code is more important here.
644 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
645 # 4 might get confused. If so, consider rewriting as a UNION.
647 # If the target namespace doesn't allow subpages, moving with subpages
648 # would mean that you couldn't move them back in one operation, which
650 # @todo FIXME: A specific error message should be given in this case.
652 // @todo FIXME: Use Title::moveSubpages() here
653 $dbr = wfGetDB( DB_MASTER
);
654 if ( $this->moveSubpages
&& (
655 MWNamespace
::hasSubpages( $nt->getNamespace() ) ||
(
657 && MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() )
661 'page_title' . $dbr->buildLike( $ot->getDBkey() . '/', $dbr->anyString() )
662 . ' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
664 $conds['page_namespace'] = array();
665 if ( MWNamespace
::hasSubpages( $nt->getNamespace() ) ) {
666 $conds['page_namespace'][] = $ot->getNamespace();
668 if ( $this->moveTalk
&&
669 MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() )
671 $conds['page_namespace'][] = $ot->getTalkPage()->getNamespace();
673 } elseif ( $this->moveTalk
) {
675 'page_namespace' => $ot->getTalkPage()->getNamespace(),
676 'page_title' => $ot->getDBkey()
683 $extraPages = array();
684 if ( !is_null( $conds ) ) {
685 $extraPages = TitleArray
::newFromResult(
686 $dbr->select( 'page',
687 array( 'page_id', 'page_namespace', 'page_title' ),
694 $extraOutput = array();
696 foreach ( $extraPages as $oldSubpage ) {
697 if ( $ot->equals( $oldSubpage ) ||
$nt->equals( $oldSubpage ) ) {
698 # Already did this one.
702 $newPageName = preg_replace(
703 '#^' . preg_quote( $ot->getDBkey(), '#' ) . '#',
704 StringUtils
::escapeRegexReplacement( $nt->getDBkey() ), # bug 21234
705 $oldSubpage->getDBkey()
708 if ( $oldSubpage->isSubpage() && ( $ot->isTalkPage() xor $nt->isTalkPage() ) ) {
709 // Moving a subpage from a subject namespace to a talk namespace or vice-versa
710 $newNs = $nt->getNamespace();
711 } elseif ( $oldSubpage->isTalkPage() ) {
712 $newNs = $nt->getTalkPage()->getNamespace();
714 $newNs = $nt->getSubjectPage()->getNamespace();
717 # Bug 14385: we need makeTitleSafe because the new page names may
718 # be longer than 255 characters.
719 $newSubpage = Title
::makeTitleSafe( $newNs, $newPageName );
720 if ( !$newSubpage ) {
721 $oldLink = Linker
::linkKnown( $oldSubpage );
722 $extraOutput[] = $this->msg( 'movepage-page-unmoved' )->rawParams( $oldLink )
723 ->params( Title
::makeName( $newNs, $newPageName ) )->escaped();
727 # This was copy-pasted from Renameuser, bleh.
728 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
729 $link = Linker
::linkKnown( $newSubpage );
730 $extraOutput[] = $this->msg( 'movepage-page-exists' )->rawParams( $link )->escaped();
732 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason
, $createRedirect );
734 if ( $success === true ) {
735 if ( $this->fixRedirects
) {
736 DoubleRedirectJob
::fixRedirects( 'move', $oldSubpage, $newSubpage );
738 $oldLink = Linker
::link(
742 array( 'redirect' => 'no' )
745 $newLink = Linker
::linkKnown( $newSubpage );
746 $extraOutput[] = $this->msg( 'movepage-page-moved' )
747 ->rawParams( $oldLink, $newLink )->escaped();
750 $maximumMovedPages = $this->getConfig()->get( 'MaximumMovedPages' );
751 if ( $count >= $maximumMovedPages ) {
752 $extraOutput[] = $this->msg( 'movepage-max-pages' )
753 ->numParams( $maximumMovedPages )->escaped();
757 $oldLink = Linker
::linkKnown( $oldSubpage );
758 $newLink = Linker
::link( $newSubpage );
759 $extraOutput[] = $this->msg( 'movepage-page-unmoved' )
760 ->rawParams( $oldLink, $newLink )->escaped();
765 if ( $extraOutput !== array() ) {
766 $out->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
769 # Deal with watches (we don't watch subpages)
770 WatchAction
::doWatchOrUnwatch( $this->watch
, $ot, $user );
771 WatchAction
::doWatchOrUnwatch( $this->watch
, $nt, $user );
774 function showLogFragment( $title ) {
775 $moveLogPage = new LogPage( 'move' );
776 $out = $this->getOutput();
777 $out->addHTML( Xml
::element( 'h2', null, $moveLogPage->getName()->text() ) );
778 LogEventsList
::showLogExtract( $out, 'move', $title );
781 function showSubpages( $title ) {
782 if ( !MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
786 $subpages = $title->getSubpages();
787 $count = $subpages instanceof TitleArray ?
$subpages->count() : 0;
789 $out = $this->getOutput();
790 $out->wrapWikiMsg( '== $1 ==', array( 'movesubpage', $count ) );
794 $out->addWikiMsg( 'movenosubpage' );
799 $out->addWikiMsg( 'movesubpagetext', $this->getLanguage()->formatNum( $count ) );
800 $out->addHTML( "<ul>\n" );
802 foreach ( $subpages as $subpage ) {
803 $link = Linker
::link( $subpage );
804 $out->addHTML( "<li>$link</li>\n" );
806 $out->addHTML( "</ul>\n" );
809 protected function getGroupName() {