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->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 if ( is_string( $oldTitleText ) ) {
79 $this->oldTitle
= Title
::newFromText( $oldTitleText );
82 if ( $this->oldTitle
=== null ) {
83 // Either oldTitle wasn't passed, or newFromText returned null
84 throw new ErrorPageError( 'notargettitle', 'notargettext' );
86 if ( !$this->oldTitle
->exists() ) {
87 throw new ErrorPageError( 'nopagetitle', 'nopagetext' );
90 $newTitleTextMain = $request->getText( 'wpNewTitleMain' );
91 $newTitleTextNs = $request->getInt( 'wpNewTitleNs', $this->oldTitle
->getNamespace() );
92 // Backwards compatibility for forms submitting here from other sources
93 // which is more common than it should be..
94 $newTitleText_bc = $request->getText( 'wpNewTitle' );
95 $this->newTitle
= strlen( $newTitleText_bc ) > 0
96 ? Title
::newFromText( $newTitleText_bc )
97 : Title
::makeTitleSafe( $newTitleTextNs, $newTitleTextMain );
99 $user = $this->getUser();
102 $permErrors = $this->oldTitle
->getUserPermissionsErrors( 'move', $user );
103 if ( count( $permErrors ) ) {
104 // Auto-block user's IP if the account was "hard" blocked
105 $user->spreadAnyEditBlock();
106 throw new PermissionsError( 'move', $permErrors );
109 $def = !$request->wasPosted();
111 $this->reason
= $request->getText( 'wpReason' );
112 $this->moveTalk
= $request->getBool( 'wpMovetalk', $def );
113 $this->fixRedirects
= $request->getBool( 'wpFixRedirects', $def );
114 $this->leaveRedirect
= $request->getBool( 'wpLeaveRedirect', $def );
115 $this->moveSubpages
= $request->getBool( 'wpMovesubpages', false );
116 $this->deleteAndMove
= $request->getBool( 'wpDeleteAndMove' ) && $request->getBool( 'wpConfirm' );
117 $this->moveOverShared
= $request->getBool( 'wpMoveOverSharedFile', false );
118 $this->watch
= $request->getCheck( 'wpWatch' ) && $user->isLoggedIn();
120 if ( 'submit' == $request->getVal( 'action' ) && $request->wasPosted()
121 && $user->matchEditToken( $request->getVal( 'wpEditToken' ) )
125 $this->showForm( array() );
132 * @param array $err Error messages. Each item is an error message.
133 * It may either be a string message name or array message name and
134 * parameters, like the second argument to OutputPage::wrapWikiMsg().
136 function showForm( $err ) {
139 $this->getSkin()->setRelevantTitle( $this->oldTitle
);
141 $oldTitleLink = Linker
::link( $this->oldTitle
);
143 $out = $this->getOutput();
144 $out->setPageTitle( $this->msg( 'move-page', $this->oldTitle
->getPrefixedText() ) );
145 $out->addModules( 'mediawiki.special.movePage' );
146 $this->addHelpLink( 'Help:Moving a page' );
148 $newTitle = $this->newTitle
;
151 # Show the current title as a default
152 # when the form is first opened.
153 $newTitle = $this->oldTitle
;
154 } elseif ( !count( $err ) ) {
155 # If a title was supplied, probably from the move log revert
156 # link, check for validity. We can then show some diagnostic
157 # information and save a click.
158 $newerr = $this->oldTitle
->isValidMoveOperation( $newTitle );
159 if ( is_array( $newerr ) ) {
164 $user = $this->getUser();
166 if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'articleexists'
167 && $newTitle->quickUserCan( 'delete', $user )
169 $out->addWikiMsg( 'delete_and_move_text', $newTitle->getPrefixedText() );
170 $movepagebtn = $this->msg( 'delete_and_move' )->text();
171 $submitVar = 'wpDeleteAndMove';
175 if ( $this->oldTitle
->getNamespace() == NS_USER
&& !$this->oldTitle
->isSubpage() ) {
177 "<div class=\"error mw-moveuserpage-warning\">\n$1\n</div>",
178 'moveuserpage-warning'
180 } elseif ( $this->oldTitle
->getNamespace() == NS_CATEGORY
) {
182 "<div class=\"error mw-movecategorypage-warning\">\n$1\n</div>",
183 'movecategorypage-warning'
187 $out->addWikiMsg( $this->getConfig()->get( 'FixDoubleRedirects' ) ?
189 'movepagetext-noredirectfixer'
191 $movepagebtn = $this->msg( 'movepagebtn' )->text();
192 $submitVar = 'wpMove';
196 if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'file-exists-sharedrepo'
197 && $user->isAllowed( 'reupload-shared' )
199 $out->addWikiMsg( 'move-over-sharedrepo', $newTitle->getPrefixedText() );
200 $submitVar = 'wpMoveOverSharedFile';
204 $oldTalk = $this->oldTitle
->getTalkPage();
205 $oldTitleSubpages = $this->oldTitle
->hasSubpages();
206 $oldTitleTalkSubpages = $this->oldTitle
->getTalkPage()->hasSubpages();
208 $canMoveSubpage = ( $oldTitleSubpages ||
$oldTitleTalkSubpages ) &&
209 !count( $this->oldTitle
->getUserPermissionsErrors( 'move-subpages', $user ) );
211 # We also want to be able to move assoc. subpage talk-pages even if base page
212 # has no associated talk page, so || with $oldTitleTalkSubpages.
213 $considerTalk = !$this->oldTitle
->isTalkPage() &&
215 ||
( $oldTitleTalkSubpages && $canMoveSubpage ) );
217 $dbr = wfGetDB( DB_SLAVE
);
218 if ( $this->getConfig()->get( 'FixDoubleRedirects' ) ) {
219 $hasRedirects = $dbr->selectField( 'redirect', '1',
221 'rd_namespace' => $this->oldTitle
->getNamespace(),
222 'rd_title' => $this->oldTitle
->getDBkey(),
225 $hasRedirects = false;
228 if ( $considerTalk ) {
229 $out->addWikiMsg( 'movepagetalktext' );
232 if ( count( $err ) ) {
233 $out->addHTML( "<div class='error'>\n" );
234 $action_desc = $this->msg( 'action-move' )->plain();
235 $out->addWikiMsg( 'permissionserrorstext-withaction', count( $err ), $action_desc );
237 if ( count( $err ) == 1 ) {
239 $errMsgName = array_shift( $errMsg );
241 if ( $errMsgName == 'hookaborted' ) {
242 $out->addHTML( "<p>{$errMsg[0]}</p>\n" );
244 $out->addWikiMsgArray( $errMsgName, $errMsg );
249 foreach ( $err as $errMsg ) {
250 if ( $errMsg[0] == 'hookaborted' ) {
251 $errStr[] = $errMsg[1];
253 $errMsgName = array_shift( $errMsg );
254 $errStr[] = $this->msg( $errMsgName, $errMsg )->parse();
258 $out->addHTML( '<ul><li>' . implode( "</li>\n<li>", $errStr ) . "</li></ul>\n" );
260 $out->addHTML( "</div>\n" );
263 if ( $this->oldTitle
->isProtected( 'move' ) ) {
264 # Is the title semi-protected?
265 if ( $this->oldTitle
->isSemiProtected( 'move' ) ) {
266 $noticeMsg = 'semiprotectedpagemovewarning';
267 $classes[] = 'mw-textarea-sprotected';
269 # Then it must be protected based on static groups (regular)
270 $noticeMsg = 'protectedpagemovewarning';
271 $classes[] = 'mw-textarea-protected';
273 $out->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" );
274 $out->addWikiMsg( $noticeMsg );
275 LogEventsList
::showLogExtract(
282 $out->addHTML( "</div>\n" );
285 // Byte limit (not string length limit) for wpReason and wpNewTitleMain
286 // is enforced in the mediawiki.special.movePage module
288 $immovableNamespaces = array();
290 foreach ( array_keys( $this->getLanguage()->getNamespaces() ) as $nsId ) {
291 if ( !MWNamespace
::isMovable( $nsId ) ) {
292 $immovableNamespaces[] = $nsId;
296 $handler = ContentHandler
::getForTitle( $this->oldTitle
);
303 'action' => $this->getPageTitle()->getLocalURL( 'action=submit' ),
307 Xml
::openElement( 'fieldset' ) .
308 Xml
::element( 'legend', null, $this->msg( 'move-page-legend' )->text() ) .
309 Xml
::openElement( 'table', array( 'id' => 'mw-movepage-table' ) )
314 <td class='mw-label'>" .
315 $this->msg( 'movearticle' )->escaped() .
317 <td class='mw-input'>
318 <strong>{$oldTitleLink}</strong>
322 <td class='mw-label'>" .
323 Xml
::label( $this->msg( 'newtitle' )->text(), 'wpNewTitleMain' ) .
325 <td class='mw-input'>" .
326 Html
::namespaceSelector(
328 'selected' => $newTitle->getNamespace(),
329 'exclude' => $immovableNamespaces
331 array( 'name' => 'wpNewTitleNs', 'id' => 'wpNewTitleNs' )
336 $wgContLang->recodeForEdit( $newTitle->getText() ),
339 'id' => 'wpNewTitleMain',
343 Html
::hidden( 'wpOldTitle', $this->oldTitle
->getPrefixedText() ) .
347 <td class='mw-label'>" .
348 Xml
::label( $this->msg( 'movereason' )->text(), 'wpReason' ) .
350 <td class='mw-input'>" .
351 Xml
::input( 'wpReason', 60, $this->reason
, array(
360 if ( $considerTalk ) {
364 <td class='mw-input'>" .
366 $this->msg( 'movetalk' )->text(),
376 if ( $user->isAllowed( 'suppressredirect' ) ) {
377 if ( $handler->supportsRedirects() ) {
378 $isChecked = $this->leaveRedirect
;
383 'disabled' => 'disabled'
389 <td class='mw-input'>" .
391 $this->msg( 'move-leave-redirect' )->text(),
402 if ( $hasRedirects ) {
406 <td class='mw-input'>" .
408 $this->msg( 'fix-double-redirects' )->text(),
418 if ( $canMoveSubpage ) {
419 $maximumMovedPages = $this->getConfig()->get( 'MaximumMovedPages' );
423 <td class='mw-input'>" .
426 # Don't check the box if we only have talk subpages to
427 # move and we aren't moving the talk page.
428 $this->moveSubpages
&& ( $this->oldTitle
->hasSubpages() ||
$this->moveTalk
),
429 array( 'id' => 'wpMovesubpages' )
433 array( 'for' => 'wpMovesubpages' ),
435 ( $this->oldTitle
->hasSubpages()
437 : 'move-talk-subpages' )
438 )->numParams( $maximumMovedPages )->params( $maximumMovedPages )->parse()
445 $watchChecked = $user->isLoggedIn() && ( $this->watch ||
$user->getBoolOption( 'watchmoves' )
446 ||
$user->isWatched( $this->oldTitle
) );
447 # Don't allow watching if user is not logged in
448 if ( $user->isLoggedIn() ) {
452 <td class='mw-input'>" .
454 $this->msg( 'move-watch' )->text(),
468 <td class='mw-input'>" .
470 $this->msg( 'delete_and_move_confirm' )->text(),
482 <td class='mw-submit'>" .
483 Xml
::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
489 Xml
::closeElement( 'table' ) .
490 Html
::hidden( 'wpEditToken', $user->getEditToken() ) .
491 Xml
::closeElement( 'fieldset' ) .
492 Xml
::closeElement( 'form' ) .
496 $this->showLogFragment( $this->oldTitle
);
497 $this->showSubpages( $this->oldTitle
);
500 function doSubmit() {
501 $user = $this->getUser();
503 if ( $user->pingLimiter( 'move' ) ) {
504 throw new ThrottledError
;
507 $ot = $this->oldTitle
;
508 $nt = $this->newTitle
;
510 # don't allow moving to pages with # in
511 if ( !$nt ||
$nt->hasFragment() ) {
512 $this->showForm( array( array( 'badtitletext' ) ) );
517 # Show a warning if the target file exists on a shared repo
518 if ( $nt->getNamespace() == NS_FILE
519 && !( $this->moveOverShared
&& $user->isAllowed( 'reupload-shared' ) )
520 && !RepoGroup
::singleton()->getLocalRepo()->findFile( $nt )
523 $this->showForm( array( array( 'file-exists-sharedrepo' ) ) );
528 # Delete to make way if requested
529 if ( $this->deleteAndMove
) {
530 $permErrors = $nt->getUserPermissionsErrors( 'delete', $user );
531 if ( count( $permErrors ) ) {
532 # Only show the first error
533 $this->showForm( $permErrors );
538 $reason = $this->msg( 'delete_and_move_reason', $ot )->inContentLanguage()->text();
540 // Delete an associated image if there is
541 if ( $nt->getNamespace() == NS_FILE
) {
542 $file = wfLocalFile( $nt );
543 $file->load( File
::READ_LATEST
);
544 if ( $file->exists() ) {
545 $file->delete( $reason, false, $user );
549 $error = ''; // passed by ref
550 $page = WikiPage
::factory( $nt );
551 $deleteStatus = $page->doDeleteArticleReal( $reason, false, 0, true, $error, $user );
552 if ( !$deleteStatus->isGood() ) {
553 $this->showForm( $deleteStatus->getErrorsArray() );
559 $handler = ContentHandler
::getForTitle( $ot );
561 if ( !$handler->supportsRedirects() ) {
562 $createRedirect = false;
563 } elseif ( $user->isAllowed( 'suppressredirect' ) ) {
564 $createRedirect = $this->leaveRedirect
;
566 $createRedirect = true;
569 # Do the actual move.
570 $mp = new MovePage( $ot, $nt );
571 $valid = $mp->isValidMove();
572 if ( !$valid->isOK() ) {
573 $this->showForm( $valid->getErrorsArray() );
577 $permStatus = $mp->checkPermissions( $user, $this->reason
);
578 if ( !$permStatus->isOK() ) {
579 $this->showForm( $permStatus->getErrorsArray() );
583 $status = $mp->move( $user, $this->reason
, $createRedirect );
584 if ( !$status->isOK() ) {
585 $this->showForm( $status->getErrorsArray() );
589 if ( $this->getConfig()->get( 'FixDoubleRedirects' ) && $this->fixRedirects
) {
590 DoubleRedirectJob
::fixRedirects( 'move', $ot, $nt );
593 $out = $this->getOutput();
594 $out->setPageTitle( $this->msg( 'pagemovedsub' ) );
596 $oldLink = Linker
::link(
599 array( 'id' => 'movepage-oldlink' ),
600 array( 'redirect' => 'no' )
602 $newLink = Linker
::linkKnown(
605 array( 'id' => 'movepage-newlink' )
607 $oldText = $ot->getPrefixedText();
608 $newText = $nt->getPrefixedText();
610 if ( $ot->exists() ) {
611 //NOTE: we assume that if the old title exists, it's because it was re-created as
612 // a redirect to the new title. This is not safe, but what we did before was
613 // even worse: we just determined whether a redirect should have been created,
614 // and reported that it was created if it should have, without any checks.
615 // Also note that isRedirect() is unreliable because of bug 37209.
616 $msgName = 'movepage-moved-redirect';
618 $msgName = 'movepage-moved-noredirect';
621 $out->addHTML( $this->msg( 'movepage-moved' )->rawParams( $oldLink,
622 $newLink )->params( $oldText, $newText )->parseAsBlock() );
623 $out->addWikiMsg( $msgName );
625 Hooks
::run( 'SpecialMovepageAfterMove', array( &$this, &$ot, &$nt ) );
627 # Now we move extra pages we've been asked to move: subpages and talk
628 # pages. First, if the old page or the new page is a talk page, we
629 # can't move any talk pages: cancel that.
630 if ( $ot->isTalkPage() ||
$nt->isTalkPage() ) {
631 $this->moveTalk
= false;
634 if ( count( $ot->getUserPermissionsErrors( 'move-subpages', $user ) ) ) {
635 $this->moveSubpages
= false;
638 # Next make a list of id's. This might be marginally less efficient
639 # than a more direct method, but this is not a highly performance-cri-
640 # tical code path and readable code is more important here.
642 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
643 # 4 might get confused. If so, consider rewriting as a UNION.
645 # If the target namespace doesn't allow subpages, moving with subpages
646 # would mean that you couldn't move them back in one operation, which
648 # @todo FIXME: A specific error message should be given in this case.
650 // @todo FIXME: Use Title::moveSubpages() here
651 $dbr = wfGetDB( DB_MASTER
);
652 if ( $this->moveSubpages
&& (
653 MWNamespace
::hasSubpages( $nt->getNamespace() ) ||
(
655 && MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() )
659 'page_title' . $dbr->buildLike( $ot->getDBkey() . '/', $dbr->anyString() )
660 . ' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
662 $conds['page_namespace'] = array();
663 if ( MWNamespace
::hasSubpages( $nt->getNamespace() ) ) {
664 $conds['page_namespace'][] = $ot->getNamespace();
666 if ( $this->moveTalk
&&
667 MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() )
669 $conds['page_namespace'][] = $ot->getTalkPage()->getNamespace();
671 } elseif ( $this->moveTalk
) {
673 'page_namespace' => $ot->getTalkPage()->getNamespace(),
674 'page_title' => $ot->getDBkey()
681 $extraPages = array();
682 if ( !is_null( $conds ) ) {
683 $extraPages = TitleArray
::newFromResult(
684 $dbr->select( 'page',
685 array( 'page_id', 'page_namespace', 'page_title' ),
692 $extraOutput = array();
694 foreach ( $extraPages as $oldSubpage ) {
695 if ( $ot->equals( $oldSubpage ) ||
$nt->equals( $oldSubpage ) ) {
696 # Already did this one.
700 $newPageName = preg_replace(
701 '#^' . preg_quote( $ot->getDBkey(), '#' ) . '#',
702 StringUtils
::escapeRegexReplacement( $nt->getDBkey() ), # bug 21234
703 $oldSubpage->getDBkey()
706 if ( $oldSubpage->isSubpage() && ( $ot->isTalkPage() xor $nt->isTalkPage() ) ) {
707 // Moving a subpage from a subject namespace to a talk namespace or vice-versa
708 $newNs = $nt->getNamespace();
709 } elseif ( $oldSubpage->isTalkPage() ) {
710 $newNs = $nt->getTalkPage()->getNamespace();
712 $newNs = $nt->getSubjectPage()->getNamespace();
715 # Bug 14385: we need makeTitleSafe because the new page names may
716 # be longer than 255 characters.
717 $newSubpage = Title
::makeTitleSafe( $newNs, $newPageName );
718 if ( !$newSubpage ) {
719 $oldLink = Linker
::linkKnown( $oldSubpage );
720 $extraOutput[] = $this->msg( 'movepage-page-unmoved' )->rawParams( $oldLink )
721 ->params( Title
::makeName( $newNs, $newPageName ) )->escaped();
725 # This was copy-pasted from Renameuser, bleh.
726 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
727 $link = Linker
::linkKnown( $newSubpage );
728 $extraOutput[] = $this->msg( 'movepage-page-exists' )->rawParams( $link )->escaped();
730 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason
, $createRedirect );
732 if ( $success === true ) {
733 if ( $this->fixRedirects
) {
734 DoubleRedirectJob
::fixRedirects( 'move', $oldSubpage, $newSubpage );
736 $oldLink = Linker
::link(
740 array( 'redirect' => 'no' )
743 $newLink = Linker
::linkKnown( $newSubpage );
744 $extraOutput[] = $this->msg( 'movepage-page-moved' )
745 ->rawParams( $oldLink, $newLink )->escaped();
748 $maximumMovedPages = $this->getConfig()->get( 'MaximumMovedPages' );
749 if ( $count >= $maximumMovedPages ) {
750 $extraOutput[] = $this->msg( 'movepage-max-pages' )
751 ->numParams( $maximumMovedPages )->escaped();
755 $oldLink = Linker
::linkKnown( $oldSubpage );
756 $newLink = Linker
::link( $newSubpage );
757 $extraOutput[] = $this->msg( 'movepage-page-unmoved' )
758 ->rawParams( $oldLink, $newLink )->escaped();
763 if ( $extraOutput !== array() ) {
764 $out->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
767 # Deal with watches (we don't watch subpages)
768 WatchAction
::doWatchOrUnwatch( $this->watch
, $ot, $user );
769 WatchAction
::doWatchOrUnwatch( $this->watch
, $nt, $user );
772 function showLogFragment( $title ) {
773 $moveLogPage = new LogPage( 'move' );
774 $out = $this->getOutput();
775 $out->addHTML( Xml
::element( 'h2', null, $moveLogPage->getName()->text() ) );
776 LogEventsList
::showLogExtract( $out, 'move', $title );
779 function showSubpages( $title ) {
780 if ( !MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
784 $subpages = $title->getSubpages();
785 $count = $subpages instanceof TitleArray ?
$subpages->count() : 0;
787 $out = $this->getOutput();
788 $out->wrapWikiMsg( '== $1 ==', array( 'movesubpage', $count ) );
792 $out->addWikiMsg( 'movenosubpage' );
797 $out->addWikiMsg( 'movesubpagetext', $this->getLanguage()->formatNum( $count ) );
798 $out->addHTML( "<ul>\n" );
800 foreach ( $subpages as $subpage ) {
801 $link = Linker
::link( $subpage );
802 $out->addHTML( "<li>$link</li>\n" );
804 $out->addHTML( "</ul>\n" );
807 protected function getGroupName() {