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  $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',
175  ApiBase::PARAM_ISMULTI => true,
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 }
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:74
ApiImageRotate\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiImageRotate.php:156
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:146
ApiContinuationManager
This manages continuation state.
Definition: ApiContinuationManager.php:26
ApiBase\PARAM_HELP_MSG
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:104
true
return true
Definition: router.php:90
ApiBase\PARAM_TYPE
const PARAM_TYPE
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:68
ApiImageRotate\getPageSet
getPageSet()
Get a cached instance of an ApiPageSet object.
Definition: ApiImageRotate.php:148
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:546
ApiBase\setContinuationManager
setContinuationManager(ApiContinuationManager $manager=null)
Set the continuation manager.
Definition: ApiBase.php:600
NS_FILE
const NS_FILE
Definition: Defines.php:75
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1198
ContextSource\getUser
getUser()
Definition: ContextSource.php:120
ApiPageSet
This class contains a list of pages that the client has requested.
Definition: ApiPageSet.php:42
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:50
ApiImageRotate\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiImageRotate.php:185
ApiImageRotate
Definition: ApiImageRotate.php:26
ApiImageRotate\getAllowedParams
getAllowedParams( $flags=0)
Definition: ApiImageRotate.php:164
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:695
$title
$title
Definition: testCompression.php:38
ApiMessage\create
static create( $msg, $code=null, array $data=null)
Create an IApiMessage for the message.
Definition: ApiMessage.php:40
ApiResult\setIndexedTagName
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
Definition: ApiResult.php:604
ApiBase\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1272
ChangeTags\canAddTagsAccompanyingChange
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:531
ApiImageRotate\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiImageRotate.php:29
ApiImageRotate\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiImageRotate.php:189
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1473
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:426
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:67
$ext
if(!is_readable( $file)) $ext
Definition: router.php:48
ApiBase\checkTitleUserPermissions
checkTitleUserPermissions(LinkTarget $linkTarget, $actions, array $options=[])
Helper function for permission-denied errors.
Definition: ApiBase.php:1542
ApiBase\getErrorFormatter
getErrorFormatter()
Get the error formatter.
Definition: ApiBase.php:560
ApiImageRotate\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiImageRotate.php:160
ApiQueryBase\addTitleInfo
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
Definition: ApiQueryBase.php:463
ApiImageRotate\$mPageSet
$mPageSet
Definition: ApiImageRotate.php:27