MediaWiki  master
ApiImageRotate.php
Go to the documentation of this file.
1 <?php
22 
26 class 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();
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',
195  ApiBase::PARAM_ISMULTI => true,
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 }
ApiBase\checkTitleUserPermissions
checkTitleUserPermissions( $pageIdentity, $actions, array $options=[])
Helper function for permission-denied errors.
Definition: ApiBase.php:1514
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:49
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:70
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
Definition: ApiBase.php:78
ApiImageRotate\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiImageRotate.php:176
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:105
true
return true
Definition: router.php:90
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:72
ApiImageRotate\getPageSet
getPageSet()
Get a cached instance of an ApiPageSet object.
Definition: ApiImageRotate.php:168
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:571
ApiBase\setContinuationManager
setContinuationManager(ApiContinuationManager $manager=null)
Definition: ApiBase.php:615
$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:1186
ApiPageSet
This class contains a list of pages that the client has requested.
Definition: ApiPageSet.php:45
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:55
ApiImageRotate\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiImageRotate.php:205
ApiImageRotate
Definition: ApiImageRotate.php:26
ApiImageRotate\getAllowedParams
getAllowedParams( $flags=0)
Definition: ApiImageRotate.php:184
ApiImageRotate\__construct
__construct(ApiMain $mainModule, $moduleName, RepoGroup $repoGroup, TempFSFileFactory $tempFSFileFactory)
Definition: ApiImageRotate.php:41
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:707
$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:43
MediaWiki\FileBackend\FSFile\TempFSFileFactory
Definition: TempFSFileFactory.php:10
ChangeTags\canAddTagsAccompanyingChange
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...
Definition: ChangeTags.php:625
ApiResult\setIndexedTagName
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
Definition: ApiResult.php:603
ContextSource\getAuthority
getAuthority()
Definition: ContextSource.php:144
ApiBase\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1236
ApiImageRotate\$repoGroup
RepoGroup $repoGroup
Definition: ApiImageRotate.php:30
ApiImageRotate\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiImageRotate.php:52
ApiImageRotate\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiImageRotate.php:209
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1442
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:440
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
Definition: ApiBase.php:71
RepoGroup
Prioritized list of file repositories.
Definition: RepoGroup.php:32
$ext
if(!is_readable( $file)) $ext
Definition: router.php:48
NS_FILE
const NS_FILE
Definition: Defines.php:70
ApiImageRotate\$tempFSFileFactory
TempFSFileFactory $tempFSFileFactory
Definition: ApiImageRotate.php:33
ApiBase\getErrorFormatter
getErrorFormatter()
Definition: ApiBase.php:582
ApiImageRotate\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiImageRotate.php:180
ApiQueryBase\addTitleInfo
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
Definition: ApiQueryBase.php:466
ApiImageRotate\$mPageSet
$mPageSet
Definition: ApiImageRotate.php:27