ApiMove.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /**
  3. * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
  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. */
  22. use MediaWiki\MediaWikiServices;
  23. /**
  24. * API Module to move pages
  25. * @ingroup API
  26. */
  27. class ApiMove extends ApiBase {
  28. public function execute() {
  29. $this->useTransactionalTimeLimit();
  30. $user = $this->getUser();
  31. $params = $this->extractRequestParams();
  32. $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
  33. if ( isset( $params['from'] ) ) {
  34. $fromTitle = Title::newFromText( $params['from'] );
  35. if ( !$fromTitle || $fromTitle->isExternal() ) {
  36. $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['from'] ) ] );
  37. }
  38. } elseif ( isset( $params['fromid'] ) ) {
  39. $fromTitle = Title::newFromID( $params['fromid'] );
  40. if ( !$fromTitle ) {
  41. $this->dieWithError( [ 'apierror-nosuchpageid', $params['fromid'] ] );
  42. }
  43. }
  44. if ( !$fromTitle->exists() ) {
  45. $this->dieWithError( 'apierror-missingtitle' );
  46. }
  47. $fromTalk = $fromTitle->getTalkPage();
  48. $toTitle = Title::newFromText( $params['to'] );
  49. if ( !$toTitle || $toTitle->isExternal() ) {
  50. $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['to'] ) ] );
  51. }
  52. $toTalk = $toTitle->getTalkPageIfDefined();
  53. $repoGroup = MediaWikiServices::getInstance()->getRepoGroup();
  54. if ( $toTitle->getNamespace() == NS_FILE
  55. && !$repoGroup->getLocalRepo()->findFile( $toTitle )
  56. && $repoGroup->findFile( $toTitle )
  57. ) {
  58. if ( !$params['ignorewarnings'] &&
  59. $this->getPermissionManager()->userHasRight( $user, 'reupload-shared' ) ) {
  60. $this->dieWithError( 'apierror-fileexists-sharedrepo-perm' );
  61. } elseif ( !$this->getPermissionManager()->userHasRight( $user, 'reupload-shared' ) ) {
  62. $this->dieWithError( 'apierror-cantoverwrite-sharedfile' );
  63. }
  64. }
  65. // Rate limit
  66. if ( $user->pingLimiter( 'move' ) ) {
  67. $this->dieWithError( 'apierror-ratelimited' );
  68. }
  69. // Check if the user is allowed to add the specified changetags
  70. if ( $params['tags'] ) {
  71. $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
  72. if ( !$ableToTag->isOK() ) {
  73. $this->dieStatus( $ableToTag );
  74. }
  75. }
  76. // Move the page
  77. $toTitleExists = $toTitle->exists();
  78. $status = $this->movePage( $fromTitle, $toTitle, $params['reason'], !$params['noredirect'],
  79. $params['tags'] ?: [] );
  80. if ( !$status->isOK() ) {
  81. $user->spreadAnyEditBlock();
  82. $this->dieStatus( $status );
  83. }
  84. $r = [
  85. 'from' => $fromTitle->getPrefixedText(),
  86. 'to' => $toTitle->getPrefixedText(),
  87. 'reason' => $params['reason']
  88. ];
  89. // NOTE: we assume that if the old title exists, it's because it was re-created as
  90. // a redirect to the new title. This is not safe, but what we did before was
  91. // even worse: we just determined whether a redirect should have been created,
  92. // and reported that it was created if it should have, without any checks.
  93. $r['redirectcreated'] = $fromTitle->exists();
  94. $r['moveoverredirect'] = $toTitleExists;
  95. // Move the talk page
  96. if ( $params['movetalk'] && $toTalk && $fromTalk->exists() && !$fromTitle->isTalkPage() ) {
  97. $toTalkExists = $toTalk->exists();
  98. $status = $this->movePage(
  99. $fromTalk,
  100. $toTalk,
  101. $params['reason'],
  102. !$params['noredirect'],
  103. $params['tags'] ?: []
  104. );
  105. if ( $status->isOK() ) {
  106. $r['talkfrom'] = $fromTalk->getPrefixedText();
  107. $r['talkto'] = $toTalk->getPrefixedText();
  108. $r['talkmoveoverredirect'] = $toTalkExists;
  109. } else {
  110. // We're not going to dieWithError() on failure, since we already changed something
  111. $r['talkmove-errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
  112. }
  113. }
  114. $result = $this->getResult();
  115. // Move subpages
  116. if ( $params['movesubpages'] ) {
  117. $r['subpages'] = $this->moveSubpages(
  118. $fromTitle,
  119. $toTitle,
  120. $params['reason'],
  121. $params['noredirect'],
  122. $params['tags'] ?: []
  123. );
  124. ApiResult::setIndexedTagName( $r['subpages'], 'subpage' );
  125. if ( $params['movetalk'] ) {
  126. $r['subpages-talk'] = $this->moveSubpages(
  127. $fromTalk,
  128. $toTalk,
  129. $params['reason'],
  130. $params['noredirect'],
  131. $params['tags'] ?: []
  132. );
  133. ApiResult::setIndexedTagName( $r['subpages-talk'], 'subpage' );
  134. }
  135. }
  136. $watch = 'preferences';
  137. if ( isset( $params['watchlist'] ) ) {
  138. $watch = $params['watchlist'];
  139. }
  140. // Watch pages
  141. $this->setWatch( $watch, $fromTitle, 'watchmoves' );
  142. $this->setWatch( $watch, $toTitle, 'watchmoves' );
  143. $result->addValue( null, $this->getModuleName(), $r );
  144. }
  145. /**
  146. * @param Title $from
  147. * @param Title $to
  148. * @param string $reason
  149. * @param bool $createRedirect
  150. * @param array $changeTags Applied to the entry in the move log and redirect page revision
  151. * @return Status
  152. */
  153. protected function movePage( Title $from, Title $to, $reason, $createRedirect, $changeTags ) {
  154. $mp = MediaWikiServices::getInstance()->getMovePageFactory()->newMovePage( $from, $to );
  155. $valid = $mp->isValidMove();
  156. if ( !$valid->isOK() ) {
  157. return $valid;
  158. }
  159. $user = $this->getUser();
  160. $permStatus = $mp->checkPermissions( $user, $reason );
  161. if ( !$permStatus->isOK() ) {
  162. return $permStatus;
  163. }
  164. // Check suppressredirect permission
  165. if ( !$this->getPermissionManager()->userHasRight( $user, 'suppressredirect' ) ) {
  166. $createRedirect = true;
  167. }
  168. return $mp->move( $user, $reason, $createRedirect, $changeTags );
  169. }
  170. /**
  171. * @param Title $fromTitle
  172. * @param Title $toTitle
  173. * @param string $reason
  174. * @param bool $noredirect
  175. * @param array $changeTags Applied to the entry in the move log and redirect page revisions
  176. * @return array
  177. */
  178. public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect, $changeTags = [] ) {
  179. $retval = [];
  180. $mp = new MovePage( $fromTitle, $toTitle );
  181. $result =
  182. $mp->moveSubpagesIfAllowed( $this->getUser(), $reason, !$noredirect, $changeTags );
  183. if ( !$result->isOK() ) {
  184. // This means the whole thing failed
  185. return [ 'errors' => $this->getErrorFormatter()->arrayFromStatus( $result ) ];
  186. }
  187. // At least some pages could be moved
  188. // Report each of them separately
  189. foreach ( $result->getValue() as $oldTitle => $status ) {
  190. $r = [ 'from' => $oldTitle ];
  191. if ( $status->isOK() ) {
  192. $r['to'] = $status->getValue();
  193. } else {
  194. $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
  195. }
  196. $retval[] = $r;
  197. }
  198. return $retval;
  199. }
  200. public function mustBePosted() {
  201. return true;
  202. }
  203. public function isWriteMode() {
  204. return true;
  205. }
  206. public function getAllowedParams() {
  207. return [
  208. 'from' => null,
  209. 'fromid' => [
  210. ApiBase::PARAM_TYPE => 'integer'
  211. ],
  212. 'to' => [
  213. ApiBase::PARAM_TYPE => 'string',
  214. ApiBase::PARAM_REQUIRED => true
  215. ],
  216. 'reason' => '',
  217. 'movetalk' => false,
  218. 'movesubpages' => false,
  219. 'noredirect' => false,
  220. 'watchlist' => [
  221. ApiBase::PARAM_DFLT => 'preferences',
  222. ApiBase::PARAM_TYPE => [
  223. 'watch',
  224. 'unwatch',
  225. 'preferences',
  226. 'nochange'
  227. ],
  228. ],
  229. 'ignorewarnings' => false,
  230. 'tags' => [
  231. ApiBase::PARAM_TYPE => 'tags',
  232. ApiBase::PARAM_ISMULTI => true,
  233. ],
  234. ];
  235. }
  236. public function needsToken() {
  237. return 'csrf';
  238. }
  239. protected function getExamplesMessages() {
  240. return [
  241. 'action=move&from=Badtitle&to=Goodtitle&token=123ABC&' .
  242. 'reason=Misspelled%20title&movetalk=&noredirect='
  243. => 'apihelp-move-example-move',
  244. ];
  245. }
  246. public function getHelpUrls() {
  247. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Move';
  248. }
  249. }