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 global $wgUser, $wgOut, $wgRequest;
47 # Check for database lock
49 $wgOut->readOnlyPage();
54 $this->outputHeader();
56 $target = !is_null( $par ) ?
$par : $wgRequest->getVal( 'target' );
58 // Yes, the use of getVal() and getText() is wanted, see bug 20365
59 $oldTitleText = $wgRequest->getVal( 'wpOldTitle', $target );
60 $newTitleText = $wgRequest->getText( 'wpNewTitle' );
62 $this->oldTitle
= Title
::newFromText( $oldTitleText );
63 $this->newTitle
= Title
::newFromText( $newTitleText );
65 if( is_null( $this->oldTitle
) ) {
66 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
69 if( !$this->oldTitle
->exists() ) {
70 $wgOut->showErrorPage( 'nopagetitle', 'nopagetext' );
75 $permErrors = $this->oldTitle
->getUserPermissionsErrors( 'move', $wgUser );
76 if( !empty( $permErrors ) ) {
77 $wgOut->showPermissionsErrorPage( $permErrors );
81 $def = !$wgRequest->wasPosted();
83 $this->reason
= $wgRequest->getText( 'wpReason' );
84 $this->moveTalk
= $wgRequest->getBool( 'wpMovetalk', $def );
85 $this->fixRedirects
= $wgRequest->getBool( 'wpFixRedirects', $def );
86 $this->leaveRedirect
= $wgRequest->getBool( 'wpLeaveRedirect', $def );
87 $this->moveSubpages
= $wgRequest->getBool( 'wpMovesubpages', false );
88 $this->deleteAndMove
= $wgRequest->getBool( 'wpDeleteAndMove' ) && $wgRequest->getBool( 'wpConfirm' );
89 $this->moveOverShared
= $wgRequest->getBool( 'wpMoveOverSharedFile', false );
90 $this->watch
= $wgRequest->getCheck( 'wpWatch' ) && $wgUser->isLoggedIn();
92 if ( 'submit' == $wgRequest->getVal( 'action' ) && $wgRequest->wasPosted()
93 && $wgUser->matchEditToken( $wgRequest->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 $wgOut, $wgUser, $wgContLang, $wgFixDoubleRedirects;
110 $skin = $wgUser->getSkin();
112 $oldTitleLink = $skin->link( $this->oldTitle
);
114 $wgOut->setPagetitle( wfMsg( 'move-page', $this->oldTitle
->getPrefixedText() ) );
115 $skin->setRelevantTitle( $this->oldTitle
);
117 $newTitle = $this->newTitle
;
120 # Show the current title as a default
121 # when the form is first opened.
122 $newTitle = $this->oldTitle
;
126 # If a title was supplied, probably from the move log revert
127 # link, check for validity. We can then show some diagnostic
128 # information and save a click.
129 $newerr = $this->oldTitle
->isValidMoveOperation( $newTitle );
136 if ( !empty($err) && $err[0] == 'articleexists' && $wgUser->isAllowed( 'delete' ) ) {
137 $wgOut->addWikiMsg( 'delete_and_move_text', $newTitle->getPrefixedText() );
138 $movepagebtn = wfMsg( 'delete_and_move' );
139 $submitVar = 'wpDeleteAndMove';
143 <td class='mw-input'>" .
144 Xml
::checkLabel( wfMsg( 'delete_and_move_confirm' ), 'wpConfirm', 'wpConfirm' ) .
149 if ($this->oldTitle
->getNamespace() == NS_USER
&& !$this->oldTitle
->isSubpage() ) {
150 $wgOut->wrapWikiMsg( "<div class=\"error mw-moveuserpage-warning\">\n$1\n</div>", 'moveuserpage-warning' );
152 $wgOut->addWikiMsg( $wgFixDoubleRedirects ?
'movepagetext' :
153 'movepagetext-noredirectfixer' );
154 $movepagebtn = wfMsg( 'movepagebtn' );
155 $submitVar = 'wpMove';
159 if ( !empty($err) && $err[0] == 'file-exists-sharedrepo' && $wgUser->isAllowed( 'reupload-shared' ) ) {
160 $wgOut->addWikiMsg( 'move-over-sharedrepo', $newTitle->getPrefixedText() );
161 $submitVar = 'wpMoveOverSharedFile';
165 $oldTalk = $this->oldTitle
->getTalkPage();
166 $considerTalk = ( !$this->oldTitle
->isTalkPage() && $oldTalk->exists() );
168 $dbr = wfGetDB( DB_SLAVE
);
169 if ( $wgFixDoubleRedirects ) {
170 $hasRedirects = $dbr->selectField( 'redirect', '1',
172 'rd_namespace' => $this->oldTitle
->getNamespace(),
173 'rd_title' => $this->oldTitle
->getDBkey(),
176 $hasRedirects = false;
179 if ( $considerTalk ) {
180 $wgOut->addWikiMsg( 'movepagetalktext' );
183 $token = htmlspecialchars( $wgUser->editToken() );
185 if ( !empty($err) ) {
186 $wgOut->setSubtitle( wfMsg( 'formerror' ) );
187 if( $err[0] == 'hookaborted' ) {
189 $errMsg = "<p><strong class=\"error\">$hookErr</strong></p>\n";
190 $wgOut->addHTML( $errMsg );
192 $wgOut->wrapWikiMsg( "<p><strong class=\"error\">\n$1\n</strong></p>", $err );
196 if ( $this->oldTitle
->isProtected( 'move' ) ) {
197 # Is the title semi-protected?
198 if ( $this->oldTitle
->isSemiProtected( 'move' ) ) {
199 $noticeMsg = 'semiprotectedpagemovewarning';
200 $classes[] = 'mw-textarea-sprotected';
202 # Then it must be protected based on static groups (regular)
203 $noticeMsg = 'protectedpagemovewarning';
204 $classes[] = 'mw-textarea-protected';
206 $wgOut->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" );
207 $wgOut->addWikiMsg( $noticeMsg );
208 LogEventsList
::showLogExtract( $wgOut, 'protect', $this->oldTitle
->getPrefixedText(), '', array( 'lim' => 1 ) );
209 $wgOut->addHTML( "</div>\n" );
213 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL( 'action=submit' ), 'id' => 'movepage' ) ) .
214 Xml
::openElement( 'fieldset' ) .
215 Xml
::element( 'legend', null, wfMsg( 'move-page-legend' ) ) .
216 Xml
::openElement( 'table', array( 'border' => '0', 'id' => 'mw-movepage-table' ) ) .
218 <td class='mw-label'>" .
219 wfMsgHtml( 'movearticle' ) .
221 <td class='mw-input'>
222 <strong>{$oldTitleLink}</strong>
226 <td class='mw-label'>" .
227 Xml
::label( wfMsg( 'newtitle' ), 'wpNewTitle' ) .
229 <td class='mw-input'>" .
230 Xml
::input( 'wpNewTitle', 40, $wgContLang->recodeForEdit( $newTitle->getPrefixedText() ), array( 'type' => 'text', 'id' => 'wpNewTitle' ) ) .
231 Html
::hidden( 'wpOldTitle', $this->oldTitle
->getPrefixedText() ) .
235 <td class='mw-label'>" .
236 Xml
::label( wfMsg( 'movereason' ), 'wpReason' ) .
238 <td class='mw-input'>" .
239 Html
::element( 'textarea', array( 'name' => 'wpReason', 'id' => 'wpReason', 'cols' => 60, 'rows' => 2,
240 'maxlength' => 200 ), $this->reason
) .
245 if( $considerTalk ) {
249 <td class='mw-input'>" .
250 Xml
::checkLabel( wfMsg( 'movetalk' ), 'wpMovetalk', 'wpMovetalk', $this->moveTalk
) .
256 if ( $wgUser->isAllowed( 'suppressredirect' ) ) {
260 <td class='mw-input' >" .
261 Xml
::checkLabel( wfMsg( 'move-leave-redirect' ), 'wpLeaveRedirect',
262 'wpLeaveRedirect', $this->leaveRedirect
) .
268 if ( $hasRedirects ) {
272 <td class='mw-input' >" .
273 Xml
::checkLabel( wfMsg( 'fix-double-redirects' ), 'wpFixRedirects',
274 'wpFixRedirects', $this->fixRedirects
) .
280 if( ($this->oldTitle
->hasSubpages() ||
$this->oldTitle
->getTalkPage()->hasSubpages())
281 && $this->oldTitle
->userCan( 'move-subpages' ) )
283 global $wgMaximumMovedPages, $wgLang;
288 <td class=\"mw-input\">" .
291 # Don't check the box if we only have talk subpages to
292 # move and we aren't moving the talk page.
293 $this->moveSubpages
&& ($this->oldTitle
->hasSubpages() ||
$this->moveTalk
),
294 array( 'id' => 'wpMovesubpages' )
296 Xml
::tags( 'label', array( 'for' => 'wpMovesubpages' ),
298 ( $this->oldTitle
->hasSubpages()
300 : 'move-talk-subpages' ),
301 array( 'parseinline' ),
302 $wgLang->formatNum( $wgMaximumMovedPages ),
303 # $2 to allow use of PLURAL in message.
312 $watchChecked = $wgUser->isLoggedIn() && ($this->watch ||
$wgUser->getBoolOption( 'watchmoves' )
313 ||
$this->oldTitle
->userIsWatching());
314 # Don't allow watching if user is not logged in
315 if( $wgUser->isLoggedIn() ) {
319 <td class='mw-input'>" .
320 Xml
::checkLabel( wfMsg( 'move-watch' ), 'wpWatch', 'watch', $watchChecked ) .
329 <td class='mw-submit'>" .
330 Xml
::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
333 Xml
::closeElement( 'table' ) .
334 Html
::hidden( 'wpEditToken', $token ) .
335 Xml
::closeElement( 'fieldset' ) .
336 Xml
::closeElement( 'form' ) .
340 $this->showLogFragment( $this->oldTitle
, $wgOut );
341 $this->showSubpages( $this->oldTitle
, $wgOut );
345 function doSubmit() {
346 global $wgOut, $wgUser, $wgMaximumMovedPages, $wgLang;
347 global $wgFixDoubleRedirects;
349 if ( $wgUser->pingLimiter( 'move' ) ) {
350 $wgOut->rateLimited();
354 $ot = $this->oldTitle
;
355 $nt = $this->newTitle
;
357 # Delete to make way if requested
358 if ( $wgUser->isAllowed( 'delete' ) && $this->deleteAndMove
) {
359 $article = new Article( $nt );
361 # Disallow deletions of big articles
362 $bigHistory = $article->isBigDeletion();
363 if( $bigHistory && !$nt->userCan( 'bigdelete' ) ) {
364 global $wgDeleteRevisionsLimit;
365 $this->showForm( array('delete-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ) );
369 // Delete an associated image if there is
370 $file = wfLocalFile( $nt );
371 if( $file->exists() ) {
372 $file->delete( wfMsgForContent( 'delete_and_move_reason' ), false );
375 // This may output an error message and exit
376 $article->doDelete( wfMsgForContent( 'delete_and_move_reason' ) );
379 # don't allow moving to pages with # in
380 if ( !$nt ||
$nt->getFragment() != '' ) {
381 $this->showForm( 'badtitletext' );
385 # Show a warning if the target file exists on a shared repo
386 if ( $nt->getNamespace() == NS_FILE
387 && !( $this->moveOverShared
&& $wgUser->isAllowed( 'reupload-shared' ) )
388 && !RepoGroup
::singleton()->getLocalRepo()->findFile( $nt )
389 && wfFindFile( $nt ) )
391 $this->showForm( array('file-exists-sharedrepo') );
396 if ( $wgUser->isAllowed( 'suppressredirect' ) ) {
397 $createRedirect = $this->leaveRedirect
;
399 $createRedirect = true;
402 # Do the actual move.
403 $error = $ot->moveTo( $nt, true, $this->reason
, $createRedirect );
404 if ( $error !== true ) {
405 # FIXME: show all the errors in a list, not just the first one
406 $this->showForm( reset( $error ) );
410 if ( $wgFixDoubleRedirects && $this->fixRedirects
) {
411 DoubleRedirectJob
::fixRedirects( 'move', $ot, $nt );
414 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this , &$ot , &$nt ) ) ;
416 $wgOut->setPagetitle( wfMsg( 'pagemovedsub' ) );
418 $oldUrl = $ot->getFullUrl( 'redirect=no' );
419 $newUrl = $nt->getFullUrl();
420 $oldText = $ot->getPrefixedText();
421 $newText = $nt->getPrefixedText();
422 $oldLink = "<span class='plainlinks'>[$oldUrl $oldText]</span>";
423 $newLink = "<span class='plainlinks'>[$newUrl $newText]</span>";
425 $msgName = $createRedirect ?
'movepage-moved-redirect' : 'movepage-moved-noredirect';
426 $wgOut->addWikiMsg( 'movepage-moved', $oldLink, $newLink, $oldText, $newText );
427 $wgOut->addWikiMsg( $msgName );
429 # Now we move extra pages we've been asked to move: subpages and talk
430 # pages. First, if the old page or the new page is a talk page, we
431 # can't move any talk pages: cancel that.
432 if( $ot->isTalkPage() ||
$nt->isTalkPage() ) {
433 $this->moveTalk
= false;
436 if( !$ot->userCan( 'move-subpages' ) ) {
437 $this->moveSubpages
= false;
440 # Next make a list of id's. This might be marginally less efficient
441 # than a more direct method, but this is not a highly performance-cri-
442 # tical code path and readable code is more important here.
444 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
445 # 4 might get confused. If so, consider rewriting as a UNION.
447 # If the target namespace doesn't allow subpages, moving with subpages
448 # would mean that you couldn't move them back in one operation, which
449 # is bad. FIXME: A specific error message should be given in this
452 // FIXME: Use Title::moveSubpages() here
453 $dbr = wfGetDB( DB_MASTER
);
454 if( $this->moveSubpages
&& (
455 MWNamespace
::hasSubpages( $nt->getNamespace() ) ||
(
457 MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() )
461 'page_title' . $dbr->buildLike( $ot->getDBkey() . '/', $dbr->anyString() )
462 .' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
464 $conds['page_namespace'] = array();
465 if( MWNamespace
::hasSubpages( $nt->getNamespace() ) ) {
466 $conds['page_namespace'] []= $ot->getNamespace();
468 if( $this->moveTalk
&& MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() ) ) {
469 $conds['page_namespace'] []= $ot->getTalkPage()->getNamespace();
471 } elseif( $this->moveTalk
) {
473 'page_namespace' => $ot->getTalkPage()->getNamespace(),
474 'page_title' => $ot->getDBkey()
481 $extraPages = array();
482 if( !is_null( $conds ) ) {
483 $extraPages = TitleArray
::newFromResult(
484 $dbr->select( 'page',
485 array( 'page_id', 'page_namespace', 'page_title' ),
492 $extraOutput = array();
493 $skin = $wgUser->getSkin();
495 foreach( $extraPages as $oldSubpage ) {
496 if( $ot->equals( $oldSubpage ) ) {
497 # Already did this one.
501 $newPageName = preg_replace(
502 '#^'.preg_quote( $ot->getDBkey(), '#' ).'#',
503 StringUtils
::escapeRegexReplacement( $nt->getDBkey() ), # bug 21234
504 $oldSubpage->getDBkey()
506 if( $oldSubpage->isTalkPage() ) {
507 $newNs = $nt->getTalkPage()->getNamespace();
509 $newNs = $nt->getSubjectPage()->getNamespace();
511 # Bug 14385: we need makeTitleSafe because the new page names may
512 # be longer than 255 characters.
513 $newSubpage = Title
::makeTitleSafe( $newNs, $newPageName );
515 $oldLink = $skin->linkKnown( $oldSubpage );
516 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink,
517 htmlspecialchars(Title
::makeName( $newNs, $newPageName )));
521 # This was copy-pasted from Renameuser, bleh.
522 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
523 $link = $skin->linkKnown( $newSubpage );
524 $extraOutput []= wfMsgHtml( 'movepage-page-exists', $link );
526 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason
, $createRedirect );
527 if( $success === true ) {
528 if ( $this->fixRedirects
) {
529 DoubleRedirectJob
::fixRedirects( 'move', $oldSubpage, $newSubpage );
531 $oldLink = $skin->linkKnown(
535 array( 'redirect' => 'no' )
537 $newLink = $skin->linkKnown( $newSubpage );
538 $extraOutput []= wfMsgHtml( 'movepage-page-moved', $oldLink, $newLink );
540 if( $count >= $wgMaximumMovedPages ) {
541 $extraOutput []= wfMsgExt( 'movepage-max-pages', array( 'parsemag', 'escape' ), $wgLang->formatNum( $wgMaximumMovedPages ) );
545 $oldLink = $skin->linkKnown( $oldSubpage );
546 $newLink = $skin->link( $newSubpage );
547 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink, $newLink );
553 if( $extraOutput !== array() ) {
554 $wgOut->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
557 # Deal with watches (we don't watch subpages)
558 if( $this->watch
&& $wgUser->isLoggedIn() ) {
559 $wgUser->addWatch( $ot );
560 $wgUser->addWatch( $nt );
562 $wgUser->removeWatch( $ot );
563 $wgUser->removeWatch( $nt );
566 # Re-clear the file redirect cache, which may have been polluted by
567 # parsing in messages above. See CR r56745.
568 # FIXME: needs a more robust solution inside FileRepo.
569 if( $ot->getNamespace() == NS_FILE
) {
570 RepoGroup
::singleton()->getLocalRepo()->invalidateImageRedirect( $ot );
574 function showLogFragment( $title, &$out ) {
575 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'move' ) ) );
576 LogEventsList
::showLogExtract( $out, 'move', $title->getPrefixedText() );
579 function showSubpages( $title, $out ) {
580 global $wgUser, $wgLang;
582 if( !MWNamespace
::hasSubpages( $title->getNamespace() ) )
585 $subpages = $title->getSubpages();
586 $count = $subpages instanceof TitleArray ?
$subpages->count() : 0;
588 $out->wrapWikiMsg( '== $1 ==', array( 'movesubpage', $count ) );
592 $out->addWikiMsg( 'movenosubpage' );
596 $out->addWikiMsg( 'movesubpagetext', $wgLang->formatNum( $count ) );
597 $skin = $wgUser->getSkin();
598 $out->addHTML( "<ul>\n" );
600 foreach( $subpages as $subpage ) {
601 $link = $skin->link( $subpage );
602 $out->addHTML( "<li>$link</li>\n" );
604 $out->addHTML( "</ul>\n" );