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 $wgOut->setSubtitle( wfMsg( 'move-page-backlink', $oldTitleLink ) );
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( 'movepagetext' );
149 $movepagebtn = wfMsg( 'movepagebtn' );
150 $submitVar = 'wpMove';
154 if ( !empty($err) && $err[0] == 'file-exists-sharedrepo' && $wgUser->isAllowed( 'reupload-shared' ) ) {
155 $wgOut->addWikiMsg( 'move-over-sharedrepo', $newTitle->getPrefixedText() );
156 $submitVar = 'wpMoveOverSharedFile';
160 $oldTalk = $this->oldTitle
->getTalkPage();
161 $considerTalk = ( !$this->oldTitle
->isTalkPage() && $oldTalk->exists() );
163 $dbr = wfGetDB( DB_SLAVE
);
164 if ( $wgFixDoubleRedirects ) {
165 $hasRedirects = $dbr->selectField( 'redirect', '1',
167 'rd_namespace' => $this->oldTitle
->getNamespace(),
168 'rd_title' => $this->oldTitle
->getDBkey(),
171 $hasRedirects = false;
174 if ( $considerTalk ) {
175 $wgOut->addWikiMsg( 'movepagetalktext' );
178 $token = htmlspecialchars( $wgUser->editToken() );
180 if ( !empty($err) ) {
181 $wgOut->setSubtitle( wfMsg( 'formerror' ) );
182 if( $err[0] == 'hookaborted' ) {
184 $errMsg = "<p><strong class=\"error\">$hookErr</strong></p>\n";
185 $wgOut->addHTML( $errMsg );
187 $wgOut->wrapWikiMsg( "<p><strong class=\"error\">\n$1\n</strong></p>", $err );
191 if ( $this->oldTitle
->isProtected( 'move' ) ) {
192 # Is the title semi-protected?
193 if ( $this->oldTitle
->isSemiProtected( 'move' ) ) {
194 $noticeMsg = 'semiprotectedpagemovewarning';
195 $classes[] = 'mw-textarea-sprotected';
197 # Then it must be protected based on static groups (regular)
198 $noticeMsg = 'protectedpagemovewarning';
199 $classes[] = 'mw-textarea-protected';
201 $wgOut->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" );
202 $wgOut->addWikiMsg( $noticeMsg );
203 LogEventsList
::showLogExtract( $wgOut, 'protect', $this->oldTitle
->getPrefixedText(), '', array( 'lim' => 1 ) );
204 $wgOut->addHTML( "</div>\n" );
208 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL( 'action=submit' ), 'id' => 'movepage' ) ) .
209 Xml
::openElement( 'fieldset' ) .
210 Xml
::element( 'legend', null, wfMsg( 'move-page-legend' ) ) .
211 Xml
::openElement( 'table', array( 'border' => '0', 'id' => 'mw-movepage-table' ) ) .
213 <td class='mw-label'>" .
214 wfMsgHtml( 'movearticle' ) .
216 <td class='mw-input'>
217 <strong>{$oldTitleLink}</strong>
221 <td class='mw-label'>" .
222 Xml
::label( wfMsg( 'newtitle' ), 'wpNewTitle' ) .
224 <td class='mw-input'>" .
225 Xml
::input( 'wpNewTitle', 40, $wgContLang->recodeForEdit( $newTitle->getPrefixedText() ), array( 'type' => 'text', 'id' => 'wpNewTitle' ) ) .
226 Html
::hidden( 'wpOldTitle', $this->oldTitle
->getPrefixedText() ) .
230 <td class='mw-label'>" .
231 Xml
::label( wfMsg( 'movereason' ), 'wpReason' ) .
233 <td class='mw-input'>" .
234 Html
::element( 'textarea', array( 'name' => 'wpReason', 'id' => 'wpReason', 'cols' => 60, 'rows' => 2,
235 'maxlength' => 200 ), $this->reason
) .
240 if( $considerTalk ) {
244 <td class='mw-input'>" .
245 Xml
::checkLabel( wfMsg( 'movetalk' ), 'wpMovetalk', 'wpMovetalk', $this->moveTalk
) .
251 if ( $wgUser->isAllowed( 'suppressredirect' ) ) {
255 <td class='mw-input' >" .
256 Xml
::checkLabel( wfMsg( 'move-leave-redirect' ), 'wpLeaveRedirect',
257 'wpLeaveRedirect', $this->leaveRedirect
) .
263 if ( $hasRedirects ) {
267 <td class='mw-input' >" .
268 Xml
::checkLabel( wfMsg( 'fix-double-redirects' ), 'wpFixRedirects',
269 'wpFixRedirects', $this->fixRedirects
) .
275 if( ($this->oldTitle
->hasSubpages() ||
$this->oldTitle
->getTalkPage()->hasSubpages())
276 && $this->oldTitle
->userCan( 'move-subpages' ) )
278 global $wgMaximumMovedPages, $wgLang;
283 <td class=\"mw-input\">" .
286 # Don't check the box if we only have talk subpages to
287 # move and we aren't moving the talk page.
288 $this->moveSubpages
&& ($this->oldTitle
->hasSubpages() ||
$this->moveTalk
),
289 array( 'id' => 'wpMovesubpages' )
291 Xml
::tags( 'label', array( 'for' => 'wpMovesubpages' ),
293 ( $this->oldTitle
->hasSubpages()
295 : 'move-talk-subpages' ),
296 array( 'parseinline' ),
297 $wgLang->formatNum( $wgMaximumMovedPages ),
298 # $2 to allow use of PLURAL in message.
307 $watchChecked = $wgUser->isLoggedIn() && ($this->watch ||
$wgUser->getBoolOption( 'watchmoves' )
308 ||
$this->oldTitle
->userIsWatching());
309 # Don't allow watching if user is not logged in
310 if( $wgUser->isLoggedIn() ) {
314 <td class='mw-input'>" .
315 Xml
::checkLabel( wfMsg( 'move-watch' ), 'wpWatch', 'watch', $watchChecked ) .
324 <td class='mw-submit'>" .
325 Xml
::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
328 Xml
::closeElement( 'table' ) .
329 Html
::hidden( 'wpEditToken', $token ) .
330 Xml
::closeElement( 'fieldset' ) .
331 Xml
::closeElement( 'form' ) .
335 $this->showLogFragment( $this->oldTitle
, $wgOut );
336 $this->showSubpages( $this->oldTitle
, $wgOut );
340 function doSubmit() {
341 global $wgOut, $wgUser, $wgMaximumMovedPages, $wgLang;
342 global $wgFixDoubleRedirects;
344 if ( $wgUser->pingLimiter( 'move' ) ) {
345 $wgOut->rateLimited();
349 $ot = $this->oldTitle
;
350 $nt = $this->newTitle
;
352 # Delete to make way if requested
353 if ( $wgUser->isAllowed( 'delete' ) && $this->deleteAndMove
) {
354 $article = new Article( $nt );
356 # Disallow deletions of big articles
357 $bigHistory = $article->isBigDeletion();
358 if( $bigHistory && !$nt->userCan( 'bigdelete' ) ) {
359 global $wgDeleteRevisionsLimit;
360 $this->showForm( array('delete-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ) );
364 // Delete an associated image if there is
365 $file = wfLocalFile( $nt );
366 if( $file->exists() ) {
367 $file->delete( wfMsgForContent( 'delete_and_move_reason' ), false );
370 // This may output an error message and exit
371 $article->doDelete( wfMsgForContent( 'delete_and_move_reason' ) );
374 # don't allow moving to pages with # in
375 if ( !$nt ||
$nt->getFragment() != '' ) {
376 $this->showForm( 'badtitletext' );
380 # Show a warning if the target file exists on a shared repo
381 if ( $nt->getNamespace() == NS_FILE
382 && !( $this->moveOverShared
&& $wgUser->isAllowed( 'reupload-shared' ) )
383 && !RepoGroup
::singleton()->getLocalRepo()->findFile( $nt )
384 && wfFindFile( $nt ) )
386 $this->showForm( array('file-exists-sharedrepo') );
391 if ( $wgUser->isAllowed( 'suppressredirect' ) ) {
392 $createRedirect = $this->leaveRedirect
;
394 $createRedirect = true;
397 # Do the actual move.
398 $error = $ot->moveTo( $nt, true, $this->reason
, $createRedirect );
399 if ( $error !== true ) {
400 # FIXME: show all the errors in a list, not just the first one
401 $this->showForm( reset( $error ) );
405 if ( $wgFixDoubleRedirects && $this->fixRedirects
) {
406 DoubleRedirectJob
::fixRedirects( 'move', $ot, $nt );
409 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this , &$ot , &$nt ) ) ;
411 $wgOut->setPagetitle( wfMsg( 'pagemovedsub' ) );
413 $oldUrl = $ot->getFullUrl( 'redirect=no' );
414 $newUrl = $nt->getFullUrl();
415 $oldText = $ot->getPrefixedText();
416 $newText = $nt->getPrefixedText();
417 $oldLink = "<span class='plainlinks'>[$oldUrl $oldText]</span>";
418 $newLink = "<span class='plainlinks'>[$newUrl $newText]</span>";
420 $msgName = $createRedirect ?
'movepage-moved-redirect' : 'movepage-moved-noredirect';
421 $wgOut->addWikiMsg( 'movepage-moved', $oldLink, $newLink, $oldText, $newText );
422 $wgOut->addWikiMsg( $msgName );
424 # Now we move extra pages we've been asked to move: subpages and talk
425 # pages. First, if the old page or the new page is a talk page, we
426 # can't move any talk pages: cancel that.
427 if( $ot->isTalkPage() ||
$nt->isTalkPage() ) {
428 $this->moveTalk
= false;
431 if( !$ot->userCan( 'move-subpages' ) ) {
432 $this->moveSubpages
= false;
435 # Next make a list of id's. This might be marginally less efficient
436 # than a more direct method, but this is not a highly performance-cri-
437 # tical code path and readable code is more important here.
439 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
440 # 4 might get confused. If so, consider rewriting as a UNION.
442 # If the target namespace doesn't allow subpages, moving with subpages
443 # would mean that you couldn't move them back in one operation, which
444 # is bad. FIXME: A specific error message should be given in this
447 // FIXME: Use Title::moveSubpages() here
448 $dbr = wfGetDB( DB_MASTER
);
449 if( $this->moveSubpages
&& (
450 MWNamespace
::hasSubpages( $nt->getNamespace() ) ||
(
452 MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() )
456 'page_title' . $dbr->buildLike( $ot->getDBkey() . '/', $dbr->anyString() )
457 .' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
459 $conds['page_namespace'] = array();
460 if( MWNamespace
::hasSubpages( $nt->getNamespace() ) ) {
461 $conds['page_namespace'] []= $ot->getNamespace();
463 if( $this->moveTalk
&& MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() ) ) {
464 $conds['page_namespace'] []= $ot->getTalkPage()->getNamespace();
466 } elseif( $this->moveTalk
) {
468 'page_namespace' => $ot->getTalkPage()->getNamespace(),
469 'page_title' => $ot->getDBkey()
476 $extraPages = array();
477 if( !is_null( $conds ) ) {
478 $extraPages = TitleArray
::newFromResult(
479 $dbr->select( 'page',
480 array( 'page_id', 'page_namespace', 'page_title' ),
487 $extraOutput = array();
488 $skin = $wgUser->getSkin();
490 foreach( $extraPages as $oldSubpage ) {
491 if( $ot->equals( $oldSubpage ) ) {
492 # Already did this one.
496 $newPageName = preg_replace(
497 '#^'.preg_quote( $ot->getDBkey(), '#' ).'#',
498 StringUtils
::escapeRegexReplacement( $nt->getDBkey() ), # bug 21234
499 $oldSubpage->getDBkey()
501 if( $oldSubpage->isTalkPage() ) {
502 $newNs = $nt->getTalkPage()->getNamespace();
504 $newNs = $nt->getSubjectPage()->getNamespace();
506 # Bug 14385: we need makeTitleSafe because the new page names may
507 # be longer than 255 characters.
508 $newSubpage = Title
::makeTitleSafe( $newNs, $newPageName );
510 $oldLink = $skin->linkKnown( $oldSubpage );
511 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink,
512 htmlspecialchars(Title
::makeName( $newNs, $newPageName )));
516 # This was copy-pasted from Renameuser, bleh.
517 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
518 $link = $skin->linkKnown( $newSubpage );
519 $extraOutput []= wfMsgHtml( 'movepage-page-exists', $link );
521 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason
, $createRedirect );
522 if( $success === true ) {
523 if ( $this->fixRedirects
) {
524 DoubleRedirectJob
::fixRedirects( 'move', $oldSubpage, $newSubpage );
526 $oldLink = $skin->linkKnown(
530 array( 'redirect' => 'no' )
532 $newLink = $skin->linkKnown( $newSubpage );
533 $extraOutput []= wfMsgHtml( 'movepage-page-moved', $oldLink, $newLink );
535 if( $count >= $wgMaximumMovedPages ) {
536 $extraOutput []= wfMsgExt( 'movepage-max-pages', array( 'parsemag', 'escape' ), $wgLang->formatNum( $wgMaximumMovedPages ) );
540 $oldLink = $skin->linkKnown( $oldSubpage );
541 $newLink = $skin->link( $newSubpage );
542 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink, $newLink );
548 if( $extraOutput !== array() ) {
549 $wgOut->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
552 # Deal with watches (we don't watch subpages)
553 if( $this->watch
&& $wgUser->isLoggedIn() ) {
554 $wgUser->addWatch( $ot );
555 $wgUser->addWatch( $nt );
557 $wgUser->removeWatch( $ot );
558 $wgUser->removeWatch( $nt );
561 # Re-clear the file redirect cache, which may have been polluted by
562 # parsing in messages above. See CR r56745.
563 # FIXME: needs a more robust solution inside FileRepo.
564 if( $ot->getNamespace() == NS_FILE
) {
565 RepoGroup
::singleton()->getLocalRepo()->invalidateImageRedirect( $ot );
569 function showLogFragment( $title, &$out ) {
570 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'move' ) ) );
571 LogEventsList
::showLogExtract( $out, 'move', $title->getPrefixedText() );
574 function showSubpages( $title, $out ) {
575 global $wgUser, $wgLang;
577 if( !MWNamespace
::hasSubpages( $title->getNamespace() ) )
580 $subpages = $title->getSubpages();
581 $count = $subpages instanceof TitleArray ?
$subpages->count() : 0;
583 $out->wrapWikiMsg( '== $1 ==', array( 'movesubpage', $count ) );
587 $out->addWikiMsg( 'movenosubpage' );
591 $out->addWikiMsg( 'movesubpagetext', $wgLang->formatNum( $count ) );
592 $skin = $wgUser->getSkin();
593 $out->addHTML( "<ul>\n" );
595 foreach( $subpages as $subpage ) {
596 $link = $skin->link( $subpage );
597 $out->addHTML( "<li>$link</li>\n" );
599 $out->addHTML( "</ul>\n" );