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
{
34 var $oldTitle, $newTitle;
38 var $moveTalk, $deleteAndMove, $moveSubpages, $fixRedirects, $leaveRedirect, $moveOverShared;
40 private $watch = false;
42 public function __construct() {
43 parent
::__construct( 'Movepage' );
46 public function execute( $par ) {
47 $this->checkReadOnly();
50 $this->outputHeader();
52 $request = $this->getRequest();
53 $target = !is_null( $par ) ?
$par : $request->getVal( 'target' );
55 // Yes, the use of getVal() and getText() is wanted, see bug 20365
57 $oldTitleText = $request->getVal( 'wpOldTitle', $target );
58 $this->oldTitle
= Title
::newFromText( $oldTitleText );
60 if ( is_null( $this->oldTitle
) ) {
61 throw new ErrorPageError( 'notargettitle', 'notargettext' );
63 if ( !$this->oldTitle
->exists() ) {
64 throw new ErrorPageError( 'nopagetitle', 'nopagetext' );
67 $newTitleTextMain = $request->getText( 'wpNewTitleMain' );
68 $newTitleTextNs = $request->getInt( 'wpNewTitleNs', $this->oldTitle
->getNamespace() );
69 // Backwards compatibility for forms submitting here from other sources
70 // which is more common than it should be..
71 $newTitleText_bc = $request->getText( 'wpNewTitle' );
72 $this->newTitle
= strlen( $newTitleText_bc ) > 0
73 ? Title
::newFromText( $newTitleText_bc )
74 : Title
::makeTitleSafe( $newTitleTextNs, $newTitleTextMain );
76 $user = $this->getUser();
79 $permErrors = $this->oldTitle
->getUserPermissionsErrors( 'move', $user );
80 if ( count( $permErrors ) ) {
81 // Auto-block user's IP if the account was "hard" blocked
82 $user->spreadAnyEditBlock();
83 throw new PermissionsError( 'move', $permErrors );
86 $def = !$request->wasPosted();
88 $this->reason
= $request->getText( 'wpReason' );
89 $this->moveTalk
= $request->getBool( 'wpMovetalk', $def );
90 $this->fixRedirects
= $request->getBool( 'wpFixRedirects', $def );
91 $this->leaveRedirect
= $request->getBool( 'wpLeaveRedirect', $def );
92 $this->moveSubpages
= $request->getBool( 'wpMovesubpages', false );
93 $this->deleteAndMove
= $request->getBool( 'wpDeleteAndMove' ) && $request->getBool( 'wpConfirm' );
94 $this->moveOverShared
= $request->getBool( 'wpMoveOverSharedFile', false );
95 $this->watch
= $request->getCheck( 'wpWatch' ) && $user->isLoggedIn();
97 if ( 'submit' == $request->getVal( 'action' ) && $request->wasPosted()
98 && $user->matchEditToken( $request->getVal( 'wpEditToken' ) )
102 $this->showForm( array() );
109 * @param array $err error messages. Each item is an error message.
110 * It may either be a string message name or array message name and
111 * parameters, like the second argument to OutputPage::wrapWikiMsg().
113 function showForm( $err ) {
114 global $wgContLang, $wgFixDoubleRedirects, $wgMaximumMovedPages;
116 $this->getSkin()->setRelevantTitle( $this->oldTitle
);
118 $oldTitleLink = Linker
::link( $this->oldTitle
);
120 $out = $this->getOutput();
121 $out->setPageTitle( $this->msg( 'move-page', $this->oldTitle
->getPrefixedText() ) );
122 $out->addModules( 'mediawiki.special.movePage' );
124 $newTitle = $this->newTitle
;
127 # Show the current title as a default
128 # when the form is first opened.
129 $newTitle = $this->oldTitle
;
130 } elseif ( !count( $err ) ) {
131 # If a title was supplied, probably from the move log revert
132 # link, check for validity. We can then show some diagnostic
133 # information and save a click.
134 $newerr = $this->oldTitle
->isValidMoveOperation( $newTitle );
135 if ( is_array( $newerr ) ) {
140 $user = $this->getUser();
142 if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'articleexists'
143 && $newTitle->quickUserCan( 'delete', $user )
145 $out->addWikiMsg( 'delete_and_move_text', $newTitle->getPrefixedText() );
146 $movepagebtn = $this->msg( 'delete_and_move' )->text();
147 $submitVar = 'wpDeleteAndMove';
151 <td class='mw-input'>" .
153 $this->msg( 'delete_and_move_confirm' )->text(),
161 if ( $this->oldTitle
->getNamespace() == NS_USER
&& !$this->oldTitle
->isSubpage() ) {
163 "<div class=\"error mw-moveuserpage-warning\">\n$1\n</div>",
164 'moveuserpage-warning'
166 } elseif ( $this->oldTitle
->getNamespace() == NS_CATEGORY
) {
168 "<div class=\"error mw-movecategorypage-warning\">\n$1\n</div>",
169 'movecategorypage-warning'
173 $out->addWikiMsg( $wgFixDoubleRedirects ?
175 'movepagetext-noredirectfixer'
177 $movepagebtn = $this->msg( 'movepagebtn' )->text();
178 $submitVar = 'wpMove';
182 if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'file-exists-sharedrepo'
183 && $user->isAllowed( 'reupload-shared' )
185 $out->addWikiMsg( 'move-over-sharedrepo', $newTitle->getPrefixedText() );
186 $submitVar = 'wpMoveOverSharedFile';
190 $oldTalk = $this->oldTitle
->getTalkPage();
191 $oldTitleSubpages = $this->oldTitle
->hasSubpages();
192 $oldTitleTalkSubpages = $this->oldTitle
->getTalkPage()->hasSubpages();
194 $canMoveSubpage = ( $oldTitleSubpages ||
$oldTitleTalkSubpages ) &&
195 !count( $this->oldTitle
->getUserPermissionsErrors( 'move-subpages', $user ) );
197 # We also want to be able to move assoc. subpage talk-pages even if base page
198 # has no associated talk page, so || with $oldTitleTalkSubpages.
199 $considerTalk = !$this->oldTitle
->isTalkPage() &&
201 ||
( $oldTitleTalkSubpages && $canMoveSubpage ) );
203 $dbr = wfGetDB( DB_SLAVE
);
204 if ( $wgFixDoubleRedirects ) {
205 $hasRedirects = $dbr->selectField( 'redirect', '1',
207 'rd_namespace' => $this->oldTitle
->getNamespace(),
208 'rd_title' => $this->oldTitle
->getDBkey(),
211 $hasRedirects = false;
214 if ( $considerTalk ) {
215 $out->addWikiMsg( 'movepagetalktext' );
218 if ( count( $err ) ) {
219 $out->addHTML( "<div class='error'>\n" );
220 $action_desc = $this->msg( 'action-move' )->plain();
221 $out->addWikiMsg( 'permissionserrorstext-withaction', count( $err ), $action_desc );
223 if ( count( $err ) == 1 ) {
225 $errMsgName = array_shift( $errMsg );
227 if ( $errMsgName == 'hookaborted' ) {
228 $out->addHTML( "<p>{$errMsg[0]}</p>\n" );
230 $out->addWikiMsgArray( $errMsgName, $errMsg );
235 foreach ( $err as $errMsg ) {
236 if ( $errMsg[0] == 'hookaborted' ) {
237 $errStr[] = $errMsg[1];
239 $errMsgName = array_shift( $errMsg );
240 $errStr[] = $this->msg( $errMsgName, $errMsg )->parse();
244 $out->addHTML( '<ul><li>' . implode( "</li>\n<li>", $errStr ) . "</li></ul>\n" );
246 $out->addHTML( "</div>\n" );
249 if ( $this->oldTitle
->isProtected( 'move' ) ) {
250 # Is the title semi-protected?
251 if ( $this->oldTitle
->isSemiProtected( 'move' ) ) {
252 $noticeMsg = 'semiprotectedpagemovewarning';
253 $classes[] = 'mw-textarea-sprotected';
255 # Then it must be protected based on static groups (regular)
256 $noticeMsg = 'protectedpagemovewarning';
257 $classes[] = 'mw-textarea-protected';
259 $out->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" );
260 $out->addWikiMsg( $noticeMsg );
261 LogEventsList
::showLogExtract(
268 $out->addHTML( "</div>\n" );
271 // Byte limit (not string length limit) for wpReason and wpNewTitleMain
272 // is enforced in the mediawiki.special.movePage module
274 $immovableNamespaces = array();
276 foreach ( array_keys( $this->getLanguage()->getNamespaces() ) as $nsId ) {
277 if ( !MWNamespace
::isMovable( $nsId ) ) {
278 $immovableNamespaces[] = $nsId;
282 $handler = ContentHandler
::getForTitle( $this->oldTitle
);
289 'action' => $this->getPageTitle()->getLocalURL( 'action=submit' ),
293 Xml
::openElement( 'fieldset' ) .
294 Xml
::element( 'legend', null, $this->msg( 'move-page-legend' )->text() ) .
295 Xml
::openElement( 'table', array( 'id' => 'mw-movepage-table' ) ) .
297 <td class='mw-label'>" .
298 $this->msg( 'movearticle' )->escaped() .
300 <td class='mw-input'>
301 <strong>{$oldTitleLink}</strong>
305 <td class='mw-label'>" .
306 Xml
::label( $this->msg( 'newtitle' )->text(), 'wpNewTitleMain' ) .
308 <td class='mw-input'>" .
309 Html
::namespaceSelector(
311 'selected' => $newTitle->getNamespace(),
312 'exclude' => $immovableNamespaces
314 array( 'name' => 'wpNewTitleNs', 'id' => 'wpNewTitleNs' )
319 $wgContLang->recodeForEdit( $newTitle->getText() ),
322 'id' => 'wpNewTitleMain',
326 Html
::hidden( 'wpOldTitle', $this->oldTitle
->getPrefixedText() ) .
330 <td class='mw-label'>" .
331 Xml
::label( $this->msg( 'movereason' )->text(), 'wpReason' ) .
333 <td class='mw-input'>" .
334 Xml
::input( 'wpReason', 60, $this->reason
, array(
343 if ( $considerTalk ) {
347 <td class='mw-input'>" .
349 $this->msg( 'movetalk' )->text(),
359 if ( $user->isAllowed( 'suppressredirect' ) ) {
360 if ( $handler->supportsRedirects() ) {
361 $isChecked = $this->leaveRedirect
;
366 'disabled' => 'disabled'
372 <td class='mw-input' >" .
374 $this->msg( 'move-leave-redirect' )->text(),
385 if ( $hasRedirects ) {
389 <td class='mw-input' >" .
391 $this->msg( 'fix-double-redirects' )->text(),
401 if ( $canMoveSubpage ) {
405 <td class=\"mw-input\">" .
408 # Don't check the box if we only have talk subpages to
409 # move and we aren't moving the talk page.
410 $this->moveSubpages
&& ( $this->oldTitle
->hasSubpages() ||
$this->moveTalk
),
411 array( 'id' => 'wpMovesubpages' )
413 Xml
::tags( 'label', array( 'for' => 'wpMovesubpages' ),
415 ( $this->oldTitle
->hasSubpages()
417 : 'move-talk-subpages' )
418 )->numParams( $wgMaximumMovedPages )->params( $wgMaximumMovedPages )->parse()
425 $watchChecked = $user->isLoggedIn() && ( $this->watch ||
$user->getBoolOption( 'watchmoves' )
426 ||
$user->isWatched( $this->oldTitle
) );
427 # Don't allow watching if user is not logged in
428 if ( $user->isLoggedIn() ) {
432 <td class='mw-input'>" .
434 $this->msg( 'move-watch' )->text(),
447 <td class='mw-submit'>" .
448 Xml
::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
451 Xml
::closeElement( 'table' ) .
452 Html
::hidden( 'wpEditToken', $user->getEditToken() ) .
453 Xml
::closeElement( 'fieldset' ) .
454 Xml
::closeElement( 'form' ) .
458 $this->showLogFragment( $this->oldTitle
);
459 $this->showSubpages( $this->oldTitle
);
462 function doSubmit() {
463 global $wgMaximumMovedPages, $wgFixDoubleRedirects;
465 $user = $this->getUser();
467 if ( $user->pingLimiter( 'move' ) ) {
468 throw new ThrottledError
;
471 $ot = $this->oldTitle
;
472 $nt = $this->newTitle
;
474 # don't allow moving to pages with # in
475 if ( !$nt ||
$nt->hasFragment() ) {
476 $this->showForm( array( array( 'badtitletext' ) ) );
481 # Show a warning if the target file exists on a shared repo
482 if ( $nt->getNamespace() == NS_FILE
483 && !( $this->moveOverShared
&& $user->isAllowed( 'reupload-shared' ) )
484 && !RepoGroup
::singleton()->getLocalRepo()->findFile( $nt )
487 $this->showForm( array( array( 'file-exists-sharedrepo' ) ) );
492 # Delete to make way if requested
493 if ( $this->deleteAndMove
) {
494 $permErrors = $nt->getUserPermissionsErrors( 'delete', $user );
495 if ( count( $permErrors ) ) {
496 # Only show the first error
497 $this->showForm( $permErrors );
502 $reason = $this->msg( 'delete_and_move_reason', $ot )->inContentLanguage()->text();
504 // Delete an associated image if there is
505 if ( $nt->getNamespace() == NS_FILE
) {
506 $file = wfLocalFile( $nt );
507 if ( $file->exists() ) {
508 $file->delete( $reason, false );
512 $error = ''; // passed by ref
513 $page = WikiPage
::factory( $nt );
514 $deleteStatus = $page->doDeleteArticleReal( $reason, false, 0, true, $error, $user );
515 if ( !$deleteStatus->isGood() ) {
516 $this->showForm( $deleteStatus->getErrorsArray() );
522 $handler = ContentHandler
::getForTitle( $ot );
524 if ( !$handler->supportsRedirects() ) {
525 $createRedirect = false;
526 } elseif ( $user->isAllowed( 'suppressredirect' ) ) {
527 $createRedirect = $this->leaveRedirect
;
529 $createRedirect = true;
532 # Do the actual move.
533 $error = $ot->moveTo( $nt, true, $this->reason
, $createRedirect );
534 if ( $error !== true ) {
535 $this->showForm( $error );
540 if ( $wgFixDoubleRedirects && $this->fixRedirects
) {
541 DoubleRedirectJob
::fixRedirects( 'move', $ot, $nt );
544 $out = $this->getOutput();
545 $out->setPageTitle( $this->msg( 'pagemovedsub' ) );
547 $oldLink = Linker
::link(
551 array( 'redirect' => 'no' )
553 $newLink = Linker
::linkKnown( $nt );
554 $oldText = $ot->getPrefixedText();
555 $newText = $nt->getPrefixedText();
557 if ( $ot->exists() ) {
558 //NOTE: we assume that if the old title exists, it's because it was re-created as
559 // a redirect to the new title. This is not safe, but what we did before was
560 // even worse: we just determined whether a redirect should have been created,
561 // and reported that it was created if it should have, without any checks.
562 // Also note that isRedirect() is unreliable because of bug 37209.
563 $msgName = 'movepage-moved-redirect';
565 $msgName = 'movepage-moved-noredirect';
568 $out->addHTML( $this->msg( 'movepage-moved' )->rawParams( $oldLink,
569 $newLink )->params( $oldText, $newText )->parseAsBlock() );
570 $out->addWikiMsg( $msgName );
572 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this, &$ot, &$nt ) );
574 # Now we move extra pages we've been asked to move: subpages and talk
575 # pages. First, if the old page or the new page is a talk page, we
576 # can't move any talk pages: cancel that.
577 if ( $ot->isTalkPage() ||
$nt->isTalkPage() ) {
578 $this->moveTalk
= false;
581 if ( count( $ot->getUserPermissionsErrors( 'move-subpages', $user ) ) ) {
582 $this->moveSubpages
= false;
585 # Next make a list of id's. This might be marginally less efficient
586 # than a more direct method, but this is not a highly performance-cri-
587 # tical code path and readable code is more important here.
589 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
590 # 4 might get confused. If so, consider rewriting as a UNION.
592 # If the target namespace doesn't allow subpages, moving with subpages
593 # would mean that you couldn't move them back in one operation, which
595 # @todo FIXME: A specific error message should be given in this case.
597 // @todo FIXME: Use Title::moveSubpages() here
598 $dbr = wfGetDB( DB_MASTER
);
599 if ( $this->moveSubpages
&& (
600 MWNamespace
::hasSubpages( $nt->getNamespace() ) ||
(
602 && MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() )
606 'page_title' . $dbr->buildLike( $ot->getDBkey() . '/', $dbr->anyString() )
607 . ' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
609 $conds['page_namespace'] = array();
610 if ( MWNamespace
::hasSubpages( $nt->getNamespace() ) ) {
611 $conds['page_namespace'][] = $ot->getNamespace();
613 if ( $this->moveTalk
&&
614 MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() )
616 $conds['page_namespace'][] = $ot->getTalkPage()->getNamespace();
618 } elseif ( $this->moveTalk
) {
620 'page_namespace' => $ot->getTalkPage()->getNamespace(),
621 'page_title' => $ot->getDBkey()
628 $extraPages = array();
629 if ( !is_null( $conds ) ) {
630 $extraPages = TitleArray
::newFromResult(
631 $dbr->select( 'page',
632 array( 'page_id', 'page_namespace', 'page_title' ),
639 $extraOutput = array();
641 foreach ( $extraPages as $oldSubpage ) {
642 if ( $ot->equals( $oldSubpage ) ||
$nt->equals( $oldSubpage ) ) {
643 # Already did this one.
647 $newPageName = preg_replace(
648 '#^' . preg_quote( $ot->getDBkey(), '#' ) . '#',
649 StringUtils
::escapeRegexReplacement( $nt->getDBkey() ), # bug 21234
650 $oldSubpage->getDBkey()
653 if ( $oldSubpage->isTalkPage() ) {
654 $newNs = $nt->getTalkPage()->getNamespace();
656 $newNs = $nt->getSubjectPage()->getNamespace();
659 # Bug 14385: we need makeTitleSafe because the new page names may
660 # be longer than 255 characters.
661 $newSubpage = Title
::makeTitleSafe( $newNs, $newPageName );
662 if ( !$newSubpage ) {
663 $oldLink = Linker
::linkKnown( $oldSubpage );
664 $extraOutput[] = $this->msg( 'movepage-page-unmoved' )->rawParams( $oldLink )
665 ->params( Title
::makeName( $newNs, $newPageName ) )->escaped();
669 # This was copy-pasted from Renameuser, bleh.
670 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
671 $link = Linker
::linkKnown( $newSubpage );
672 $extraOutput[] = $this->msg( 'movepage-page-exists' )->rawParams( $link )->escaped();
674 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason
, $createRedirect );
676 if ( $success === true ) {
677 if ( $this->fixRedirects
) {
678 DoubleRedirectJob
::fixRedirects( 'move', $oldSubpage, $newSubpage );
680 $oldLink = Linker
::link(
684 array( 'redirect' => 'no' )
687 $newLink = Linker
::linkKnown( $newSubpage );
688 $extraOutput[] = $this->msg( 'movepage-page-moved' )->rawParams( $oldLink, $newLink )->escaped();
691 if ( $count >= $wgMaximumMovedPages ) {
692 $extraOutput[] = $this->msg( 'movepage-max-pages' )->numParams( $wgMaximumMovedPages )->escaped();
696 $oldLink = Linker
::linkKnown( $oldSubpage );
697 $newLink = Linker
::link( $newSubpage );
698 $extraOutput[] = $this->msg( 'movepage-page-unmoved' )->rawParams( $oldLink, $newLink )->escaped();
703 if ( $extraOutput !== array() ) {
704 $out->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
707 # Deal with watches (we don't watch subpages)
708 WatchAction
::doWatchOrUnwatch( $this->watch
, $ot, $user );
709 WatchAction
::doWatchOrUnwatch( $this->watch
, $nt, $user );
712 function showLogFragment( $title ) {
713 $moveLogPage = new LogPage( 'move' );
714 $out = $this->getOutput();
715 $out->addHTML( Xml
::element( 'h2', null, $moveLogPage->getName()->text() ) );
716 LogEventsList
::showLogExtract( $out, 'move', $title );
719 function showSubpages( $title ) {
720 if ( !MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
724 $subpages = $title->getSubpages();
725 $count = $subpages instanceof TitleArray ?
$subpages->count() : 0;
727 $out = $this->getOutput();
728 $out->wrapWikiMsg( '== $1 ==', array( 'movesubpage', $count ) );
732 $out->addWikiMsg( 'movenosubpage' );
737 $out->addWikiMsg( 'movesubpagetext', $this->getLanguage()->formatNum( $count ) );
738 $out->addHTML( "<ul>\n" );
740 foreach ( $subpages as $subpage ) {
741 $link = Linker
::link( $subpage );
742 $out->addHTML( "<li>$link</li>\n" );
744 $out->addHTML( "</ul>\n" );
747 protected function getGroupName() {