4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
21 * Implements Special:Movepage
22 * @ingroup SpecialPage
24 class MovePageForm
extends UnlistedSpecialPage
{
25 var $oldTitle, $newTitle; # Objects
26 var $reason; # Text input
27 var $moveTalk, $deleteAndMove, $moveSubpages, $fixRedirects, $leaveRedirect, $moveOverShared; # Checks
29 private $watch = false;
31 public function __construct() {
32 parent
::__construct( 'Movepage' );
35 public function execute( $par ) {
36 global $wgUser, $wgOut, $wgRequest;
38 # Check for database lock
40 $wgOut->readOnlyPage();
45 $this->outputHeader();
47 $target = !is_null( $par ) ?
$par : $wgRequest->getVal( 'target' );
49 // Yes, the use of getVal() and getText() is wanted, see bug 20365
50 $oldTitleText = $wgRequest->getVal( 'wpOldTitle', $target );
51 $newTitleText = $wgRequest->getText( 'wpNewTitle' );
53 $this->oldTitle
= Title
::newFromText( $oldTitleText );
54 $this->newTitle
= Title
::newFromText( $newTitleText );
56 if( is_null( $this->oldTitle
) ) {
57 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
60 if( !$this->oldTitle
->exists() ) {
61 $wgOut->showErrorPage( 'nopagetitle', 'nopagetext' );
66 $permErrors = $this->oldTitle
->getUserPermissionsErrors( 'move', $wgUser );
67 if( !empty( $permErrors ) ) {
68 $wgOut->showPermissionsErrorPage( $permErrors );
72 $def = !$wgRequest->wasPosted();
74 $this->reason
= $wgRequest->getText( 'wpReason' );
75 $this->moveTalk
= $wgRequest->getBool( 'wpMovetalk', $def );
76 $this->fixRedirects
= $wgRequest->getBool( 'wpFixRedirects', $def );
77 $this->leaveRedirect
= $wgRequest->getBool( 'wpLeaveRedirect', $def );
78 $this->moveSubpages
= $wgRequest->getBool( 'wpMovesubpages', false );
79 $this->deleteAndMove
= $wgRequest->getBool( 'wpDeleteAndMove' ) && $wgRequest->getBool( 'wpConfirm' );
80 $this->moveOverShared
= $wgRequest->getBool( 'wpMoveOverSharedFile', false );
81 $this->watch
= $wgRequest->getCheck( 'wpWatch' ) && $wgUser->isLoggedIn();
83 if ( 'submit' == $wgRequest->getVal( 'action' ) && $wgRequest->wasPosted()
84 && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
87 $this->showForm( '' );
94 * @param $err Mixed: error message. May either be a string message name or
95 * array message name and parameters, like the second argument to
96 * OutputPage::wrapWikiMsg().
98 function showForm( $err ) {
99 global $wgOut, $wgUser, $wgContLang, $wgFixDoubleRedirects;
101 $skin = $wgUser->getSkin();
103 $oldTitleLink = $skin->link( $this->oldTitle
);
105 $wgOut->setPagetitle( wfMsg( 'move-page', $this->oldTitle
->getPrefixedText() ) );
106 $wgOut->setSubtitle( wfMsg( 'move-page-backlink', $oldTitleLink ) );
108 $newTitle = $this->newTitle
;
111 # Show the current title as a default
112 # when the form is first opened.
113 $newTitle = $this->oldTitle
;
117 # If a title was supplied, probably from the move log revert
118 # link, check for validity. We can then show some diagnostic
119 # information and save a click.
120 $newerr = $this->oldTitle
->isValidMoveOperation( $newTitle );
127 if ( !empty($err) && $err[0] == 'articleexists' && $wgUser->isAllowed( 'delete' ) ) {
128 $wgOut->addWikiMsg( 'delete_and_move_text', $newTitle->getPrefixedText() );
129 $movepagebtn = wfMsg( 'delete_and_move' );
130 $submitVar = 'wpDeleteAndMove';
134 <td class='mw-input'>" .
135 Xml
::checkLabel( wfMsg( 'delete_and_move_confirm' ), 'wpConfirm', 'wpConfirm' ) .
140 if ($this->oldTitle
->getNamespace() == NS_USER
&& !$this->oldTitle
->isSubpage() ) {
141 $wgOut->wrapWikiMsg( "<div class=\"error mw-moveuserpage-warning\">\n$1\n</div>", 'moveuserpage-warning' );
143 $wgOut->addWikiMsg( 'movepagetext' );
144 $movepagebtn = wfMsg( 'movepagebtn' );
145 $submitVar = 'wpMove';
149 if ( !empty($err) && $err[0] == 'file-exists-sharedrepo' && $wgUser->isAllowed( 'reupload-shared' ) ) {
150 $wgOut->addWikiMsg( 'move-over-sharedrepo', $newTitle->getPrefixedText() );
151 $submitVar = 'wpMoveOverSharedFile';
155 $oldTalk = $this->oldTitle
->getTalkPage();
156 $considerTalk = ( !$this->oldTitle
->isTalkPage() && $oldTalk->exists() );
158 $dbr = wfGetDB( DB_SLAVE
);
159 if ( $wgFixDoubleRedirects ) {
160 $hasRedirects = $dbr->selectField( 'redirect', '1',
162 'rd_namespace' => $this->oldTitle
->getNamespace(),
163 'rd_title' => $this->oldTitle
->getDBkey(),
166 $hasRedirects = false;
169 if ( $considerTalk ) {
170 $wgOut->addWikiMsg( 'movepagetalktext' );
173 $token = htmlspecialchars( $wgUser->editToken() );
175 if ( !empty($err) ) {
176 $wgOut->setSubtitle( wfMsg( 'formerror' ) );
177 if( $err[0] == 'hookaborted' ) {
179 $errMsg = "<p><strong class=\"error\">$hookErr</strong></p>\n";
180 $wgOut->addHTML( $errMsg );
182 $wgOut->wrapWikiMsg( "<p><strong class=\"error\">\n$1\n</strong></p>", $err );
186 if ( $this->oldTitle
->isProtected( 'move' ) ) {
187 # Is the title semi-protected?
188 if ( $this->oldTitle
->isSemiProtected( 'move' ) ) {
189 $noticeMsg = 'semiprotectedpagemovewarning';
190 $classes[] = 'mw-textarea-sprotected';
192 # Then it must be protected based on static groups (regular)
193 $noticeMsg = 'protectedpagemovewarning';
194 $classes[] = 'mw-textarea-protected';
196 $wgOut->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" );
197 $wgOut->addWikiMsg( $noticeMsg );
198 LogEventsList
::showLogExtract( $wgOut, 'protect', $this->oldTitle
->getPrefixedText(), '', array( 'lim' => 1 ) );
199 $wgOut->addHTML( "</div>\n" );
203 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL( 'action=submit' ), 'id' => 'movepage' ) ) .
204 Xml
::openElement( 'fieldset' ) .
205 Xml
::element( 'legend', null, wfMsg( 'move-page-legend' ) ) .
206 Xml
::openElement( 'table', array( 'border' => '0', 'id' => 'mw-movepage-table' ) ) .
208 <td class='mw-label'>" .
209 wfMsgHtml( 'movearticle' ) .
211 <td class='mw-input'>
212 <strong>{$oldTitleLink}</strong>
216 <td class='mw-label'>" .
217 Xml
::label( wfMsg( 'newtitle' ), 'wpNewTitle' ) .
219 <td class='mw-input'>" .
220 Xml
::input( 'wpNewTitle', 40, $wgContLang->recodeForEdit( $newTitle->getPrefixedText() ), array( 'type' => 'text', 'id' => 'wpNewTitle' ) ) .
221 Xml
::hidden( 'wpOldTitle', $this->oldTitle
->getPrefixedText() ) .
225 <td class='mw-label'>" .
226 Xml
::label( wfMsg( 'movereason' ), 'wpReason' ) .
228 <td class='mw-input'>" .
229 Html
::element( 'textarea', array( 'name' => 'wpReason', 'id' => 'wpReason', 'cols' => 60, 'rows' => 2,
230 'maxlength' => 200 ), $this->reason
) .
235 if( $considerTalk ) {
239 <td class='mw-input'>" .
240 Xml
::checkLabel( wfMsg( 'movetalk' ), 'wpMovetalk', 'wpMovetalk', $this->moveTalk
) .
246 if ( $wgUser->isAllowed( 'suppressredirect' ) ) {
250 <td class='mw-input' >" .
251 Xml
::checkLabel( wfMsg( 'move-leave-redirect' ), 'wpLeaveRedirect',
252 'wpLeaveRedirect', $this->leaveRedirect
) .
258 if ( $hasRedirects ) {
262 <td class='mw-input' >" .
263 Xml
::checkLabel( wfMsg( 'fix-double-redirects' ), 'wpFixRedirects',
264 'wpFixRedirects', $this->fixRedirects
) .
270 if( ($this->oldTitle
->hasSubpages() ||
$this->oldTitle
->getTalkPage()->hasSubpages())
271 && $this->oldTitle
->userCan( 'move-subpages' ) )
273 global $wgMaximumMovedPages, $wgLang;
278 <td class=\"mw-input\">" .
281 # Don't check the box if we only have talk subpages to
282 # move and we aren't moving the talk page.
283 $this->moveSubpages
&& ($this->oldTitle
->hasSubpages() ||
$this->moveTalk
),
284 array( 'id' => 'wpMovesubpages' )
286 Xml
::tags( 'label', array( 'for' => 'wpMovesubpages' ),
288 ( $this->oldTitle
->hasSubpages()
290 : 'move-talk-subpages' ),
291 array( 'parseinline' ),
292 $wgLang->formatNum( $wgMaximumMovedPages ),
293 # $2 to allow use of PLURAL in message.
302 $watchChecked = $wgUser->isLoggedIn() && ($this->watch ||
$wgUser->getBoolOption( 'watchmoves' )
303 ||
$this->oldTitle
->userIsWatching());
304 # Don't allow watching if user is not logged in
305 if( $wgUser->isLoggedIn() ) {
309 <td class='mw-input'>" .
310 Xml
::checkLabel( wfMsg( 'move-watch' ), 'wpWatch', 'watch', $watchChecked ) .
319 <td class='mw-submit'>" .
320 Xml
::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
323 Xml
::closeElement( 'table' ) .
324 Xml
::hidden( 'wpEditToken', $token ) .
325 Xml
::closeElement( 'fieldset' ) .
326 Xml
::closeElement( 'form' ) .
330 $this->showLogFragment( $this->oldTitle
, $wgOut );
331 $this->showSubpages( $this->oldTitle
, $wgOut );
335 function doSubmit() {
336 global $wgOut, $wgUser, $wgMaximumMovedPages, $wgLang;
337 global $wgFixDoubleRedirects;
339 if ( $wgUser->pingLimiter( 'move' ) ) {
340 $wgOut->rateLimited();
344 $ot = $this->oldTitle
;
345 $nt = $this->newTitle
;
347 # Delete to make way if requested
348 if ( $wgUser->isAllowed( 'delete' ) && $this->deleteAndMove
) {
349 $article = new Article( $nt );
351 # Disallow deletions of big articles
352 $bigHistory = $article->isBigDeletion();
353 if( $bigHistory && !$nt->userCan( 'bigdelete' ) ) {
354 global $wgDeleteRevisionsLimit;
355 $this->showForm( array('delete-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ) );
359 // Delete an associated image if there is
360 $file = wfLocalFile( $nt );
361 if( $file->exists() ) {
362 $file->delete( wfMsgForContent( 'delete_and_move_reason' ), false );
365 // This may output an error message and exit
366 $article->doDelete( wfMsgForContent( 'delete_and_move_reason' ) );
369 # don't allow moving to pages with # in
370 if ( !$nt ||
$nt->getFragment() != '' ) {
371 $this->showForm( 'badtitletext' );
375 # Show a warning if the target file exists on a shared repo
376 if ( $nt->getNamespace() == NS_FILE
377 && !( $this->moveOverShared
&& $wgUser->isAllowed( 'reupload-shared' ) )
378 && !RepoGroup
::singleton()->getLocalRepo()->findFile( $nt )
379 && wfFindFile( $nt ) )
381 $this->showForm( array('file-exists-sharedrepo') );
386 if ( $wgUser->isAllowed( 'suppressredirect' ) ) {
387 $createRedirect = $this->leaveRedirect
;
389 $createRedirect = true;
392 # Do the actual move.
393 $error = $ot->moveTo( $nt, true, $this->reason
, $createRedirect );
394 if ( $error !== true ) {
395 # FIXME: show all the errors in a list, not just the first one
396 $this->showForm( reset( $error ) );
400 if ( $wgFixDoubleRedirects && $this->fixRedirects
) {
401 DoubleRedirectJob
::fixRedirects( 'move', $ot, $nt );
404 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this , &$ot , &$nt ) ) ;
406 $wgOut->setPagetitle( wfMsg( 'pagemovedsub' ) );
408 $oldUrl = $ot->getFullUrl( 'redirect=no' );
409 $newUrl = $nt->getFullUrl();
410 $oldText = $ot->getPrefixedText();
411 $newText = $nt->getPrefixedText();
412 $oldLink = "<span class='plainlinks'>[$oldUrl $oldText]</span>";
413 $newLink = "<span class='plainlinks'>[$newUrl $newText]</span>";
415 $msgName = $createRedirect ?
'movepage-moved-redirect' : 'movepage-moved-noredirect';
416 $wgOut->addWikiMsg( 'movepage-moved', $oldLink, $newLink, $oldText, $newText );
417 $wgOut->addWikiMsg( $msgName );
419 # Now we move extra pages we've been asked to move: subpages and talk
420 # pages. First, if the old page or the new page is a talk page, we
421 # can't move any talk pages: cancel that.
422 if( $ot->isTalkPage() ||
$nt->isTalkPage() ) {
423 $this->moveTalk
= false;
426 if( !$ot->userCan( 'move-subpages' ) ) {
427 $this->moveSubpages
= false;
430 # Next make a list of id's. This might be marginally less efficient
431 # than a more direct method, but this is not a highly performance-cri-
432 # tical code path and readable code is more important here.
434 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
435 # 4 might get confused. If so, consider rewriting as a UNION.
437 # If the target namespace doesn't allow subpages, moving with subpages
438 # would mean that you couldn't move them back in one operation, which
439 # is bad. FIXME: A specific error message should be given in this
442 // FIXME: Use Title::moveSubpages() here
443 $dbr = wfGetDB( DB_MASTER
);
444 if( $this->moveSubpages
&& (
445 MWNamespace
::hasSubpages( $nt->getNamespace() ) ||
(
447 MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() )
451 'page_title' . $dbr->buildLike( $ot->getDBkey() . '/', $dbr->anyString() )
452 .' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
454 $conds['page_namespace'] = array();
455 if( MWNamespace
::hasSubpages( $nt->getNamespace() ) ) {
456 $conds['page_namespace'] []= $ot->getNamespace();
458 if( $this->moveTalk
&& MWNamespace
::hasSubpages( $nt->getTalkPage()->getNamespace() ) ) {
459 $conds['page_namespace'] []= $ot->getTalkPage()->getNamespace();
461 } elseif( $this->moveTalk
) {
463 'page_namespace' => $ot->getTalkPage()->getNamespace(),
464 'page_title' => $ot->getDBkey()
471 $extraPages = array();
472 if( !is_null( $conds ) ) {
473 $extraPages = TitleArray
::newFromResult(
474 $dbr->select( 'page',
475 array( 'page_id', 'page_namespace', 'page_title' ),
482 $extraOutput = array();
483 $skin = $wgUser->getSkin();
485 foreach( $extraPages as $oldSubpage ) {
486 if( $ot->equals( $oldSubpage ) ) {
487 # Already did this one.
491 $newPageName = preg_replace(
492 '#^'.preg_quote( $ot->getDBkey(), '#' ).'#',
493 StringUtils
::escapeRegexReplacement( $nt->getDBkey() ), # bug 21234
494 $oldSubpage->getDBkey()
496 if( $oldSubpage->isTalkPage() ) {
497 $newNs = $nt->getTalkPage()->getNamespace();
499 $newNs = $nt->getSubjectPage()->getNamespace();
501 # Bug 14385: we need makeTitleSafe because the new page names may
502 # be longer than 255 characters.
503 $newSubpage = Title
::makeTitleSafe( $newNs, $newPageName );
505 $oldLink = $skin->linkKnown( $oldSubpage );
506 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink,
507 htmlspecialchars(Title
::makeName( $newNs, $newPageName )));
511 # This was copy-pasted from Renameuser, bleh.
512 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
513 $link = $skin->linkKnown( $newSubpage );
514 $extraOutput []= wfMsgHtml( 'movepage-page-exists', $link );
516 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason
, $createRedirect );
517 if( $success === true ) {
518 if ( $this->fixRedirects
) {
519 DoubleRedirectJob
::fixRedirects( 'move', $oldSubpage, $newSubpage );
521 $oldLink = $skin->linkKnown(
525 array( 'redirect' => 'no' )
527 $newLink = $skin->linkKnown( $newSubpage );
528 $extraOutput []= wfMsgHtml( 'movepage-page-moved', $oldLink, $newLink );
530 if( $count >= $wgMaximumMovedPages ) {
531 $extraOutput []= wfMsgExt( 'movepage-max-pages', array( 'parsemag', 'escape' ), $wgLang->formatNum( $wgMaximumMovedPages ) );
535 $oldLink = $skin->linkKnown( $oldSubpage );
536 $newLink = $skin->link( $newSubpage );
537 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink, $newLink );
543 if( $extraOutput !== array() ) {
544 $wgOut->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
547 # Deal with watches (we don't watch subpages)
548 if( $this->watch
&& $wgUser->isLoggedIn() ) {
549 $wgUser->addWatch( $ot );
550 $wgUser->addWatch( $nt );
552 $wgUser->removeWatch( $ot );
553 $wgUser->removeWatch( $nt );
556 # Re-clear the file redirect cache, which may have been polluted by
557 # parsing in messages above. See CR r56745.
558 # FIXME: needs a more robust solution inside FileRepo.
559 if( $ot->getNamespace() == NS_FILE
) {
560 RepoGroup
::singleton()->getLocalRepo()->invalidateImageRedirect( $ot );
564 function showLogFragment( $title, &$out ) {
565 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'move' ) ) );
566 LogEventsList
::showLogExtract( $out, 'move', $title->getPrefixedText() );
569 function showSubpages( $title, $out ) {
570 global $wgUser, $wgLang;
572 if( !MWNamespace
::hasSubpages( $title->getNamespace() ) )
575 $subpages = $title->getSubpages();
576 $count = $subpages instanceof TitleArray ?
$subpages->count() : 0;
578 $out->wrapWikiMsg( '== $1 ==', array( 'movesubpage', $count ) );
582 $out->addWikiMsg( 'movenosubpage' );
586 $out->addWikiMsg( 'movesubpagetext', $wgLang->formatNum( $count ) );
587 $skin = $wgUser->getSkin();
588 $out->addHTML( "<ul>\n" );
590 foreach( $subpages as $subpage ) {
591 $link = $skin->link( $subpage );
592 $out->addHTML( "<li>$link</li>\n" );
594 $out->addHTML( "</ul>\n" );