MediaWiki  master
ApiImageRotate.php
Go to the documentation of this file.
1 <?php
22 
26 class 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();
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  $permError = $this->checkTitleUserPermissions( $file->getTitle(), [ 'edit', 'upload' ] );
87  if ( $permError ) {
88  $r['result'] = 'Failure';
89  $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
90  $this->errorArrayToStatus( $permError )
91  );
92  $result[] = $r;
93  continue;
94  }
95 
96  $srcPath = $file->getLocalRefPath();
97  if ( $srcPath === false ) {
98  $r['result'] = 'Failure';
99  $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
100  Status::newFatal( 'apierror-filenopath' )
101  );
102  $result[] = $r;
103  continue;
104  }
105  $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION ) );
106  $tmpFile = MediaWikiServices::getInstance()->getTempFSFileFactory()
107  ->newTempFSFile( 'rotate_', $ext );
108  $dstPath = $tmpFile->getPath();
109  // @phan-suppress-next-line PhanUndeclaredMethod
110  $err = $handler->rotate( $file, [
111  'srcPath' => $srcPath,
112  'dstPath' => $dstPath,
113  'rotation' => $rotation
114  ] );
115  if ( !$err ) {
116  $comment = wfMessage(
117  'rotate-comment'
118  )->numParams( $rotation )->inContentLanguage()->text();
119  // @phan-suppress-next-line PhanUndeclaredMethod
120  $status = $file->upload(
121  $dstPath,
122  $comment,
123  $comment,
124  0,
125  false,
126  false,
127  $this->getUser(),
128  $params['tags'] ?: []
129  );
130  if ( $status->isGood() ) {
131  $r['result'] = 'Success';
132  } else {
133  $r['result'] = 'Failure';
134  $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
135  }
136  } else {
137  $r['result'] = 'Failure';
138  $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
139  Status::newFatal( ApiMessage::create( $err->getMsg() ) )
140  );
141  }
142  $result[] = $r;
143  }
144  $apiResult = $this->getResult();
145  ApiResult::setIndexedTagName( $result, 'page' );
146  $apiResult->addValue( null, $this->getModuleName(), $result );
147 
148  $this->setContinuationManager( null );
149  $continuationManager->setContinuationIntoResult( $apiResult );
150  }
151 
156  private function getPageSet() {
157  if ( $this->mPageSet === null ) {
158  $this->mPageSet = new ApiPageSet( $this, 0, NS_FILE );
159  }
160 
161  return $this->mPageSet;
162  }
163 
164  public function mustBePosted() {
165  return true;
166  }
167 
168  public function isWriteMode() {
169  return true;
170  }
171 
172  public function getAllowedParams( $flags = 0 ) {
173  $result = [
174  'rotation' => [
175  ApiBase::PARAM_TYPE => [ '90', '180', '270' ],
177  ],
178  'continue' => [
179  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
180  ],
181  'tags' => [
182  ApiBase::PARAM_TYPE => 'tags',
183  ApiBase::PARAM_ISMULTI => true,
184  ],
185  ];
186  if ( $flags ) {
187  $result += $this->getPageSet()->getFinalParams( $flags );
188  }
189 
190  return $result;
191  }
192 
193  public function needsToken() {
194  return 'csrf';
195  }
196 
197  protected function getExamplesMessages() {
198  return [
199  'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
200  => 'apihelp-imagerotate-example-simple',
201  'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
202  'rotation=180&token=123ABC'
203  => 'apihelp-imagerotate-example-generator',
204  ];
205  }
206 }
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below...
Definition: ApiBase.php:94
getErrorFormatter()
Get the error formatter.
Definition: ApiBase.php:654
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
getResult()
Get the result object.
Definition: ApiBase.php:640
This class contains a list of pages that the client has requested.
Definition: ApiPageSet.php:40
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:2086
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1871
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition: ApiBase.php:118
getAllowedParams( $flags=0)
getPageSet()
Get a cached instance of an ApiPageSet object.
This manages continuation state.
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user...
Definition: ApiBase.php:761
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
Definition: ApiResult.php:616
static newFatal( $message)
Factory function for fatal errors.
Definition: StatusValue.php:68
setContinuationManager(ApiContinuationManager $manager=null)
Set the continuation manager.
Definition: ApiBase.php:694
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:520
const NS_FILE
Definition: Defines.php:66
checkTitleUserPermissions(LinkTarget $linkTarget, $actions, $options=[])
Helper function for permission-denied errors.
Definition: ApiBase.php:2156
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter...
Definition: ApiBase.php:131
errorArrayToStatus(array $errors, User $user=null)
Turn an array of message keys or key+param arrays into a Status.
Definition: ApiBase.php:1825
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:58
if(!is_readable( $file)) $ext
Definition: router.php:48
This abstract class implements many basic API functions, and is the base of all API classes...
Definition: ApiBase.php:42
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...
Definition: ChangeTags.php:521
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
return true
Definition: router.php:82
static create( $msg, $code=null, array $data=null)
Create an IApiMessage for the message.
Definition: ApiMessage.php:40