00001 <?php
00010 function wfSpecialMovepage( $par = null ) {
00011 global $wgUser, $wgOut, $wgRequest, $action;
00012
00013 # Check for database lock
00014 if ( wfReadOnly() ) {
00015 $wgOut->readOnlyPage();
00016 return;
00017 }
00018
00019 $target = isset( $par ) ? $par : $wgRequest->getVal( 'target' );
00020 $oldTitleText = $wgRequest->getText( 'wpOldTitle', $target );
00021 $newTitleText = $wgRequest->getText( 'wpNewTitle' );
00022
00023 $oldTitle = Title::newFromText( $oldTitleText );
00024 $newTitle = Title::newFromText( $newTitleText );
00025
00026 if( is_null( $oldTitle ) ) {
00027 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
00028 return;
00029 }
00030 if( !$oldTitle->exists() ) {
00031 $wgOut->showErrorPage( 'nopagetitle', 'nopagetext' );
00032 return;
00033 }
00034
00035 # Check rights
00036 $permErrors = $oldTitle->getUserPermissionsErrors( 'move', $wgUser );
00037 if( !empty( $permErrors ) ) {
00038 $wgOut->showPermissionsErrorPage( $permErrors );
00039 return;
00040 }
00041
00042 $form = new MovePageForm( $oldTitle, $newTitle );
00043
00044 if ( 'submit' == $action && $wgRequest->wasPosted()
00045 && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
00046 $form->doSubmit();
00047 } else {
00048 $form->showForm( '' );
00049 }
00050 }
00051
00056 class MovePageForm {
00057 var $oldTitle, $newTitle; # Objects
00058 var $reason; # Text input
00059 var $moveTalk, $deleteAndMove, $moveSubpages, $fixRedirects, $leaveRedirect; # Checks
00060
00061 private $watch = false;
00062
00063 function __construct( $oldTitle, $newTitle ) {
00064 global $wgRequest;
00065 $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
00066 $this->oldTitle = $oldTitle;
00067 $this->newTitle = $newTitle;
00068 $this->reason = $wgRequest->getText( 'wpReason' );
00069 if ( $wgRequest->wasPosted() ) {
00070 $this->moveTalk = $wgRequest->getBool( 'wpMovetalk', false );
00071 $this->fixRedirects = $wgRequest->getBool( 'wpFixRedirects', false );
00072 $this->leaveRedirect = $wgRequest->getBool( 'wpLeaveRedirect', false );
00073 } else {
00074 $this->moveTalk = $wgRequest->getBool( 'wpMovetalk', true );
00075 $this->fixRedirects = $wgRequest->getBool( 'wpFixRedirects', true );
00076 $this->leaveRedirect = $wgRequest->getBool( 'wpLeaveRedirect', true );
00077 }
00078 $this->moveSubpages = $wgRequest->getBool( 'wpMovesubpages', false );
00079 $this->deleteAndMove = $wgRequest->getBool( 'wpDeleteAndMove' ) && $wgRequest->getBool( 'wpConfirm' );
00080 $this->watch = $wgRequest->getCheck( 'wpWatch' );
00081 }
00082
00089 function showForm( $err ) {
00090 global $wgOut, $wgUser, $wgFixDoubleRedirects;
00091
00092 $skin = $wgUser->getSkin();
00093
00094 $oldTitleLink = $skin->makeLinkObj( $this->oldTitle );
00095
00096 $wgOut->setPagetitle( wfMsg( 'move-page', $this->oldTitle->getPrefixedText() ) );
00097 $wgOut->setSubtitle( wfMsg( 'move-page-backlink', $oldTitleLink ) );
00098
00099 $newTitle = $this->newTitle;
00100
00101 if( !$newTitle ) {
00102 # Show the current title as a default
00103 # when the form is first opened.
00104 $newTitle = $this->oldTitle;
00105 }
00106 else {
00107 if( empty($err) ) {
00108 # If a title was supplied, probably from the move log revert
00109 # link, check for validity. We can then show some diagnostic
00110 # information and save a click.
00111 $newerr = $this->oldTitle->isValidMoveOperation( $newTitle );
00112 if( $newerr ) {
00113 $err = $newerr[0];
00114 }
00115 }
00116 }
00117
00118 if ( !empty($err) && $err[0] == 'articleexists' && $wgUser->isAllowed( 'delete' ) ) {
00119 $wgOut->addWikiMsg( 'delete_and_move_text', $newTitle->getPrefixedText() );
00120 $movepagebtn = wfMsg( 'delete_and_move' );
00121 $submitVar = 'wpDeleteAndMove';
00122 $confirm = "
00123 <tr>
00124 <td></td>
00125 <td class='mw-input'>" .
00126 Xml::checkLabel( wfMsg( 'delete_and_move_confirm' ), 'wpConfirm', 'wpConfirm' ) .
00127 "</td>
00128 </tr>";
00129 $err = '';
00130 } else {
00131 $wgOut->addWikiMsg( 'movepagetext' );
00132 $movepagebtn = wfMsg( 'movepagebtn' );
00133 $submitVar = 'wpMove';
00134 $confirm = false;
00135 }
00136
00137 $oldTalk = $this->oldTitle->getTalkPage();
00138 $considerTalk = ( !$this->oldTitle->isTalkPage() && $oldTalk->exists() );
00139
00140 $dbr = wfGetDB( DB_SLAVE );
00141 if ( $wgFixDoubleRedirects ) {
00142 $hasRedirects = $dbr->selectField( 'redirect', '1',
00143 array(
00144 'rd_namespace' => $this->oldTitle->getNamespace(),
00145 'rd_title' => $this->oldTitle->getDBkey(),
00146 ) , __METHOD__ );
00147 } else {
00148 $hasRedirects = false;
00149 }
00150
00151 if ( $considerTalk ) {
00152 $wgOut->addWikiMsg( 'movepagetalktext' );
00153 }
00154
00155 $titleObj = SpecialPage::getTitleFor( 'Movepage' );
00156 $token = htmlspecialchars( $wgUser->editToken() );
00157
00158 if ( !empty($err) ) {
00159 $wgOut->setSubtitle( wfMsg( 'formerror' ) );
00160 if( $err[0] == 'hookaborted' ) {
00161 $hookErr = $err[1];
00162 $errMsg = "<p><strong class=\"error\">$hookErr</strong></p>\n";
00163 $wgOut->addHTML( $errMsg );
00164 } else {
00165 $wgOut->wrapWikiMsg( '<p><strong class="error">$1</strong></p>', $err );
00166 }
00167 }
00168
00169 $wgOut->addHTML(
00170 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( 'action=submit' ), 'id' => 'movepage' ) ) .
00171 Xml::openElement( 'fieldset' ) .
00172 Xml::element( 'legend', null, wfMsg( 'move-page-legend' ) ) .
00173 Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-movepage-table' ) ) .
00174 "<tr>
00175 <td class='mw-label'>" .
00176 wfMsgHtml( 'movearticle' ) .
00177 "</td>
00178 <td class='mw-input'>
00179 <strong>{$oldTitleLink}</strong>
00180 </td>
00181 </tr>
00182 <tr>
00183 <td class='mw-label'>" .
00184 Xml::label( wfMsg( 'newtitle' ), 'wpNewTitle' ) .
00185 "</td>
00186 <td class='mw-input'>" .
00187 Xml::input( 'wpNewTitle', 40, $newTitle->getPrefixedText(), array( 'type' => 'text', 'id' => 'wpNewTitle' ) ) .
00188 Xml::hidden( 'wpOldTitle', $this->oldTitle->getPrefixedText() ) .
00189 "</td>
00190 </tr>
00191 <tr>
00192 <td class='mw-label'>" .
00193 Xml::label( wfMsg( 'movereason' ), 'wpReason' ) .
00194 "</td>
00195 <td class='mw-input'>" .
00196 Xml::tags( 'textarea', array( 'name' => 'wpReason', 'id' => 'wpReason', 'cols' => 60, 'rows' => 2 ), htmlspecialchars( $this->reason ) ) .
00197 "</td>
00198 </tr>"
00199 );
00200
00201 if( $considerTalk ) {
00202 $wgOut->addHTML( "
00203 <tr>
00204 <td></td>
00205 <td class='mw-input'>" .
00206 Xml::checkLabel( wfMsg( 'movetalk' ), 'wpMovetalk', 'wpMovetalk', $this->moveTalk ) .
00207 "</td>
00208 </tr>"
00209 );
00210 }
00211
00212 if ( $wgUser->isAllowed( 'suppressredirect' ) ) {
00213 $wgOut->addHTML( "
00214 <tr>
00215 <td></td>
00216 <td class='mw-input' >" .
00217 Xml::checkLabel( wfMsg( 'move-leave-redirect' ), 'wpLeaveRedirect',
00218 'wpLeaveRedirect', $this->leaveRedirect ) .
00219 "</td>
00220 </tr>"
00221 );
00222 }
00223
00224 if ( $hasRedirects ) {
00225 $wgOut->addHTML( "
00226 <tr>
00227 <td></td>
00228 <td class='mw-input' >" .
00229 Xml::checkLabel( wfMsg( 'fix-double-redirects' ), 'wpFixRedirects',
00230 'wpFixRedirects', $this->fixRedirects ) .
00231 "</td>
00232 </tr>"
00233 );
00234 }
00235
00236 if( ($this->oldTitle->hasSubpages() || $this->oldTitle->getTalkPage()->hasSubpages())
00237 && $this->oldTitle->userCan( 'move-subpages' ) )
00238 {
00239 global $wgMaximumMovedPages, $wgLang;
00240
00241 $wgOut->addHTML( "
00242 <tr>
00243 <td></td>
00244 <td class=\"mw-input\">" .
00245 Xml::checkLabel( wfMsgExt(
00246 ( $this->oldTitle->hasSubpages()
00247 ? 'move-subpages'
00248 : 'move-talk-subpages' ),
00249 array( 'parsemag' ),
00250 $wgLang->formatNum( $wgMaximumMovedPages ),
00251 # $2 to allow use of PLURAL in message.
00252 $wgMaximumMovedPages
00253 ),
00254 'wpMovesubpages', 'wpMovesubpages',
00255 # Don't check the box if we only have talk subpages to
00256 # move and we aren't moving the talk page.
00257 $this->moveSubpages && ($this->oldTitle->hasSubpages() || $this->moveTalk)
00258 ) .
00259 "</td>
00260 </tr>"
00261 );
00262 }
00263
00264 $watchChecked = $this->watch || $wgUser->getBoolOption( 'watchmoves' )
00265 || $this->oldTitle->userIsWatching();
00266 $wgOut->addHTML( "
00267 <tr>
00268 <td></td>
00269 <td class='mw-input'>" .
00270 Xml::checkLabel( wfMsg( 'move-watch' ), 'wpWatch', 'watch', $watchChecked ) .
00271 "</td>
00272 </tr>
00273 {$confirm}
00274 <tr>
00275 <td> </td>
00276 <td class='mw-submit'>" .
00277 Xml::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
00278 "</td>
00279 </tr>" .
00280 Xml::closeElement( 'table' ) .
00281 Xml::hidden( 'wpEditToken', $token ) .
00282 Xml::closeElement( 'fieldset' ) .
00283 Xml::closeElement( 'form' ) .
00284 "\n"
00285 );
00286
00287 $this->showLogFragment( $this->oldTitle, $wgOut );
00288 $this->showSubpages( $this->oldTitle, $wgOut );
00289
00290 }
00291
00292 function doSubmit() {
00293 global $wgOut, $wgUser, $wgRequest, $wgMaximumMovedPages, $wgLang;
00294 global $wgFixDoubleRedirects;
00295
00296 if ( $wgUser->pingLimiter( 'move' ) ) {
00297 $wgOut->rateLimited();
00298 return;
00299 }
00300
00301 $ot = $this->oldTitle;
00302 $nt = $this->newTitle;
00303
00304 # Delete to make way if requested
00305 if ( $wgUser->isAllowed( 'delete' ) && $this->deleteAndMove ) {
00306 $article = new Article( $nt );
00307
00308 # Disallow deletions of big articles
00309 $bigHistory = $article->isBigDeletion();
00310 if( $bigHistory && !$nt->userCan( 'bigdelete' ) ) {
00311 global $wgLang, $wgDeleteRevisionsLimit;
00312 $this->showForm( array('delete-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ) );
00313 return;
00314 }
00315
00316
00317 $file = wfLocalFile( $nt );
00318 if( $file->exists() ) {
00319 $file->delete( wfMsgForContent( 'delete_and_move_reason' ), false );
00320 }
00321
00322
00323 $article->doDelete( wfMsgForContent( 'delete_and_move_reason' ) );
00324 }
00325
00326 # don't allow moving to pages with # in
00327 if ( !$nt || $nt->getFragment() != '' ) {
00328 $this->showForm( 'badtitletext' );
00329 return;
00330 }
00331
00332 if ( $wgUser->isAllowed( 'suppressredirect' ) ) {
00333 $createRedirect = $this->leaveRedirect;
00334 } else {
00335 $createRedirect = true;
00336 }
00337
00338 $error = $ot->moveTo( $nt, true, $this->reason, $createRedirect );
00339 if ( $error !== true ) {
00340 # FIXME: show all the errors in a list, not just the first one
00341 $this->showForm( reset( $error ) );
00342 return;
00343 }
00344
00345 if ( $wgFixDoubleRedirects && $this->fixRedirects ) {
00346 DoubleRedirectJob::fixRedirects( 'move', $ot, $nt );
00347 }
00348
00349 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this , &$ot , &$nt ) ) ;
00350
00351 $wgOut->setPagetitle( wfMsg( 'pagemovedsub' ) );
00352
00353 $oldUrl = $ot->getFullUrl( 'redirect=no' );
00354 $newUrl = $nt->getFullUrl();
00355 $oldText = $ot->getPrefixedText();
00356 $newText = $nt->getPrefixedText();
00357 $oldLink = "<span class='plainlinks'>[$oldUrl $oldText]</span>";
00358 $newLink = "<span class='plainlinks'>[$newUrl $newText]</span>";
00359
00360 $msgName = $createRedirect ? 'movepage-moved-redirect' : 'movepage-moved-noredirect';
00361 $wgOut->addWikiMsg( 'movepage-moved', $oldLink, $newLink, $oldText, $newText );
00362 $wgOut->addWikiMsg( $msgName );
00363
00364 # Now we move extra pages we've been asked to move: subpages and talk
00365 # pages. First, if the old page or the new page is a talk page, we
00366 # can't move any talk pages: cancel that.
00367 if( $ot->isTalkPage() || $nt->isTalkPage() ) {
00368 $this->moveTalk = false;
00369 }
00370
00371 if( !$ot->userCan( 'move-subpages' ) ) {
00372 $this->moveSubpages = false;
00373 }
00374
00375 # Next make a list of id's. This might be marginally less efficient
00376 # than a more direct method, but this is not a highly performance-cri-
00377 # tical code path and readable code is more important here.
00378 #
00379 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
00380 # 4 might get confused. If so, consider rewriting as a UNION.
00381 #
00382 # If the target namespace doesn't allow subpages, moving with subpages
00383 # would mean that you couldn't move them back in one operation, which
00384 # is bad. FIXME: A specific error message should be given in this
00385 # case.
00386
00387
00388 $dbr = wfGetDB( DB_MASTER );
00389 if( $this->moveSubpages && (
00390 MWNamespace::hasSubpages( $nt->getNamespace() ) || (
00391 $this->moveTalk &&
00392 MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() )
00393 )
00394 ) ) {
00395 $conds = array(
00396 'page_title LIKE '.$dbr->addQuotes( $dbr->escapeLike( $ot->getDBkey() ) . '/%' )
00397 .' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
00398 );
00399 $conds['page_namespace'] = array();
00400 if( MWNamespace::hasSubpages( $nt->getNamespace() ) ) {
00401 $conds['page_namespace'] []= $ot->getNamespace();
00402 }
00403 if( $this->moveTalk && MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() ) ) {
00404 $conds['page_namespace'] []= $ot->getTalkPage()->getNamespace();
00405 }
00406 } elseif( $this->moveTalk ) {
00407 $conds = array(
00408 'page_namespace' => $ot->getTalkPage()->getNamespace(),
00409 'page_title' => $ot->getDBKey()
00410 );
00411 } else {
00412 # Skip the query
00413 $conds = null;
00414 }
00415
00416 $extraPages = array();
00417 if( !is_null( $conds ) ) {
00418 $extraPages = TitleArray::newFromResult(
00419 $dbr->select( 'page',
00420 array( 'page_id', 'page_namespace', 'page_title' ),
00421 $conds,
00422 __METHOD__
00423 )
00424 );
00425 }
00426
00427 $extraOutput = array();
00428 $skin = $wgUser->getSkin();
00429 $count = 1;
00430 foreach( $extraPages as $oldSubpage ) {
00431 if( $oldSubpage->getArticleId() == $ot->getArticleId() ) {
00432 # Already did this one.
00433 continue;
00434 }
00435
00436 $newPageName = preg_replace(
00437 '#^'.preg_quote( $ot->getDBKey(), '#' ).'#',
00438 $nt->getDBKey(),
00439 $oldSubpage->getDBKey()
00440 );
00441 if( $oldSubpage->isTalkPage() ) {
00442 $newNs = $nt->getTalkPage()->getNamespace();
00443 } else {
00444 $newNs = $nt->getSubjectPage()->getNamespace();
00445 }
00446 # Bug 14385: we need makeTitleSafe because the new page names may
00447 # be longer than 255 characters.
00448 $newSubpage = Title::makeTitleSafe( $newNs, $newPageName );
00449 if( !$newSubpage ) {
00450 $oldLink = $skin->makeKnownLinkObj( $oldSubpage );
00451 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink,
00452 htmlspecialchars(Title::makeName( $newNs, $newPageName )));
00453 continue;
00454 }
00455
00456 # This was copy-pasted from Renameuser, bleh.
00457 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
00458 $link = $skin->makeKnownLinkObj( $newSubpage );
00459 $extraOutput []= wfMsgHtml( 'movepage-page-exists', $link );
00460 } else {
00461 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason, $createRedirect );
00462 if( $success === true ) {
00463 if ( $this->fixRedirects ) {
00464 DoubleRedirectJob::fixRedirects( 'move', $oldSubpage, $newSubpage );
00465 }
00466 $oldLink = $skin->makeKnownLinkObj( $oldSubpage, '', 'redirect=no' );
00467 $newLink = $skin->makeKnownLinkObj( $newSubpage );
00468 $extraOutput []= wfMsgHtml( 'movepage-page-moved', $oldLink, $newLink );
00469 } else {
00470 $oldLink = $skin->makeKnownLinkObj( $oldSubpage );
00471 $newLink = $skin->makeLinkObj( $newSubpage );
00472 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink, $newLink );
00473 }
00474 }
00475
00476 ++$count;
00477 if( $count >= $wgMaximumMovedPages ) {
00478 $extraOutput []= wfMsgExt( 'movepage-max-pages', array( 'parsemag', 'escape' ), $wgLang->formatNum( $wgMaximumMovedPages ) );
00479 break;
00480 }
00481 }
00482
00483 if( $extraOutput !== array() ) {
00484 $wgOut->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
00485 }
00486
00487 # Deal with watches (we don't watch subpages)
00488 if( $this->watch ) {
00489 $wgUser->addWatch( $ot );
00490 $wgUser->addWatch( $nt );
00491 } else {
00492 $wgUser->removeWatch( $ot );
00493 $wgUser->removeWatch( $nt );
00494 }
00495 }
00496
00497 function showLogFragment( $title, &$out ) {
00498 $out->addHTML( Xml::element( 'h2', NULL, LogPage::logName( 'move' ) ) );
00499 LogEventsList::showLogExtract( $out, 'move', $title->getPrefixedText() );
00500 }
00501
00502 function showSubpages( $title, $out ) {
00503 global $wgUser, $wgLang;
00504
00505 if( !MWNamespace::hasSubpages( $title->getNamespace() ) )
00506 return;
00507
00508 $subpages = $title->getSubpages();
00509 $count = $subpages instanceof TitleArray ? $subpages->count() : 0;
00510
00511 $out->wrapWikiMsg( '== $1 ==', array( 'movesubpage', $count ) );
00512
00513 # No subpages.
00514 if ( $count == 0 ) {
00515 $out->addWikiMsg( 'movenosubpage' );
00516 return;
00517 }
00518
00519 $out->addWikiMsg( 'movesubpagetext', $wgLang->formatNum( $count ) );
00520 $skin = $wgUser->getSkin();
00521 $out->addHTML( "<ul>\n" );
00522
00523 foreach( $subpages as $subpage ) {
00524 $link = $skin->link( $subpage );
00525 $out->addHTML( "<li>$link</li>\n" );
00526 }
00527 $out->addHTML( "</ul>\n" );
00528 }
00529 }
00530