MediaWiki REL1_37
ApiImageRotate.php
Go to the documentation of this file.
1<?php
22
26class ApiImageRotate extends ApiBase {
27 private $mPageSet = null;
28
30 private $repoGroup;
31
34
41 public function __construct(
42 ApiMain $mainModule,
43 $moduleName,
46 ) {
47 parent::__construct( $mainModule, $moduleName );
48 $this->repoGroup = $repoGroup;
49 $this->tempFSFileFactory = $tempFSFileFactory;
50 }
51
52 public function execute() {
54
55 $params = $this->extractRequestParams();
56 $rotation = $params['rotation'];
57
58 $continuationManager = new ApiContinuationManager( $this, [], [] );
59 $this->setContinuationManager( $continuationManager );
60
61 $pageSet = $this->getPageSet();
62 $pageSet->execute();
63
64 $result = $pageSet->getInvalidTitlesAndRevisions( [
65 'invalidTitles', 'special', 'missingIds', 'missingRevIds', 'interwikiTitles',
66 ] );
67
68 // Check if user can add tags
69 if ( $params['tags'] ) {
70 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() );
71 if ( !$ableToTag->isOK() ) {
72 $this->dieStatus( $ableToTag );
73 }
74 }
75
76 foreach ( $pageSet->getTitles() as $title ) {
77 $r = [];
78 $r['id'] = $title->getArticleID();
79 ApiQueryBase::addTitleInfo( $r, $title );
80 if ( !$title->exists() ) {
81 $r['missing'] = true;
82 if ( $title->isKnown() ) {
83 $r['known'] = true;
84 }
85 }
86
87 $file = $this->repoGroup->findFile( $title, [ 'latest' => true ] );
88 if ( !$file ) {
89 $r['result'] = 'Failure';
90 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
91 Status::newFatal( 'apierror-filedoesnotexist' )
92 );
93 $result[] = $r;
94 continue;
95 }
96 $handler = $file->getHandler();
97 if ( !$handler || !$handler->canRotate() ) {
98 $r['result'] = 'Failure';
99 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
100 Status::newFatal( 'apierror-filetypecannotberotated' )
101 );
102 $result[] = $r;
103 continue;
104 }
105
106 // Check whether we're allowed to rotate this file
107 $this->checkTitleUserPermissions( $file->getTitle(), [ 'edit', 'upload' ] );
108
109 $srcPath = $file->getLocalRefPath();
110 if ( $srcPath === false ) {
111 $r['result'] = 'Failure';
112 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
113 Status::newFatal( 'apierror-filenopath' )
114 );
115 $result[] = $r;
116 continue;
117 }
118 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION ) );
119 $tmpFile = $this->tempFSFileFactory->newTempFSFile( 'rotate_', $ext );
120 $dstPath = $tmpFile->getPath();
121 // @phan-suppress-next-line PhanUndeclaredMethod
122 $err = $handler->rotate( $file, [
123 'srcPath' => $srcPath,
124 'dstPath' => $dstPath,
125 'rotation' => $rotation
126 ] );
127 if ( !$err ) {
128 $comment = wfMessage(
129 'rotate-comment'
130 )->numParams( $rotation )->inContentLanguage()->text();
131 // @phan-suppress-next-line PhanUndeclaredMethod
132 $status = $file->upload(
133 $dstPath,
134 $comment,
135 $comment,
136 0,
137 false,
138 false,
139 $this->getAuthority(),
140 $params['tags'] ?: []
141 );
142 if ( $status->isGood() ) {
143 $r['result'] = 'Success';
144 } else {
145 $r['result'] = 'Failure';
146 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
147 }
148 } else {
149 $r['result'] = 'Failure';
150 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
151 Status::newFatal( ApiMessage::create( $err->getMsg() ) )
152 );
153 }
154 $result[] = $r;
155 }
156 $apiResult = $this->getResult();
157 ApiResult::setIndexedTagName( $result, 'page' );
158 $apiResult->addValue( null, $this->getModuleName(), $result );
159
160 $this->setContinuationManager( null );
161 $continuationManager->setContinuationIntoResult( $apiResult );
162 }
163
168 private function getPageSet() {
169 if ( $this->mPageSet === null ) {
170 $this->mPageSet = new ApiPageSet( $this, 0, NS_FILE );
171 }
172
173 return $this->mPageSet;
174 }
175
176 public function mustBePosted() {
177 return true;
178 }
179
180 public function isWriteMode() {
181 return true;
182 }
183
184 public function getAllowedParams( $flags = 0 ) {
185 $result = [
186 'rotation' => [
187 ApiBase::PARAM_TYPE => [ '90', '180', '270' ],
189 ],
190 'continue' => [
191 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
192 ],
193 'tags' => [
194 ApiBase::PARAM_TYPE => 'tags',
196 ],
197 ];
198 if ( $flags ) {
199 $result += $this->getPageSet()->getFinalParams( $flags );
200 }
201
202 return $result;
203 }
204
205 public function needsToken() {
206 return 'csrf';
207 }
208
209 protected function getExamplesMessages() {
210 return [
211 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
212 => 'apihelp-imagerotate-example-simple',
213 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
214 'rotation=180&token=123ABC'
215 => 'apihelp-imagerotate-example-generator',
216 ];
217 }
218}
const NS_FILE
Definition Defines.php:70
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:55
const PARAM_REQUIRED
Definition ApiBase.php:105
const PARAM_TYPE
Definition ApiBase.php:81
getErrorFormatter()
Definition ApiBase.php:639
setContinuationManager(ApiContinuationManager $manager=null)
Definition ApiBase.php:672
getResult()
Get the result object.
Definition ApiBase.php:628
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:764
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:162
checkTitleUserPermissions( $pageIdentity, $actions, array $options=[])
Helper function for permission-denied errors.
Definition ApiBase.php:1565
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:497
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1495
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1293
const PARAM_ISMULTI
Definition ApiBase.php:77
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.
RepoGroup $repoGroup
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.
getPageSet()
Get a cached instance of an ApiPageSet object.
TempFSFileFactory $tempFSFileFactory
__construct(ApiMain $mainModule, $moduleName, RepoGroup $repoGroup, TempFSFileFactory $tempFSFileFactory)
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:49
This class contains a list of pages that the client has requested.
static canAddTagsAccompanyingChange(array $tags, Authority $performer=null)
Is it OK to allow the user to apply all the specified tags at the same time as they edit/make the cha...
Prioritized list of file repositories.
Definition RepoGroup.php:33
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