SpecialMovepage.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. <?php
  2. /**
  3. * Implements Special:Movepage
  4. *
  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.
  9. *
  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.
  14. *
  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
  19. *
  20. * @file
  21. * @ingroup SpecialPage
  22. */
  23. use MediaWiki\MediaWikiServices;
  24. /**
  25. * A special page that allows users to change page titles
  26. *
  27. * @ingroup SpecialPage
  28. */
  29. class MovePageForm extends UnlistedSpecialPage {
  30. /** @var Title */
  31. protected $oldTitle = null;
  32. /** @var Title */
  33. protected $newTitle;
  34. /** @var string Text input */
  35. protected $reason;
  36. // Checks
  37. /** @var bool */
  38. protected $moveTalk;
  39. /** @var bool */
  40. protected $deleteAndMove;
  41. /** @var bool */
  42. protected $moveSubpages;
  43. /** @var bool */
  44. protected $fixRedirects;
  45. /** @var bool */
  46. protected $leaveRedirect;
  47. /** @var bool */
  48. protected $moveOverShared;
  49. private $watch = false;
  50. public function __construct() {
  51. parent::__construct( 'Movepage' );
  52. }
  53. public function doesWrites() {
  54. return true;
  55. }
  56. public function execute( $par ) {
  57. $this->useTransactionalTimeLimit();
  58. $this->checkReadOnly();
  59. $this->setHeaders();
  60. $this->outputHeader();
  61. $request = $this->getRequest();
  62. $target = $par ?? $request->getVal( 'target' );
  63. // Yes, the use of getVal() and getText() is wanted, see T22365
  64. $oldTitleText = $request->getVal( 'wpOldTitle', $target );
  65. $this->oldTitle = Title::newFromText( $oldTitleText );
  66. if ( !$this->oldTitle ) {
  67. // Either oldTitle wasn't passed, or newFromText returned null
  68. throw new ErrorPageError( 'notargettitle', 'notargettext' );
  69. }
  70. if ( !$this->oldTitle->exists() ) {
  71. throw new ErrorPageError( 'nopagetitle', 'nopagetext' );
  72. }
  73. $newTitleTextMain = $request->getText( 'wpNewTitleMain' );
  74. $newTitleTextNs = $request->getInt( 'wpNewTitleNs', $this->oldTitle->getNamespace() );
  75. // Backwards compatibility for forms submitting here from other sources
  76. // which is more common than it should be..
  77. $newTitleText_bc = $request->getText( 'wpNewTitle' );
  78. $this->newTitle = strlen( $newTitleText_bc ) > 0
  79. ? Title::newFromText( $newTitleText_bc )
  80. : Title::makeTitleSafe( $newTitleTextNs, $newTitleTextMain );
  81. $user = $this->getUser();
  82. # Check rights
  83. $permErrors = $this->oldTitle->getUserPermissionsErrors( 'move', $user );
  84. if ( count( $permErrors ) ) {
  85. // Auto-block user's IP if the account was "hard" blocked
  86. DeferredUpdates::addCallableUpdate( function () use ( $user ) {
  87. $user->spreadAnyEditBlock();
  88. } );
  89. throw new PermissionsError( 'move', $permErrors );
  90. }
  91. $def = !$request->wasPosted();
  92. $this->reason = $request->getText( 'wpReason' );
  93. $this->moveTalk = $request->getBool( 'wpMovetalk', $def );
  94. $this->fixRedirects = $request->getBool( 'wpFixRedirects', $def );
  95. $this->leaveRedirect = $request->getBool( 'wpLeaveRedirect', $def );
  96. $this->moveSubpages = $request->getBool( 'wpMovesubpages' );
  97. $this->deleteAndMove = $request->getBool( 'wpDeleteAndMove' );
  98. $this->moveOverShared = $request->getBool( 'wpMoveOverSharedFile' );
  99. $this->watch = $request->getCheck( 'wpWatch' ) && $user->isLoggedIn();
  100. if ( $request->getVal( 'action' ) == 'submit' && $request->wasPosted()
  101. && $user->matchEditToken( $request->getVal( 'wpEditToken' ) )
  102. ) {
  103. $this->doSubmit();
  104. } else {
  105. $this->showForm( [] );
  106. }
  107. }
  108. /**
  109. * Show the form
  110. *
  111. * @param array $err Error messages. Each item is an error message.
  112. * It may either be a string message name or array message name and
  113. * parameters, like the second argument to OutputPage::wrapWikiMsg().
  114. * @param bool $isPermError Whether the error message is about user permissions.
  115. */
  116. function showForm( $err, $isPermError = false ) {
  117. $this->getSkin()->setRelevantTitle( $this->oldTitle );
  118. $out = $this->getOutput();
  119. $out->setPageTitle( $this->msg( 'move-page', $this->oldTitle->getPrefixedText() ) );
  120. $out->addModuleStyles( 'mediawiki.special' );
  121. $out->addModules( 'mediawiki.misc-authed-ooui' );
  122. $this->addHelpLink( 'Help:Moving a page' );
  123. $handlerSupportsRedirects = ContentHandler::getForTitle( $this->oldTitle )
  124. ->supportsRedirects();
  125. if ( $this->getConfig()->get( 'FixDoubleRedirects' ) ) {
  126. $out->addWikiMsg( 'movepagetext' );
  127. } else {
  128. $out->addWikiMsg( $handlerSupportsRedirects ?
  129. 'movepagetext-noredirectfixer' :
  130. 'movepagetext-noredirectsupport' );
  131. }
  132. if ( $this->oldTitle->getNamespace() == NS_USER && !$this->oldTitle->isSubpage() ) {
  133. $out->wrapWikiMsg(
  134. "<div class=\"warningbox mw-moveuserpage-warning\">\n$1\n</div>",
  135. 'moveuserpage-warning'
  136. );
  137. } elseif ( $this->oldTitle->getNamespace() == NS_CATEGORY ) {
  138. $out->wrapWikiMsg(
  139. "<div class=\"warningbox mw-movecategorypage-warning\">\n$1\n</div>",
  140. 'movecategorypage-warning'
  141. );
  142. }
  143. $deleteAndMove = false;
  144. $moveOverShared = false;
  145. $user = $this->getUser();
  146. $newTitle = $this->newTitle;
  147. if ( !$newTitle ) {
  148. # Show the current title as a default
  149. # when the form is first opened.
  150. $newTitle = $this->oldTitle;
  151. } elseif ( !count( $err ) ) {
  152. # If a title was supplied, probably from the move log revert
  153. # link, check for validity. We can then show some diagnostic
  154. # information and save a click.
  155. $mp = new MovePage( $this->oldTitle, $newTitle );
  156. $status = $mp->isValidMove();
  157. $status->merge( $mp->checkPermissions( $user, null ) );
  158. if ( $status->getErrors() ) {
  159. $err = $status->getErrorsArray();
  160. }
  161. }
  162. if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'articleexists'
  163. && MediaWikiServices::getInstance()->getPermissionManager()
  164. ->quickUserCan( 'delete', $user, $newTitle )
  165. ) {
  166. $out->wrapWikiMsg(
  167. "<div class='warningbox'>\n$1\n</div>\n",
  168. [ 'delete_and_move_text', $newTitle->getPrefixedText() ]
  169. );
  170. $deleteAndMove = true;
  171. $err = [];
  172. }
  173. if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'file-exists-sharedrepo'
  174. && MediaWikiServices::getInstance()
  175. ->getPermissionManager()
  176. ->userHasRight( $user, 'reupload-shared' )
  177. ) {
  178. $out->wrapWikiMsg(
  179. "<div class='warningbox'>\n$1\n</div>\n",
  180. [
  181. 'move-over-sharedrepo',
  182. $newTitle->getPrefixedText()
  183. ]
  184. );
  185. $moveOverShared = true;
  186. $err = [];
  187. }
  188. $oldTalk = $this->oldTitle->getTalkPage();
  189. $oldTitleSubpages = $this->oldTitle->hasSubpages();
  190. $oldTitleTalkSubpages = $this->oldTitle->getTalkPage()->hasSubpages();
  191. $canMoveSubpage = ( $oldTitleSubpages || $oldTitleTalkSubpages ) &&
  192. !count( $this->oldTitle->getUserPermissionsErrors( 'move-subpages', $user ) );
  193. # We also want to be able to move assoc. subpage talk-pages even if base page
  194. # has no associated talk page, so || with $oldTitleTalkSubpages.
  195. $considerTalk = !$this->oldTitle->isTalkPage() &&
  196. ( $oldTalk->exists()
  197. || ( $oldTitleTalkSubpages && $canMoveSubpage ) );
  198. $dbr = wfGetDB( DB_REPLICA );
  199. if ( $this->getConfig()->get( 'FixDoubleRedirects' ) ) {
  200. $hasRedirects = $dbr->selectField( 'redirect', '1',
  201. [
  202. 'rd_namespace' => $this->oldTitle->getNamespace(),
  203. 'rd_title' => $this->oldTitle->getDBkey(),
  204. ], __METHOD__ );
  205. } else {
  206. $hasRedirects = false;
  207. }
  208. if ( count( $err ) ) {
  209. if ( $isPermError ) {
  210. $action_desc = $this->msg( 'action-move' )->plain();
  211. $errMsgHtml = $this->msg( 'permissionserrorstext-withaction',
  212. count( $err ), $action_desc )->parseAsBlock();
  213. } else {
  214. $errMsgHtml = $this->msg( 'cannotmove', count( $err ) )->parseAsBlock();
  215. }
  216. if ( count( $err ) == 1 ) {
  217. $errMsg = $err[0];
  218. $errMsgName = array_shift( $errMsg );
  219. if ( $errMsgName == 'hookaborted' ) {
  220. $errMsgHtml .= "<p>{$errMsg[0]}</p>\n";
  221. } else {
  222. $errMsgHtml .= $this->msg( $errMsgName, $errMsg )->parseAsBlock();
  223. }
  224. } else {
  225. $errStr = [];
  226. foreach ( $err as $errMsg ) {
  227. if ( $errMsg[0] == 'hookaborted' ) {
  228. $errStr[] = $errMsg[1];
  229. } else {
  230. $errMsgName = array_shift( $errMsg );
  231. $errStr[] = $this->msg( $errMsgName, $errMsg )->parse();
  232. }
  233. }
  234. $errMsgHtml .= '<ul><li>' . implode( "</li>\n<li>", $errStr ) . "</li></ul>\n";
  235. }
  236. $out->addHTML( Html::errorBox( $errMsgHtml ) );
  237. }
  238. if ( $this->oldTitle->isProtected( 'move' ) ) {
  239. # Is the title semi-protected?
  240. if ( $this->oldTitle->isSemiProtected( 'move' ) ) {
  241. $noticeMsg = 'semiprotectedpagemovewarning';
  242. } else {
  243. # Then it must be protected based on static groups (regular)
  244. $noticeMsg = 'protectedpagemovewarning';
  245. }
  246. $out->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" );
  247. $out->addWikiMsg( $noticeMsg );
  248. LogEventsList::showLogExtract(
  249. $out,
  250. 'protect',
  251. $this->oldTitle,
  252. '',
  253. [ 'lim' => 1 ]
  254. );
  255. $out->addHTML( "</div>\n" );
  256. }
  257. // Length limit for wpReason and wpNewTitleMain is enforced in the
  258. // mediawiki.special.movePage module
  259. $immovableNamespaces = [];
  260. $namespaceInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
  261. foreach ( array_keys( $this->getLanguage()->getNamespaces() ) as $nsId ) {
  262. if ( !$namespaceInfo->isMovable( $nsId ) ) {
  263. $immovableNamespaces[] = $nsId;
  264. }
  265. }
  266. $out->enableOOUI();
  267. $fields = [];
  268. $fields[] = new OOUI\FieldLayout(
  269. new MediaWiki\Widget\ComplexTitleInputWidget( [
  270. 'id' => 'wpNewTitle',
  271. 'namespace' => [
  272. 'id' => 'wpNewTitleNs',
  273. 'name' => 'wpNewTitleNs',
  274. 'value' => $newTitle->getNamespace(),
  275. 'exclude' => $immovableNamespaces,
  276. ],
  277. 'title' => [
  278. 'id' => 'wpNewTitleMain',
  279. 'name' => 'wpNewTitleMain',
  280. 'value' => $newTitle->getText(),
  281. // Inappropriate, since we're expecting the user to input a non-existent page's title
  282. 'suggestions' => false,
  283. ],
  284. 'infusable' => true,
  285. ] ),
  286. [
  287. 'label' => $this->msg( 'newtitle' )->text(),
  288. 'align' => 'top',
  289. ]
  290. );
  291. // HTML maxlength uses "UTF-16 code units", which means that characters outside BMP
  292. // (e.g. emojis) count for two each. This limit is overridden in JS to instead count
  293. // Unicode codepoints.
  294. $fields[] = new OOUI\FieldLayout(
  295. new OOUI\TextInputWidget( [
  296. 'name' => 'wpReason',
  297. 'id' => 'wpReason',
  298. 'maxLength' => CommentStore::COMMENT_CHARACTER_LIMIT,
  299. 'infusable' => true,
  300. 'value' => $this->reason,
  301. ] ),
  302. [
  303. 'label' => $this->msg( 'movereason' )->text(),
  304. 'align' => 'top',
  305. ]
  306. );
  307. if ( $considerTalk ) {
  308. $fields[] = new OOUI\FieldLayout(
  309. new OOUI\CheckboxInputWidget( [
  310. 'name' => 'wpMovetalk',
  311. 'id' => 'wpMovetalk',
  312. 'value' => '1',
  313. 'selected' => $this->moveTalk,
  314. ] ),
  315. [
  316. 'label' => $this->msg( 'movetalk' )->text(),
  317. 'help' => new OOUI\HtmlSnippet( $this->msg( 'movepagetalktext' )->parseAsBlock() ),
  318. 'helpInline' => true,
  319. 'align' => 'inline',
  320. 'id' => 'wpMovetalk-field',
  321. ]
  322. );
  323. }
  324. if ( MediaWikiServices::getInstance()
  325. ->getPermissionManager()
  326. ->userHasRight( $user, 'suppressredirect' )
  327. ) {
  328. if ( $handlerSupportsRedirects ) {
  329. $isChecked = $this->leaveRedirect;
  330. $isDisabled = false;
  331. } else {
  332. $isChecked = false;
  333. $isDisabled = true;
  334. }
  335. $fields[] = new OOUI\FieldLayout(
  336. new OOUI\CheckboxInputWidget( [
  337. 'name' => 'wpLeaveRedirect',
  338. 'id' => 'wpLeaveRedirect',
  339. 'value' => '1',
  340. 'selected' => $isChecked,
  341. 'disabled' => $isDisabled,
  342. ] ),
  343. [
  344. 'label' => $this->msg( 'move-leave-redirect' )->text(),
  345. 'align' => 'inline',
  346. ]
  347. );
  348. }
  349. if ( $hasRedirects ) {
  350. $fields[] = new OOUI\FieldLayout(
  351. new OOUI\CheckboxInputWidget( [
  352. 'name' => 'wpFixRedirects',
  353. 'id' => 'wpFixRedirects',
  354. 'value' => '1',
  355. 'selected' => $this->fixRedirects,
  356. ] ),
  357. [
  358. 'label' => $this->msg( 'fix-double-redirects' )->text(),
  359. 'align' => 'inline',
  360. ]
  361. );
  362. }
  363. if ( $canMoveSubpage ) {
  364. $maximumMovedPages = $this->getConfig()->get( 'MaximumMovedPages' );
  365. $fields[] = new OOUI\FieldLayout(
  366. new OOUI\CheckboxInputWidget( [
  367. 'name' => 'wpMovesubpages',
  368. 'id' => 'wpMovesubpages',
  369. 'value' => '1',
  370. 'selected' => true, // T222953 Always check the box
  371. ] ),
  372. [
  373. 'label' => new OOUI\HtmlSnippet(
  374. $this->msg(
  375. ( $this->oldTitle->hasSubpages()
  376. ? 'move-subpages'
  377. : 'move-talk-subpages' )
  378. )->numParams( $maximumMovedPages )->params( $maximumMovedPages )->parse()
  379. ),
  380. 'align' => 'inline',
  381. ]
  382. );
  383. }
  384. # Don't allow watching if user is not logged in
  385. if ( $user->isLoggedIn() ) {
  386. $watchChecked = $user->isLoggedIn() && ( $this->watch || $user->getBoolOption( 'watchmoves' )
  387. || $user->isWatched( $this->oldTitle ) );
  388. $fields[] = new OOUI\FieldLayout(
  389. new OOUI\CheckboxInputWidget( [
  390. 'name' => 'wpWatch',
  391. 'id' => 'watch', # ew
  392. 'value' => '1',
  393. 'selected' => $watchChecked,
  394. ] ),
  395. [
  396. 'label' => $this->msg( 'move-watch' )->text(),
  397. 'align' => 'inline',
  398. ]
  399. );
  400. }
  401. $hiddenFields = '';
  402. if ( $moveOverShared ) {
  403. $hiddenFields .= Html::hidden( 'wpMoveOverSharedFile', '1' );
  404. }
  405. if ( $deleteAndMove ) {
  406. $fields[] = new OOUI\FieldLayout(
  407. new OOUI\CheckboxInputWidget( [
  408. 'name' => 'wpDeleteAndMove',
  409. 'id' => 'wpDeleteAndMove',
  410. 'value' => '1',
  411. ] ),
  412. [
  413. 'label' => $this->msg( 'delete_and_move_confirm' )->text(),
  414. 'align' => 'inline',
  415. ]
  416. );
  417. }
  418. $fields[] = new OOUI\FieldLayout(
  419. new OOUI\ButtonInputWidget( [
  420. 'name' => 'wpMove',
  421. 'value' => $this->msg( 'movepagebtn' )->text(),
  422. 'label' => $this->msg( 'movepagebtn' )->text(),
  423. 'flags' => [ 'primary', 'progressive' ],
  424. 'type' => 'submit',
  425. ] ),
  426. [
  427. 'align' => 'top',
  428. ]
  429. );
  430. $fieldset = new OOUI\FieldsetLayout( [
  431. 'label' => $this->msg( 'move-page-legend' )->text(),
  432. 'id' => 'mw-movepage-table',
  433. 'items' => $fields,
  434. ] );
  435. $form = new OOUI\FormLayout( [
  436. 'method' => 'post',
  437. 'action' => $this->getPageTitle()->getLocalURL( 'action=submit' ),
  438. 'id' => 'movepage',
  439. ] );
  440. $form->appendContent(
  441. $fieldset,
  442. new OOUI\HtmlSnippet(
  443. $hiddenFields .
  444. Html::hidden( 'wpOldTitle', $this->oldTitle->getPrefixedText() ) .
  445. Html::hidden( 'wpEditToken', $user->getEditToken() )
  446. )
  447. );
  448. $out->addHTML(
  449. new OOUI\PanelLayout( [
  450. 'classes' => [ 'movepage-wrapper' ],
  451. 'expanded' => false,
  452. 'padded' => true,
  453. 'framed' => true,
  454. 'content' => $form,
  455. ] )
  456. );
  457. $this->showLogFragment( $this->oldTitle );
  458. $this->showSubpages( $this->oldTitle );
  459. }
  460. function doSubmit() {
  461. $user = $this->getUser();
  462. $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
  463. if ( $user->pingLimiter( 'move' ) ) {
  464. throw new ThrottledError;
  465. }
  466. $ot = $this->oldTitle;
  467. $nt = $this->newTitle;
  468. # don't allow moving to pages with # in
  469. if ( !$nt || $nt->hasFragment() ) {
  470. $this->showForm( [ [ 'badtitletext' ] ] );
  471. return;
  472. }
  473. $services = MediaWikiServices::getInstance();
  474. # Show a warning if the target file exists on a shared repo
  475. $repoGroup = $services->getRepoGroup();
  476. if ( $nt->getNamespace() == NS_FILE
  477. && !( $this->moveOverShared && $permissionManager->userHasRight( $user, 'reupload-shared' ) )
  478. && !$repoGroup->getLocalRepo()->findFile( $nt )
  479. && $repoGroup->findFile( $nt )
  480. ) {
  481. $this->showForm( [ [ 'file-exists-sharedrepo' ] ] );
  482. return;
  483. }
  484. # Delete to make way if requested
  485. if ( $this->deleteAndMove ) {
  486. $permErrors = $permissionManager->getPermissionErrors( 'delete', $user, $nt );
  487. if ( count( $permErrors ) ) {
  488. # Only show the first error
  489. $this->showForm( $permErrors, true );
  490. return;
  491. }
  492. $page = WikiPage::factory( $nt );
  493. // Small safety margin to guard against concurrent edits
  494. if ( $page->isBatchedDelete( 5 ) ) {
  495. $this->showForm( [ [ 'movepage-delete-first' ] ] );
  496. return;
  497. }
  498. $reason = $this->msg( 'delete_and_move_reason', $ot )->inContentLanguage()->text();
  499. // Delete an associated image if there is
  500. if ( $nt->getNamespace() == NS_FILE ) {
  501. $file = $repoGroup->getLocalRepo()->newFile( $nt );
  502. $file->load( File::READ_LATEST );
  503. if ( $file->exists() ) {
  504. $file->delete( $reason, false, $user );
  505. }
  506. }
  507. $error = ''; // passed by ref
  508. $deleteStatus = $page->doDeleteArticleReal( $reason, false, 0, true, $error, $user );
  509. if ( !$deleteStatus->isGood() ) {
  510. $this->showForm( $deleteStatus->getErrorsArray() );
  511. return;
  512. }
  513. }
  514. $handler = ContentHandler::getForTitle( $ot );
  515. if ( !$handler->supportsRedirects() ) {
  516. $createRedirect = false;
  517. } elseif ( $permissionManager->userHasRight( $user, 'suppressredirect' ) ) {
  518. $createRedirect = $this->leaveRedirect;
  519. } else {
  520. $createRedirect = true;
  521. }
  522. # Do the actual move.
  523. $mp = new MovePage( $ot, $nt );
  524. # check whether the requested actions are permitted / possible
  525. $userPermitted = $mp->checkPermissions( $user, $this->reason )->isOK();
  526. if ( $ot->isTalkPage() || $nt->isTalkPage() ) {
  527. $this->moveTalk = false;
  528. }
  529. if ( $this->moveSubpages ) {
  530. $this->moveSubpages = $permissionManager->userCan( 'move-subpages', $user, $ot );
  531. }
  532. $status = $mp->moveIfAllowed( $user, $this->reason, $createRedirect );
  533. if ( !$status->isOK() ) {
  534. $this->showForm( $status->getErrorsArray(), !$userPermitted );
  535. return;
  536. }
  537. if ( $this->getConfig()->get( 'FixDoubleRedirects' ) && $this->fixRedirects ) {
  538. DoubleRedirectJob::fixRedirects( 'move', $ot, $nt );
  539. }
  540. $out = $this->getOutput();
  541. $out->setPageTitle( $this->msg( 'pagemovedsub' ) );
  542. $linkRenderer = $this->getLinkRenderer();
  543. $oldLink = $linkRenderer->makeLink(
  544. $ot,
  545. null,
  546. [ 'id' => 'movepage-oldlink' ],
  547. [ 'redirect' => 'no' ]
  548. );
  549. $newLink = $linkRenderer->makeKnownLink(
  550. $nt,
  551. null,
  552. [ 'id' => 'movepage-newlink' ]
  553. );
  554. $oldText = $ot->getPrefixedText();
  555. $newText = $nt->getPrefixedText();
  556. if ( $ot->exists() ) {
  557. // NOTE: we assume that if the old title exists, it's because it was re-created as
  558. // a redirect to the new title. This is not safe, but what we did before was
  559. // even worse: we just determined whether a redirect should have been created,
  560. // and reported that it was created if it should have, without any checks.
  561. // Also note that isRedirect() is unreliable because of T39209.
  562. $msgName = 'movepage-moved-redirect';
  563. } else {
  564. $msgName = 'movepage-moved-noredirect';
  565. }
  566. $out->addHTML( $this->msg( 'movepage-moved' )->rawParams( $oldLink,
  567. $newLink )->params( $oldText, $newText )->parseAsBlock() );
  568. $out->addWikiMsg( $msgName );
  569. // Avoid PHP 7.1 warning from passing $this by reference
  570. $movePage = $this;
  571. Hooks::run( 'SpecialMovepageAfterMove', [ &$movePage, &$ot, &$nt ] );
  572. /*
  573. * Now we move extra pages we've been asked to move: subpages and talk
  574. * pages.
  575. *
  576. * First, make a list of id's. This might be marginally less efficient
  577. * than a more direct method, but this is not a highly performance-cri-
  578. * tical code path and readable code is more important here.
  579. *
  580. * If the target namespace doesn't allow subpages, moving with subpages
  581. * would mean that you couldn't move them back in one operation, which
  582. * is bad.
  583. * @todo FIXME: A specific error message should be given in this case.
  584. */
  585. // @todo FIXME: Use Title::moveSubpages() here
  586. $nsInfo = $services->getNamespaceInfo();
  587. $dbr = wfGetDB( DB_MASTER );
  588. if ( $this->moveSubpages && (
  589. $nsInfo->hasSubpages( $nt->getNamespace() ) || (
  590. $this->moveTalk
  591. && $nsInfo->hasSubpages( $nt->getTalkPage()->getNamespace() )
  592. )
  593. ) ) {
  594. $conds = [
  595. 'page_title' . $dbr->buildLike( $ot->getDBkey() . '/', $dbr->anyString() )
  596. . ' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
  597. ];
  598. $conds['page_namespace'] = [];
  599. if ( $nsInfo->hasSubpages( $nt->getNamespace() ) ) {
  600. $conds['page_namespace'][] = $ot->getNamespace();
  601. }
  602. if ( $this->moveTalk &&
  603. $nsInfo->hasSubpages( $nt->getTalkPage()->getNamespace() )
  604. ) {
  605. $conds['page_namespace'][] = $ot->getTalkPage()->getNamespace();
  606. }
  607. } elseif ( $this->moveTalk ) {
  608. $conds = [
  609. 'page_namespace' => $ot->getTalkPage()->getNamespace(),
  610. 'page_title' => $ot->getDBkey()
  611. ];
  612. } else {
  613. # Skip the query
  614. $conds = null;
  615. }
  616. $extraPages = [];
  617. if ( !is_null( $conds ) ) {
  618. $extraPages = TitleArray::newFromResult(
  619. $dbr->select( 'page',
  620. [ 'page_id', 'page_namespace', 'page_title' ],
  621. $conds,
  622. __METHOD__
  623. )
  624. );
  625. }
  626. $extraOutput = [];
  627. $count = 1;
  628. foreach ( $extraPages as $oldSubpage ) {
  629. if ( $ot->equals( $oldSubpage ) || $nt->equals( $oldSubpage ) ) {
  630. # Already did this one.
  631. continue;
  632. }
  633. $newPageName = preg_replace(
  634. '#^' . preg_quote( $ot->getDBkey(), '#' ) . '#',
  635. StringUtils::escapeRegexReplacement( $nt->getDBkey() ), # T23234
  636. $oldSubpage->getDBkey()
  637. );
  638. if ( $oldSubpage->isSubpage() && ( $ot->isTalkPage() xor $nt->isTalkPage() ) ) {
  639. // Moving a subpage from a subject namespace to a talk namespace or vice-versa
  640. $newNs = $nt->getNamespace();
  641. } elseif ( $oldSubpage->isTalkPage() ) {
  642. $newNs = $nt->getTalkPage()->getNamespace();
  643. } else {
  644. $newNs = $nt->getSubjectPage()->getNamespace();
  645. }
  646. # T16385: we need makeTitleSafe because the new page names may
  647. # be longer than 255 characters.
  648. $newSubpage = Title::makeTitleSafe( $newNs, $newPageName );
  649. if ( !$newSubpage ) {
  650. $oldLink = $linkRenderer->makeKnownLink( $oldSubpage );
  651. $extraOutput[] = $this->msg( 'movepage-page-unmoved' )->rawParams( $oldLink )
  652. ->params( Title::makeName( $newNs, $newPageName ) )->escaped();
  653. continue;
  654. }
  655. $mp = new MovePage( $oldSubpage, $newSubpage );
  656. # This was copy-pasted from Renameuser, bleh.
  657. if ( $newSubpage->exists() && !$mp->isValidMove()->isOK() ) {
  658. $link = $linkRenderer->makeKnownLink( $newSubpage );
  659. $extraOutput[] = $this->msg( 'movepage-page-exists' )->rawParams( $link )->escaped();
  660. } else {
  661. $status = $mp->moveIfAllowed( $user, $this->reason, $createRedirect );
  662. if ( $status->isOK() ) {
  663. if ( $this->fixRedirects ) {
  664. DoubleRedirectJob::fixRedirects( 'move', $oldSubpage, $newSubpage );
  665. }
  666. $oldLink = $linkRenderer->makeLink(
  667. $oldSubpage,
  668. null,
  669. [],
  670. [ 'redirect' => 'no' ]
  671. );
  672. $newLink = $linkRenderer->makeKnownLink( $newSubpage );
  673. $extraOutput[] = $this->msg( 'movepage-page-moved' )
  674. ->rawParams( $oldLink, $newLink )->escaped();
  675. ++$count;
  676. $maximumMovedPages = $this->getConfig()->get( 'MaximumMovedPages' );
  677. if ( $count >= $maximumMovedPages ) {
  678. $extraOutput[] = $this->msg( 'movepage-max-pages' )
  679. ->numParams( $maximumMovedPages )->escaped();
  680. break;
  681. }
  682. } else {
  683. $oldLink = $linkRenderer->makeKnownLink( $oldSubpage );
  684. $newLink = $linkRenderer->makeLink( $newSubpage );
  685. $extraOutput[] = $this->msg( 'movepage-page-unmoved' )
  686. ->rawParams( $oldLink, $newLink )->escaped();
  687. }
  688. }
  689. }
  690. if ( $extraOutput !== [] ) {
  691. $out->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
  692. }
  693. # Deal with watches (we don't watch subpages)
  694. WatchAction::doWatchOrUnwatch( $this->watch, $ot, $user );
  695. WatchAction::doWatchOrUnwatch( $this->watch, $nt, $user );
  696. }
  697. function showLogFragment( $title ) {
  698. $moveLogPage = new LogPage( 'move' );
  699. $out = $this->getOutput();
  700. $out->addHTML( Xml::element( 'h2', null, $moveLogPage->getName()->text() ) );
  701. LogEventsList::showLogExtract( $out, 'move', $title );
  702. }
  703. /**
  704. * Show subpages of the page being moved. Section is not shown if both current
  705. * namespace does not support subpages and no talk subpages were found.
  706. *
  707. * @param Title $title Page being moved.
  708. */
  709. function showSubpages( $title ) {
  710. $nsHasSubpages = MediaWikiServices::getInstance()->getNamespaceInfo()->
  711. hasSubpages( $title->getNamespace() );
  712. $subpages = $title->getSubpages();
  713. $count = $subpages instanceof TitleArray ? $subpages->count() : 0;
  714. $titleIsTalk = $title->isTalkPage();
  715. $subpagesTalk = $title->getTalkPage()->getSubpages();
  716. $countTalk = $subpagesTalk instanceof TitleArray ? $subpagesTalk->count() : 0;
  717. $totalCount = $count + $countTalk;
  718. if ( !$nsHasSubpages && $countTalk == 0 ) {
  719. return;
  720. }
  721. $this->getOutput()->wrapWikiMsg(
  722. '== $1 ==',
  723. [ 'movesubpage', ( $titleIsTalk ? $count : $totalCount ) ]
  724. );
  725. if ( $nsHasSubpages ) {
  726. $this->showSubpagesList( $subpages, $count, 'movesubpagetext', true );
  727. }
  728. if ( !$titleIsTalk && $countTalk > 0 ) {
  729. $this->showSubpagesList( $subpagesTalk, $countTalk, 'movesubpagetalktext' );
  730. }
  731. }
  732. function showSubpagesList( $subpages, $pagecount, $wikiMsg, $noSubpageMsg = false ) {
  733. $out = $this->getOutput();
  734. # No subpages.
  735. if ( $pagecount == 0 && $noSubpageMsg ) {
  736. $out->addWikiMsg( 'movenosubpage' );
  737. return;
  738. }
  739. $out->addWikiMsg( $wikiMsg, $this->getLanguage()->formatNum( $pagecount ) );
  740. $out->addHTML( "<ul>\n" );
  741. $linkBatch = new LinkBatch( $subpages );
  742. $linkBatch->setCaller( __METHOD__ );
  743. $linkBatch->execute();
  744. $linkRenderer = $this->getLinkRenderer();
  745. foreach ( $subpages as $subpage ) {
  746. $link = $linkRenderer->makeLink( $subpage );
  747. $out->addHTML( "<li>$link</li>\n" );
  748. }
  749. $out->addHTML( "</ul>\n" );
  750. }
  751. /**
  752. * Return an array of subpages beginning with $search that this special page will accept.
  753. *
  754. * @param string $search Prefix to search for
  755. * @param int $limit Maximum number of results to return (usually 10)
  756. * @param int $offset Number of results to skip (usually 0)
  757. * @return string[] Matching subpages
  758. */
  759. public function prefixSearchSubpages( $search, $limit, $offset ) {
  760. return $this->prefixSearchString( $search, $limit, $offset );
  761. }
  762. protected function getGroupName() {
  763. return 'pagetools';
  764. }
  765. }