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