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 # Check for database lock
47 throw new ReadOnlyError
;
51 $this->outputHeader();
53 $request = $this->getRequest();
54 $target = !is_null( $par ) ?
$par : $request->getVal( 'target' );
56 // Yes, the use of getVal() and getText() is wanted, see bug 20365
57 $oldTitleText = $request->getVal( 'wpOldTitle', $target );
58 $newTitleText = $request->getText( 'wpNewTitle' );
60 $this->oldTitle
= Title
::newFromText( $oldTitleText );
61 $this->newTitle
= Title
::newFromText( $newTitleText );
63 if( is_null( $this->oldTitle
) ) {
64 throw new ErrorPageError( 'notargettitle', 'notargettext' );
66 if( !$this->oldTitle
->exists() ) {
67 throw new ErrorPageError( 'nopagetitle', 'nopagetext' );
70 $user = $this->getUser();
73 $permErrors = $this->oldTitle
->getUserPermissionsErrors( 'move', $user );
74 if( !empty( $permErrors ) ) {
75 // Auto-block user's IP if the account was "hard" blocked
76 $user->spreadAnyEditBlock();
77 $this->getOutput()->showPermissionsErrorPage( $permErrors );
81 $def = !$request->wasPosted();
83 $this->reason
= $request->getText( 'wpReason' );
84 $this->moveTalk
= $request->getBool( 'wpMovetalk', $def );
85 $this->fixRedirects
= $request->getBool( 'wpFixRedirects', $def );
86 $this->leaveRedirect
= $request->getBool( 'wpLeaveRedirect', $def );
87 $this->moveSubpages
= $request->getBool( 'wpMovesubpages', false );
88 $this->deleteAndMove
= $request->getBool( 'wpDeleteAndMove' ) && $request->getBool( 'wpConfirm' );
89 $this->moveOverShared
= $request->getBool( 'wpMoveOverSharedFile', false );
90 $this->watch
= $request->getCheck( 'wpWatch' ) && $user->isLoggedIn();
92 if ( 'submit' == $request->getVal( 'action' ) && $request->wasPosted()
93 && $user->matchEditToken( $request->getVal( 'wpEditToken' ) ) ) {
96 $this->showForm( '' );
103 * @param $err Mixed: error message. May either be a string message name or
104 * array message name and parameters, like the second argument to
105 * OutputPage::wrapWikiMsg().
107 function showForm( $err ) {
108 global $wgContLang, $wgFixDoubleRedirects, $wgMaximumMovedPages;
110 $this->getSkin()->setRelevantTitle( $this->oldTitle
);
112 $oldTitleLink = Linker
::link( $this->oldTitle
);
114 $out = $this->getOutput();
115 $out->setPagetitle( wfMsg( 'move-page', $this->oldTitle
->getPrefixedText() ) );
116 $out->addModules( 'mediawiki.special.movePage' );
118 $newTitle = $this->newTitle
;
121 # Show the current title as a default
122 # when the form is first opened.
123 $newTitle = $this->oldTitle
;
127 # If a title was supplied, probably from the move log revert
128 # link, check for validity. We can then show some diagnostic
129 # information and save a click.
130 $newerr = $this->oldTitle
->isValidMoveOperation( $newTitle );
137 $user = $this->getUser();
139 if ( !empty($err) && $err[0] == 'articleexists' && $user->isAllowed( 'delete' ) ) {
140 $out->addWikiMsg( 'delete_and_move_text', $newTitle->getPrefixedText() );
141 $movepagebtn = wfMsg( 'delete_and_move' );
142 $submitVar = 'wpDeleteAndMove';
146 <td class='mw-input'>" .
147 Xml
::checkLabel( wfMsg( 'delete_and_move_confirm' ), 'wpConfirm', 'wpConfirm' ) .
152 if ($this->oldTitle
->getNamespace() == NS_USER
&& !$this->oldTitle
->isSubpage() ) {
153 $out->wrapWikiMsg( "<div class=\"error mw-moveuserpage-warning\">\n$1\n</div>", 'moveuserpage-warning' );
155 $out->addWikiMsg( $wgFixDoubleRedirects ?
'movepagetext' :
156 'movepagetext-noredirectfixer' );
157 $movepagebtn = wfMsg( 'movepagebtn' );
158 $submitVar = 'wpMove';
162 if ( !empty($err) && $err[0] == 'file-exists-sharedrepo' && $user->isAllowed( 'reupload-shared' ) ) {
163 $out->addWikiMsg( 'move-over-sharedrepo', $newTitle->getPrefixedText() );
164 $submitVar = 'wpMoveOverSharedFile';
168 $oldTalk = $this->oldTitle
->getTalkPage();
169 $oldTitleSubpages = $this->oldTitle
->hasSubpages();
170 $oldTitleTalkSubpages = $this->oldTitle
->getTalkPage()->hasSubpages();
172 $canMoveSubpage = ( $oldTitleSubpages ||
$oldTitleTalkSubpages ) &&
173 !count( $this->oldTitle
->getUserPermissionsErrors( 'move-subpages', $user ) );
175 # We also want to be able to move assoc. subpage talk-pages even if base page
176 # has no associated talk page, so || with $oldTitleTalkSubpages.
177 $considerTalk = !$this->oldTitle
->isTalkPage() &&
179 ||
( $oldTitleTalkSubpages && $canMoveSubpage ) );
181 $dbr = wfGetDB( DB_SLAVE
);
182 if ( $wgFixDoubleRedirects ) {
183 $hasRedirects = $dbr->selectField( 'redirect', '1',
185 'rd_namespace' => $this->oldTitle
->getNamespace(),
186 'rd_title' => $this->oldTitle
->getDBkey(),
189 $hasRedirects = false;
192 if ( $considerTalk ) {
193 $out->addWikiMsg( 'movepagetalktext' );
196 $token = htmlspecialchars( $user->editToken() );
198 if ( !empty($err) ) {
199 $out->setSubtitle( wfMsg( 'formerror' ) );
200 if( $err[0] == 'hookaborted' ) {
202 $errMsg = "<p><strong class=\"error\">$hookErr</strong></p>\n";
203 $out->addHTML( $errMsg );
205 $out->wrapWikiMsg( "<p><strong class=\"error\">\n$1\n</strong></p>", $err );
209 if ( $this->oldTitle
->isProtected( 'move' ) ) {
210 # Is the title semi-protected?
211 if ( $this->oldTitle
->isSemiProtected( 'move' ) ) {
212 $noticeMsg = 'semiprotectedpagemovewarning';
213 $classes[] = 'mw-textarea-sprotected';
215 # Then it must be protected based on static groups (regular)
216 $noticeMsg = 'protectedpagemovewarning';
217 $classes[] = 'mw-textarea-protected';
219 $out->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" );
220 $out->addWikiMsg( $noticeMsg );
221 LogEventsList
::showLogExtract( $out, 'protect', $this->oldTitle
, '', array( 'lim' => 1 ) );
222 $out->addHTML( "</div>\n" );
226 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL( 'action=submit' ), 'id' => 'movepage' ) ) .
227 Xml
::openElement( 'fieldset' ) .
228 Xml
::element( 'legend', null, wfMsg( 'move-page-legend' ) ) .
229 Xml
::openElement( 'table', array( 'border' => '0', 'id' => 'mw-movepage-table' ) ) .
231 <td class='mw-label'>" .
232 wfMsgHtml( 'movearticle' ) .
234 <td class='mw-input'>
235 <strong>{$oldTitleLink}</strong>
239 <td class='mw-label'>" .
240 Xml
::label( wfMsg( 'newtitle' ), 'wpNewTitle' ) .
242 <td class='mw-input'>" .
243 Xml
::input( 'wpNewTitle', 40, $wgContLang->recodeForEdit( $newTitle->getPrefixedText() ), array( 'type' => 'text', 'id' => 'wpNewTitle' ) ) .
244 Html
::hidden( 'wpOldTitle', $this->oldTitle
->getPrefixedText() ) .
248 <td class='mw-label'>" .
249 Xml
::label( wfMsg( 'movereason' ), 'wpReason' ) .
251 <td class='mw-input'>" .
252 Html
::element( 'textarea', array( 'name' => 'wpReason', 'id' => 'wpReason', 'cols' => 60, 'rows' => 2,
253 'maxlength' => 200 ), $this->reason
) . // maxlength byte limit is enforce in mediawiki.special.movePage.js
258 if( $considerTalk ) {
262 <td class='mw-input'>" .
263 Xml
::checkLabel( wfMsg( 'movetalk' ), 'wpMovetalk', 'wpMovetalk', $this->moveTalk
) .
269 if ( $user->isAllowed( 'suppressredirect' ) ) {
273 <td class='mw-input' >" .
274 Xml
::checkLabel( wfMsg( 'move-leave-redirect' ), 'wpLeaveRedirect',
275 'wpLeaveRedirect', $this->leaveRedirect
) .
281 if ( $hasRedirects ) {
285 <td class='mw-input' >" .
286 Xml
::checkLabel( wfMsg( 'fix-double-redirects' ), 'wpFixRedirects',
287 'wpFixRedirects', $this->fixRedirects
) .
293 if( $canMoveSubpage ) {
297 <td class=\"mw-input\">" .
300 # Don't check the box if we only have talk subpages to
301 # move and we aren't moving the talk page.
302 $this->moveSubpages
&& ($this->oldTitle
->hasSubpages() ||
$this->moveTalk
),
303 array( 'id' => 'wpMovesubpages' )
305 Xml
::tags( 'label', array( 'for' => 'wpMovesubpages' ),
307 ( $this->oldTitle
->hasSubpages()
309 : 'move-talk-subpages' ),
310 array( 'parseinline' ),
311 $this->getLang()->formatNum( $wgMaximumMovedPages ),
312 # $2 to allow use of PLURAL in message.
321 $watchChecked = $user->isLoggedIn() && ($this->watch ||
$user->getBoolOption( 'watchmoves' )
322 ||
$this->oldTitle
->userIsWatching());
323 # Don't allow watching if user is not logged in
324 if( $user->isLoggedIn() ) {
328 <td class='mw-input'>" .
329 Xml
::checkLabel( wfMsg( 'move-watch' ), 'wpWatch', 'watch', $watchChecked ) .
338 <td class='mw-submit'>" .
339 Xml
::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
342 Xml
::closeElement( 'table' ) .
343 Html
::hidden( 'wpEditToken', $token ) .
344 Xml
::closeElement( 'fieldset' ) .
345 Xml
::closeElement( 'form' ) .
349 $this->showLogFragment( $this->oldTitle
);
350 $this->showSubpages( $this->oldTitle
);
354 function doSubmit() {
355 global $wgMaximumMovedPages, $wgFixDoubleRedirects, $wgDeleteRevisionsLimit;
357 $user = $this->getUser();
359 if ( $user->pingLimiter( 'move' ) ) {
360 throw new ThrottledError
;
363 $ot = $this->oldTitle
;
364 $nt = $this->newTitle
;
366 # Delete to make way if requested
367 if ( $user->isAllowed( 'delete' ) && $this->deleteAndMove
) {
368 $article = new Article( $nt );
370 # Disallow deletions of big articles
371 $bigHistory = $article->isBigDeletion();
372 if( $bigHistory && count( $nt->getUserPermissionsErrors( 'bigdelete', $user ) ) ) {
373 $this->showForm( array('delete-toobig', $this->getLang()->formatNum( $wgDeleteRevisionsLimit ) ) );
377 // Delete an associated image if there is
378 $file = wfLocalFile( $nt );
379 if( $file->exists() ) {
380 $file->delete( wfMsgForContent( 'delete_and_move_reason' ), false );
383 // This may output an error message and exit
384 $article->doDelete( wfMsgForContent( 'delete_and_move_reason' ) );
387 # don't allow moving to pages with # in
388 if ( !$nt ||
$nt->getFragment() != '' ) {
389 $this->showForm( 'badtitletext' );
393 # Show a warning if the target file exists on a shared repo
394 if ( $nt->getNamespace() == NS_FILE
395 && !( $this->moveOverShared
&& $user->isAllowed( 'reupload-shared' ) )
396 && !RepoGroup
::singleton()->getLocalRepo()->findFile( $nt )
397 && wfFindFile( $nt ) )
399 $this->showForm( array('file-exists-sharedrepo') );
404 if ( $user->isAllowed( 'suppressredirect' ) ) {
405 $createRedirect = $this->leaveRedirect
;
407 $createRedirect = true;
410 # Do the actual move.
411 $error = $ot->moveTo( $nt, true, $this->reason
, $createRedirect );
412 if ( $error !== true ) {
413 # @todo FIXME: Show all the errors in a list, not just the first one
414 $this->showForm( reset( $error ) );
418 if ( $wgFixDoubleRedirects && $this->fixRedirects
) {
419 DoubleRedirectJob
::fixRedirects( 'move', $ot, $nt );
422 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this, &$ot, &$nt ) );
424 $out = $this->getOutput();
425 $out->setPagetitle( wfMsg( 'pagemovedsub' ) );
427 $oldUrl = $ot->getFullUrl( 'redirect=no' );
428 $newUrl = $nt->getFullUrl();
429 $oldText = $ot->getPrefixedText();
430 $newText = $nt->getPrefixedText();
431 $oldLink = "<span class='plainlinks'>[$oldUrl $oldText]</span>";
432 $newLink = "<span class='plainlinks'>[$newUrl $newText]</span>";
434 $msgName = $createRedirect ?
'movepage-moved-redirect' : 'movepage-moved-noredirect';
435 $out->addWikiMsg( 'movepage-moved', $oldLink, $newLink, $oldText, $newText );
436 $out->addWikiMsg( $msgName );
438 # Now we move extra pages we've been asked to move: subpages and talk
439 # pages. First, if the old page or the new page is a talk page, we
440 # can't move any talk pages: cancel that.
441 if( $ot->isTalkPage() ||
$nt->isTalkPage() ) {
442 $this->moveTalk
= false;
445 if ( count( $ot->getUserPermissionsErrors( 'move-subpages', $user ) ) ) {
446 $this->moveSubpages
= false;
449 # Next make a list of id's. This might be marginally less efficient
450 # than a more direct method, but this is not a highly performance-cri-
451 # tical code path and readable code is more important here.
453 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
454 # 4 might get confused. If so, consider rewriting as a UNION.
456 # If the target namespace doesn't allow subpages, moving with subpages
457 # would mean that you couldn't move them back in one operation, which
459 # @todo FIXME: A specific error message should be given in this case.
461 // @todo FIXME: Use Title::moveSubpages() here
462 $dbr = wfGetDB( DB_MASTER
);
463 if( $this->moveSubpages
&& (
464 MWNamespace
::hasSubpages( $nt->getNamespace() ) ||
(
466 MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() )
470 'page_title' . $dbr->buildLike( $ot->getDBkey() . '/', $dbr->anyString() )
471 .' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
473 $conds['page_namespace'] = array();
474 if( MWNamespace
::hasSubpages( $nt->getNamespace() ) ) {
475 $conds['page_namespace'] []= $ot->getNamespace();
477 if( $this->moveTalk
&& MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() ) ) {
478 $conds['page_namespace'] []= $ot->getTalkPage()->getNamespace();
480 } elseif( $this->moveTalk
) {
482 'page_namespace' => $ot->getTalkPage()->getNamespace(),
483 'page_title' => $ot->getDBkey()
490 $extraPages = array();
491 if( !is_null( $conds ) ) {
492 $extraPages = TitleArray
::newFromResult(
493 $dbr->select( 'page',
494 array( 'page_id', 'page_namespace', 'page_title' ),
501 $extraOutput = array();
503 foreach( $extraPages as $oldSubpage ) {
504 if( $ot->equals( $oldSubpage ) ) {
505 # Already did this one.
509 $newPageName = preg_replace(
510 '#^'.preg_quote( $ot->getDBkey(), '#' ).'#',
511 StringUtils
::escapeRegexReplacement( $nt->getDBkey() ), # bug 21234
512 $oldSubpage->getDBkey()
514 if( $oldSubpage->isTalkPage() ) {
515 $newNs = $nt->getTalkPage()->getNamespace();
517 $newNs = $nt->getSubjectPage()->getNamespace();
519 # Bug 14385: we need makeTitleSafe because the new page names may
520 # be longer than 255 characters.
521 $newSubpage = Title
::makeTitleSafe( $newNs, $newPageName );
523 $oldLink = Linker
::linkKnown( $oldSubpage );
524 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink,
525 htmlspecialchars(Title
::makeName( $newNs, $newPageName )));
529 # This was copy-pasted from Renameuser, bleh.
530 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
531 $link = Linker
::linkKnown( $newSubpage );
532 $extraOutput []= wfMsgHtml( 'movepage-page-exists', $link );
534 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason
, $createRedirect );
535 if( $success === true ) {
536 if ( $this->fixRedirects
) {
537 DoubleRedirectJob
::fixRedirects( 'move', $oldSubpage, $newSubpage );
539 $oldLink = Linker
::linkKnown(
543 array( 'redirect' => 'no' )
545 $newLink = Linker
::linkKnown( $newSubpage );
546 $extraOutput []= wfMsgHtml( 'movepage-page-moved', $oldLink, $newLink );
548 if( $count >= $wgMaximumMovedPages ) {
549 $extraOutput []= wfMsgExt( 'movepage-max-pages', array( 'parsemag', 'escape' ), $this->getLang()->formatNum( $wgMaximumMovedPages ) );
553 $oldLink = Linker
::linkKnown( $oldSubpage );
554 $newLink = Linker
::link( $newSubpage );
555 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink, $newLink );
561 if( $extraOutput !== array() ) {
562 $out->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
565 # Deal with watches (we don't watch subpages)
566 if( $this->watch
&& $user->isLoggedIn() ) {
567 $user->addWatch( $ot );
568 $user->addWatch( $nt );
570 $user->removeWatch( $ot );
571 $user->removeWatch( $nt );
574 # Re-clear the file redirect cache, which may have been polluted by
575 # parsing in messages above. See CR r56745.
576 # @todo FIXME: Needs a more robust solution inside FileRepo.
577 if( $ot->getNamespace() == NS_FILE
) {
578 RepoGroup
::singleton()->getLocalRepo()->invalidateImageRedirect( $ot );
582 function showLogFragment( $title ) {
583 $out = $this->getOutput();
584 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'move' ) ) );
585 LogEventsList
::showLogExtract( $out, 'move', $title );
588 function showSubpages( $title ) {
589 if( !MWNamespace
::hasSubpages( $title->getNamespace() ) )
592 $subpages = $title->getSubpages();
593 $count = $subpages instanceof TitleArray ?
$subpages->count() : 0;
595 $out = $this->getOutput();
596 $out->wrapWikiMsg( '== $1 ==', array( 'movesubpage', $count ) );
600 $out->addWikiMsg( 'movenosubpage' );
604 $out->addWikiMsg( 'movesubpagetext', $this->getLang()->formatNum( $count ) );
605 $out->addHTML( "<ul>\n" );
607 foreach( $subpages as $subpage ) {
608 $link = Linker
::link( $subpage );
609 $out->addHTML( "<li>$link</li>\n" );
611 $out->addHTML( "</ul>\n" );