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 $this->getOutput()->showPermissionsErrorPage( $permErrors );
79 $def = !$request->wasPosted();
81 $this->reason
= $request->getText( 'wpReason' );
82 $this->moveTalk
= $request->getBool( 'wpMovetalk', $def );
83 $this->fixRedirects
= $request->getBool( 'wpFixRedirects', $def );
84 $this->leaveRedirect
= $request->getBool( 'wpLeaveRedirect', $def );
85 $this->moveSubpages
= $request->getBool( 'wpMovesubpages', false );
86 $this->deleteAndMove
= $request->getBool( 'wpDeleteAndMove' ) && $request->getBool( 'wpConfirm' );
87 $this->moveOverShared
= $request->getBool( 'wpMoveOverSharedFile', false );
88 $this->watch
= $request->getCheck( 'wpWatch' ) && $user->isLoggedIn();
90 if ( 'submit' == $request->getVal( 'action' ) && $request->wasPosted()
91 && $user->matchEditToken( $request->getVal( 'wpEditToken' ) ) ) {
94 $this->showForm( '' );
101 * @param $err Mixed: error message. May either be a string message name or
102 * array message name and parameters, like the second argument to
103 * OutputPage::wrapWikiMsg().
105 function showForm( $err ) {
106 global $wgContLang, $wgFixDoubleRedirects, $wgMaximumMovedPages;
108 $this->getSkin()->setRelevantTitle( $this->oldTitle
);
110 $oldTitleLink = Linker
::link( $this->oldTitle
);
112 $out = $this->getOutput();
113 $out->setPagetitle( wfMsg( 'move-page', $this->oldTitle
->getPrefixedText() ) );
114 $out->addModules( 'mediawiki.special.movePage' );
116 $newTitle = $this->newTitle
;
119 # Show the current title as a default
120 # when the form is first opened.
121 $newTitle = $this->oldTitle
;
125 # If a title was supplied, probably from the move log revert
126 # link, check for validity. We can then show some diagnostic
127 # information and save a click.
128 $newerr = $this->oldTitle
->isValidMoveOperation( $newTitle );
135 $user = $this->getUser();
137 if ( !empty($err) && $err[0] == 'articleexists' && $user->isAllowed( 'delete' ) ) {
138 $out->addWikiMsg( 'delete_and_move_text', $newTitle->getPrefixedText() );
139 $movepagebtn = wfMsg( 'delete_and_move' );
140 $submitVar = 'wpDeleteAndMove';
144 <td class='mw-input'>" .
145 Xml
::checkLabel( wfMsg( 'delete_and_move_confirm' ), 'wpConfirm', 'wpConfirm' ) .
150 if ($this->oldTitle
->getNamespace() == NS_USER
&& !$this->oldTitle
->isSubpage() ) {
151 $out->wrapWikiMsg( "<div class=\"error mw-moveuserpage-warning\">\n$1\n</div>", 'moveuserpage-warning' );
153 $out->addWikiMsg( $wgFixDoubleRedirects ?
'movepagetext' :
154 'movepagetext-noredirectfixer' );
155 $movepagebtn = wfMsg( 'movepagebtn' );
156 $submitVar = 'wpMove';
160 if ( !empty($err) && $err[0] == 'file-exists-sharedrepo' && $user->isAllowed( 'reupload-shared' ) ) {
161 $out->addWikiMsg( 'move-over-sharedrepo', $newTitle->getPrefixedText() );
162 $submitVar = 'wpMoveOverSharedFile';
166 $oldTalk = $this->oldTitle
->getTalkPage();
167 $oldTitleSubpages = $this->oldTitle
->hasSubpages();
168 $oldTitleTalkSubpages = $this->oldTitle
->getTalkPage()->hasSubpages();
170 $canMoveSubpage = ( $oldTitleSubpages ||
$oldTitleTalkSubpages ) &&
171 !count( $this->oldTitle
->getUserPermissionsErrors( 'move-subpages', $user ) );
173 # We also want to be able to move assoc. subpage talk-pages even if base page
174 # has no associated talk page, so || with $oldTitleTalkSubpages.
175 $considerTalk = !$this->oldTitle
->isTalkPage() &&
177 ||
( $oldTitleTalkSubpages && $canMoveSubpage ) );
179 $dbr = wfGetDB( DB_SLAVE
);
180 if ( $wgFixDoubleRedirects ) {
181 $hasRedirects = $dbr->selectField( 'redirect', '1',
183 'rd_namespace' => $this->oldTitle
->getNamespace(),
184 'rd_title' => $this->oldTitle
->getDBkey(),
187 $hasRedirects = false;
190 if ( $considerTalk ) {
191 $out->addWikiMsg( 'movepagetalktext' );
194 $token = htmlspecialchars( $user->editToken() );
196 if ( !empty($err) ) {
197 $out->setSubtitle( wfMsg( 'formerror' ) );
198 if( $err[0] == 'hookaborted' ) {
200 $errMsg = "<p><strong class=\"error\">$hookErr</strong></p>\n";
201 $out->addHTML( $errMsg );
203 $out->wrapWikiMsg( "<p><strong class=\"error\">\n$1\n</strong></p>", $err );
207 if ( $this->oldTitle
->isProtected( 'move' ) ) {
208 # Is the title semi-protected?
209 if ( $this->oldTitle
->isSemiProtected( 'move' ) ) {
210 $noticeMsg = 'semiprotectedpagemovewarning';
211 $classes[] = 'mw-textarea-sprotected';
213 # Then it must be protected based on static groups (regular)
214 $noticeMsg = 'protectedpagemovewarning';
215 $classes[] = 'mw-textarea-protected';
217 $out->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" );
218 $out->addWikiMsg( $noticeMsg );
219 LogEventsList
::showLogExtract( $out, 'protect', $this->oldTitle
->getPrefixedText(), '', array( 'lim' => 1 ) );
220 $out->addHTML( "</div>\n" );
224 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL( 'action=submit' ), 'id' => 'movepage' ) ) .
225 Xml
::openElement( 'fieldset' ) .
226 Xml
::element( 'legend', null, wfMsg( 'move-page-legend' ) ) .
227 Xml
::openElement( 'table', array( 'border' => '0', 'id' => 'mw-movepage-table' ) ) .
229 <td class='mw-label'>" .
230 wfMsgHtml( 'movearticle' ) .
232 <td class='mw-input'>
233 <strong>{$oldTitleLink}</strong>
237 <td class='mw-label'>" .
238 Xml
::label( wfMsg( 'newtitle' ), 'wpNewTitle' ) .
240 <td class='mw-input'>" .
241 Xml
::input( 'wpNewTitle', 40, $wgContLang->recodeForEdit( $newTitle->getPrefixedText() ), array( 'type' => 'text', 'id' => 'wpNewTitle' ) ) .
242 Html
::hidden( 'wpOldTitle', $this->oldTitle
->getPrefixedText() ) .
246 <td class='mw-label'>" .
247 Xml
::label( wfMsg( 'movereason' ), 'wpReason' ) .
249 <td class='mw-input'>" .
250 Html
::element( 'textarea', array( 'name' => 'wpReason', 'id' => 'wpReason', 'cols' => 60, 'rows' => 2,
251 'maxlength' => 200 ), $this->reason
) . // maxlength byte limit is enforce in mediawiki.special.movePage.js
256 if( $considerTalk ) {
260 <td class='mw-input'>" .
261 Xml
::checkLabel( wfMsg( 'movetalk' ), 'wpMovetalk', 'wpMovetalk', $this->moveTalk
) .
267 if ( $user->isAllowed( 'suppressredirect' ) ) {
271 <td class='mw-input' >" .
272 Xml
::checkLabel( wfMsg( 'move-leave-redirect' ), 'wpLeaveRedirect',
273 'wpLeaveRedirect', $this->leaveRedirect
) .
279 if ( $hasRedirects ) {
283 <td class='mw-input' >" .
284 Xml
::checkLabel( wfMsg( 'fix-double-redirects' ), 'wpFixRedirects',
285 'wpFixRedirects', $this->fixRedirects
) .
291 if( $canMoveSubpage ) {
295 <td class=\"mw-input\">" .
298 # Don't check the box if we only have talk subpages to
299 # move and we aren't moving the talk page.
300 $this->moveSubpages
&& ($this->oldTitle
->hasSubpages() ||
$this->moveTalk
),
301 array( 'id' => 'wpMovesubpages' )
303 Xml
::tags( 'label', array( 'for' => 'wpMovesubpages' ),
305 ( $this->oldTitle
->hasSubpages()
307 : 'move-talk-subpages' ),
308 array( 'parseinline' ),
309 $this->getLang()->formatNum( $wgMaximumMovedPages ),
310 # $2 to allow use of PLURAL in message.
319 $watchChecked = $user->isLoggedIn() && ($this->watch ||
$user->getBoolOption( 'watchmoves' )
320 ||
$this->oldTitle
->userIsWatching());
321 # Don't allow watching if user is not logged in
322 if( $user->isLoggedIn() ) {
326 <td class='mw-input'>" .
327 Xml
::checkLabel( wfMsg( 'move-watch' ), 'wpWatch', 'watch', $watchChecked ) .
336 <td class='mw-submit'>" .
337 Xml
::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
340 Xml
::closeElement( 'table' ) .
341 Html
::hidden( 'wpEditToken', $token ) .
342 Xml
::closeElement( 'fieldset' ) .
343 Xml
::closeElement( 'form' ) .
347 $this->showLogFragment( $this->oldTitle
);
348 $this->showSubpages( $this->oldTitle
);
352 function doSubmit() {
353 global $wgMaximumMovedPages, $wgFixDoubleRedirects, $wgDeleteRevisionsLimit;
355 $user = $this->getUser();
357 if ( $user->pingLimiter( 'move' ) ) {
358 throw new ThrottledError
;
361 $ot = $this->oldTitle
;
362 $nt = $this->newTitle
;
364 # Delete to make way if requested
365 if ( $user->isAllowed( 'delete' ) && $this->deleteAndMove
) {
366 $article = new Article( $nt );
368 # Disallow deletions of big articles
369 $bigHistory = $article->isBigDeletion();
370 if( $bigHistory && count( $nt->getUserPermissionsErrors( 'bigdelete', $user ) ) ) {
371 $this->showForm( array('delete-toobig', $this->getLang()->formatNum( $wgDeleteRevisionsLimit ) ) );
375 // Delete an associated image if there is
376 $file = wfLocalFile( $nt );
377 if( $file->exists() ) {
378 $file->delete( wfMsgForContent( 'delete_and_move_reason' ), false );
381 // This may output an error message and exit
382 $article->doDelete( wfMsgForContent( 'delete_and_move_reason' ) );
385 # don't allow moving to pages with # in
386 if ( !$nt ||
$nt->getFragment() != '' ) {
387 $this->showForm( 'badtitletext' );
391 # Show a warning if the target file exists on a shared repo
392 if ( $nt->getNamespace() == NS_FILE
393 && !( $this->moveOverShared
&& $user->isAllowed( 'reupload-shared' ) )
394 && !RepoGroup
::singleton()->getLocalRepo()->findFile( $nt )
395 && wfFindFile( $nt ) )
397 $this->showForm( array('file-exists-sharedrepo') );
402 if ( $user->isAllowed( 'suppressredirect' ) ) {
403 $createRedirect = $this->leaveRedirect
;
405 $createRedirect = true;
408 # Do the actual move.
409 $error = $ot->moveTo( $nt, true, $this->reason
, $createRedirect );
410 if ( $error !== true ) {
411 # @todo FIXME: Show all the errors in a list, not just the first one
412 $this->showForm( reset( $error ) );
416 if ( $wgFixDoubleRedirects && $this->fixRedirects
) {
417 DoubleRedirectJob
::fixRedirects( 'move', $ot, $nt );
420 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this, &$ot, &$nt ) );
422 $out = $this->getOutput();
423 $out->setPagetitle( wfMsg( 'pagemovedsub' ) );
425 $oldUrl = $ot->getFullUrl( 'redirect=no' );
426 $newUrl = $nt->getFullUrl();
427 $oldText = $ot->getPrefixedText();
428 $newText = $nt->getPrefixedText();
429 $oldLink = "<span class='plainlinks'>[$oldUrl $oldText]</span>";
430 $newLink = "<span class='plainlinks'>[$newUrl $newText]</span>";
432 $msgName = $createRedirect ?
'movepage-moved-redirect' : 'movepage-moved-noredirect';
433 $out->addWikiMsg( 'movepage-moved', $oldLink, $newLink, $oldText, $newText );
434 $out->addWikiMsg( $msgName );
436 # Now we move extra pages we've been asked to move: subpages and talk
437 # pages. First, if the old page or the new page is a talk page, we
438 # can't move any talk pages: cancel that.
439 if( $ot->isTalkPage() ||
$nt->isTalkPage() ) {
440 $this->moveTalk
= false;
443 if ( count( $ot->getUserPermissionsErrors( 'move-subpages', $user ) ) ) {
444 $this->moveSubpages
= false;
447 # Next make a list of id's. This might be marginally less efficient
448 # than a more direct method, but this is not a highly performance-cri-
449 # tical code path and readable code is more important here.
451 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
452 # 4 might get confused. If so, consider rewriting as a UNION.
454 # If the target namespace doesn't allow subpages, moving with subpages
455 # would mean that you couldn't move them back in one operation, which
457 # @todo FIXME: A specific error message should be given in this case.
459 // @todo FIXME: Use Title::moveSubpages() here
460 $dbr = wfGetDB( DB_MASTER
);
461 if( $this->moveSubpages
&& (
462 MWNamespace
::hasSubpages( $nt->getNamespace() ) ||
(
464 MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() )
468 'page_title' . $dbr->buildLike( $ot->getDBkey() . '/', $dbr->anyString() )
469 .' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
471 $conds['page_namespace'] = array();
472 if( MWNamespace
::hasSubpages( $nt->getNamespace() ) ) {
473 $conds['page_namespace'] []= $ot->getNamespace();
475 if( $this->moveTalk
&& MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() ) ) {
476 $conds['page_namespace'] []= $ot->getTalkPage()->getNamespace();
478 } elseif( $this->moveTalk
) {
480 'page_namespace' => $ot->getTalkPage()->getNamespace(),
481 'page_title' => $ot->getDBkey()
488 $extraPages = array();
489 if( !is_null( $conds ) ) {
490 $extraPages = TitleArray
::newFromResult(
491 $dbr->select( 'page',
492 array( 'page_id', 'page_namespace', 'page_title' ),
499 $extraOutput = array();
501 foreach( $extraPages as $oldSubpage ) {
502 if( $ot->equals( $oldSubpage ) ) {
503 # Already did this one.
507 $newPageName = preg_replace(
508 '#^'.preg_quote( $ot->getDBkey(), '#' ).'#',
509 StringUtils
::escapeRegexReplacement( $nt->getDBkey() ), # bug 21234
510 $oldSubpage->getDBkey()
512 if( $oldSubpage->isTalkPage() ) {
513 $newNs = $nt->getTalkPage()->getNamespace();
515 $newNs = $nt->getSubjectPage()->getNamespace();
517 # Bug 14385: we need makeTitleSafe because the new page names may
518 # be longer than 255 characters.
519 $newSubpage = Title
::makeTitleSafe( $newNs, $newPageName );
521 $oldLink = Linker
::linkKnown( $oldSubpage );
522 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink,
523 htmlspecialchars(Title
::makeName( $newNs, $newPageName )));
527 # This was copy-pasted from Renameuser, bleh.
528 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
529 $link = Linker
::linkKnown( $newSubpage );
530 $extraOutput []= wfMsgHtml( 'movepage-page-exists', $link );
532 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason
, $createRedirect );
533 if( $success === true ) {
534 if ( $this->fixRedirects
) {
535 DoubleRedirectJob
::fixRedirects( 'move', $oldSubpage, $newSubpage );
537 $oldLink = Linker
::linkKnown(
541 array( 'redirect' => 'no' )
543 $newLink = Linker
::linkKnown( $newSubpage );
544 $extraOutput []= wfMsgHtml( 'movepage-page-moved', $oldLink, $newLink );
546 if( $count >= $wgMaximumMovedPages ) {
547 $extraOutput []= wfMsgExt( 'movepage-max-pages', array( 'parsemag', 'escape' ), $this->getLang()->formatNum( $wgMaximumMovedPages ) );
551 $oldLink = Linker
::linkKnown( $oldSubpage );
552 $newLink = Linker
::link( $newSubpage );
553 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink, $newLink );
559 if( $extraOutput !== array() ) {
560 $out->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
563 # Deal with watches (we don't watch subpages)
564 if( $this->watch
&& $user->isLoggedIn() ) {
565 $user->addWatch( $ot );
566 $user->addWatch( $nt );
568 $user->removeWatch( $ot );
569 $user->removeWatch( $nt );
572 # Re-clear the file redirect cache, which may have been polluted by
573 # parsing in messages above. See CR r56745.
574 # @todo FIXME: Needs a more robust solution inside FileRepo.
575 if( $ot->getNamespace() == NS_FILE
) {
576 RepoGroup
::singleton()->getLocalRepo()->invalidateImageRedirect( $ot );
580 function showLogFragment( $title ) {
581 $out = $this->getOutput();
582 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'move' ) ) );
583 LogEventsList
::showLogExtract( $out, 'move', $title->getPrefixedText() );
586 function showSubpages( $title ) {
587 if( !MWNamespace
::hasSubpages( $title->getNamespace() ) )
590 $subpages = $title->getSubpages();
591 $count = $subpages instanceof TitleArray ?
$subpages->count() : 0;
593 $out = $this->getOutput();
594 $out->wrapWikiMsg( '== $1 ==', array( 'movesubpage', $count ) );
598 $out->addWikiMsg( 'movenosubpage' );
602 $out->addWikiMsg( 'movesubpagetext', $this->getLang()->formatNum( $count ) );
603 $out->addHTML( "<ul>\n" );
605 foreach( $subpages as $subpage ) {
606 $link = Linker
::link( $subpage );
607 $out->addHTML( "<li>$link</li>\n" );
609 $out->addHTML( "</ul>\n" );