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 ( count( $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( array() );
99 * @param $err Array: error messages. Each item is an error message.
100 * It may either be a string message name or array message name and
101 * parameters, like the second argument to 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
;
120 } elseif ( !count( $err ) ) {
121 # If a title was supplied, probably from the move log revert
122 # link, check for validity. We can then show some diagnostic
123 # information and save a click.
124 $newerr = $this->oldTitle
->isValidMoveOperation( $newTitle );
125 if( is_array( $newerr ) ) {
130 $user = $this->getUser();
132 if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'articleexists'
133 && $newTitle->quickUserCan( 'delete', $user )
135 $out->addWikiMsg( 'delete_and_move_text', $newTitle->getPrefixedText() );
136 $movepagebtn = wfMsg( 'delete_and_move' );
137 $submitVar = 'wpDeleteAndMove';
141 <td class='mw-input'>" .
142 Xml
::checkLabel( wfMsg( 'delete_and_move_confirm' ), 'wpConfirm', 'wpConfirm' ) .
147 if ($this->oldTitle
->getNamespace() == NS_USER
&& !$this->oldTitle
->isSubpage() ) {
148 $out->wrapWikiMsg( "<div class=\"error mw-moveuserpage-warning\">\n$1\n</div>", 'moveuserpage-warning' );
150 $out->addWikiMsg( $wgFixDoubleRedirects ?
'movepagetext' :
151 'movepagetext-noredirectfixer' );
152 $movepagebtn = wfMsg( 'movepagebtn' );
153 $submitVar = 'wpMove';
157 if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'file-exists-sharedrepo'
158 && $user->isAllowed( 'reupload-shared' )
160 $out->addWikiMsg( 'move-over-sharedrepo', $newTitle->getPrefixedText() );
161 $submitVar = 'wpMoveOverSharedFile';
165 $oldTalk = $this->oldTitle
->getTalkPage();
166 $oldTitleSubpages = $this->oldTitle
->hasSubpages();
167 $oldTitleTalkSubpages = $this->oldTitle
->getTalkPage()->hasSubpages();
169 $canMoveSubpage = ( $oldTitleSubpages ||
$oldTitleTalkSubpages ) &&
170 !count( $this->oldTitle
->getUserPermissionsErrors( 'move-subpages', $user ) );
172 # We also want to be able to move assoc. subpage talk-pages even if base page
173 # has no associated talk page, so || with $oldTitleTalkSubpages.
174 $considerTalk = !$this->oldTitle
->isTalkPage() &&
176 ||
( $oldTitleTalkSubpages && $canMoveSubpage ) );
178 $dbr = wfGetDB( DB_SLAVE
);
179 if ( $wgFixDoubleRedirects ) {
180 $hasRedirects = $dbr->selectField( 'redirect', '1',
182 'rd_namespace' => $this->oldTitle
->getNamespace(),
183 'rd_title' => $this->oldTitle
->getDBkey(),
186 $hasRedirects = false;
189 if ( $considerTalk ) {
190 $out->addWikiMsg( 'movepagetalktext' );
193 if ( count( $err ) ) {
194 $out->addHTML( "<div class='error'>\n" );
195 $action_desc = $this->msg( 'action-move' )->plain();
196 $out->addWikiMsg( 'permissionserrorstext-withaction', count( $err ), $action_desc );
198 if ( count( $err ) ) {
200 $errMsgName = array_shift( $errMsg );
201 if ( $errMsgName == 'hookaborted' ) {
202 $out->addHTML( "<p>{$errMsg[0]}</p>\n" );
204 $out->addWikiMsgArray( $errMsg, $errMsgName );
208 foreach( $err as $errMsg ) {
209 if( $errMsg[0] == 'hookaborted' ) {
210 $errStr[] = $errMsg[1];
212 $errMsgName = array_shift( $errMsg );
213 $errStr[] = $this->msg( $errMsgName, $errMsg )->parse();
217 $out->addHTML( '<ul><li>' . implode( "</li>\n<li>", $errStr ) . "</li></ul>\n" );
219 $out->addHTML( "</div>\n" );
222 if ( $this->oldTitle
->isProtected( 'move' ) ) {
223 # Is the title semi-protected?
224 if ( $this->oldTitle
->isSemiProtected( 'move' ) ) {
225 $noticeMsg = 'semiprotectedpagemovewarning';
226 $classes[] = 'mw-textarea-sprotected';
228 # Then it must be protected based on static groups (regular)
229 $noticeMsg = 'protectedpagemovewarning';
230 $classes[] = 'mw-textarea-protected';
232 $out->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" );
233 $out->addWikiMsg( $noticeMsg );
234 LogEventsList
::showLogExtract( $out, 'protect', $this->oldTitle
, '', array( 'lim' => 1 ) );
235 $out->addHTML( "</div>\n" );
239 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL( 'action=submit' ), 'id' => 'movepage' ) ) .
240 Xml
::openElement( 'fieldset' ) .
241 Xml
::element( 'legend', null, wfMsg( 'move-page-legend' ) ) .
242 Xml
::openElement( 'table', array( 'border' => '0', 'id' => 'mw-movepage-table' ) ) .
244 <td class='mw-label'>" .
245 wfMsgHtml( 'movearticle' ) .
247 <td class='mw-input'>
248 <strong>{$oldTitleLink}</strong>
252 <td class='mw-label'>" .
253 Xml
::label( wfMsg( 'newtitle' ), 'wpNewTitle' ) .
255 <td class='mw-input'>" .
256 Xml
::input( 'wpNewTitle', 60, $wgContLang->recodeForEdit( $newTitle->getPrefixedText() ), array( 'type' => 'text', 'id' => 'wpNewTitle' ) ) .
257 Html
::hidden( 'wpOldTitle', $this->oldTitle
->getPrefixedText() ) .
261 <td class='mw-label'>" .
262 Xml
::label( wfMsg( 'movereason' ), 'wpReason' ) .
264 <td class='mw-input'>" .
265 Html
::element( 'textarea', array( 'name' => 'wpReason', 'id' => 'wpReason', 'cols' => 60, 'rows' => 2,
266 'maxlength' => 200 ), $this->reason
) . // maxlength byte limit is enforce in mediawiki.special.movePage.js
271 if( $considerTalk ) {
275 <td class='mw-input'>" .
276 Xml
::checkLabel( wfMsg( 'movetalk' ), 'wpMovetalk', 'wpMovetalk', $this->moveTalk
) .
282 if ( $user->isAllowed( 'suppressredirect' ) ) {
286 <td class='mw-input' >" .
287 Xml
::checkLabel( wfMsg( 'move-leave-redirect' ), 'wpLeaveRedirect',
288 'wpLeaveRedirect', $this->leaveRedirect
) .
294 if ( $hasRedirects ) {
298 <td class='mw-input' >" .
299 Xml
::checkLabel( wfMsg( 'fix-double-redirects' ), 'wpFixRedirects',
300 'wpFixRedirects', $this->fixRedirects
) .
306 if( $canMoveSubpage ) {
310 <td class=\"mw-input\">" .
313 # Don't check the box if we only have talk subpages to
314 # move and we aren't moving the talk page.
315 $this->moveSubpages
&& ($this->oldTitle
->hasSubpages() ||
$this->moveTalk
),
316 array( 'id' => 'wpMovesubpages' )
318 Xml
::tags( 'label', array( 'for' => 'wpMovesubpages' ),
320 ( $this->oldTitle
->hasSubpages()
322 : 'move-talk-subpages' ),
323 array( 'parseinline' ),
324 $this->getLanguage()->formatNum( $wgMaximumMovedPages ),
325 # $2 to allow use of PLURAL in message.
334 $watchChecked = $user->isLoggedIn() && ($this->watch ||
$user->getBoolOption( 'watchmoves' )
335 ||
$this->oldTitle
->userIsWatching());
336 # Don't allow watching if user is not logged in
337 if( $user->isLoggedIn() ) {
341 <td class='mw-input'>" .
342 Xml
::checkLabel( wfMsg( 'move-watch' ), 'wpWatch', 'watch', $watchChecked ) .
351 <td class='mw-submit'>" .
352 Xml
::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
355 Xml
::closeElement( 'table' ) .
356 Html
::hidden( 'wpEditToken', $user->getEditToken() ) .
357 Xml
::closeElement( 'fieldset' ) .
358 Xml
::closeElement( 'form' ) .
362 $this->showLogFragment( $this->oldTitle
);
363 $this->showSubpages( $this->oldTitle
);
367 function doSubmit() {
368 global $wgMaximumMovedPages, $wgFixDoubleRedirects, $wgDeleteRevisionsLimit;
370 $user = $this->getUser();
372 if ( $user->pingLimiter( 'move' ) ) {
373 throw new ThrottledError
;
376 $ot = $this->oldTitle
;
377 $nt = $this->newTitle
;
379 # don't allow moving to pages with # in
380 if ( !$nt ||
$nt->getFragment() != '' ) {
381 $this->showForm( array( array( 'badtitletext' ) ) );
385 # Show a warning if the target file exists on a shared repo
386 if ( $nt->getNamespace() == NS_FILE
387 && !( $this->moveOverShared
&& $user->isAllowed( 'reupload-shared' ) )
388 && !RepoGroup
::singleton()->getLocalRepo()->findFile( $nt )
389 && wfFindFile( $nt ) )
391 $this->showForm( array( array( 'file-exists-sharedrepo' ) ) );
396 # Delete to make way if requested
397 if ( $this->deleteAndMove
) {
398 $permErrors = $nt->getUserPermissionsErrors( 'delete', $user );
399 if ( count( $permErrors ) ) {
400 # Only show the first error
401 $this->showForm( $permErrors );
405 $reason = wfMessage( 'delete_and_move_reason', $ot )->inContentLanguage()->text();
407 // Delete an associated image if there is
408 if ( $nt->getNamespace() == NS_FILE
) {
409 $file = wfLocalFile( $nt );
410 if ( $file->exists() ) {
411 $file->delete( $reason, false );
415 $error = ''; // passed by ref
416 $page = WikiPage
::factory( $nt );
417 if ( !$page->doDeleteArticle( $reason, false, 0, true, $error, $user ) ) {
418 $this->showForm( array( array( 'cannotdelete', wfEscapeWikiText( $nt->getPrefixedText() ) ) ) );
423 if ( $user->isAllowed( 'suppressredirect' ) ) {
424 $createRedirect = $this->leaveRedirect
;
426 $createRedirect = true;
429 # Do the actual move.
430 $error = $ot->moveTo( $nt, true, $this->reason
, $createRedirect );
431 if ( $error !== true ) {
432 $this->showForm( $error );
436 if ( $wgFixDoubleRedirects && $this->fixRedirects
) {
437 DoubleRedirectJob
::fixRedirects( 'move', $ot, $nt );
440 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this, &$ot, &$nt ) );
442 $out = $this->getOutput();
443 $out->setPagetitle( wfMsg( 'pagemovedsub' ) );
445 $oldLink = Linker
::link(
449 array( 'redirect' => 'no' )
451 $newLink = Linker
::linkKnown( $nt );
452 $oldText = $ot->getPrefixedText();
453 $newText = $nt->getPrefixedText();
455 $msgName = $createRedirect ?
'movepage-moved-redirect' : 'movepage-moved-noredirect';
456 $out->addHTML( wfMessage( 'movepage-moved' )->rawParams( $oldLink, $newLink, $oldText, $newText )->parseAsBlock() );
457 $out->addWikiMsg( $msgName );
459 # Now we move extra pages we've been asked to move: subpages and talk
460 # pages. First, if the old page or the new page is a talk page, we
461 # can't move any talk pages: cancel that.
462 if( $ot->isTalkPage() ||
$nt->isTalkPage() ) {
463 $this->moveTalk
= false;
466 if ( count( $ot->getUserPermissionsErrors( 'move-subpages', $user ) ) ) {
467 $this->moveSubpages
= false;
470 # Next make a list of id's. This might be marginally less efficient
471 # than a more direct method, but this is not a highly performance-cri-
472 # tical code path and readable code is more important here.
474 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
475 # 4 might get confused. If so, consider rewriting as a UNION.
477 # If the target namespace doesn't allow subpages, moving with subpages
478 # would mean that you couldn't move them back in one operation, which
480 # @todo FIXME: A specific error message should be given in this case.
482 // @todo FIXME: Use Title::moveSubpages() here
483 $dbr = wfGetDB( DB_MASTER
);
484 if( $this->moveSubpages
&& (
485 MWNamespace
::hasSubpages( $nt->getNamespace() ) ||
(
487 MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() )
491 'page_title' . $dbr->buildLike( $ot->getDBkey() . '/', $dbr->anyString() )
492 .' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
494 $conds['page_namespace'] = array();
495 if( MWNamespace
::hasSubpages( $nt->getNamespace() ) ) {
496 $conds['page_namespace'] []= $ot->getNamespace();
498 if( $this->moveTalk
&& MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() ) ) {
499 $conds['page_namespace'] []= $ot->getTalkPage()->getNamespace();
501 } elseif( $this->moveTalk
) {
503 'page_namespace' => $ot->getTalkPage()->getNamespace(),
504 'page_title' => $ot->getDBkey()
511 $extraPages = array();
512 if( !is_null( $conds ) ) {
513 $extraPages = TitleArray
::newFromResult(
514 $dbr->select( 'page',
515 array( 'page_id', 'page_namespace', 'page_title' ),
522 $extraOutput = array();
524 foreach( $extraPages as $oldSubpage ) {
525 if( $ot->equals( $oldSubpage ) ) {
526 # Already did this one.
530 $newPageName = preg_replace(
531 '#^'.preg_quote( $ot->getDBkey(), '#' ).'#',
532 StringUtils
::escapeRegexReplacement( $nt->getDBkey() ), # bug 21234
533 $oldSubpage->getDBkey()
535 if( $oldSubpage->isTalkPage() ) {
536 $newNs = $nt->getTalkPage()->getNamespace();
538 $newNs = $nt->getSubjectPage()->getNamespace();
540 # Bug 14385: we need makeTitleSafe because the new page names may
541 # be longer than 255 characters.
542 $newSubpage = Title
::makeTitleSafe( $newNs, $newPageName );
544 $oldLink = Linker
::linkKnown( $oldSubpage );
545 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink,
546 htmlspecialchars(Title
::makeName( $newNs, $newPageName )));
550 # This was copy-pasted from Renameuser, bleh.
551 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
552 $link = Linker
::linkKnown( $newSubpage );
553 $extraOutput []= wfMsgHtml( 'movepage-page-exists', $link );
555 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason
, $createRedirect );
556 if( $success === true ) {
557 if ( $this->fixRedirects
) {
558 DoubleRedirectJob
::fixRedirects( 'move', $oldSubpage, $newSubpage );
560 $oldLink = Linker
::link(
564 array( 'redirect' => 'no' )
566 $newLink = Linker
::linkKnown( $newSubpage );
567 $extraOutput []= wfMsgHtml( 'movepage-page-moved', $oldLink, $newLink );
569 if( $count >= $wgMaximumMovedPages ) {
570 $extraOutput []= wfMsgExt( 'movepage-max-pages', array( 'parsemag', 'escape' ), $this->getLanguage()->formatNum( $wgMaximumMovedPages ) );
574 $oldLink = Linker
::linkKnown( $oldSubpage );
575 $newLink = Linker
::link( $newSubpage );
576 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink, $newLink );
582 if( $extraOutput !== array() ) {
583 $out->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
586 # Deal with watches (we don't watch subpages)
587 if( $this->watch
&& $user->isLoggedIn() ) {
588 $user->addWatch( $ot );
589 $user->addWatch( $nt );
591 $user->removeWatch( $ot );
592 $user->removeWatch( $nt );
595 # Re-clear the file redirect cache, which may have been polluted by
596 # parsing in messages above. See CR r56745.
597 # @todo FIXME: Needs a more robust solution inside FileRepo.
598 if( $ot->getNamespace() == NS_FILE
) {
599 RepoGroup
::singleton()->getLocalRepo()->invalidateImageRedirect( $ot );
603 function showLogFragment( $title ) {
604 $out = $this->getOutput();
605 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'move' ) ) );
606 LogEventsList
::showLogExtract( $out, 'move', $title );
609 function showSubpages( $title ) {
610 if( !MWNamespace
::hasSubpages( $title->getNamespace() ) )
613 $subpages = $title->getSubpages();
614 $count = $subpages instanceof TitleArray ?
$subpages->count() : 0;
616 $out = $this->getOutput();
617 $out->wrapWikiMsg( '== $1 ==', array( 'movesubpage', $count ) );
621 $out->addWikiMsg( 'movenosubpage' );
625 $out->addWikiMsg( 'movesubpagetext', $this->getLanguage()->formatNum( $count ) );
626 $out->addHTML( "<ul>\n" );
628 foreach( $subpages as $subpage ) {
629 $link = Linker
::link( $subpage );
630 $out->addHTML( "<li>$link</li>\n" );
632 $out->addHTML( "</ul>\n" );