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; # Objects
35 var $reason; # Text input
36 var $moveTalk, $deleteAndMove, $moveSubpages, $fixRedirects, $leaveRedirect, $moveOverShared; # Checks
38 private $watch = false;
40 public function __construct() {
41 parent
::__construct( 'Movepage' );
44 public function execute( $par ) {
45 $this->checkReadOnly();
48 $this->outputHeader();
50 $request = $this->getRequest();
51 $target = !is_null( $par ) ?
$par : $request->getVal( 'target' );
53 // Yes, the use of getVal() and getText() is wanted, see bug 20365
54 $oldTitleText = $request->getVal( 'wpOldTitle', $target );
55 $newTitleText = $request->getText( 'wpNewTitle' );
57 $this->oldTitle
= Title
::newFromText( $oldTitleText );
58 $this->newTitle
= Title
::newFromText( $newTitleText );
60 if( is_null( $this->oldTitle
) ) {
61 throw new ErrorPageError( 'notargettitle', 'notargettext' );
63 if( !$this->oldTitle
->exists() ) {
64 throw new ErrorPageError( 'nopagetitle', 'nopagetext' );
67 $user = $this->getUser();
70 $permErrors = $this->oldTitle
->getUserPermissionsErrors( 'move', $user );
71 if( !empty( $permErrors ) ) {
72 // Auto-block user's IP if the account was "hard" blocked
73 $user->spreadAnyEditBlock();
74 throw new PermissionsError( 'move', $permErrors );
77 $def = !$request->wasPosted();
79 $this->reason
= $request->getText( 'wpReason' );
80 $this->moveTalk
= $request->getBool( 'wpMovetalk', $def );
81 $this->fixRedirects
= $request->getBool( 'wpFixRedirects', $def );
82 $this->leaveRedirect
= $request->getBool( 'wpLeaveRedirect', $def );
83 $this->moveSubpages
= $request->getBool( 'wpMovesubpages', false );
84 $this->deleteAndMove
= $request->getBool( 'wpDeleteAndMove' ) && $request->getBool( 'wpConfirm' );
85 $this->moveOverShared
= $request->getBool( 'wpMoveOverSharedFile', false );
86 $this->watch
= $request->getCheck( 'wpWatch' ) && $user->isLoggedIn();
88 if ( 'submit' == $request->getVal( 'action' ) && $request->wasPosted()
89 && $user->matchEditToken( $request->getVal( 'wpEditToken' ) ) ) {
92 $this->showForm( '' );
99 * @param $err Mixed: error message. May either be a string message name or
100 * array message name and parameters, like the second argument to
101 * OutputPage::wrapWikiMsg().
103 function showForm( $err ) {
104 global $wgContLang, $wgFixDoubleRedirects, $wgMaximumMovedPages;
106 $this->getSkin()->setRelevantTitle( $this->oldTitle
);
108 $oldTitleLink = Linker
::link( $this->oldTitle
);
110 $out = $this->getOutput();
111 $out->setPageTitle( $this->msg( 'move-page', $this->oldTitle
->getPrefixedText() ) );
112 $out->addModules( 'mediawiki.special.movePage' );
114 $newTitle = $this->newTitle
;
117 # Show the current title as a default
118 # when the form is first opened.
119 $newTitle = $this->oldTitle
;
123 # If a title was supplied, probably from the move log revert
124 # link, check for validity. We can then show some diagnostic
125 # information and save a click.
126 $newerr = $this->oldTitle
->isValidMoveOperation( $newTitle );
133 $user = $this->getUser();
135 if ( !empty($err) && $err[0] == 'articleexists' && $user->isAllowed( 'delete' ) ) {
136 $out->addWikiMsg( 'delete_and_move_text', $newTitle->getPrefixedText() );
137 $movepagebtn = wfMsg( 'delete_and_move' );
138 $submitVar = 'wpDeleteAndMove';
142 <td class='mw-input'>" .
143 Xml
::checkLabel( wfMsg( 'delete_and_move_confirm' ), 'wpConfirm', 'wpConfirm' ) .
148 if ($this->oldTitle
->getNamespace() == NS_USER
&& !$this->oldTitle
->isSubpage() ) {
149 $out->wrapWikiMsg( "<div class=\"error mw-moveuserpage-warning\">\n$1\n</div>", 'moveuserpage-warning' );
151 $out->addWikiMsg( $wgFixDoubleRedirects ?
'movepagetext' :
152 'movepagetext-noredirectfixer' );
153 $movepagebtn = wfMsg( 'movepagebtn' );
154 $submitVar = 'wpMove';
158 if ( !empty($err) && $err[0] == 'file-exists-sharedrepo' && $user->isAllowed( 'reupload-shared' ) ) {
159 $out->addWikiMsg( 'move-over-sharedrepo', $newTitle->getPrefixedText() );
160 $submitVar = 'wpMoveOverSharedFile';
164 $oldTalk = $this->oldTitle
->getTalkPage();
165 $oldTitleSubpages = $this->oldTitle
->hasSubpages();
166 $oldTitleTalkSubpages = $this->oldTitle
->getTalkPage()->hasSubpages();
168 $canMoveSubpage = ( $oldTitleSubpages ||
$oldTitleTalkSubpages ) &&
169 !count( $this->oldTitle
->getUserPermissionsErrors( 'move-subpages', $user ) );
171 # We also want to be able to move assoc. subpage talk-pages even if base page
172 # has no associated talk page, so || with $oldTitleTalkSubpages.
173 $considerTalk = !$this->oldTitle
->isTalkPage() &&
175 ||
( $oldTitleTalkSubpages && $canMoveSubpage ) );
177 $dbr = wfGetDB( DB_SLAVE
);
178 if ( $wgFixDoubleRedirects ) {
179 $hasRedirects = $dbr->selectField( 'redirect', '1',
181 'rd_namespace' => $this->oldTitle
->getNamespace(),
182 'rd_title' => $this->oldTitle
->getDBkey(),
185 $hasRedirects = false;
188 if ( $considerTalk ) {
189 $out->addWikiMsg( 'movepagetalktext' );
192 $token = htmlspecialchars( $user->getEditToken() );
194 if ( !empty($err) ) {
195 $out->addSubtitle( $this->msg( 'formerror' ) );
196 if( $err[0] == 'hookaborted' ) {
198 $errMsg = "<p><strong class=\"error\">$hookErr</strong></p>\n";
199 $out->addHTML( $errMsg );
201 $out->wrapWikiMsg( "<p><strong class=\"error\">\n$1\n</strong></p>", $err );
205 if ( $this->oldTitle
->isProtected( 'move' ) ) {
206 # Is the title semi-protected?
207 if ( $this->oldTitle
->isSemiProtected( 'move' ) ) {
208 $noticeMsg = 'semiprotectedpagemovewarning';
209 $classes[] = 'mw-textarea-sprotected';
211 # Then it must be protected based on static groups (regular)
212 $noticeMsg = 'protectedpagemovewarning';
213 $classes[] = 'mw-textarea-protected';
215 $out->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" );
216 $out->addWikiMsg( $noticeMsg );
217 LogEventsList
::showLogExtract( $out, 'protect', $this->oldTitle
, '', array( 'lim' => 1 ) );
218 $out->addHTML( "</div>\n" );
222 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL( 'action=submit' ), 'id' => 'movepage' ) ) .
223 Xml
::openElement( 'fieldset' ) .
224 Xml
::element( 'legend', null, wfMsg( 'move-page-legend' ) ) .
225 Xml
::openElement( 'table', array( 'border' => '0', 'id' => 'mw-movepage-table' ) ) .
227 <td class='mw-label'>" .
228 wfMsgHtml( 'movearticle' ) .
230 <td class='mw-input'>
231 <strong>{$oldTitleLink}</strong>
235 <td class='mw-label'>" .
236 Xml
::label( wfMsg( 'newtitle' ), 'wpNewTitle' ) .
238 <td class='mw-input'>" .
239 Xml
::input( 'wpNewTitle', 60, $wgContLang->recodeForEdit( $newTitle->getPrefixedText() ), array( 'type' => 'text', 'id' => 'wpNewTitle' ) ) .
240 Html
::hidden( 'wpOldTitle', $this->oldTitle
->getPrefixedText() ) .
244 <td class='mw-label'>" .
245 Xml
::label( wfMsg( 'movereason' ), 'wpReason' ) .
247 <td class='mw-input'>" .
248 Html
::element( 'textarea', array( 'name' => 'wpReason', 'id' => 'wpReason', 'cols' => 60, 'rows' => 2,
249 'maxlength' => 200 ), $this->reason
) . // maxlength byte limit is enforce in mediawiki.special.movePage.js
254 if( $considerTalk ) {
258 <td class='mw-input'>" .
259 Xml
::checkLabel( wfMsg( 'movetalk' ), 'wpMovetalk', 'wpMovetalk', $this->moveTalk
) .
265 if ( $user->isAllowed( 'suppressredirect' ) ) {
269 <td class='mw-input' >" .
270 Xml
::checkLabel( wfMsg( 'move-leave-redirect' ), 'wpLeaveRedirect',
271 'wpLeaveRedirect', $this->leaveRedirect
) .
277 if ( $hasRedirects ) {
281 <td class='mw-input' >" .
282 Xml
::checkLabel( wfMsg( 'fix-double-redirects' ), 'wpFixRedirects',
283 'wpFixRedirects', $this->fixRedirects
) .
289 if( $canMoveSubpage ) {
293 <td class=\"mw-input\">" .
296 # Don't check the box if we only have talk subpages to
297 # move and we aren't moving the talk page.
298 $this->moveSubpages
&& ($this->oldTitle
->hasSubpages() ||
$this->moveTalk
),
299 array( 'id' => 'wpMovesubpages' )
301 Xml
::tags( 'label', array( 'for' => 'wpMovesubpages' ),
303 ( $this->oldTitle
->hasSubpages()
305 : 'move-talk-subpages' ),
306 array( 'parseinline' ),
307 $this->getLang()->formatNum( $wgMaximumMovedPages ),
308 # $2 to allow use of PLURAL in message.
317 $watchChecked = $user->isLoggedIn() && ($this->watch ||
$user->getBoolOption( 'watchmoves' )
318 ||
$this->oldTitle
->userIsWatching());
319 # Don't allow watching if user is not logged in
320 if( $user->isLoggedIn() ) {
324 <td class='mw-input'>" .
325 Xml
::checkLabel( wfMsg( 'move-watch' ), 'wpWatch', 'watch', $watchChecked ) .
334 <td class='mw-submit'>" .
335 Xml
::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
338 Xml
::closeElement( 'table' ) .
339 Html
::hidden( 'wpEditToken', $token ) .
340 Xml
::closeElement( 'fieldset' ) .
341 Xml
::closeElement( 'form' ) .
345 $this->showLogFragment( $this->oldTitle
);
346 $this->showSubpages( $this->oldTitle
);
350 function doSubmit() {
351 global $wgMaximumMovedPages, $wgFixDoubleRedirects, $wgDeleteRevisionsLimit;
353 $user = $this->getUser();
355 if ( $user->pingLimiter( 'move' ) ) {
356 throw new ThrottledError
;
359 $ot = $this->oldTitle
;
360 $nt = $this->newTitle
;
362 # Delete to make way if requested
363 if ( !count( $nt->getUserPermissionsErrors( 'delete', $user ) ) && $this->deleteAndMove
) {
364 $page = WikiPage
::factory( $nt );
366 # Disallow deletions of big articles
367 $bigHistory = $page->isBigDeletion();
368 if( $bigHistory && count( $nt->getUserPermissionsErrors( 'bigdelete', $user ) ) ) {
369 $this->showForm( array( 'delete-toobig', $this->getLang()->formatNum( $wgDeleteRevisionsLimit ) ) );
373 $reason = wfMessage( 'delete_and_move_reason', $ot )->inContentLanguage()->text();
375 // Delete an associated image if there is
376 if ( $nt->getNamespace() == NS_FILE
) {
377 $file = wfLocalFile( $nt );
378 if ( $file->exists() ) {
379 $file->delete( $reason, false );
383 $error = ''; // passed by ref
384 if ( !$page->doDeleteArticle( $reason, false, 0, true, $error, $user ) ) {
385 $this->showForm( array( 'cannotdelete', wfEscapeWikiText( $nt->getPrefixedText() ) ) );
391 # don't allow moving to pages with # in
392 if ( !$nt ||
$nt->getFragment() != '' ) {
393 $this->showForm( 'badtitletext' );
397 # Show a warning if the target file exists on a shared repo
398 if ( $nt->getNamespace() == NS_FILE
399 && !( $this->moveOverShared
&& $user->isAllowed( 'reupload-shared' ) )
400 && !RepoGroup
::singleton()->getLocalRepo()->findFile( $nt )
401 && wfFindFile( $nt ) )
403 $this->showForm( array('file-exists-sharedrepo') );
408 if ( $user->isAllowed( 'suppressredirect' ) ) {
409 $createRedirect = $this->leaveRedirect
;
411 $createRedirect = true;
414 # Do the actual move.
415 $error = $ot->moveTo( $nt, true, $this->reason
, $createRedirect );
416 if ( $error !== true ) {
417 # @todo FIXME: Show all the errors in a list, not just the first one
418 $this->showForm( reset( $error ) );
422 if ( $wgFixDoubleRedirects && $this->fixRedirects
) {
423 DoubleRedirectJob
::fixRedirects( 'move', $ot, $nt );
426 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this, &$ot, &$nt ) );
428 $out = $this->getOutput();
429 $out->setPagetitle( wfMsg( 'pagemovedsub' ) );
431 $oldLink = Linker
::link(
435 array( 'redirect' => 'no' )
437 $newLink = Linker
::linkKnown( $nt );
438 $oldText = $ot->getPrefixedText();
439 $newText = $nt->getPrefixedText();
441 $msgName = $createRedirect ?
'movepage-moved-redirect' : 'movepage-moved-noredirect';
442 $out->addHTML( wfMessage( 'movepage-moved' )->rawParams( $oldLink, $newLink, $oldText, $newText )->parseAsBlock() );
443 $out->addWikiMsg( $msgName );
445 # Now we move extra pages we've been asked to move: subpages and talk
446 # pages. First, if the old page or the new page is a talk page, we
447 # can't move any talk pages: cancel that.
448 if( $ot->isTalkPage() ||
$nt->isTalkPage() ) {
449 $this->moveTalk
= false;
452 if ( count( $ot->getUserPermissionsErrors( 'move-subpages', $user ) ) ) {
453 $this->moveSubpages
= false;
456 # Next make a list of id's. This might be marginally less efficient
457 # than a more direct method, but this is not a highly performance-cri-
458 # tical code path and readable code is more important here.
460 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
461 # 4 might get confused. If so, consider rewriting as a UNION.
463 # If the target namespace doesn't allow subpages, moving with subpages
464 # would mean that you couldn't move them back in one operation, which
466 # @todo FIXME: A specific error message should be given in this case.
468 // @todo FIXME: Use Title::moveSubpages() here
469 $dbr = wfGetDB( DB_MASTER
);
470 if( $this->moveSubpages
&& (
471 MWNamespace
::hasSubpages( $nt->getNamespace() ) ||
(
473 MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() )
477 'page_title' . $dbr->buildLike( $ot->getDBkey() . '/', $dbr->anyString() )
478 .' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
480 $conds['page_namespace'] = array();
481 if( MWNamespace
::hasSubpages( $nt->getNamespace() ) ) {
482 $conds['page_namespace'] []= $ot->getNamespace();
484 if( $this->moveTalk
&& MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() ) ) {
485 $conds['page_namespace'] []= $ot->getTalkPage()->getNamespace();
487 } elseif( $this->moveTalk
) {
489 'page_namespace' => $ot->getTalkPage()->getNamespace(),
490 'page_title' => $ot->getDBkey()
497 $extraPages = array();
498 if( !is_null( $conds ) ) {
499 $extraPages = TitleArray
::newFromResult(
500 $dbr->select( 'page',
501 array( 'page_id', 'page_namespace', 'page_title' ),
508 $extraOutput = array();
510 foreach( $extraPages as $oldSubpage ) {
511 if( $ot->equals( $oldSubpage ) ) {
512 # Already did this one.
516 $newPageName = preg_replace(
517 '#^'.preg_quote( $ot->getDBkey(), '#' ).'#',
518 StringUtils
::escapeRegexReplacement( $nt->getDBkey() ), # bug 21234
519 $oldSubpage->getDBkey()
521 if( $oldSubpage->isTalkPage() ) {
522 $newNs = $nt->getTalkPage()->getNamespace();
524 $newNs = $nt->getSubjectPage()->getNamespace();
526 # Bug 14385: we need makeTitleSafe because the new page names may
527 # be longer than 255 characters.
528 $newSubpage = Title
::makeTitleSafe( $newNs, $newPageName );
530 $oldLink = Linker
::linkKnown( $oldSubpage );
531 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink,
532 htmlspecialchars(Title
::makeName( $newNs, $newPageName )));
536 # This was copy-pasted from Renameuser, bleh.
537 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
538 $link = Linker
::linkKnown( $newSubpage );
539 $extraOutput []= wfMsgHtml( 'movepage-page-exists', $link );
541 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason
, $createRedirect );
542 if( $success === true ) {
543 if ( $this->fixRedirects
) {
544 DoubleRedirectJob
::fixRedirects( 'move', $oldSubpage, $newSubpage );
546 $oldLink = Linker
::link(
550 array( 'redirect' => 'no' )
552 $newLink = Linker
::linkKnown( $newSubpage );
553 $extraOutput []= wfMsgHtml( 'movepage-page-moved', $oldLink, $newLink );
555 if( $count >= $wgMaximumMovedPages ) {
556 $extraOutput []= wfMsgExt( 'movepage-max-pages', array( 'parsemag', 'escape' ), $this->getLang()->formatNum( $wgMaximumMovedPages ) );
560 $oldLink = Linker
::linkKnown( $oldSubpage );
561 $newLink = Linker
::link( $newSubpage );
562 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink, $newLink );
568 if( $extraOutput !== array() ) {
569 $out->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
572 # Deal with watches (we don't watch subpages)
573 if( $this->watch
&& $user->isLoggedIn() ) {
574 $user->addWatch( $ot );
575 $user->addWatch( $nt );
577 $user->removeWatch( $ot );
578 $user->removeWatch( $nt );
581 # Re-clear the file redirect cache, which may have been polluted by
582 # parsing in messages above. See CR r56745.
583 # @todo FIXME: Needs a more robust solution inside FileRepo.
584 if( $ot->getNamespace() == NS_FILE
) {
585 RepoGroup
::singleton()->getLocalRepo()->invalidateImageRedirect( $ot );
589 function showLogFragment( $title ) {
590 $out = $this->getOutput();
591 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'move' ) ) );
592 LogEventsList
::showLogExtract( $out, 'move', $title );
595 function showSubpages( $title ) {
596 if( !MWNamespace
::hasSubpages( $title->getNamespace() ) )
599 $subpages = $title->getSubpages();
600 $count = $subpages instanceof TitleArray ?
$subpages->count() : 0;
602 $out = $this->getOutput();
603 $out->wrapWikiMsg( '== $1 ==', array( 'movesubpage', $count ) );
607 $out->addWikiMsg( 'movenosubpage' );
611 $out->addWikiMsg( 'movesubpagetext', $this->getLang()->formatNum( $count ) );
612 $out->addHTML( "<ul>\n" );
614 foreach( $subpages as $subpage ) {
615 $link = Linker
::link( $subpage );
616 $out->addHTML( "<li>$link</li>\n" );
618 $out->addHTML( "</ul>\n" );