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 if ( $this->oldTitle
->getNamespace() == NS_USER
&& !$this->oldTitle
->isSubpage() ) {
173 "<div class=\"error mw-moveuserpage-warning\">\n$1\n</div>",
174 'moveuserpage-warning'
176 } elseif ( $this->oldTitle
->getNamespace() == NS_CATEGORY
) {
178 "<div class=\"error mw-movecategorypage-warning\">\n$1\n</div>",
179 'movecategorypage-warning'
183 $out->addWikiMsg( $this->getConfig()->get( 'FixDoubleRedirects' ) ?
185 'movepagetext-noredirectfixer'
187 $movepagebtn = $this->msg( 'movepagebtn' )->text();
188 $submitVar = 'wpMove';
192 if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'file-exists-sharedrepo'
193 && $user->isAllowed( 'reupload-shared' )
195 $out->addWikiMsg( 'move-over-sharedrepo', $newTitle->getPrefixedText() );
196 $submitVar = 'wpMoveOverSharedFile';
200 $oldTalk = $this->oldTitle
->getTalkPage();
201 $oldTitleSubpages = $this->oldTitle
->hasSubpages();
202 $oldTitleTalkSubpages = $this->oldTitle
->getTalkPage()->hasSubpages();
204 $canMoveSubpage = ( $oldTitleSubpages ||
$oldTitleTalkSubpages ) &&
205 !count( $this->oldTitle
->getUserPermissionsErrors( 'move-subpages', $user ) );
207 # We also want to be able to move assoc. subpage talk-pages even if base page
208 # has no associated talk page, so || with $oldTitleTalkSubpages.
209 $considerTalk = !$this->oldTitle
->isTalkPage() &&
211 ||
( $oldTitleTalkSubpages && $canMoveSubpage ) );
213 $dbr = wfGetDB( DB_SLAVE
);
214 if ( $this->getConfig()->get( 'FixDoubleRedirects' ) ) {
215 $hasRedirects = $dbr->selectField( 'redirect', '1',
217 'rd_namespace' => $this->oldTitle
->getNamespace(),
218 'rd_title' => $this->oldTitle
->getDBkey(),
221 $hasRedirects = false;
224 if ( $considerTalk ) {
225 $out->addWikiMsg( 'movepagetalktext' );
228 if ( count( $err ) ) {
229 $out->addHTML( "<div class='error'>\n" );
230 $action_desc = $this->msg( 'action-move' )->plain();
231 $out->addWikiMsg( 'permissionserrorstext-withaction', count( $err ), $action_desc );
233 if ( count( $err ) == 1 ) {
235 $errMsgName = array_shift( $errMsg );
237 if ( $errMsgName == 'hookaborted' ) {
238 $out->addHTML( "<p>{$errMsg[0]}</p>\n" );
240 $out->addWikiMsgArray( $errMsgName, $errMsg );
245 foreach ( $err as $errMsg ) {
246 if ( $errMsg[0] == 'hookaborted' ) {
247 $errStr[] = $errMsg[1];
249 $errMsgName = array_shift( $errMsg );
250 $errStr[] = $this->msg( $errMsgName, $errMsg )->parse();
254 $out->addHTML( '<ul><li>' . implode( "</li>\n<li>", $errStr ) . "</li></ul>\n" );
256 $out->addHTML( "</div>\n" );
259 if ( $this->oldTitle
->isProtected( 'move' ) ) {
260 # Is the title semi-protected?
261 if ( $this->oldTitle
->isSemiProtected( 'move' ) ) {
262 $noticeMsg = 'semiprotectedpagemovewarning';
263 $classes[] = 'mw-textarea-sprotected';
265 # Then it must be protected based on static groups (regular)
266 $noticeMsg = 'protectedpagemovewarning';
267 $classes[] = 'mw-textarea-protected';
269 $out->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" );
270 $out->addWikiMsg( $noticeMsg );
271 LogEventsList
::showLogExtract(
278 $out->addHTML( "</div>\n" );
281 // Byte limit (not string length limit) for wpReason and wpNewTitleMain
282 // is enforced in the mediawiki.special.movePage module
284 $immovableNamespaces = array();
286 foreach ( array_keys( $this->getLanguage()->getNamespaces() ) as $nsId ) {
287 if ( !MWNamespace
::isMovable( $nsId ) ) {
288 $immovableNamespaces[] = $nsId;
292 $handler = ContentHandler
::getForTitle( $this->oldTitle
);
299 'action' => $this->getPageTitle()->getLocalURL( 'action=submit' ),
303 Xml
::openElement( 'fieldset' ) .
304 Xml
::element( 'legend', null, $this->msg( 'move-page-legend' )->text() ) .
305 Xml
::openElement( 'table', array( 'id' => 'mw-movepage-table' ) )
310 <td class='mw-label'>" .
311 $this->msg( 'movearticle' )->escaped() .
313 <td class='mw-input'>
314 <strong>{$oldTitleLink}</strong>
318 <td class='mw-label'>" .
319 Xml
::label( $this->msg( 'newtitle' )->text(), 'wpNewTitleMain' ) .
321 <td class='mw-input'>" .
322 Html
::namespaceSelector(
324 'selected' => $newTitle->getNamespace(),
325 'exclude' => $immovableNamespaces
327 array( 'name' => 'wpNewTitleNs', 'id' => 'wpNewTitleNs' )
332 $wgContLang->recodeForEdit( $newTitle->getText() ),
335 'id' => 'wpNewTitleMain',
339 Html
::hidden( 'wpOldTitle', $this->oldTitle
->getPrefixedText() ) .
343 <td class='mw-label'>" .
344 Xml
::label( $this->msg( 'movereason' )->text(), 'wpReason' ) .
346 <td class='mw-input'>" .
347 Xml
::input( 'wpReason', 60, $this->reason
, array(
356 if ( $considerTalk ) {
360 <td class='mw-input'>" .
362 $this->msg( 'movetalk' )->text(),
372 if ( $user->isAllowed( 'suppressredirect' ) ) {
373 if ( $handler->supportsRedirects() ) {
374 $isChecked = $this->leaveRedirect
;
379 'disabled' => 'disabled'
385 <td class='mw-input'>" .
387 $this->msg( 'move-leave-redirect' )->text(),
398 if ( $hasRedirects ) {
402 <td class='mw-input'>" .
404 $this->msg( 'fix-double-redirects' )->text(),
414 if ( $canMoveSubpage ) {
415 $maximumMovedPages = $this->getConfig()->get( 'MaximumMovedPages' );
419 <td class='mw-input'>" .
422 # Don't check the box if we only have talk subpages to
423 # move and we aren't moving the talk page.
424 $this->moveSubpages
&& ( $this->oldTitle
->hasSubpages() ||
$this->moveTalk
),
425 array( 'id' => 'wpMovesubpages' )
429 array( 'for' => 'wpMovesubpages' ),
431 ( $this->oldTitle
->hasSubpages()
433 : 'move-talk-subpages' )
434 )->numParams( $maximumMovedPages )->params( $maximumMovedPages )->parse()
441 $watchChecked = $user->isLoggedIn() && ( $this->watch ||
$user->getBoolOption( 'watchmoves' )
442 ||
$user->isWatched( $this->oldTitle
) );
443 # Don't allow watching if user is not logged in
444 if ( $user->isLoggedIn() ) {
448 <td class='mw-input'>" .
450 $this->msg( 'move-watch' )->text(),
464 <td class='mw-input'>" .
466 $this->msg( 'delete_and_move_confirm' )->text(),
478 <td class='mw-submit'>" .
479 Xml
::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
485 Xml
::closeElement( 'table' ) .
486 Html
::hidden( 'wpEditToken', $user->getEditToken() ) .
487 Xml
::closeElement( 'fieldset' ) .
488 Xml
::closeElement( 'form' ) .
492 $this->showLogFragment( $this->oldTitle
);
493 $this->showSubpages( $this->oldTitle
);
496 function doSubmit() {
497 $user = $this->getUser();
499 if ( $user->pingLimiter( 'move' ) ) {
500 throw new ThrottledError
;
503 $ot = $this->oldTitle
;
504 $nt = $this->newTitle
;
506 # don't allow moving to pages with # in
507 if ( !$nt ||
$nt->hasFragment() ) {
508 $this->showForm( array( array( 'badtitletext' ) ) );
513 # Show a warning if the target file exists on a shared repo
514 if ( $nt->getNamespace() == NS_FILE
515 && !( $this->moveOverShared
&& $user->isAllowed( 'reupload-shared' ) )
516 && !RepoGroup
::singleton()->getLocalRepo()->findFile( $nt )
519 $this->showForm( array( array( 'file-exists-sharedrepo' ) ) );
524 # Delete to make way if requested
525 if ( $this->deleteAndMove
) {
526 $permErrors = $nt->getUserPermissionsErrors( 'delete', $user );
527 if ( count( $permErrors ) ) {
528 # Only show the first error
529 $this->showForm( $permErrors );
534 $reason = $this->msg( 'delete_and_move_reason', $ot )->inContentLanguage()->text();
536 // Delete an associated image if there is
537 if ( $nt->getNamespace() == NS_FILE
) {
538 $file = wfLocalFile( $nt );
539 if ( $file->exists() ) {
540 $file->delete( $reason, false );
544 $error = ''; // passed by ref
545 $page = WikiPage
::factory( $nt );
546 $deleteStatus = $page->doDeleteArticleReal( $reason, false, 0, true, $error, $user );
547 if ( !$deleteStatus->isGood() ) {
548 $this->showForm( $deleteStatus->getErrorsArray() );
554 $handler = ContentHandler
::getForTitle( $ot );
556 if ( !$handler->supportsRedirects() ) {
557 $createRedirect = false;
558 } elseif ( $user->isAllowed( 'suppressredirect' ) ) {
559 $createRedirect = $this->leaveRedirect
;
561 $createRedirect = true;
564 # Do the actual move.
565 $mp = new MovePage( $ot, $nt );
566 $valid = $mp->isValidMove();
567 if ( !$valid->isOK() ) {
568 $this->showForm( $valid->getErrorsArray() );
572 $permStatus = $mp->checkPermissions( $user, $this->reason
);
573 if ( !$permStatus->isOK() ) {
574 $this->showForm( $permStatus->getErrorsArray() );
578 $status = $mp->move( $user, $this->reason
, $createRedirect );
579 if ( !$status->isOK() ) {
580 $this->showForm( $status->getErrorsArray() );
584 if ( $this->getConfig()->get( 'FixDoubleRedirects' ) && $this->fixRedirects
) {
585 DoubleRedirectJob
::fixRedirects( 'move', $ot, $nt );
588 $out = $this->getOutput();
589 $out->setPageTitle( $this->msg( 'pagemovedsub' ) );
591 $oldLink = Linker
::link(
594 array( 'id' => 'movepage-oldlink' ),
595 array( 'redirect' => 'no' )
597 $newLink = Linker
::linkKnown(
600 array( 'id' => 'movepage-newlink' )
602 $oldText = $ot->getPrefixedText();
603 $newText = $nt->getPrefixedText();
605 if ( $ot->exists() ) {
606 //NOTE: we assume that if the old title exists, it's because it was re-created as
607 // a redirect to the new title. This is not safe, but what we did before was
608 // even worse: we just determined whether a redirect should have been created,
609 // and reported that it was created if it should have, without any checks.
610 // Also note that isRedirect() is unreliable because of bug 37209.
611 $msgName = 'movepage-moved-redirect';
613 $msgName = 'movepage-moved-noredirect';
616 $out->addHTML( $this->msg( 'movepage-moved' )->rawParams( $oldLink,
617 $newLink )->params( $oldText, $newText )->parseAsBlock() );
618 $out->addWikiMsg( $msgName );
620 Hooks
::run( 'SpecialMovepageAfterMove', array( &$this, &$ot, &$nt ) );
622 # Now we move extra pages we've been asked to move: subpages and talk
623 # pages. First, if the old page or the new page is a talk page, we
624 # can't move any talk pages: cancel that.
625 if ( $ot->isTalkPage() ||
$nt->isTalkPage() ) {
626 $this->moveTalk
= false;
629 if ( count( $ot->getUserPermissionsErrors( 'move-subpages', $user ) ) ) {
630 $this->moveSubpages
= false;
633 # Next make a list of id's. This might be marginally less efficient
634 # than a more direct method, but this is not a highly performance-cri-
635 # tical code path and readable code is more important here.
637 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
638 # 4 might get confused. If so, consider rewriting as a UNION.
640 # If the target namespace doesn't allow subpages, moving with subpages
641 # would mean that you couldn't move them back in one operation, which
643 # @todo FIXME: A specific error message should be given in this case.
645 // @todo FIXME: Use Title::moveSubpages() here
646 $dbr = wfGetDB( DB_MASTER
);
647 if ( $this->moveSubpages
&& (
648 MWNamespace
::hasSubpages( $nt->getNamespace() ) ||
(
650 && MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() )
654 'page_title' . $dbr->buildLike( $ot->getDBkey() . '/', $dbr->anyString() )
655 . ' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
657 $conds['page_namespace'] = array();
658 if ( MWNamespace
::hasSubpages( $nt->getNamespace() ) ) {
659 $conds['page_namespace'][] = $ot->getNamespace();
661 if ( $this->moveTalk
&&
662 MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() )
664 $conds['page_namespace'][] = $ot->getTalkPage()->getNamespace();
666 } elseif ( $this->moveTalk
) {
668 'page_namespace' => $ot->getTalkPage()->getNamespace(),
669 'page_title' => $ot->getDBkey()
676 $extraPages = array();
677 if ( !is_null( $conds ) ) {
678 $extraPages = TitleArray
::newFromResult(
679 $dbr->select( 'page',
680 array( 'page_id', 'page_namespace', 'page_title' ),
687 $extraOutput = array();
689 foreach ( $extraPages as $oldSubpage ) {
690 if ( $ot->equals( $oldSubpage ) ||
$nt->equals( $oldSubpage ) ) {
691 # Already did this one.
695 $newPageName = preg_replace(
696 '#^' . preg_quote( $ot->getDBkey(), '#' ) . '#',
697 StringUtils
::escapeRegexReplacement( $nt->getDBkey() ), # bug 21234
698 $oldSubpage->getDBkey()
701 if ( $oldSubpage->isTalkPage() ) {
702 $newNs = $nt->getTalkPage()->getNamespace();
704 $newNs = $nt->getSubjectPage()->getNamespace();
707 # Bug 14385: we need makeTitleSafe because the new page names may
708 # be longer than 255 characters.
709 $newSubpage = Title
::makeTitleSafe( $newNs, $newPageName );
710 if ( !$newSubpage ) {
711 $oldLink = Linker
::linkKnown( $oldSubpage );
712 $extraOutput[] = $this->msg( 'movepage-page-unmoved' )->rawParams( $oldLink )
713 ->params( Title
::makeName( $newNs, $newPageName ) )->escaped();
717 # This was copy-pasted from Renameuser, bleh.
718 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
719 $link = Linker
::linkKnown( $newSubpage );
720 $extraOutput[] = $this->msg( 'movepage-page-exists' )->rawParams( $link )->escaped();
722 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason
, $createRedirect );
724 if ( $success === true ) {
725 if ( $this->fixRedirects
) {
726 DoubleRedirectJob
::fixRedirects( 'move', $oldSubpage, $newSubpage );
728 $oldLink = Linker
::link(
732 array( 'redirect' => 'no' )
735 $newLink = Linker
::linkKnown( $newSubpage );
736 $extraOutput[] = $this->msg( 'movepage-page-moved' )
737 ->rawParams( $oldLink, $newLink )->escaped();
740 $maximumMovedPages = $this->getConfig()->get( 'MaximumMovedPages' );
741 if ( $count >= $maximumMovedPages ) {
742 $extraOutput[] = $this->msg( 'movepage-max-pages' )
743 ->numParams( $maximumMovedPages )->escaped();
747 $oldLink = Linker
::linkKnown( $oldSubpage );
748 $newLink = Linker
::link( $newSubpage );
749 $extraOutput[] = $this->msg( 'movepage-page-unmoved' )
750 ->rawParams( $oldLink, $newLink )->escaped();
755 if ( $extraOutput !== array() ) {
756 $out->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
759 # Deal with watches (we don't watch subpages)
760 WatchAction
::doWatchOrUnwatch( $this->watch
, $ot, $user );
761 WatchAction
::doWatchOrUnwatch( $this->watch
, $nt, $user );
764 function showLogFragment( $title ) {
765 $moveLogPage = new LogPage( 'move' );
766 $out = $this->getOutput();
767 $out->addHTML( Xml
::element( 'h2', null, $moveLogPage->getName()->text() ) );
768 LogEventsList
::showLogExtract( $out, 'move', $title );
771 function showSubpages( $title ) {
772 if ( !MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
776 $subpages = $title->getSubpages();
777 $count = $subpages instanceof TitleArray ?
$subpages->count() : 0;
779 $out = $this->getOutput();
780 $out->wrapWikiMsg( '== $1 ==', array( 'movesubpage', $count ) );
784 $out->addWikiMsg( 'movenosubpage' );
789 $out->addWikiMsg( 'movesubpagetext', $this->getLanguage()->formatNum( $count ) );
790 $out->addHTML( "<ul>\n" );
792 foreach ( $subpages as $subpage ) {
793 $link = Linker
::link( $subpage );
794 $out->addHTML( "<li>$link</li>\n" );
796 $out->addHTML( "</ul>\n" );
799 protected function getGroupName() {