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