MediaWiki master
ApiImageRotate.php
Go to the documentation of this file.
1<?php
25
29class ApiImageRotate extends ApiBase {
30 private $mPageSet = null;
31
32 private RepoGroup $repoGroup;
33 private TempFSFileFactory $tempFSFileFactory;
34 private TitleFactory $titleFactory;
35
43 public function __construct(
44 ApiMain $mainModule,
45 $moduleName,
46 RepoGroup $repoGroup,
47 TempFSFileFactory $tempFSFileFactory,
48 TitleFactory $titleFactory
49 ) {
50 parent::__construct( $mainModule, $moduleName );
51 $this->repoGroup = $repoGroup;
52 $this->tempFSFileFactory = $tempFSFileFactory;
53 $this->titleFactory = $titleFactory;
54 }
55
56 public function execute() {
58
59 $params = $this->extractRequestParams();
60 $rotation = $params['rotation'];
61
62 $continuationManager = new ApiContinuationManager( $this, [], [] );
63 $this->setContinuationManager( $continuationManager );
64
65 $pageSet = $this->getPageSet();
66 $pageSet->execute();
67
68 $result = $pageSet->getInvalidTitlesAndRevisions( [
69 'invalidTitles', 'special', 'missingIds', 'missingRevIds', 'interwikiTitles',
70 ] );
71
72 // Check if user can add tags
73 if ( $params['tags'] ) {
74 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() );
75 if ( !$ableToTag->isOK() ) {
76 $this->dieStatus( $ableToTag );
77 }
78 }
79
80 foreach ( $pageSet->getPages() as $page ) {
81 $title = $this->titleFactory->newFromPageIdentity( $page );
82 $r = [];
83 $r['id'] = $title->getArticleID();
84 ApiQueryBase::addTitleInfo( $r, $title );
85 if ( !$title->exists() ) {
86 $r['missing'] = true;
87 if ( $title->isKnown() ) {
88 $r['known'] = true;
89 }
90 }
91
92 $file = $this->repoGroup->findFile( $title, [ 'latest' => true ] );
93 if ( !$file ) {
94 $r['result'] = 'Failure';
95 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
96 Status::newFatal( 'apierror-filedoesnotexist' )
97 );
98 $result[] = $r;
99 continue;
100 }
101 $handler = $file->getHandler();
102 if ( !$handler || !$handler->canRotate() ) {
103 $r['result'] = 'Failure';
104 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
105 Status::newFatal( 'apierror-filetypecannotberotated' )
106 );
107 $result[] = $r;
108 continue;
109 }
110
111 // Check whether we're allowed to rotate this file
112 $this->checkTitleUserPermissions( $file->getTitle(), [ 'edit', 'upload' ] );
113
114 $srcPath = $file->getLocalRefPath();
115 if ( $srcPath === false ) {
116 $r['result'] = 'Failure';
117 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
118 Status::newFatal( 'apierror-filenopath' )
119 );
120 $result[] = $r;
121 continue;
122 }
123 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION ) );
124 $tmpFile = $this->tempFSFileFactory->newTempFSFile( 'rotate_', $ext );
125 $dstPath = $tmpFile->getPath();
126 // @phan-suppress-next-line PhanUndeclaredMethod
127 $err = $handler->rotate( $file, [
128 'srcPath' => $srcPath,
129 'dstPath' => $dstPath,
130 'rotation' => $rotation
131 ] );
132 if ( !$err ) {
133 $comment = $this->msg(
134 'rotate-comment'
135 )->numParams( $rotation )->inContentLanguage()->text();
136 // @phan-suppress-next-line PhanUndeclaredMethod
137 $status = $file->upload(
138 $dstPath,
139 $comment,
140 $comment,
141 0,
142 false,
143 false,
144 $this->getAuthority(),
145 $params['tags'] ?: []
146 );
147 if ( $status->isGood() ) {
148 $r['result'] = 'Success';
149 } else {
150 $r['result'] = 'Failure';
151 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
152 }
153 } else {
154 $r['result'] = 'Failure';
155 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
156 Status::newFatal( ApiMessage::create( $err->getMsg() ) )
157 );
158 }
159 $result[] = $r;
160 }
161 $apiResult = $this->getResult();
162 ApiResult::setIndexedTagName( $result, 'page' );
163 $apiResult->addValue( null, $this->getModuleName(), $result );
164
165 $this->setContinuationManager( null );
166 $continuationManager->setContinuationIntoResult( $apiResult );
167 }
168
173 private function getPageSet() {
174 $this->mPageSet ??= new ApiPageSet( $this, 0, NS_FILE );
175
176 return $this->mPageSet;
177 }
178
179 public function mustBePosted() {
180 return true;
181 }
182
183 public function isWriteMode() {
184 return true;
185 }
186
187 public function getAllowedParams( $flags = 0 ) {
188 $result = [
189 'rotation' => [
190 ParamValidator::PARAM_TYPE => [ '90', '180', '270' ],
191 ParamValidator::PARAM_REQUIRED => true
192 ],
193 'continue' => [
194 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
195 ],
196 'tags' => [
197 ParamValidator::PARAM_TYPE => 'tags',
198 ParamValidator::PARAM_ISMULTI => true,
199 ],
200 ];
201 if ( $flags ) {
202 $result += $this->getPageSet()->getFinalParams( $flags );
203 }
204
205 return $result;
206 }
207
208 public function needsToken() {
209 return 'csrf';
210 }
211
212 protected function getExamplesMessages() {
213 return [
214 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
215 => 'apihelp-imagerotate-example-simple',
216 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
217 'rotation=180&token=123ABC'
218 => 'apihelp-imagerotate-example-generator',
219 ];
220 }
221
222 public function getHelpUrls() {
223 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Imagerotate';
224 }
225}
const NS_FILE
Definition Defines.php:70
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:64
checkTitleUserPermissions(PageIdentity $pageIdentity, $actions, array $options=[])
Helper function for permission-denied errors.
Definition ApiBase.php:1672
getErrorFormatter()
Definition ApiBase.php:682
setContinuationManager(ApiContinuationManager $manager=null)
Definition ApiBase.php:718
getResult()
Get the result object.
Definition ApiBase.php:671
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:811
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:171
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:532
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1589
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1381
This manages continuation state.
mustBePosted()
Indicates whether this module must be called with a POST request.
isWriteMode()
Indicates whether this module requires write mode.
getAllowedParams( $flags=0)
getExamplesMessages()
Returns usage examples for this module.
getHelpUrls()
Return links to more detailed help pages about the module.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
needsToken()
Returns the token type this module requires in order to execute.
__construct(ApiMain $mainModule, $moduleName, RepoGroup $repoGroup, TempFSFileFactory $tempFSFileFactory, TitleFactory $titleFactory)
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:65
This class contains a list of pages that the client has requested.
static canAddTagsAccompanyingChange(array $tags, Authority $performer=null, $checkBlock=true)
Is it OK to allow the user to apply all the specified tags at the same time as they edit/make the cha...
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
Creates Title objects.
Prioritized list of file repositories.
Definition RepoGroup.php:30
Service for formatting and validating API parameters.