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
{
30 var $oldTitle, $newTitle; # Objects
31 var $reason; # Text input
32 var $moveTalk, $deleteAndMove, $moveSubpages, $fixRedirects, $leaveRedirect, $moveOverShared; # Checks
34 private $watch = false;
36 public function __construct() {
37 parent
::__construct( 'Movepage' );
40 public function execute( $par ) {
41 global $wgUser, $wgOut, $wgRequest;
43 # Check for database lock
45 $wgOut->readOnlyPage();
50 $this->outputHeader();
52 $target = !is_null( $par ) ?
$par : $wgRequest->getVal( 'target' );
54 // Yes, the use of getVal() and getText() is wanted, see bug 20365
55 $oldTitleText = $wgRequest->getVal( 'wpOldTitle', $target );
56 $newTitleText = $wgRequest->getText( 'wpNewTitle' );
58 $this->oldTitle
= Title
::newFromText( $oldTitleText );
59 $this->newTitle
= Title
::newFromText( $newTitleText );
61 if( is_null( $this->oldTitle
) ) {
62 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
65 if( !$this->oldTitle
->exists() ) {
66 $wgOut->showErrorPage( 'nopagetitle', 'nopagetext' );
71 $permErrors = $this->oldTitle
->getUserPermissionsErrors( 'move', $wgUser );
72 if( !empty( $permErrors ) ) {
73 $wgOut->showPermissionsErrorPage( $permErrors );
77 $def = !$wgRequest->wasPosted();
79 $this->reason
= $wgRequest->getText( 'wpReason' );
80 $this->moveTalk
= $wgRequest->getBool( 'wpMovetalk', $def );
81 $this->fixRedirects
= $wgRequest->getBool( 'wpFixRedirects', $def );
82 $this->leaveRedirect
= $wgRequest->getBool( 'wpLeaveRedirect', $def );
83 $this->moveSubpages
= $wgRequest->getBool( 'wpMovesubpages', false );
84 $this->deleteAndMove
= $wgRequest->getBool( 'wpDeleteAndMove' ) && $wgRequest->getBool( 'wpConfirm' );
85 $this->moveOverShared
= $wgRequest->getBool( 'wpMoveOverSharedFile', false );
86 $this->watch
= $wgRequest->getCheck( 'wpWatch' ) && $wgUser->isLoggedIn();
88 if ( 'submit' == $wgRequest->getVal( 'action' ) && $wgRequest->wasPosted()
89 && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
92 $this->showForm( '' );
99 * @param $err Mixed: error message. May either be a string message name or
100 * array message name and parameters, like the second argument to
101 * OutputPage::wrapWikiMsg().
103 function showForm( $err ) {
104 global $wgOut, $wgUser, $wgContLang, $wgFixDoubleRedirects;
106 $skin = $wgUser->getSkin();
108 $oldTitleLink = $skin->link( $this->oldTitle
);
110 $wgOut->setPagetitle( wfMsg( 'move-page', $this->oldTitle
->getPrefixedText() ) );
111 $skin->setRelevantTitle( $this->oldTitle
);
113 $newTitle = $this->newTitle
;
116 # Show the current title as a default
117 # when the form is first opened.
118 $newTitle = $this->oldTitle
;
122 # If a title was supplied, probably from the move log revert
123 # link, check for validity. We can then show some diagnostic
124 # information and save a click.
125 $newerr = $this->oldTitle
->isValidMoveOperation( $newTitle );
132 if ( !empty($err) && $err[0] == 'articleexists' && $wgUser->isAllowed( 'delete' ) ) {
133 $wgOut->addWikiMsg( 'delete_and_move_text', $newTitle->getPrefixedText() );
134 $movepagebtn = wfMsg( 'delete_and_move' );
135 $submitVar = 'wpDeleteAndMove';
139 <td class='mw-input'>" .
140 Xml
::checkLabel( wfMsg( 'delete_and_move_confirm' ), 'wpConfirm', 'wpConfirm' ) .
145 if ($this->oldTitle
->getNamespace() == NS_USER
&& !$this->oldTitle
->isSubpage() ) {
146 $wgOut->wrapWikiMsg( "<div class=\"error mw-moveuserpage-warning\">\n$1\n</div>", 'moveuserpage-warning' );
148 $wgOut->addWikiMsg( $wgFixDoubleRedirects ?
'movepagetext' :
149 'movepagetext-noredirectfixer' );
150 $movepagebtn = wfMsg( 'movepagebtn' );
151 $submitVar = 'wpMove';
155 if ( !empty($err) && $err[0] == 'file-exists-sharedrepo' && $wgUser->isAllowed( 'reupload-shared' ) ) {
156 $wgOut->addWikiMsg( 'move-over-sharedrepo', $newTitle->getPrefixedText() );
157 $submitVar = 'wpMoveOverSharedFile';
161 $oldTalk = $this->oldTitle
->getTalkPage();
162 $considerTalk = ( !$this->oldTitle
->isTalkPage() && $oldTalk->exists() );
164 $dbr = wfGetDB( DB_SLAVE
);
165 if ( $wgFixDoubleRedirects ) {
166 $hasRedirects = $dbr->selectField( 'redirect', '1',
168 'rd_namespace' => $this->oldTitle
->getNamespace(),
169 'rd_title' => $this->oldTitle
->getDBkey(),
172 $hasRedirects = false;
175 if ( $considerTalk ) {
176 $wgOut->addWikiMsg( 'movepagetalktext' );
179 $token = htmlspecialchars( $wgUser->editToken() );
181 if ( !empty($err) ) {
182 $wgOut->setSubtitle( wfMsg( 'formerror' ) );
183 if( $err[0] == 'hookaborted' ) {
185 $errMsg = "<p><strong class=\"error\">$hookErr</strong></p>\n";
186 $wgOut->addHTML( $errMsg );
188 $wgOut->wrapWikiMsg( "<p><strong class=\"error\">\n$1\n</strong></p>", $err );
192 if ( $this->oldTitle
->isProtected( 'move' ) ) {
193 # Is the title semi-protected?
194 if ( $this->oldTitle
->isSemiProtected( 'move' ) ) {
195 $noticeMsg = 'semiprotectedpagemovewarning';
196 $classes[] = 'mw-textarea-sprotected';
198 # Then it must be protected based on static groups (regular)
199 $noticeMsg = 'protectedpagemovewarning';
200 $classes[] = 'mw-textarea-protected';
202 $wgOut->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" );
203 $wgOut->addWikiMsg( $noticeMsg );
204 LogEventsList
::showLogExtract( $wgOut, 'protect', $this->oldTitle
->getPrefixedText(), '', array( 'lim' => 1 ) );
205 $wgOut->addHTML( "</div>\n" );
209 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL( 'action=submit' ), 'id' => 'movepage' ) ) .
210 Xml
::openElement( 'fieldset' ) .
211 Xml
::element( 'legend', null, wfMsg( 'move-page-legend' ) ) .
212 Xml
::openElement( 'table', array( 'border' => '0', 'id' => 'mw-movepage-table' ) ) .
214 <td class='mw-label'>" .
215 wfMsgHtml( 'movearticle' ) .
217 <td class='mw-input'>
218 <strong>{$oldTitleLink}</strong>
222 <td class='mw-label'>" .
223 Xml
::label( wfMsg( 'newtitle' ), 'wpNewTitle' ) .
225 <td class='mw-input'>" .
226 Xml
::input( 'wpNewTitle', 40, $wgContLang->recodeForEdit( $newTitle->getPrefixedText() ), array( 'type' => 'text', 'id' => 'wpNewTitle' ) ) .
227 Html
::hidden( 'wpOldTitle', $this->oldTitle
->getPrefixedText() ) .
231 <td class='mw-label'>" .
232 Xml
::label( wfMsg( 'movereason' ), 'wpReason' ) .
234 <td class='mw-input'>" .
235 Html
::element( 'textarea', array( 'name' => 'wpReason', 'id' => 'wpReason', 'cols' => 60, 'rows' => 2,
236 'maxlength' => 200 ), $this->reason
) .
241 if( $considerTalk ) {
245 <td class='mw-input'>" .
246 Xml
::checkLabel( wfMsg( 'movetalk' ), 'wpMovetalk', 'wpMovetalk', $this->moveTalk
) .
252 if ( $wgUser->isAllowed( 'suppressredirect' ) ) {
256 <td class='mw-input' >" .
257 Xml
::checkLabel( wfMsg( 'move-leave-redirect' ), 'wpLeaveRedirect',
258 'wpLeaveRedirect', $this->leaveRedirect
) .
264 if ( $hasRedirects ) {
268 <td class='mw-input' >" .
269 Xml
::checkLabel( wfMsg( 'fix-double-redirects' ), 'wpFixRedirects',
270 'wpFixRedirects', $this->fixRedirects
) .
276 if( ($this->oldTitle
->hasSubpages() ||
$this->oldTitle
->getTalkPage()->hasSubpages())
277 && $this->oldTitle
->userCan( 'move-subpages' ) )
279 global $wgMaximumMovedPages, $wgLang;
284 <td class=\"mw-input\">" .
287 # Don't check the box if we only have talk subpages to
288 # move and we aren't moving the talk page.
289 $this->moveSubpages
&& ($this->oldTitle
->hasSubpages() ||
$this->moveTalk
),
290 array( 'id' => 'wpMovesubpages' )
292 Xml
::tags( 'label', array( 'for' => 'wpMovesubpages' ),
294 ( $this->oldTitle
->hasSubpages()
296 : 'move-talk-subpages' ),
297 array( 'parseinline' ),
298 $wgLang->formatNum( $wgMaximumMovedPages ),
299 # $2 to allow use of PLURAL in message.
308 $watchChecked = $wgUser->isLoggedIn() && ($this->watch ||
$wgUser->getBoolOption( 'watchmoves' )
309 ||
$this->oldTitle
->userIsWatching());
310 # Don't allow watching if user is not logged in
311 if( $wgUser->isLoggedIn() ) {
315 <td class='mw-input'>" .
316 Xml
::checkLabel( wfMsg( 'move-watch' ), 'wpWatch', 'watch', $watchChecked ) .
325 <td class='mw-submit'>" .
326 Xml
::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
329 Xml
::closeElement( 'table' ) .
330 Html
::hidden( 'wpEditToken', $token ) .
331 Xml
::closeElement( 'fieldset' ) .
332 Xml
::closeElement( 'form' ) .
336 $this->showLogFragment( $this->oldTitle
, $wgOut );
337 $this->showSubpages( $this->oldTitle
, $wgOut );
341 function doSubmit() {
342 global $wgOut, $wgUser, $wgMaximumMovedPages, $wgLang;
343 global $wgFixDoubleRedirects;
345 if ( $wgUser->pingLimiter( 'move' ) ) {
346 $wgOut->rateLimited();
350 $ot = $this->oldTitle
;
351 $nt = $this->newTitle
;
353 # Delete to make way if requested
354 if ( $wgUser->isAllowed( 'delete' ) && $this->deleteAndMove
) {
355 $article = new Article( $nt );
357 # Disallow deletions of big articles
358 $bigHistory = $article->isBigDeletion();
359 if( $bigHistory && !$nt->userCan( 'bigdelete' ) ) {
360 global $wgDeleteRevisionsLimit;
361 $this->showForm( array('delete-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ) );
365 // Delete an associated image if there is
366 $file = wfLocalFile( $nt );
367 if( $file->exists() ) {
368 $file->delete( wfMsgForContent( 'delete_and_move_reason' ), false );
371 // This may output an error message and exit
372 $article->doDelete( wfMsgForContent( 'delete_and_move_reason' ) );
375 # don't allow moving to pages with # in
376 if ( !$nt ||
$nt->getFragment() != '' ) {
377 $this->showForm( 'badtitletext' );
381 # Show a warning if the target file exists on a shared repo
382 if ( $nt->getNamespace() == NS_FILE
383 && !( $this->moveOverShared
&& $wgUser->isAllowed( 'reupload-shared' ) )
384 && !RepoGroup
::singleton()->getLocalRepo()->findFile( $nt )
385 && wfFindFile( $nt ) )
387 $this->showForm( array('file-exists-sharedrepo') );
392 if ( $wgUser->isAllowed( 'suppressredirect' ) ) {
393 $createRedirect = $this->leaveRedirect
;
395 $createRedirect = true;
398 # Do the actual move.
399 $error = $ot->moveTo( $nt, true, $this->reason
, $createRedirect );
400 if ( $error !== true ) {
401 # FIXME: show all the errors in a list, not just the first one
402 $this->showForm( reset( $error ) );
406 if ( $wgFixDoubleRedirects && $this->fixRedirects
) {
407 DoubleRedirectJob
::fixRedirects( 'move', $ot, $nt );
410 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this , &$ot , &$nt ) ) ;
412 $wgOut->setPagetitle( wfMsg( 'pagemovedsub' ) );
414 $oldUrl = $ot->getFullUrl( 'redirect=no' );
415 $newUrl = $nt->getFullUrl();
416 $oldText = $ot->getPrefixedText();
417 $newText = $nt->getPrefixedText();
418 $oldLink = "<span class='plainlinks'>[$oldUrl $oldText]</span>";
419 $newLink = "<span class='plainlinks'>[$newUrl $newText]</span>";
421 $msgName = $createRedirect ?
'movepage-moved-redirect' : 'movepage-moved-noredirect';
422 $wgOut->addWikiMsg( 'movepage-moved', $oldLink, $newLink, $oldText, $newText );
423 $wgOut->addWikiMsg( $msgName );
425 # Now we move extra pages we've been asked to move: subpages and talk
426 # pages. First, if the old page or the new page is a talk page, we
427 # can't move any talk pages: cancel that.
428 if( $ot->isTalkPage() ||
$nt->isTalkPage() ) {
429 $this->moveTalk
= false;
432 if( !$ot->userCan( 'move-subpages' ) ) {
433 $this->moveSubpages
= false;
436 # Next make a list of id's. This might be marginally less efficient
437 # than a more direct method, but this is not a highly performance-cri-
438 # tical code path and readable code is more important here.
440 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
441 # 4 might get confused. If so, consider rewriting as a UNION.
443 # If the target namespace doesn't allow subpages, moving with subpages
444 # would mean that you couldn't move them back in one operation, which
445 # is bad. FIXME: A specific error message should be given in this
448 // FIXME: Use Title::moveSubpages() here
449 $dbr = wfGetDB( DB_MASTER
);
450 if( $this->moveSubpages
&& (
451 MWNamespace
::hasSubpages( $nt->getNamespace() ) ||
(
453 MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() )
457 'page_title' . $dbr->buildLike( $ot->getDBkey() . '/', $dbr->anyString() )
458 .' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
460 $conds['page_namespace'] = array();
461 if( MWNamespace
::hasSubpages( $nt->getNamespace() ) ) {
462 $conds['page_namespace'] []= $ot->getNamespace();
464 if( $this->moveTalk
&& MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() ) ) {
465 $conds['page_namespace'] []= $ot->getTalkPage()->getNamespace();
467 } elseif( $this->moveTalk
) {
469 'page_namespace' => $ot->getTalkPage()->getNamespace(),
470 'page_title' => $ot->getDBkey()
477 $extraPages = array();
478 if( !is_null( $conds ) ) {
479 $extraPages = TitleArray
::newFromResult(
480 $dbr->select( 'page',
481 array( 'page_id', 'page_namespace', 'page_title' ),
488 $extraOutput = array();
489 $skin = $wgUser->getSkin();
491 foreach( $extraPages as $oldSubpage ) {
492 if( $ot->equals( $oldSubpage ) ) {
493 # Already did this one.
497 $newPageName = preg_replace(
498 '#^'.preg_quote( $ot->getDBkey(), '#' ).'#',
499 StringUtils
::escapeRegexReplacement( $nt->getDBkey() ), # bug 21234
500 $oldSubpage->getDBkey()
502 if( $oldSubpage->isTalkPage() ) {
503 $newNs = $nt->getTalkPage()->getNamespace();
505 $newNs = $nt->getSubjectPage()->getNamespace();
507 # Bug 14385: we need makeTitleSafe because the new page names may
508 # be longer than 255 characters.
509 $newSubpage = Title
::makeTitleSafe( $newNs, $newPageName );
511 $oldLink = $skin->linkKnown( $oldSubpage );
512 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink,
513 htmlspecialchars(Title
::makeName( $newNs, $newPageName )));
517 # This was copy-pasted from Renameuser, bleh.
518 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
519 $link = $skin->linkKnown( $newSubpage );
520 $extraOutput []= wfMsgHtml( 'movepage-page-exists', $link );
522 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason
, $createRedirect );
523 if( $success === true ) {
524 if ( $this->fixRedirects
) {
525 DoubleRedirectJob
::fixRedirects( 'move', $oldSubpage, $newSubpage );
527 $oldLink = $skin->linkKnown(
531 array( 'redirect' => 'no' )
533 $newLink = $skin->linkKnown( $newSubpage );
534 $extraOutput []= wfMsgHtml( 'movepage-page-moved', $oldLink, $newLink );
536 if( $count >= $wgMaximumMovedPages ) {
537 $extraOutput []= wfMsgExt( 'movepage-max-pages', array( 'parsemag', 'escape' ), $wgLang->formatNum( $wgMaximumMovedPages ) );
541 $oldLink = $skin->linkKnown( $oldSubpage );
542 $newLink = $skin->link( $newSubpage );
543 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink, $newLink );
549 if( $extraOutput !== array() ) {
550 $wgOut->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
553 # Deal with watches (we don't watch subpages)
554 if( $this->watch
&& $wgUser->isLoggedIn() ) {
555 $wgUser->addWatch( $ot );
556 $wgUser->addWatch( $nt );
558 $wgUser->removeWatch( $ot );
559 $wgUser->removeWatch( $nt );
562 # Re-clear the file redirect cache, which may have been polluted by
563 # parsing in messages above. See CR r56745.
564 # FIXME: needs a more robust solution inside FileRepo.
565 if( $ot->getNamespace() == NS_FILE
) {
566 RepoGroup
::singleton()->getLocalRepo()->invalidateImageRedirect( $ot );
570 function showLogFragment( $title, &$out ) {
571 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'move' ) ) );
572 LogEventsList
::showLogExtract( $out, 'move', $title->getPrefixedText() );
575 function showSubpages( $title, $out ) {
576 global $wgUser, $wgLang;
578 if( !MWNamespace
::hasSubpages( $title->getNamespace() ) )
581 $subpages = $title->getSubpages();
582 $count = $subpages instanceof TitleArray ?
$subpages->count() : 0;
584 $out->wrapWikiMsg( '== $1 ==', array( 'movesubpage', $count ) );
588 $out->addWikiMsg( 'movenosubpage' );
592 $out->addWikiMsg( 'movesubpagetext', $wgLang->formatNum( $count ) );
593 $skin = $wgUser->getSkin();
594 $out->addHTML( "<ul>\n" );
596 foreach( $subpages as $subpage ) {
597 $link = $skin->link( $subpage );
598 $out->addHTML( "<li>$link</li>\n" );
600 $out->addHTML( "</ul>\n" );