ApiImageRotate.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. */
  20. use MediaWiki\MediaWikiServices;
  21. /**
  22. * @ingroup API
  23. */
  24. class ApiImageRotate extends ApiBase {
  25. private $mPageSet = null;
  26. public function execute() {
  27. $this->useTransactionalTimeLimit();
  28. $params = $this->extractRequestParams();
  29. $rotation = $params['rotation'];
  30. $continuationManager = new ApiContinuationManager( $this, [], [] );
  31. $this->setContinuationManager( $continuationManager );
  32. $pageSet = $this->getPageSet();
  33. $pageSet->execute();
  34. $result = $pageSet->getInvalidTitlesAndRevisions( [
  35. 'invalidTitles', 'special', 'missingIds', 'missingRevIds', 'interwikiTitles',
  36. ] );
  37. // Check if user can add tags
  38. if ( $params['tags'] ) {
  39. $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getUser() );
  40. if ( !$ableToTag->isOK() ) {
  41. $this->dieStatus( $ableToTag );
  42. }
  43. }
  44. foreach ( $pageSet->getTitles() as $title ) {
  45. $r = [];
  46. $r['id'] = $title->getArticleID();
  47. ApiQueryBase::addTitleInfo( $r, $title );
  48. if ( !$title->exists() ) {
  49. $r['missing'] = true;
  50. if ( $title->isKnown() ) {
  51. $r['known'] = true;
  52. }
  53. }
  54. $file = MediaWikiServices::getInstance()->getRepoGroup()->findFile(
  55. $title, [ 'latest' => true ]
  56. );
  57. if ( !$file ) {
  58. $r['result'] = 'Failure';
  59. $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
  60. Status::newFatal( 'apierror-filedoesnotexist' )
  61. );
  62. $result[] = $r;
  63. continue;
  64. }
  65. $handler = $file->getHandler();
  66. if ( !$handler || !$handler->canRotate() ) {
  67. $r['result'] = 'Failure';
  68. $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
  69. Status::newFatal( 'apierror-filetypecannotberotated' )
  70. );
  71. $result[] = $r;
  72. continue;
  73. }
  74. // Check whether we're allowed to rotate this file
  75. $permError = $this->checkTitleUserPermissions( $file->getTitle(), [ 'edit', 'upload' ] );
  76. if ( $permError ) {
  77. $r['result'] = 'Failure';
  78. $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
  79. $this->errorArrayToStatus( $permError )
  80. );
  81. $result[] = $r;
  82. continue;
  83. }
  84. $srcPath = $file->getLocalRefPath();
  85. if ( $srcPath === false ) {
  86. $r['result'] = 'Failure';
  87. $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
  88. Status::newFatal( 'apierror-filenopath' )
  89. );
  90. $result[] = $r;
  91. continue;
  92. }
  93. $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION ) );
  94. $tmpFile = MediaWikiServices::getInstance()->getTempFSFileFactory()
  95. ->newTempFSFile( 'rotate_', $ext );
  96. $dstPath = $tmpFile->getPath();
  97. // @phan-suppress-next-line PhanUndeclaredMethod
  98. $err = $handler->rotate( $file, [
  99. 'srcPath' => $srcPath,
  100. 'dstPath' => $dstPath,
  101. 'rotation' => $rotation
  102. ] );
  103. if ( !$err ) {
  104. $comment = wfMessage(
  105. 'rotate-comment'
  106. )->numParams( $rotation )->inContentLanguage()->text();
  107. // @phan-suppress-next-line PhanUndeclaredMethod
  108. $status = $file->upload(
  109. $dstPath,
  110. $comment,
  111. $comment,
  112. 0,
  113. false,
  114. false,
  115. $this->getUser(),
  116. $params['tags'] ?: []
  117. );
  118. if ( $status->isGood() ) {
  119. $r['result'] = 'Success';
  120. } else {
  121. $r['result'] = 'Failure';
  122. $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
  123. }
  124. } else {
  125. $r['result'] = 'Failure';
  126. $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
  127. Status::newFatal( ApiMessage::create( $err->getMsg() ) )
  128. );
  129. }
  130. $result[] = $r;
  131. }
  132. $apiResult = $this->getResult();
  133. ApiResult::setIndexedTagName( $result, 'page' );
  134. $apiResult->addValue( null, $this->getModuleName(), $result );
  135. $this->setContinuationManager( null );
  136. $continuationManager->setContinuationIntoResult( $apiResult );
  137. }
  138. /**
  139. * Get a cached instance of an ApiPageSet object
  140. * @return ApiPageSet
  141. */
  142. private function getPageSet() {
  143. if ( $this->mPageSet === null ) {
  144. $this->mPageSet = new ApiPageSet( $this, 0, NS_FILE );
  145. }
  146. return $this->mPageSet;
  147. }
  148. public function mustBePosted() {
  149. return true;
  150. }
  151. public function isWriteMode() {
  152. return true;
  153. }
  154. public function getAllowedParams( $flags = 0 ) {
  155. $result = [
  156. 'rotation' => [
  157. ApiBase::PARAM_TYPE => [ '90', '180', '270' ],
  158. ApiBase::PARAM_REQUIRED => true
  159. ],
  160. 'continue' => [
  161. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  162. ],
  163. 'tags' => [
  164. ApiBase::PARAM_TYPE => 'tags',
  165. ApiBase::PARAM_ISMULTI => true,
  166. ],
  167. ];
  168. if ( $flags ) {
  169. $result += $this->getPageSet()->getFinalParams( $flags );
  170. }
  171. return $result;
  172. }
  173. public function needsToken() {
  174. return 'csrf';
  175. }
  176. protected function getExamplesMessages() {
  177. return [
  178. 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
  179. => 'apihelp-imagerotate-example-simple',
  180. 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
  181. 'rotation=180&token=123ABC'
  182. => 'apihelp-imagerotate-example-generator',
  183. ];
  184. }
  185. }