SpecialMovepage.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. <?php
  2. /**
  3. * @file
  4. * @ingroup SpecialPage
  5. */
  6. /**
  7. * Constructor
  8. */
  9. function wfSpecialMovepage( $par = null ) {
  10. global $wgUser, $wgOut, $wgRequest, $action;
  11. # Check for database lock
  12. if ( wfReadOnly() ) {
  13. $wgOut->readOnlyPage();
  14. return;
  15. }
  16. $target = isset( $par ) ? $par : $wgRequest->getVal( 'target' );
  17. $oldTitleText = $wgRequest->getText( 'wpOldTitle', $target );
  18. $newTitleText = $wgRequest->getText( 'wpNewTitle' );
  19. $oldTitle = Title::newFromText( $oldTitleText );
  20. $newTitle = Title::newFromText( $newTitleText );
  21. if( is_null( $oldTitle ) ) {
  22. $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
  23. return;
  24. }
  25. if( !$oldTitle->exists() ) {
  26. $wgOut->showErrorPage( 'nopagetitle', 'nopagetext' );
  27. return;
  28. }
  29. # Check rights
  30. $permErrors = $oldTitle->getUserPermissionsErrors( 'move', $wgUser );
  31. if( !empty( $permErrors ) ) {
  32. $wgOut->showPermissionsErrorPage( $permErrors );
  33. return;
  34. }
  35. $form = new MovePageForm( $oldTitle, $newTitle );
  36. if ( 'submit' == $action && $wgRequest->wasPosted()
  37. && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
  38. $form->doSubmit();
  39. } else {
  40. $form->showForm( '' );
  41. }
  42. }
  43. /**
  44. * HTML form for Special:Movepage
  45. * @ingroup SpecialPage
  46. */
  47. class MovePageForm {
  48. var $oldTitle, $newTitle; # Objects
  49. var $reason; # Text input
  50. var $moveTalk, $deleteAndMove, $moveSubpages, $fixRedirects, $leaveRedirect; # Checks
  51. private $watch = false;
  52. function __construct( $oldTitle, $newTitle ) {
  53. global $wgRequest;
  54. $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
  55. $this->oldTitle = $oldTitle;
  56. $this->newTitle = $newTitle;
  57. $this->reason = $wgRequest->getText( 'wpReason' );
  58. if ( $wgRequest->wasPosted() ) {
  59. $this->moveTalk = $wgRequest->getBool( 'wpMovetalk', false );
  60. $this->fixRedirects = $wgRequest->getBool( 'wpFixRedirects', false );
  61. $this->leaveRedirect = $wgRequest->getBool( 'wpLeaveRedirect', false );
  62. } else {
  63. $this->moveTalk = $wgRequest->getBool( 'wpMovetalk', true );
  64. $this->fixRedirects = $wgRequest->getBool( 'wpFixRedirects', true );
  65. $this->leaveRedirect = $wgRequest->getBool( 'wpLeaveRedirect', true );
  66. }
  67. $this->moveSubpages = $wgRequest->getBool( 'wpMovesubpages', false );
  68. $this->deleteAndMove = $wgRequest->getBool( 'wpDeleteAndMove' ) && $wgRequest->getBool( 'wpConfirm' );
  69. $this->watch = $wgRequest->getCheck( 'wpWatch' );
  70. }
  71. /**
  72. * Show the form
  73. * @param mixed $err Error message. May either be a string message name or
  74. * array message name and parameters, like the second argument to
  75. * OutputPage::wrapWikiMsg().
  76. */
  77. function showForm( $err ) {
  78. global $wgOut, $wgUser, $wgFixDoubleRedirects;
  79. $skin = $wgUser->getSkin();
  80. $oldTitleLink = $skin->makeLinkObj( $this->oldTitle );
  81. $wgOut->setPagetitle( wfMsg( 'move-page', $this->oldTitle->getPrefixedText() ) );
  82. $wgOut->setSubtitle( wfMsg( 'move-page-backlink', $oldTitleLink ) );
  83. $newTitle = $this->newTitle;
  84. if( !$newTitle ) {
  85. # Show the current title as a default
  86. # when the form is first opened.
  87. $newTitle = $this->oldTitle;
  88. }
  89. else {
  90. if( empty($err) ) {
  91. # If a title was supplied, probably from the move log revert
  92. # link, check for validity. We can then show some diagnostic
  93. # information and save a click.
  94. $newerr = $this->oldTitle->isValidMoveOperation( $newTitle );
  95. if( $newerr ) {
  96. $err = $newerr[0];
  97. }
  98. }
  99. }
  100. if ( !empty($err) && $err[0] == 'articleexists' && $wgUser->isAllowed( 'delete' ) ) {
  101. $wgOut->addWikiMsg( 'delete_and_move_text', $newTitle->getPrefixedText() );
  102. $movepagebtn = wfMsg( 'delete_and_move' );
  103. $submitVar = 'wpDeleteAndMove';
  104. $confirm = "
  105. <tr>
  106. <td></td>
  107. <td class='mw-input'>" .
  108. Xml::checkLabel( wfMsg( 'delete_and_move_confirm' ), 'wpConfirm', 'wpConfirm' ) .
  109. "</td>
  110. </tr>";
  111. $err = '';
  112. } else {
  113. $wgOut->addWikiMsg( 'movepagetext' );
  114. $movepagebtn = wfMsg( 'movepagebtn' );
  115. $submitVar = 'wpMove';
  116. $confirm = false;
  117. }
  118. $oldTalk = $this->oldTitle->getTalkPage();
  119. $considerTalk = ( !$this->oldTitle->isTalkPage() && $oldTalk->exists() );
  120. $dbr = wfGetDB( DB_SLAVE );
  121. if ( $wgFixDoubleRedirects ) {
  122. $hasRedirects = $dbr->selectField( 'redirect', '1',
  123. array(
  124. 'rd_namespace' => $this->oldTitle->getNamespace(),
  125. 'rd_title' => $this->oldTitle->getDBkey(),
  126. ) , __METHOD__ );
  127. } else {
  128. $hasRedirects = false;
  129. }
  130. if ( $considerTalk ) {
  131. $wgOut->addWikiMsg( 'movepagetalktext' );
  132. }
  133. $titleObj = SpecialPage::getTitleFor( 'Movepage' );
  134. $token = htmlspecialchars( $wgUser->editToken() );
  135. if ( !empty($err) ) {
  136. $wgOut->setSubtitle( wfMsg( 'formerror' ) );
  137. if( $err[0] == 'hookaborted' ) {
  138. $hookErr = $err[1];
  139. $errMsg = "<p><strong class=\"error\">$hookErr</strong></p>\n";
  140. $wgOut->addHTML( $errMsg );
  141. } else {
  142. $wgOut->wrapWikiMsg( '<p><strong class="error">$1</strong></p>', $err );
  143. }
  144. }
  145. $wgOut->addHTML(
  146. Xml::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( 'action=submit' ), 'id' => 'movepage' ) ) .
  147. Xml::openElement( 'fieldset' ) .
  148. Xml::element( 'legend', null, wfMsg( 'move-page-legend' ) ) .
  149. Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-movepage-table' ) ) .
  150. "<tr>
  151. <td class='mw-label'>" .
  152. wfMsgHtml( 'movearticle' ) .
  153. "</td>
  154. <td class='mw-input'>
  155. <strong>{$oldTitleLink}</strong>
  156. </td>
  157. </tr>
  158. <tr>
  159. <td class='mw-label'>" .
  160. Xml::label( wfMsg( 'newtitle' ), 'wpNewTitle' ) .
  161. "</td>
  162. <td class='mw-input'>" .
  163. Xml::input( 'wpNewTitle', 40, $newTitle->getPrefixedText(), array( 'type' => 'text', 'id' => 'wpNewTitle' ) ) .
  164. Xml::hidden( 'wpOldTitle', $this->oldTitle->getPrefixedText() ) .
  165. "</td>
  166. </tr>
  167. <tr>
  168. <td class='mw-label'>" .
  169. Xml::label( wfMsg( 'movereason' ), 'wpReason' ) .
  170. "</td>
  171. <td class='mw-input'>" .
  172. Xml::tags( 'textarea', array( 'name' => 'wpReason', 'id' => 'wpReason', 'cols' => 60, 'rows' => 2 ), htmlspecialchars( $this->reason ) ) .
  173. "</td>
  174. </tr>"
  175. );
  176. if( $considerTalk ) {
  177. $wgOut->addHTML( "
  178. <tr>
  179. <td></td>
  180. <td class='mw-input'>" .
  181. Xml::checkLabel( wfMsg( 'movetalk' ), 'wpMovetalk', 'wpMovetalk', $this->moveTalk ) .
  182. "</td>
  183. </tr>"
  184. );
  185. }
  186. if ( $wgUser->isAllowed( 'suppressredirect' ) ) {
  187. $wgOut->addHTML( "
  188. <tr>
  189. <td></td>
  190. <td class='mw-input' >" .
  191. Xml::checkLabel( wfMsg( 'move-leave-redirect' ), 'wpLeaveRedirect',
  192. 'wpLeaveRedirect', $this->leaveRedirect ) .
  193. "</td>
  194. </tr>"
  195. );
  196. }
  197. if ( $hasRedirects ) {
  198. $wgOut->addHTML( "
  199. <tr>
  200. <td></td>
  201. <td class='mw-input' >" .
  202. Xml::checkLabel( wfMsg( 'fix-double-redirects' ), 'wpFixRedirects',
  203. 'wpFixRedirects', $this->fixRedirects ) .
  204. "</td>
  205. </tr>"
  206. );
  207. }
  208. if( ($this->oldTitle->hasSubpages() || $this->oldTitle->getTalkPage()->hasSubpages())
  209. && $this->oldTitle->userCan( 'move-subpages' ) )
  210. {
  211. global $wgMaximumMovedPages, $wgLang;
  212. $wgOut->addHTML( "
  213. <tr>
  214. <td></td>
  215. <td class=\"mw-input\">" .
  216. Xml::checkLabel( wfMsgExt(
  217. ( $this->oldTitle->hasSubpages()
  218. ? 'move-subpages'
  219. : 'move-talk-subpages' ),
  220. array( 'parsemag' ),
  221. $wgLang->formatNum( $wgMaximumMovedPages ),
  222. # $2 to allow use of PLURAL in message.
  223. $wgMaximumMovedPages
  224. ),
  225. 'wpMovesubpages', 'wpMovesubpages',
  226. # Don't check the box if we only have talk subpages to
  227. # move and we aren't moving the talk page.
  228. $this->moveSubpages && ($this->oldTitle->hasSubpages() || $this->moveTalk)
  229. ) .
  230. "</td>
  231. </tr>"
  232. );
  233. }
  234. $watchChecked = $this->watch || $wgUser->getBoolOption( 'watchmoves' )
  235. || $this->oldTitle->userIsWatching();
  236. $wgOut->addHTML( "
  237. <tr>
  238. <td></td>
  239. <td class='mw-input'>" .
  240. Xml::checkLabel( wfMsg( 'move-watch' ), 'wpWatch', 'watch', $watchChecked ) .
  241. "</td>
  242. </tr>
  243. {$confirm}
  244. <tr>
  245. <td>&nbsp;</td>
  246. <td class='mw-submit'>" .
  247. Xml::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
  248. "</td>
  249. </tr>" .
  250. Xml::closeElement( 'table' ) .
  251. Xml::hidden( 'wpEditToken', $token ) .
  252. Xml::closeElement( 'fieldset' ) .
  253. Xml::closeElement( 'form' ) .
  254. "\n"
  255. );
  256. $this->showLogFragment( $this->oldTitle, $wgOut );
  257. $this->showSubpages( $this->oldTitle, $wgOut );
  258. }
  259. function doSubmit() {
  260. global $wgOut, $wgUser, $wgRequest, $wgMaximumMovedPages, $wgLang;
  261. global $wgFixDoubleRedirects;
  262. if ( $wgUser->pingLimiter( 'move' ) ) {
  263. $wgOut->rateLimited();
  264. return;
  265. }
  266. $ot = $this->oldTitle;
  267. $nt = $this->newTitle;
  268. # Delete to make way if requested
  269. if ( $wgUser->isAllowed( 'delete' ) && $this->deleteAndMove ) {
  270. $article = new Article( $nt );
  271. # Disallow deletions of big articles
  272. $bigHistory = $article->isBigDeletion();
  273. if( $bigHistory && !$nt->userCan( 'bigdelete' ) ) {
  274. global $wgLang, $wgDeleteRevisionsLimit;
  275. $this->showForm( array('delete-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ) );
  276. return;
  277. }
  278. // Delete an associated image if there is
  279. $file = wfLocalFile( $nt );
  280. if( $file->exists() ) {
  281. $file->delete( wfMsgForContent( 'delete_and_move_reason' ), false );
  282. }
  283. // This may output an error message and exit
  284. $article->doDelete( wfMsgForContent( 'delete_and_move_reason' ) );
  285. }
  286. # don't allow moving to pages with # in
  287. if ( !$nt || $nt->getFragment() != '' ) {
  288. $this->showForm( 'badtitletext' );
  289. return;
  290. }
  291. if ( $wgUser->isAllowed( 'suppressredirect' ) ) {
  292. $createRedirect = $this->leaveRedirect;
  293. } else {
  294. $createRedirect = true;
  295. }
  296. $error = $ot->moveTo( $nt, true, $this->reason, $createRedirect );
  297. if ( $error !== true ) {
  298. # FIXME: show all the errors in a list, not just the first one
  299. $this->showForm( reset( $error ) );
  300. return;
  301. }
  302. if ( $wgFixDoubleRedirects && $this->fixRedirects ) {
  303. DoubleRedirectJob::fixRedirects( 'move', $ot, $nt );
  304. }
  305. wfRunHooks( 'SpecialMovepageAfterMove', array( &$this , &$ot , &$nt ) ) ;
  306. $wgOut->setPagetitle( wfMsg( 'pagemovedsub' ) );
  307. $oldUrl = $ot->getFullUrl( 'redirect=no' );
  308. $newUrl = $nt->getFullUrl();
  309. $oldText = $ot->getPrefixedText();
  310. $newText = $nt->getPrefixedText();
  311. $oldLink = "<span class='plainlinks'>[$oldUrl $oldText]</span>";
  312. $newLink = "<span class='plainlinks'>[$newUrl $newText]</span>";
  313. $msgName = $createRedirect ? 'movepage-moved-redirect' : 'movepage-moved-noredirect';
  314. $wgOut->addWikiMsg( 'movepage-moved', $oldLink, $newLink, $oldText, $newText );
  315. $wgOut->addWikiMsg( $msgName );
  316. # Now we move extra pages we've been asked to move: subpages and talk
  317. # pages. First, if the old page or the new page is a talk page, we
  318. # can't move any talk pages: cancel that.
  319. if( $ot->isTalkPage() || $nt->isTalkPage() ) {
  320. $this->moveTalk = false;
  321. }
  322. if( !$ot->userCan( 'move-subpages' ) ) {
  323. $this->moveSubpages = false;
  324. }
  325. # Next make a list of id's. This might be marginally less efficient
  326. # than a more direct method, but this is not a highly performance-cri-
  327. # tical code path and readable code is more important here.
  328. #
  329. # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
  330. # 4 might get confused. If so, consider rewriting as a UNION.
  331. #
  332. # If the target namespace doesn't allow subpages, moving with subpages
  333. # would mean that you couldn't move them back in one operation, which
  334. # is bad. FIXME: A specific error message should be given in this
  335. # case.
  336. // FIXME: Use Title::moveSubpages() here
  337. $dbr = wfGetDB( DB_MASTER );
  338. if( $this->moveSubpages && (
  339. MWNamespace::hasSubpages( $nt->getNamespace() ) || (
  340. $this->moveTalk &&
  341. MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() )
  342. )
  343. ) ) {
  344. $conds = array(
  345. 'page_title LIKE '.$dbr->addQuotes( $dbr->escapeLike( $ot->getDBkey() ) . '/%' )
  346. .' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
  347. );
  348. $conds['page_namespace'] = array();
  349. if( MWNamespace::hasSubpages( $nt->getNamespace() ) ) {
  350. $conds['page_namespace'] []= $ot->getNamespace();
  351. }
  352. if( $this->moveTalk && MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() ) ) {
  353. $conds['page_namespace'] []= $ot->getTalkPage()->getNamespace();
  354. }
  355. } elseif( $this->moveTalk ) {
  356. $conds = array(
  357. 'page_namespace' => $ot->getTalkPage()->getNamespace(),
  358. 'page_title' => $ot->getDBKey()
  359. );
  360. } else {
  361. # Skip the query
  362. $conds = null;
  363. }
  364. $extraPages = array();
  365. if( !is_null( $conds ) ) {
  366. $extraPages = TitleArray::newFromResult(
  367. $dbr->select( 'page',
  368. array( 'page_id', 'page_namespace', 'page_title' ),
  369. $conds,
  370. __METHOD__
  371. )
  372. );
  373. }
  374. $extraOutput = array();
  375. $skin = $wgUser->getSkin();
  376. $count = 1;
  377. foreach( $extraPages as $oldSubpage ) {
  378. if( $oldSubpage->getArticleId() == $ot->getArticleId() ) {
  379. # Already did this one.
  380. continue;
  381. }
  382. $newPageName = preg_replace(
  383. '#^'.preg_quote( $ot->getDBKey(), '#' ).'#',
  384. $nt->getDBKey(),
  385. $oldSubpage->getDBKey()
  386. );
  387. if( $oldSubpage->isTalkPage() ) {
  388. $newNs = $nt->getTalkPage()->getNamespace();
  389. } else {
  390. $newNs = $nt->getSubjectPage()->getNamespace();
  391. }
  392. # Bug 14385: we need makeTitleSafe because the new page names may
  393. # be longer than 255 characters.
  394. $newSubpage = Title::makeTitleSafe( $newNs, $newPageName );
  395. if( !$newSubpage ) {
  396. $oldLink = $skin->makeKnownLinkObj( $oldSubpage );
  397. $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink,
  398. htmlspecialchars(Title::makeName( $newNs, $newPageName )));
  399. continue;
  400. }
  401. # This was copy-pasted from Renameuser, bleh.
  402. if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
  403. $link = $skin->makeKnownLinkObj( $newSubpage );
  404. $extraOutput []= wfMsgHtml( 'movepage-page-exists', $link );
  405. } else {
  406. $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason, $createRedirect );
  407. if( $success === true ) {
  408. if ( $this->fixRedirects ) {
  409. DoubleRedirectJob::fixRedirects( 'move', $oldSubpage, $newSubpage );
  410. }
  411. $oldLink = $skin->makeKnownLinkObj( $oldSubpage, '', 'redirect=no' );
  412. $newLink = $skin->makeKnownLinkObj( $newSubpage );
  413. $extraOutput []= wfMsgHtml( 'movepage-page-moved', $oldLink, $newLink );
  414. } else {
  415. $oldLink = $skin->makeKnownLinkObj( $oldSubpage );
  416. $newLink = $skin->makeLinkObj( $newSubpage );
  417. $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink, $newLink );
  418. }
  419. }
  420. ++$count;
  421. if( $count >= $wgMaximumMovedPages ) {
  422. $extraOutput []= wfMsgExt( 'movepage-max-pages', array( 'parsemag', 'escape' ), $wgLang->formatNum( $wgMaximumMovedPages ) );
  423. break;
  424. }
  425. }
  426. if( $extraOutput !== array() ) {
  427. $wgOut->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
  428. }
  429. # Deal with watches (we don't watch subpages)
  430. if( $this->watch ) {
  431. $wgUser->addWatch( $ot );
  432. $wgUser->addWatch( $nt );
  433. } else {
  434. $wgUser->removeWatch( $ot );
  435. $wgUser->removeWatch( $nt );
  436. }
  437. }
  438. function showLogFragment( $title, &$out ) {
  439. $out->addHTML( Xml::element( 'h2', NULL, LogPage::logName( 'move' ) ) );
  440. LogEventsList::showLogExtract( $out, 'move', $title->getPrefixedText() );
  441. }
  442. function showSubpages( $title, $out ) {
  443. global $wgUser, $wgLang;
  444. if( !MWNamespace::hasSubpages( $title->getNamespace() ) )
  445. return;
  446. $subpages = $title->getSubpages();
  447. $count = $subpages instanceof TitleArray ? $subpages->count() : 0;
  448. $out->wrapWikiMsg( '== $1 ==', array( 'movesubpage', $count ) );
  449. # No subpages.
  450. if ( $count == 0 ) {
  451. $out->addWikiMsg( 'movenosubpage' );
  452. return;
  453. }
  454. $out->addWikiMsg( 'movesubpagetext', $wgLang->formatNum( $count ) );
  455. $skin = $wgUser->getSkin();
  456. $out->addHTML( "<ul>\n" );
  457. foreach( $subpages as $subpage ) {
  458. $link = $skin->link( $subpage );
  459. $out->addHTML( "<li>$link</li>\n" );
  460. }
  461. $out->addHTML( "</ul>\n" );
  462. }
  463. }