MediaWiki REL1_40
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 $this->mPageSet ??= new ApiPageSet( $this, 0, NS_FILE );
171
172 return $this->mPageSet;
173 }
174
175 public function mustBePosted() {
176 return true;
177 }
178
179 public function isWriteMode() {
180 return true;
181 }
182
183 public function getAllowedParams( $flags = 0 ) {
184 $result = [
185 'rotation' => [
186 ParamValidator::PARAM_TYPE => [ '90', '180', '270' ],
187 ParamValidator::PARAM_REQUIRED => true
188 ],
189 'continue' => [
190 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
191 ],
192 'tags' => [
193 ParamValidator::PARAM_TYPE => 'tags',
194 ParamValidator::PARAM_ISMULTI => true,
195 ],
196 ];
197 if ( $flags ) {
198 $result += $this->getPageSet()->getFinalParams( $flags );
199 }
200
201 return $result;
202 }
203
204 public function needsToken() {
205 return 'csrf';
206 }
207
208 protected function getExamplesMessages() {
209 return [
210 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
211 => 'apihelp-imagerotate-example-simple',
212 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
213 'rotation=180&token=123ABC'
214 => 'apihelp-imagerotate-example-generator',
215 ];
216 }
217}
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:59
getErrorFormatter()
Definition ApiBase.php:648
setContinuationManager(ApiContinuationManager $manager=null)
Definition ApiBase.php:681
getResult()
Get the result object.
Definition ApiBase.php:637
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:773
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:166
checkTitleUserPermissions( $pageIdentity, $actions, array $options=[])
Helper function for permission-denied errors.
Definition ApiBase.php:1592
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:506
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1521
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1305
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:58
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:30
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