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 DeferredUpdates
::addCallableUpdate( function() use ( $user ) {
108 $user->spreadAnyEditBlock();
110 throw new PermissionsError( 'move', $permErrors );
113 $def = !$request->wasPosted();
115 $this->reason
= $request->getText( 'wpReason' );
116 $this->moveTalk
= $request->getBool( 'wpMovetalk', $def );
117 $this->fixRedirects
= $request->getBool( 'wpFixRedirects', $def );
118 $this->leaveRedirect
= $request->getBool( 'wpLeaveRedirect', $def );
119 $this->moveSubpages
= $request->getBool( 'wpMovesubpages', false );
120 $this->deleteAndMove
= $request->getBool( 'wpDeleteAndMove' ) && $request->getBool( 'wpConfirm' );
121 $this->moveOverShared
= $request->getBool( 'wpMoveOverSharedFile', false );
122 $this->watch
= $request->getCheck( 'wpWatch' ) && $user->isLoggedIn();
124 if ( 'submit' == $request->getVal( 'action' ) && $request->wasPosted()
125 && $user->matchEditToken( $request->getVal( 'wpEditToken' ) )
129 $this->showForm( array() );
136 * @param array $err Error messages. Each item is an error message.
137 * It may either be a string message name or array message name and
138 * parameters, like the second argument to OutputPage::wrapWikiMsg().
140 function showForm( $err ) {
143 $this->getSkin()->setRelevantTitle( $this->oldTitle
);
145 $oldTitleLink = Linker
::link( $this->oldTitle
);
147 $out = $this->getOutput();
148 $out->setPageTitle( $this->msg( 'move-page', $this->oldTitle
->getPrefixedText() ) );
149 $out->addModules( 'mediawiki.special.movePage' );
150 $this->addHelpLink( 'Help:Moving a page' );
152 $newTitle = $this->newTitle
;
155 # Show the current title as a default
156 # when the form is first opened.
157 $newTitle = $this->oldTitle
;
158 } elseif ( !count( $err ) ) {
159 # If a title was supplied, probably from the move log revert
160 # link, check for validity. We can then show some diagnostic
161 # information and save a click.
162 $newerr = $this->oldTitle
->isValidMoveOperation( $newTitle );
163 if ( is_array( $newerr ) ) {
168 $user = $this->getUser();
170 if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'articleexists'
171 && $newTitle->quickUserCan( 'delete', $user )
173 $out->addWikiMsg( 'delete_and_move_text', $newTitle->getPrefixedText() );
174 $movepagebtn = $this->msg( 'delete_and_move' )->text();
175 $submitVar = 'wpDeleteAndMove';
179 if ( $this->oldTitle
->getNamespace() == NS_USER
&& !$this->oldTitle
->isSubpage() ) {
181 "<div class=\"error mw-moveuserpage-warning\">\n$1\n</div>",
182 'moveuserpage-warning'
184 } elseif ( $this->oldTitle
->getNamespace() == NS_CATEGORY
) {
186 "<div class=\"error mw-movecategorypage-warning\">\n$1\n</div>",
187 'movecategorypage-warning'
191 $out->addWikiMsg( $this->getConfig()->get( 'FixDoubleRedirects' ) ?
193 'movepagetext-noredirectfixer'
195 $movepagebtn = $this->msg( 'movepagebtn' )->text();
196 $submitVar = 'wpMove';
200 if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'file-exists-sharedrepo'
201 && $user->isAllowed( 'reupload-shared' )
203 $out->addWikiMsg( 'move-over-sharedrepo', $newTitle->getPrefixedText() );
204 $submitVar = 'wpMoveOverSharedFile';
208 $oldTalk = $this->oldTitle
->getTalkPage();
209 $oldTitleSubpages = $this->oldTitle
->hasSubpages();
210 $oldTitleTalkSubpages = $this->oldTitle
->getTalkPage()->hasSubpages();
212 $canMoveSubpage = ( $oldTitleSubpages ||
$oldTitleTalkSubpages ) &&
213 !count( $this->oldTitle
->getUserPermissionsErrors( 'move-subpages', $user ) );
215 # We also want to be able to move assoc. subpage talk-pages even if base page
216 # has no associated talk page, so || with $oldTitleTalkSubpages.
217 $considerTalk = !$this->oldTitle
->isTalkPage() &&
219 ||
( $oldTitleTalkSubpages && $canMoveSubpage ) );
221 $dbr = wfGetDB( DB_SLAVE
);
222 if ( $this->getConfig()->get( 'FixDoubleRedirects' ) ) {
223 $hasRedirects = $dbr->selectField( 'redirect', '1',
225 'rd_namespace' => $this->oldTitle
->getNamespace(),
226 'rd_title' => $this->oldTitle
->getDBkey(),
229 $hasRedirects = false;
232 if ( $considerTalk ) {
233 $out->addWikiMsg( 'movepagetalktext' );
236 if ( count( $err ) ) {
237 $out->addHTML( "<div class='error'>\n" );
238 $action_desc = $this->msg( 'action-move' )->plain();
239 $out->addWikiMsg( 'permissionserrorstext-withaction', count( $err ), $action_desc );
241 if ( count( $err ) == 1 ) {
243 $errMsgName = array_shift( $errMsg );
245 if ( $errMsgName == 'hookaborted' ) {
246 $out->addHTML( "<p>{$errMsg[0]}</p>\n" );
248 $out->addWikiMsgArray( $errMsgName, $errMsg );
253 foreach ( $err as $errMsg ) {
254 if ( $errMsg[0] == 'hookaborted' ) {
255 $errStr[] = $errMsg[1];
257 $errMsgName = array_shift( $errMsg );
258 $errStr[] = $this->msg( $errMsgName, $errMsg )->parse();
262 $out->addHTML( '<ul><li>' . implode( "</li>\n<li>", $errStr ) . "</li></ul>\n" );
264 $out->addHTML( "</div>\n" );
267 if ( $this->oldTitle
->isProtected( 'move' ) ) {
268 # Is the title semi-protected?
269 if ( $this->oldTitle
->isSemiProtected( 'move' ) ) {
270 $noticeMsg = 'semiprotectedpagemovewarning';
271 $classes[] = 'mw-textarea-sprotected';
273 # Then it must be protected based on static groups (regular)
274 $noticeMsg = 'protectedpagemovewarning';
275 $classes[] = 'mw-textarea-protected';
277 $out->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" );
278 $out->addWikiMsg( $noticeMsg );
279 LogEventsList
::showLogExtract(
286 $out->addHTML( "</div>\n" );
289 // Byte limit (not string length limit) for wpReason and wpNewTitleMain
290 // is enforced in the mediawiki.special.movePage module
292 $immovableNamespaces = array();
294 foreach ( array_keys( $this->getLanguage()->getNamespaces() ) as $nsId ) {
295 if ( !MWNamespace
::isMovable( $nsId ) ) {
296 $immovableNamespaces[] = $nsId;
300 $handler = ContentHandler
::getForTitle( $this->oldTitle
);
307 'action' => $this->getPageTitle()->getLocalURL( 'action=submit' ),
311 Xml
::openElement( 'fieldset' ) .
312 Xml
::element( 'legend', null, $this->msg( 'move-page-legend' )->text() ) .
313 Xml
::openElement( 'table', array( 'id' => 'mw-movepage-table' ) )
318 <td class='mw-label'>" .
319 $this->msg( 'movearticle' )->escaped() .
321 <td class='mw-input'>
322 <strong>{$oldTitleLink}</strong>
326 <td class='mw-label'>" .
327 Xml
::label( $this->msg( 'newtitle' )->text(), 'wpNewTitleMain' ) .
329 <td class='mw-input'>" .
330 Html
::namespaceSelector(
332 'selected' => $newTitle->getNamespace(),
333 'exclude' => $immovableNamespaces
335 array( 'name' => 'wpNewTitleNs', 'id' => 'wpNewTitleNs' )
340 $wgContLang->recodeForEdit( $newTitle->getText() ),
343 'id' => 'wpNewTitleMain',
347 Html
::hidden( 'wpOldTitle', $this->oldTitle
->getPrefixedText() ) .
351 <td class='mw-label'>" .
352 Xml
::label( $this->msg( 'movereason' )->text(), 'wpReason' ) .
354 <td class='mw-input'>" .
355 Xml
::input( 'wpReason', 60, $this->reason
, array(
364 if ( $considerTalk ) {
368 <td class='mw-input'>" .
370 $this->msg( 'movetalk' )->text(),
380 if ( $user->isAllowed( 'suppressredirect' ) ) {
381 if ( $handler->supportsRedirects() ) {
382 $isChecked = $this->leaveRedirect
;
387 'disabled' => 'disabled'
393 <td class='mw-input'>" .
395 $this->msg( 'move-leave-redirect' )->text(),
406 if ( $hasRedirects ) {
410 <td class='mw-input'>" .
412 $this->msg( 'fix-double-redirects' )->text(),
422 if ( $canMoveSubpage ) {
423 $maximumMovedPages = $this->getConfig()->get( 'MaximumMovedPages' );
427 <td class='mw-input'>" .
430 # Don't check the box if we only have talk subpages to
431 # move and we aren't moving the talk page.
432 $this->moveSubpages
&& ( $this->oldTitle
->hasSubpages() ||
$this->moveTalk
),
433 array( 'id' => 'wpMovesubpages' )
437 array( 'for' => 'wpMovesubpages' ),
439 ( $this->oldTitle
->hasSubpages()
441 : 'move-talk-subpages' )
442 )->numParams( $maximumMovedPages )->params( $maximumMovedPages )->parse()
449 $watchChecked = $user->isLoggedIn() && ( $this->watch ||
$user->getBoolOption( 'watchmoves' )
450 ||
$user->isWatched( $this->oldTitle
) );
451 # Don't allow watching if user is not logged in
452 if ( $user->isLoggedIn() ) {
456 <td class='mw-input'>" .
458 $this->msg( 'move-watch' )->text(),
472 <td class='mw-input'>" .
474 $this->msg( 'delete_and_move_confirm' )->text(),
486 <td class='mw-submit'>" .
487 Xml
::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
493 Xml
::closeElement( 'table' ) .
494 Html
::hidden( 'wpEditToken', $user->getEditToken() ) .
495 Xml
::closeElement( 'fieldset' ) .
496 Xml
::closeElement( 'form' ) .
500 $this->showLogFragment( $this->oldTitle
);
501 $this->showSubpages( $this->oldTitle
);
504 function doSubmit() {
505 $user = $this->getUser();
507 if ( $user->pingLimiter( 'move' ) ) {
508 throw new ThrottledError
;
511 $ot = $this->oldTitle
;
512 $nt = $this->newTitle
;
514 # don't allow moving to pages with # in
515 if ( !$nt ||
$nt->hasFragment() ) {
516 $this->showForm( array( array( 'badtitletext' ) ) );
521 # Show a warning if the target file exists on a shared repo
522 if ( $nt->getNamespace() == NS_FILE
523 && !( $this->moveOverShared
&& $user->isAllowed( 'reupload-shared' ) )
524 && !RepoGroup
::singleton()->getLocalRepo()->findFile( $nt )
527 $this->showForm( array( array( 'file-exists-sharedrepo' ) ) );
532 # Delete to make way if requested
533 if ( $this->deleteAndMove
) {
534 $permErrors = $nt->getUserPermissionsErrors( 'delete', $user );
535 if ( count( $permErrors ) ) {
536 # Only show the first error
537 $this->showForm( $permErrors );
542 $reason = $this->msg( 'delete_and_move_reason', $ot )->inContentLanguage()->text();
544 // Delete an associated image if there is
545 if ( $nt->getNamespace() == NS_FILE
) {
546 $file = wfLocalFile( $nt );
547 $file->load( File
::READ_LATEST
);
548 if ( $file->exists() ) {
549 $file->delete( $reason, false, $user );
553 $error = ''; // passed by ref
554 $page = WikiPage
::factory( $nt );
555 $deleteStatus = $page->doDeleteArticleReal( $reason, false, 0, true, $error, $user );
556 if ( !$deleteStatus->isGood() ) {
557 $this->showForm( $deleteStatus->getErrorsArray() );
563 $handler = ContentHandler
::getForTitle( $ot );
565 if ( !$handler->supportsRedirects() ) {
566 $createRedirect = false;
567 } elseif ( $user->isAllowed( 'suppressredirect' ) ) {
568 $createRedirect = $this->leaveRedirect
;
570 $createRedirect = true;
573 # Do the actual move.
574 $mp = new MovePage( $ot, $nt );
575 $valid = $mp->isValidMove();
576 if ( !$valid->isOK() ) {
577 $this->showForm( $valid->getErrorsArray() );
581 $permStatus = $mp->checkPermissions( $user, $this->reason
);
582 if ( !$permStatus->isOK() ) {
583 $this->showForm( $permStatus->getErrorsArray() );
587 $status = $mp->move( $user, $this->reason
, $createRedirect );
588 if ( !$status->isOK() ) {
589 $this->showForm( $status->getErrorsArray() );
593 if ( $this->getConfig()->get( 'FixDoubleRedirects' ) && $this->fixRedirects
) {
594 DoubleRedirectJob
::fixRedirects( 'move', $ot, $nt );
597 $out = $this->getOutput();
598 $out->setPageTitle( $this->msg( 'pagemovedsub' ) );
600 $oldLink = Linker
::link(
603 array( 'id' => 'movepage-oldlink' ),
604 array( 'redirect' => 'no' )
606 $newLink = Linker
::linkKnown(
609 array( 'id' => 'movepage-newlink' )
611 $oldText = $ot->getPrefixedText();
612 $newText = $nt->getPrefixedText();
614 if ( $ot->exists() ) {
615 //NOTE: we assume that if the old title exists, it's because it was re-created as
616 // a redirect to the new title. This is not safe, but what we did before was
617 // even worse: we just determined whether a redirect should have been created,
618 // and reported that it was created if it should have, without any checks.
619 // Also note that isRedirect() is unreliable because of bug 37209.
620 $msgName = 'movepage-moved-redirect';
622 $msgName = 'movepage-moved-noredirect';
625 $out->addHTML( $this->msg( 'movepage-moved' )->rawParams( $oldLink,
626 $newLink )->params( $oldText, $newText )->parseAsBlock() );
627 $out->addWikiMsg( $msgName );
629 Hooks
::run( 'SpecialMovepageAfterMove', array( &$this, &$ot, &$nt ) );
631 # Now we move extra pages we've been asked to move: subpages and talk
632 # pages. First, if the old page or the new page is a talk page, we
633 # can't move any talk pages: cancel that.
634 if ( $ot->isTalkPage() ||
$nt->isTalkPage() ) {
635 $this->moveTalk
= false;
638 if ( count( $ot->getUserPermissionsErrors( 'move-subpages', $user ) ) ) {
639 $this->moveSubpages
= false;
642 # Next make a list of id's. This might be marginally less efficient
643 # than a more direct method, but this is not a highly performance-cri-
644 # tical code path and readable code is more important here.
646 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
647 # 4 might get confused. If so, consider rewriting as a UNION.
649 # If the target namespace doesn't allow subpages, moving with subpages
650 # would mean that you couldn't move them back in one operation, which
652 # @todo FIXME: A specific error message should be given in this case.
654 // @todo FIXME: Use Title::moveSubpages() here
655 $dbr = wfGetDB( DB_MASTER
);
656 if ( $this->moveSubpages
&& (
657 MWNamespace
::hasSubpages( $nt->getNamespace() ) ||
(
659 && MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() )
663 'page_title' . $dbr->buildLike( $ot->getDBkey() . '/', $dbr->anyString() )
664 . ' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
666 $conds['page_namespace'] = array();
667 if ( MWNamespace
::hasSubpages( $nt->getNamespace() ) ) {
668 $conds['page_namespace'][] = $ot->getNamespace();
670 if ( $this->moveTalk
&&
671 MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() )
673 $conds['page_namespace'][] = $ot->getTalkPage()->getNamespace();
675 } elseif ( $this->moveTalk
) {
677 'page_namespace' => $ot->getTalkPage()->getNamespace(),
678 'page_title' => $ot->getDBkey()
685 $extraPages = array();
686 if ( !is_null( $conds ) ) {
687 $extraPages = TitleArray
::newFromResult(
688 $dbr->select( 'page',
689 array( 'page_id', 'page_namespace', 'page_title' ),
696 $extraOutput = array();
698 foreach ( $extraPages as $oldSubpage ) {
699 if ( $ot->equals( $oldSubpage ) ||
$nt->equals( $oldSubpage ) ) {
700 # Already did this one.
704 $newPageName = preg_replace(
705 '#^' . preg_quote( $ot->getDBkey(), '#' ) . '#',
706 StringUtils
::escapeRegexReplacement( $nt->getDBkey() ), # bug 21234
707 $oldSubpage->getDBkey()
710 if ( $oldSubpage->isSubpage() && ( $ot->isTalkPage() xor $nt->isTalkPage() ) ) {
711 // Moving a subpage from a subject namespace to a talk namespace or vice-versa
712 $newNs = $nt->getNamespace();
713 } elseif ( $oldSubpage->isTalkPage() ) {
714 $newNs = $nt->getTalkPage()->getNamespace();
716 $newNs = $nt->getSubjectPage()->getNamespace();
719 # Bug 14385: we need makeTitleSafe because the new page names may
720 # be longer than 255 characters.
721 $newSubpage = Title
::makeTitleSafe( $newNs, $newPageName );
722 if ( !$newSubpage ) {
723 $oldLink = Linker
::linkKnown( $oldSubpage );
724 $extraOutput[] = $this->msg( 'movepage-page-unmoved' )->rawParams( $oldLink )
725 ->params( Title
::makeName( $newNs, $newPageName ) )->escaped();
729 # This was copy-pasted from Renameuser, bleh.
730 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
731 $link = Linker
::linkKnown( $newSubpage );
732 $extraOutput[] = $this->msg( 'movepage-page-exists' )->rawParams( $link )->escaped();
734 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason
, $createRedirect );
736 if ( $success === true ) {
737 if ( $this->fixRedirects
) {
738 DoubleRedirectJob
::fixRedirects( 'move', $oldSubpage, $newSubpage );
740 $oldLink = Linker
::link(
744 array( 'redirect' => 'no' )
747 $newLink = Linker
::linkKnown( $newSubpage );
748 $extraOutput[] = $this->msg( 'movepage-page-moved' )
749 ->rawParams( $oldLink, $newLink )->escaped();
752 $maximumMovedPages = $this->getConfig()->get( 'MaximumMovedPages' );
753 if ( $count >= $maximumMovedPages ) {
754 $extraOutput[] = $this->msg( 'movepage-max-pages' )
755 ->numParams( $maximumMovedPages )->escaped();
759 $oldLink = Linker
::linkKnown( $oldSubpage );
760 $newLink = Linker
::link( $newSubpage );
761 $extraOutput[] = $this->msg( 'movepage-page-unmoved' )
762 ->rawParams( $oldLink, $newLink )->escaped();
767 if ( $extraOutput !== array() ) {
768 $out->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
771 # Deal with watches (we don't watch subpages)
772 WatchAction
::doWatchOrUnwatch( $this->watch
, $ot, $user );
773 WatchAction
::doWatchOrUnwatch( $this->watch
, $nt, $user );
776 function showLogFragment( $title ) {
777 $moveLogPage = new LogPage( 'move' );
778 $out = $this->getOutput();
779 $out->addHTML( Xml
::element( 'h2', null, $moveLogPage->getName()->text() ) );
780 LogEventsList
::showLogExtract( $out, 'move', $title );
783 function showSubpages( $title ) {
784 if ( !MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
788 $subpages = $title->getSubpages();
789 $count = $subpages instanceof TitleArray ?
$subpages->count() : 0;
791 $out = $this->getOutput();
792 $out->wrapWikiMsg( '== $1 ==', array( 'movesubpage', $count ) );
796 $out->addWikiMsg( 'movenosubpage' );
801 $out->addWikiMsg( 'movesubpagetext', $this->getLanguage()->formatNum( $count ) );
802 $out->addHTML( "<ul>\n" );
804 foreach ( $subpages as $subpage ) {
805 $link = Linker
::link( $subpage );
806 $out->addHTML( "<li>$link</li>\n" );
808 $out->addHTML( "</ul>\n" );
811 protected function getGroupName() {