3 use MediaWiki\MediaWikiServices
;
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
24 class ApiImageRotate
extends ApiBase
{
25 private $mPageSet = null;
27 public function execute() {
28 $this->useTransactionalTimeLimit();
30 $params = $this->extractRequestParams();
31 $rotation = $params['rotation'];
33 $continuationManager = new ApiContinuationManager( $this, [], [] );
34 $this->setContinuationManager( $continuationManager );
36 $pageSet = $this->getPageSet();
39 $result = $pageSet->getInvalidTitlesAndRevisions( [
40 'invalidTitles', 'special', 'missingIds', 'missingRevIds', 'interwikiTitles',
43 // Check if user can add tags
44 if ( $params['tags'] ) {
45 $ableToTag = ChangeTags
::canAddTagsAccompanyingChange( $params['tags'], $this->getUser() );
46 if ( !$ableToTag->isOK() ) {
47 $this->dieStatus( $ableToTag );
51 foreach ( $pageSet->getTitles() as $title ) {
53 $r['id'] = $title->getArticleID();
54 ApiQueryBase
::addTitleInfo( $r, $title );
55 if ( !$title->exists() ) {
57 if ( $title->isKnown() ) {
62 $file = MediaWikiServices
::getInstance()->getRepoGroup()->findFile(
63 $title, [ 'latest' => true ]
66 $r['result'] = 'Failure';
67 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
68 Status
::newFatal( 'apierror-filedoesnotexist' )
73 $handler = $file->getHandler();
74 if ( !$handler ||
!$handler->canRotate() ) {
75 $r['result'] = 'Failure';
76 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
77 Status
::newFatal( 'apierror-filetypecannotberotated' )
83 // Check whether we're allowed to rotate this file
84 $permError = $this->checkTitleUserPermissions( $file->getTitle(), [ 'edit', 'upload' ] );
86 $r['result'] = 'Failure';
87 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
88 $this->errorArrayToStatus( $permError )
94 $srcPath = $file->getLocalRefPath();
95 if ( $srcPath === false ) {
96 $r['result'] = 'Failure';
97 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
98 Status
::newFatal( 'apierror-filenopath' )
103 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION
) );
104 $tmpFile = MediaWikiServices
::getInstance()->getTempFSFileFactory()
105 ->newTempFSFile( 'rotate_', $ext );
106 $dstPath = $tmpFile->getPath();
107 $err = $handler->rotate( $file, [
108 'srcPath' => $srcPath,
109 'dstPath' => $dstPath,
110 'rotation' => $rotation
113 $comment = wfMessage(
115 )->numParams( $rotation )->inContentLanguage()->text();
116 $status = $file->upload(
124 $params['tags'] ?
: []
126 if ( $status->isGood() ) {
127 $r['result'] = 'Success';
129 $r['result'] = 'Failure';
130 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
133 $r['result'] = 'Failure';
134 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
135 Status
::newFatal( ApiMessage
::create( $err->getMsg() ) )
140 $apiResult = $this->getResult();
141 ApiResult
::setIndexedTagName( $result, 'page' );
142 $apiResult->addValue( null, $this->getModuleName(), $result );
144 $this->setContinuationManager( null );
145 $continuationManager->setContinuationIntoResult( $apiResult );
149 * Get a cached instance of an ApiPageSet object
152 private function getPageSet() {
153 if ( $this->mPageSet
=== null ) {
154 $this->mPageSet
= new ApiPageSet( $this, 0, NS_FILE
);
157 return $this->mPageSet
;
160 public function mustBePosted() {
164 public function isWriteMode() {
168 public function getAllowedParams( $flags = 0 ) {
171 ApiBase
::PARAM_TYPE
=> [ '90', '180', '270' ],
172 ApiBase
::PARAM_REQUIRED
=> true
175 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
178 ApiBase
::PARAM_TYPE
=> 'tags',
179 ApiBase
::PARAM_ISMULTI
=> true,
183 $result +
= $this->getPageSet()->getFinalParams( $flags );
189 public function needsToken() {
193 protected function getExamplesMessages() {
195 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
196 => 'apihelp-imagerotate-example-simple',
197 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
198 'rotation=180&token=123ABC'
199 => 'apihelp-imagerotate-example-generator',