MediaWiki REL1_39
ApiImageRotate.php
Go to the documentation of this file.
1<?php
23
27class ApiImageRotate extends ApiBase {
28 private $mPageSet = null;
29
31 private $repoGroup;
32
34 private $tempFSFileFactory;
35
42 public function __construct(
43 ApiMain $mainModule,
44 $moduleName,
45 RepoGroup $repoGroup,
46 TempFSFileFactory $tempFSFileFactory
47 ) {
48 parent::__construct( $mainModule, $moduleName );
49 $this->repoGroup = $repoGroup;
50 $this->tempFSFileFactory = $tempFSFileFactory;
51 }
52
53 public function execute() {
55
56 $params = $this->extractRequestParams();
57 $rotation = $params['rotation'];
58
59 $continuationManager = new ApiContinuationManager( $this, [], [] );
60 $this->setContinuationManager( $continuationManager );
61
62 $pageSet = $this->getPageSet();
63 $pageSet->execute();
64
65 $result = $pageSet->getInvalidTitlesAndRevisions( [
66 'invalidTitles', 'special', 'missingIds', 'missingRevIds', 'interwikiTitles',
67 ] );
68
69 // Check if user can add tags
70 if ( $params['tags'] ) {
71 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() );
72 if ( !$ableToTag->isOK() ) {
73 $this->dieStatus( $ableToTag );
74 }
75 }
76
77 foreach ( $pageSet->getTitles() as $title ) {
78 $r = [];
79 $r['id'] = $title->getArticleID();
80 ApiQueryBase::addTitleInfo( $r, $title );
81 if ( !$title->exists() ) {
82 $r['missing'] = true;
83 if ( $title->isKnown() ) {
84 $r['known'] = true;
85 }
86 }
87
88 $file = $this->repoGroup->findFile( $title, [ 'latest' => true ] );
89 if ( !$file ) {
90 $r['result'] = 'Failure';
91 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
92 Status::newFatal( 'apierror-filedoesnotexist' )
93 );
94 $result[] = $r;
95 continue;
96 }
97 $handler = $file->getHandler();
98 if ( !$handler || !$handler->canRotate() ) {
99 $r['result'] = 'Failure';
100 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
101 Status::newFatal( 'apierror-filetypecannotberotated' )
102 );
103 $result[] = $r;
104 continue;
105 }
106
107 // Check whether we're allowed to rotate this file
108 $this->checkTitleUserPermissions( $file->getTitle(), [ 'edit', 'upload' ] );
109
110 $srcPath = $file->getLocalRefPath();
111 if ( $srcPath === false ) {
112 $r['result'] = 'Failure';
113 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
114 Status::newFatal( 'apierror-filenopath' )
115 );
116 $result[] = $r;
117 continue;
118 }
119 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION ) );
120 $tmpFile = $this->tempFSFileFactory->newTempFSFile( 'rotate_', $ext );
121 $dstPath = $tmpFile->getPath();
122 // @phan-suppress-next-line PhanUndeclaredMethod
123 $err = $handler->rotate( $file, [
124 'srcPath' => $srcPath,
125 'dstPath' => $dstPath,
126 'rotation' => $rotation
127 ] );
128 if ( !$err ) {
129 $comment = $this->msg(
130 'rotate-comment'
131 )->numParams( $rotation )->inContentLanguage()->text();
132 // @phan-suppress-next-line PhanUndeclaredMethod
133 $status = $file->upload(
134 $dstPath,
135 $comment,
136 $comment,
137 0,
138 false,
139 false,
140 $this->getAuthority(),
141 $params['tags'] ?: []
142 );
143 if ( $status->isGood() ) {
144 $r['result'] = 'Success';
145 } else {
146 $r['result'] = 'Failure';
147 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
148 }
149 } else {
150 $r['result'] = 'Failure';
151 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
152 Status::newFatal( ApiMessage::create( $err->getMsg() ) )
153 );
154 }
155 $result[] = $r;
156 }
157 $apiResult = $this->getResult();
158 ApiResult::setIndexedTagName( $result, 'page' );
159 $apiResult->addValue( null, $this->getModuleName(), $result );
160
161 $this->setContinuationManager( null );
162 $continuationManager->setContinuationIntoResult( $apiResult );
163 }
164
169 private function getPageSet() {
170 if ( $this->mPageSet === null ) {
171 $this->mPageSet = new ApiPageSet( $this, 0, NS_FILE );
172 }
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}
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:56
getErrorFormatter()
Definition ApiBase.php:640
setContinuationManager(ApiContinuationManager $manager=null)
Definition ApiBase.php:673
getResult()
Get the result object.
Definition ApiBase.php:629
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:765
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:163
checkTitleUserPermissions( $pageIdentity, $actions, array $options=[])
Helper function for permission-denied errors.
Definition ApiBase.php:1586
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:498
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1515
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1299
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.
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)
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:52
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()
Prioritized list of file repositories.
Definition RepoGroup.php:29
Service for formatting and validating API parameters.
return true
Definition router.php:92
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42
if(!is_readable( $file)) $ext
Definition router.php:48