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 private RepoGroup $repoGroup;
24 private TempFSFileFactory $tempFSFileFactory;
25 private TitleFactory $titleFactory;
26
27 public function __construct(
28 ApiMain $mainModule,
29 string $moduleName,
30 RepoGroup $repoGroup,
31 TempFSFileFactory $tempFSFileFactory,
32 TitleFactory $titleFactory
33 ) {
34 parent::__construct( $mainModule, $moduleName );
35 $this->repoGroup = $repoGroup;
36 $this->tempFSFileFactory = $tempFSFileFactory;
37 $this->titleFactory = $titleFactory;
38 }
39
40 public function execute() {
42
43 $params = $this->extractRequestParams();
44 $rotation = $params['rotation'];
45
46 $continuationManager = new ApiContinuationManager( $this, [], [] );
47 $this->setContinuationManager( $continuationManager );
48
49 $pageSet = $this->getPageSet();
50 $pageSet->execute();
51
52 $result = $pageSet->getInvalidTitlesAndRevisions( [
53 'invalidTitles', 'special', 'missingIds', 'missingRevIds', 'interwikiTitles',
54 ] );
55
56 // Check if user can add tags
57 if ( $params['tags'] ) {
58 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() );
59 if ( !$ableToTag->isOK() ) {
60 $this->dieStatus( $ableToTag );
61 }
62 }
63
64 foreach ( $pageSet->getPages() as $page ) {
65 $title = $this->titleFactory->newFromPageIdentity( $page );
66 $r = [];
67 $r['id'] = $title->getArticleID();
68 ApiQueryBase::addTitleInfo( $r, $title );
69 if ( !$title->exists() ) {
70 $r['missing'] = true;
71 if ( $title->isKnown() ) {
72 $r['known'] = true;
73 }
74 }
75
76 $file = $this->repoGroup->findFile( $title, [ 'latest' => true ] );
77 if ( !$file ) {
78 $r['result'] = 'Failure';
79 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
80 Status::newFatal( 'apierror-filedoesnotexist' )
81 );
82 $result[] = $r;
83 continue;
84 }
85 $handler = $file->getHandler();
86 if ( !$handler || !$handler->canRotate() ) {
87 $r['result'] = 'Failure';
88 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
89 Status::newFatal( 'apierror-filetypecannotberotated' )
90 );
91 $result[] = $r;
92 continue;
93 }
94
95 // Check whether we're allowed to rotate this file
96 $this->checkTitleUserPermissions( $file->getTitle(), [ 'edit', 'upload' ] );
97
98 $srcPath = $file->getLocalRefPath();
99 if ( $srcPath === false ) {
100 $r['result'] = 'Failure';
101 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
102 Status::newFatal( 'apierror-filenopath' )
103 );
104 $result[] = $r;
105 continue;
106 }
107 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION ) );
108 $tmpFile = $this->tempFSFileFactory->newTempFSFile( 'rotate_', $ext );
109 $dstPath = $tmpFile->getPath();
110 // @phan-suppress-next-line PhanUndeclaredMethod
111 $err = $handler->rotate( $file, [
112 'srcPath' => $srcPath,
113 'dstPath' => $dstPath,
114 'rotation' => $rotation
115 ] );
116 if ( !$err ) {
117 $comment = $this->msg(
118 'rotate-comment'
119 )->numParams( $rotation )->inContentLanguage()->text();
120 // @phan-suppress-next-line PhanUndeclaredMethod
121 $status = $file->upload(
122 $dstPath,
123 $comment,
124 $comment,
125 0,
126 false,
127 false,
128 $this->getAuthority(),
129 $params['tags'] ?: []
130 );
131 if ( $status->isGood() ) {
132 $r['result'] = 'Success';
133 } else {
134 $r['result'] = 'Failure';
135 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
136 }
137 } else {
138 $r['result'] = 'Failure';
139 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
140 Status::newFatal( ApiMessage::create( $err->getMsg() ) )
141 );
142 }
143 $result[] = $r;
144 }
145 $apiResult = $this->getResult();
146 ApiResult::setIndexedTagName( $result, 'page' );
147 $apiResult->addValue( null, $this->getModuleName(), $result );
148
149 $this->setContinuationManager( null );
150 $continuationManager->setContinuationIntoResult( $apiResult );
151 }
152
157 private function getPageSet() {
158 $this->mPageSet ??= new ApiPageSet( $this, 0, NS_FILE );
159
160 return $this->mPageSet;
161 }
162
164 public function mustBePosted() {
165 return true;
166 }
167
169 public function isWriteMode() {
170 return true;
171 }
172
174 public function getAllowedParams( $flags = 0 ) {
175 $result = [
176 'rotation' => [
177 ParamValidator::PARAM_TYPE => [ '90', '180', '270' ],
178 ParamValidator::PARAM_REQUIRED => true
179 ],
180 'continue' => [
181 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
182 ],
183 'tags' => [
184 ParamValidator::PARAM_TYPE => 'tags',
185 ParamValidator::PARAM_ISMULTI => true,
186 ],
187 ];
188 if ( $flags ) {
189 $result += $this->getPageSet()->getFinalParams( $flags );
190 }
191
192 return $result;
193 }
194
196 public function needsToken() {
197 return 'csrf';
198 }
199
201 protected function getExamplesMessages() {
202 return [
203 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
204 => 'apihelp-imagerotate-example-simple',
205 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
206 'rotation=180&token=123ABC'
207 => 'apihelp-imagerotate-example-generator',
208 ];
209 }
210
212 public function getHelpUrls() {
213 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Imagerotate';
214 }
215}
216
218class_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:61
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:543
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1359
getResult()
Get the result object.
Definition ApiBase.php:682
setContinuationManager(?ApiContinuationManager $manager=null)
Definition ApiBase.php:729
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:167
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1562
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:823
checkTitleUserPermissions(PageIdentity $pageIdentity, $actions, array $options=[])
Helper function for permission-denied errors.
Definition ApiBase.php:1643
needsToken()
Returns the token type this module requires in order to execute.Modules are strongly encouraged to us...
__construct(ApiMain $mainModule, string $moduleName, RepoGroup $repoGroup, TempFSFileFactory $tempFSFileFactory, TitleFactory $titleFactory)
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.
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:66
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.
Language name search API.