MediaWiki REL1_35
ApiImageRotate.php
Go to the documentation of this file.
1<?php
22
26class ApiImageRotate extends ApiBase {
27 private $mPageSet = null;
28
29 public function execute() {
31
32 $params = $this->extractRequestParams();
33 $rotation = $params['rotation'];
34
35 $continuationManager = new ApiContinuationManager( $this, [], [] );
36 $this->setContinuationManager( $continuationManager );
37
38 $pageSet = $this->getPageSet();
39 $pageSet->execute();
40
41 $result = $pageSet->getInvalidTitlesAndRevisions( [
42 'invalidTitles', 'special', 'missingIds', 'missingRevIds', 'interwikiTitles',
43 ] );
44
45 // Check if user can add tags
46 if ( $params['tags'] ) {
47 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getUser() );
48 if ( !$ableToTag->isOK() ) {
49 $this->dieStatus( $ableToTag );
50 }
51 }
52
53 foreach ( $pageSet->getTitles() as $title ) {
54 $r = [];
55 $r['id'] = $title->getArticleID();
56 ApiQueryBase::addTitleInfo( $r, $title );
57 if ( !$title->exists() ) {
58 $r['missing'] = true;
59 if ( $title->isKnown() ) {
60 $r['known'] = true;
61 }
62 }
63
64 $file = MediaWikiServices::getInstance()->getRepoGroup()->findFile(
65 $title, [ 'latest' => true ]
66 );
67 if ( !$file ) {
68 $r['result'] = 'Failure';
69 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
70 Status::newFatal( 'apierror-filedoesnotexist' )
71 );
72 $result[] = $r;
73 continue;
74 }
75 $handler = $file->getHandler();
76 if ( !$handler || !$handler->canRotate() ) {
77 $r['result'] = 'Failure';
78 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
79 Status::newFatal( 'apierror-filetypecannotberotated' )
80 );
81 $result[] = $r;
82 continue;
83 }
84
85 // Check whether we're allowed to rotate this file
86 $this->checkTitleUserPermissions( $file->getTitle(), [ 'edit', 'upload' ] );
87
88 $srcPath = $file->getLocalRefPath();
89 if ( $srcPath === false ) {
90 $r['result'] = 'Failure';
91 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
92 Status::newFatal( 'apierror-filenopath' )
93 );
94 $result[] = $r;
95 continue;
96 }
97 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION ) );
98 $tmpFile = MediaWikiServices::getInstance()->getTempFSFileFactory()
99 ->newTempFSFile( 'rotate_', $ext );
100 $dstPath = $tmpFile->getPath();
101 // @phan-suppress-next-line PhanUndeclaredMethod
102 $err = $handler->rotate( $file, [
103 'srcPath' => $srcPath,
104 'dstPath' => $dstPath,
105 'rotation' => $rotation
106 ] );
107 if ( !$err ) {
108 $comment = wfMessage(
109 'rotate-comment'
110 )->numParams( $rotation )->inContentLanguage()->text();
111 // @phan-suppress-next-line PhanUndeclaredMethod
112 $status = $file->upload(
113 $dstPath,
114 $comment,
115 $comment,
116 0,
117 false,
118 false,
119 $this->getUser(),
120 $params['tags'] ?: []
121 );
122 if ( $status->isGood() ) {
123 $r['result'] = 'Success';
124 } else {
125 $r['result'] = 'Failure';
126 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
127 }
128 } else {
129 $r['result'] = 'Failure';
130 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
131 Status::newFatal( ApiMessage::create( $err->getMsg() ) )
132 );
133 }
134 $result[] = $r;
135 }
136 $apiResult = $this->getResult();
137 ApiResult::setIndexedTagName( $result, 'page' );
138 $apiResult->addValue( null, $this->getModuleName(), $result );
139
140 $this->setContinuationManager( null );
141 $continuationManager->setContinuationIntoResult( $apiResult );
142 }
143
148 private function getPageSet() {
149 if ( $this->mPageSet === null ) {
150 $this->mPageSet = new ApiPageSet( $this, 0, NS_FILE );
151 }
152
153 return $this->mPageSet;
154 }
155
156 public function mustBePosted() {
157 return true;
158 }
159
160 public function isWriteMode() {
161 return true;
162 }
163
164 public function getAllowedParams( $flags = 0 ) {
165 $result = [
166 'rotation' => [
167 ApiBase::PARAM_TYPE => [ '90', '180', '270' ],
169 ],
170 'continue' => [
171 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
172 ],
173 'tags' => [
174 ApiBase::PARAM_TYPE => 'tags',
176 ],
177 ];
178 if ( $flags ) {
179 $result += $this->getPageSet()->getFinalParams( $flags );
180 }
181
182 return $result;
183 }
184
185 public function needsToken() {
186 return 'csrf';
187 }
188
189 protected function getExamplesMessages() {
190 return [
191 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
192 => 'apihelp-imagerotate-example-simple',
193 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
194 'rotation=180&token=123ABC'
195 => 'apihelp-imagerotate-example-generator',
196 ];
197 }
198}
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:52
const PARAM_REQUIRED
Definition ApiBase.php:102
const PARAM_TYPE
Definition ApiBase.php:78
getErrorFormatter()
Get the error formatter Stable to override.
Definition ApiBase.php:635
setContinuationManager(ApiContinuationManager $manager=null)
Set the continuation manager.
Definition ApiBase.php:676
getResult()
Get the result object.
Definition ApiBase.php:620
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:772
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:162
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:499
checkTitleUserPermissions(LinkTarget $linkTarget, $actions, array $options=[])
Helper function for permission-denied errors.
Definition ApiBase.php:1564
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:1294
const PARAM_ISMULTI
Definition ApiBase.php:74
This manages continuation state.
mustBePosted()
Indicates whether this module must be called with a POST request Stable to override.
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.
getPageSet()
Get a cached instance of an ApiPageSet object.
This class contains a list of pages that the client has requested.
static canAddTagsAccompanyingChange(array $tags, User $user=null)
Is it OK to allow the user to apply all the specified tags at the same time as they edit/make the cha...
getUser()
Stable to override.
MediaWikiServices is the service locator for the application scope of MediaWiki.
const NS_FILE
Definition Defines.php:76
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