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
55 $oldTitleText = $request->getVal( 'wpOldTitle', $target );
56 $this->oldTitle
= Title
::newFromText( $oldTitleText );
58 if( is_null( $this->oldTitle
) ) {
59 throw new ErrorPageError( 'notargettitle', 'notargettext' );
61 if( !$this->oldTitle
->exists() ) {
62 throw new ErrorPageError( 'nopagetitle', 'nopagetext' );
65 $newTitleTextMain = $request->getText( 'wpNewTitleMain' );
66 $newTitleTextNs = $request->getInt( 'wpNewTitleNs', $this->oldTitle
->getNamespace() );
67 // Backwards compatibility for forms submitting here from other sources
68 // which is more common than it should be..
69 $newTitleText_bc = $request->getText( 'wpNewTitle' );
70 $this->newTitle
= strlen( $newTitleText_bc ) > 0
71 ? Title
::newFromText( $newTitleText_bc )
72 : Title
::makeTitleSafe( $newTitleTextNs, $newTitleTextMain );
75 $user = $this->getUser();
78 $permErrors = $this->oldTitle
->getUserPermissionsErrors( 'move', $user );
79 if ( count( $permErrors ) ) {
80 // Auto-block user's IP if the account was "hard" blocked
81 $user->spreadAnyEditBlock();
82 throw new PermissionsError( 'move', $permErrors );
85 $def = !$request->wasPosted();
87 $this->reason
= $request->getText( 'wpReason' );
88 $this->moveTalk
= $request->getBool( 'wpMovetalk', $def );
89 $this->fixRedirects
= $request->getBool( 'wpFixRedirects', $def );
90 $this->leaveRedirect
= $request->getBool( 'wpLeaveRedirect', $def );
91 $this->moveSubpages
= $request->getBool( 'wpMovesubpages', false );
92 $this->deleteAndMove
= $request->getBool( 'wpDeleteAndMove' ) && $request->getBool( 'wpConfirm' );
93 $this->moveOverShared
= $request->getBool( 'wpMoveOverSharedFile', false );
94 $this->watch
= $request->getCheck( 'wpWatch' ) && $user->isLoggedIn();
96 if ( 'submit' == $request->getVal( 'action' ) && $request->wasPosted()
97 && $user->matchEditToken( $request->getVal( 'wpEditToken' ) ) ) {
100 $this->showForm( array() );
107 * @param $err Array: error messages. Each item is an error message.
108 * It may either be a string message name or array message name and
109 * parameters, like the second argument to OutputPage::wrapWikiMsg().
111 function showForm( $err ) {
112 global $wgContLang, $wgFixDoubleRedirects, $wgMaximumMovedPages;
114 $this->getSkin()->setRelevantTitle( $this->oldTitle
);
116 $oldTitleLink = Linker
::link( $this->oldTitle
);
118 $out = $this->getOutput();
119 $out->setPageTitle( $this->msg( 'move-page', $this->oldTitle
->getPrefixedText() ) );
120 $out->addModules( 'mediawiki.special.movePage' );
122 $newTitle = $this->newTitle
;
125 # Show the current title as a default
126 # when the form is first opened.
127 $newTitle = $this->oldTitle
;
128 } elseif ( !count( $err ) ) {
129 # If a title was supplied, probably from the move log revert
130 # link, check for validity. We can then show some diagnostic
131 # information and save a click.
132 $newerr = $this->oldTitle
->isValidMoveOperation( $newTitle );
133 if( is_array( $newerr ) ) {
138 $user = $this->getUser();
140 if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'articleexists'
141 && $newTitle->quickUserCan( 'delete', $user )
143 $out->addWikiMsg( 'delete_and_move_text', $newTitle->getPrefixedText() );
144 $movepagebtn = $this->msg( 'delete_and_move' )->text();
145 $submitVar = 'wpDeleteAndMove';
149 <td class='mw-input'>" .
150 Xml
::checkLabel( $this->msg( 'delete_and_move_confirm' )->text(), 'wpConfirm', 'wpConfirm' ) .
155 if ($this->oldTitle
->getNamespace() == NS_USER
&& !$this->oldTitle
->isSubpage() ) {
156 $out->wrapWikiMsg( "<div class=\"error mw-moveuserpage-warning\">\n$1\n</div>", 'moveuserpage-warning' );
158 $out->addWikiMsg( $wgFixDoubleRedirects ?
'movepagetext' :
159 'movepagetext-noredirectfixer' );
160 $movepagebtn = $this->msg( 'movepagebtn' )->text();
161 $submitVar = 'wpMove';
165 if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'file-exists-sharedrepo'
166 && $user->isAllowed( 'reupload-shared' )
168 $out->addWikiMsg( 'move-over-sharedrepo', $newTitle->getPrefixedText() );
169 $submitVar = 'wpMoveOverSharedFile';
173 $oldTalk = $this->oldTitle
->getTalkPage();
174 $oldTitleSubpages = $this->oldTitle
->hasSubpages();
175 $oldTitleTalkSubpages = $this->oldTitle
->getTalkPage()->hasSubpages();
177 $canMoveSubpage = ( $oldTitleSubpages ||
$oldTitleTalkSubpages ) &&
178 !count( $this->oldTitle
->getUserPermissionsErrors( 'move-subpages', $user ) );
180 # We also want to be able to move assoc. subpage talk-pages even if base page
181 # has no associated talk page, so || with $oldTitleTalkSubpages.
182 $considerTalk = !$this->oldTitle
->isTalkPage() &&
184 ||
( $oldTitleTalkSubpages && $canMoveSubpage ) );
186 $dbr = wfGetDB( DB_SLAVE
);
187 if ( $wgFixDoubleRedirects ) {
188 $hasRedirects = $dbr->selectField( 'redirect', '1',
190 'rd_namespace' => $this->oldTitle
->getNamespace(),
191 'rd_title' => $this->oldTitle
->getDBkey(),
194 $hasRedirects = false;
197 if ( $considerTalk ) {
198 $out->addWikiMsg( 'movepagetalktext' );
201 if ( count( $err ) ) {
202 $out->addHTML( "<div class='error'>\n" );
203 $action_desc = $this->msg( 'action-move' )->plain();
204 $out->addWikiMsg( 'permissionserrorstext-withaction', count( $err ), $action_desc );
206 if ( count( $err ) == 1 ) {
208 $errMsgName = array_shift( $errMsg );
209 if ( $errMsgName == 'hookaborted' ) {
210 $out->addHTML( "<p>{$errMsg[0]}</p>\n" );
212 $out->addWikiMsgArray( $errMsgName, $errMsg );
216 foreach( $err as $errMsg ) {
217 if( $errMsg[0] == 'hookaborted' ) {
218 $errStr[] = $errMsg[1];
220 $errMsgName = array_shift( $errMsg );
221 $errStr[] = $this->msg( $errMsgName, $errMsg )->parse();
225 $out->addHTML( '<ul><li>' . implode( "</li>\n<li>", $errStr ) . "</li></ul>\n" );
227 $out->addHTML( "</div>\n" );
230 if ( $this->oldTitle
->isProtected( 'move' ) ) {
231 # Is the title semi-protected?
232 if ( $this->oldTitle
->isSemiProtected( 'move' ) ) {
233 $noticeMsg = 'semiprotectedpagemovewarning';
234 $classes[] = 'mw-textarea-sprotected';
236 # Then it must be protected based on static groups (regular)
237 $noticeMsg = 'protectedpagemovewarning';
238 $classes[] = 'mw-textarea-protected';
240 $out->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" );
241 $out->addWikiMsg( $noticeMsg );
242 LogEventsList
::showLogExtract( $out, 'protect', $this->oldTitle
, '', array( 'lim' => 1 ) );
243 $out->addHTML( "</div>\n" );
246 // Byte limit (not string length limit) for wpReason and wpNewTitleMain
247 // is enforced in the mediawiki.special.movePage module
250 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL( 'action=submit' ), 'id' => 'movepage' ) ) .
251 Xml
::openElement( 'fieldset' ) .
252 Xml
::element( 'legend', null, $this->msg( 'move-page-legend' )->text() ) .
253 Xml
::openElement( 'table', array( 'border' => '0', 'id' => 'mw-movepage-table' ) ) .
255 <td class='mw-label'>" .
256 $this->msg( 'movearticle' )->escaped() .
258 <td class='mw-input'>
259 <strong>{$oldTitleLink}</strong>
263 <td class='mw-label'>" .
264 Xml
::label( $this->msg( 'newtitle' )->text(), 'wpNewTitleMain' ) .
266 <td class='mw-input'>" .
267 Html
::namespaceSelector(
268 array( 'selected' => $newTitle->getNamespace() ),
269 array( 'name' => 'wpNewTitleNs', 'id' => 'wpNewTitleNs' )
271 Xml
::input( 'wpNewTitleMain', 60, $wgContLang->recodeForEdit( $newTitle->getText() ), array(
273 'id' => 'wpNewTitleMain',
276 Html
::hidden( 'wpOldTitle', $this->oldTitle
->getPrefixedText() ) .
280 <td class='mw-label'>" .
281 Xml
::label( $this->msg( 'movereason' )->text(), 'wpReason' ) .
283 <td class='mw-input'>" .
284 Html
::element( 'textarea', array( 'name' => 'wpReason', 'id' => 'wpReason', 'cols' => 60, 'rows' => 2,
285 'maxlength' => 200 ), $this->reason
) .
290 if( $considerTalk ) {
294 <td class='mw-input'>" .
295 Xml
::checkLabel( $this->msg( 'movetalk' )->text(), 'wpMovetalk', 'wpMovetalk', $this->moveTalk
) .
301 if ( $user->isAllowed( 'suppressredirect' ) ) {
305 <td class='mw-input' >" .
306 Xml
::checkLabel( $this->msg( 'move-leave-redirect' )->text(), 'wpLeaveRedirect',
307 'wpLeaveRedirect', $this->leaveRedirect
) .
313 if ( $hasRedirects ) {
317 <td class='mw-input' >" .
318 Xml
::checkLabel( $this->msg( 'fix-double-redirects' )->text(), 'wpFixRedirects',
319 'wpFixRedirects', $this->fixRedirects
) .
325 if( $canMoveSubpage ) {
329 <td class=\"mw-input\">" .
332 # Don't check the box if we only have talk subpages to
333 # move and we aren't moving the talk page.
334 $this->moveSubpages
&& ($this->oldTitle
->hasSubpages() ||
$this->moveTalk
),
335 array( 'id' => 'wpMovesubpages' )
337 Xml
::tags( 'label', array( 'for' => 'wpMovesubpages' ),
339 ( $this->oldTitle
->hasSubpages()
341 : 'move-talk-subpages' )
342 )->numParams( $wgMaximumMovedPages )->params( $wgMaximumMovedPages )->parse()
349 $watchChecked = $user->isLoggedIn() && ($this->watch ||
$user->getBoolOption( 'watchmoves' )
350 ||
$this->oldTitle
->userIsWatching());
351 # Don't allow watching if user is not logged in
352 if( $user->isLoggedIn() ) {
356 <td class='mw-input'>" .
357 Xml
::checkLabel( $this->msg( 'move-watch' )->text(), 'wpWatch', 'watch', $watchChecked ) .
366 <td class='mw-submit'>" .
367 Xml
::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
370 Xml
::closeElement( 'table' ) .
371 Html
::hidden( 'wpEditToken', $user->getEditToken() ) .
372 Xml
::closeElement( 'fieldset' ) .
373 Xml
::closeElement( 'form' ) .
377 $this->showLogFragment( $this->oldTitle
);
378 $this->showSubpages( $this->oldTitle
);
382 function doSubmit() {
383 global $wgMaximumMovedPages, $wgFixDoubleRedirects;
385 $user = $this->getUser();
387 if ( $user->pingLimiter( 'move' ) ) {
388 throw new ThrottledError
;
391 $ot = $this->oldTitle
;
392 $nt = $this->newTitle
;
394 # don't allow moving to pages with # in
395 if ( !$nt ||
$nt->getFragment() != '' ) {
396 $this->showForm( array( array( 'badtitletext' ) ) );
400 # Show a warning if the target file exists on a shared repo
401 if ( $nt->getNamespace() == NS_FILE
402 && !( $this->moveOverShared
&& $user->isAllowed( 'reupload-shared' ) )
403 && !RepoGroup
::singleton()->getLocalRepo()->findFile( $nt )
404 && wfFindFile( $nt ) )
406 $this->showForm( array( array( 'file-exists-sharedrepo' ) ) );
411 # Delete to make way if requested
412 if ( $this->deleteAndMove
) {
413 $permErrors = $nt->getUserPermissionsErrors( 'delete', $user );
414 if ( count( $permErrors ) ) {
415 # Only show the first error
416 $this->showForm( $permErrors );
420 $reason = $this->msg( 'delete_and_move_reason', $ot )->inContentLanguage()->text();
422 // Delete an associated image if there is
423 if ( $nt->getNamespace() == NS_FILE
) {
424 $file = wfLocalFile( $nt );
425 if ( $file->exists() ) {
426 $file->delete( $reason, false );
430 $error = ''; // passed by ref
431 $page = WikiPage
::factory( $nt );
432 if ( !$page->doDeleteArticle( $reason, false, 0, true, $error, $user ) ) {
433 $this->showForm( array( array( 'cannotdelete', wfEscapeWikiText( $nt->getPrefixedText() ) ) ) );
438 if ( $user->isAllowed( 'suppressredirect' ) ) {
439 $createRedirect = $this->leaveRedirect
;
441 $createRedirect = true;
444 # Do the actual move.
445 $error = $ot->moveTo( $nt, true, $this->reason
, $createRedirect );
446 if ( $error !== true ) {
447 $this->showForm( $error );
451 if ( $wgFixDoubleRedirects && $this->fixRedirects
) {
452 DoubleRedirectJob
::fixRedirects( 'move', $ot, $nt );
455 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this, &$ot, &$nt ) );
457 $out = $this->getOutput();
458 $out->setPageTitle( $this->msg( 'pagemovedsub' ) );
460 $oldLink = Linker
::link(
464 array( 'redirect' => 'no' )
466 $newLink = Linker
::linkKnown( $nt );
467 $oldText = $ot->getPrefixedText();
468 $newText = $nt->getPrefixedText();
470 $msgName = $createRedirect ?
'movepage-moved-redirect' : 'movepage-moved-noredirect';
471 $out->addHTML( $this->msg( 'movepage-moved' )->rawParams( $oldLink,
472 $newLink )->params( $oldText, $newText )->parseAsBlock() );
473 $out->addWikiMsg( $msgName );
475 # Now we move extra pages we've been asked to move: subpages and talk
476 # pages. First, if the old page or the new page is a talk page, we
477 # can't move any talk pages: cancel that.
478 if( $ot->isTalkPage() ||
$nt->isTalkPage() ) {
479 $this->moveTalk
= false;
482 if ( count( $ot->getUserPermissionsErrors( 'move-subpages', $user ) ) ) {
483 $this->moveSubpages
= false;
486 # Next make a list of id's. This might be marginally less efficient
487 # than a more direct method, but this is not a highly performance-cri-
488 # tical code path and readable code is more important here.
490 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
491 # 4 might get confused. If so, consider rewriting as a UNION.
493 # If the target namespace doesn't allow subpages, moving with subpages
494 # would mean that you couldn't move them back in one operation, which
496 # @todo FIXME: A specific error message should be given in this case.
498 // @todo FIXME: Use Title::moveSubpages() here
499 $dbr = wfGetDB( DB_MASTER
);
500 if( $this->moveSubpages
&& (
501 MWNamespace
::hasSubpages( $nt->getNamespace() ) ||
(
503 MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() )
507 'page_title' . $dbr->buildLike( $ot->getDBkey() . '/', $dbr->anyString() )
508 .' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
510 $conds['page_namespace'] = array();
511 if( MWNamespace
::hasSubpages( $nt->getNamespace() ) ) {
512 $conds['page_namespace'] []= $ot->getNamespace();
514 if( $this->moveTalk
&& MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() ) ) {
515 $conds['page_namespace'] []= $ot->getTalkPage()->getNamespace();
517 } elseif( $this->moveTalk
) {
519 'page_namespace' => $ot->getTalkPage()->getNamespace(),
520 'page_title' => $ot->getDBkey()
527 $extraPages = array();
528 if( !is_null( $conds ) ) {
529 $extraPages = TitleArray
::newFromResult(
530 $dbr->select( 'page',
531 array( 'page_id', 'page_namespace', 'page_title' ),
538 $extraOutput = array();
540 foreach( $extraPages as $oldSubpage ) {
541 if( $ot->equals( $oldSubpage ) ) {
542 # Already did this one.
546 $newPageName = preg_replace(
547 '#^'.preg_quote( $ot->getDBkey(), '#' ).'#',
548 StringUtils
::escapeRegexReplacement( $nt->getDBkey() ), # bug 21234
549 $oldSubpage->getDBkey()
551 if( $oldSubpage->isTalkPage() ) {
552 $newNs = $nt->getTalkPage()->getNamespace();
554 $newNs = $nt->getSubjectPage()->getNamespace();
556 # Bug 14385: we need makeTitleSafe because the new page names may
557 # be longer than 255 characters.
558 $newSubpage = Title
::makeTitleSafe( $newNs, $newPageName );
560 $oldLink = Linker
::linkKnown( $oldSubpage );
561 $extraOutput []= $this->msg( 'movepage-page-unmoved' )->rawParams( $oldLink
562 )->params( Title
::makeName( $newNs, $newPageName ) )->escaped();
566 # This was copy-pasted from Renameuser, bleh.
567 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
568 $link = Linker
::linkKnown( $newSubpage );
569 $extraOutput []= $this->msg( 'movepage-page-exists' )->rawParams( $link )->escaped();
571 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason
, $createRedirect );
572 if( $success === true ) {
573 if ( $this->fixRedirects
) {
574 DoubleRedirectJob
::fixRedirects( 'move', $oldSubpage, $newSubpage );
576 $oldLink = Linker
::link(
580 array( 'redirect' => 'no' )
582 $newLink = Linker
::linkKnown( $newSubpage );
583 $extraOutput []= $this->msg( 'movepage-page-moved' )->rawParams( $oldLink, $newLink )->escaped();
585 if( $count >= $wgMaximumMovedPages ) {
586 $extraOutput []= $this->msg( 'movepage-max-pages' )->numParams( $wgMaximumMovedPages )->escaped();
590 $oldLink = Linker
::linkKnown( $oldSubpage );
591 $newLink = Linker
::link( $newSubpage );
592 $extraOutput []= $this->msg( 'movepage-page-unmoved' )->rawParams( $oldLink, $newLink )->escaped();
598 if( $extraOutput !== array() ) {
599 $out->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
602 # Deal with watches (we don't watch subpages)
603 if( $this->watch
&& $user->isLoggedIn() ) {
604 $user->addWatch( $ot );
605 $user->addWatch( $nt );
607 $user->removeWatch( $ot );
608 $user->removeWatch( $nt );
611 # Re-clear the file redirect cache, which may have been polluted by
612 # parsing in messages above. See CR r56745.
613 # @todo FIXME: Needs a more robust solution inside FileRepo.
614 if( $ot->getNamespace() == NS_FILE
) {
615 RepoGroup
::singleton()->getLocalRepo()->invalidateImageRedirect( $ot );
619 function showLogFragment( $title ) {
620 $out = $this->getOutput();
621 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'move' ) ) );
622 LogEventsList
::showLogExtract( $out, 'move', $title );
625 function showSubpages( $title ) {
626 if( !MWNamespace
::hasSubpages( $title->getNamespace() ) )
629 $subpages = $title->getSubpages();
630 $count = $subpages instanceof TitleArray ?
$subpages->count() : 0;
632 $out = $this->getOutput();
633 $out->wrapWikiMsg( '== $1 ==', array( 'movesubpage', $count ) );
637 $out->addWikiMsg( 'movenosubpage' );
641 $out->addWikiMsg( 'movesubpagetext', $this->getLanguage()->formatNum( $count ) );
642 $out->addHTML( "<ul>\n" );
644 foreach( $subpages as $subpage ) {
645 $link = Linker
::link( $subpage );
646 $out->addHTML( "<li>$link</li>\n" );
648 $out->addHTML( "</ul>\n" );