4 * Created on January 3rd, 2013
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();
41 $result = $pageSet->getInvalidTitlesAndRevisions( [
42 'invalidTitles', 'special', 'missingIds', 'missingRevIds', 'interwikiTitles',
45 foreach ( $pageSet->getTitles() as $title ) {
47 $r['id'] = $title->getArticleID();
48 ApiQueryBase
::addTitleInfo( $r, $title );
49 if ( !$title->exists() ) {
51 if ( $title->isKnown() ) {
56 $file = wfFindFile( $title, [ 'latest' => true ] );
58 $r['result'] = 'Failure';
59 $r['errormessage'] = 'File does not exist';
63 $handler = $file->getHandler();
64 if ( !$handler ||
!$handler->canRotate() ) {
65 $r['result'] = 'Failure';
66 $r['errormessage'] = 'File type cannot be rotated';
71 // Check whether we're allowed to rotate this file
72 $permError = $this->checkPermissions( $this->getUser(), $file->getTitle() );
73 if ( $permError !== null ) {
74 $r['result'] = 'Failure';
75 $r['errormessage'] = $permError;
80 $srcPath = $file->getLocalRefPath();
81 if ( $srcPath === false ) {
82 $r['result'] = 'Failure';
83 $r['errormessage'] = 'Cannot get local file path';
87 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION
) );
88 $tmpFile = TempFSFile
::factory( 'rotate_', $ext, wfTempDir() );
89 $dstPath = $tmpFile->getPath();
90 $err = $handler->rotate( $file, [
91 'srcPath' => $srcPath,
92 'dstPath' => $dstPath,
93 'rotation' => $rotation
98 )->numParams( $rotation )->inContentLanguage()->text();
99 $status = $file->upload( $dstPath,
100 $comment, $comment, 0, false, false, $this->getUser() );
101 if ( $status->isGood() ) {
102 $r['result'] = 'Success';
104 $r['result'] = 'Failure';
105 $r['errormessage'] = $this->getErrorFormatter()->arrayFromStatus( $status );
108 $r['result'] = 'Failure';
109 $r['errormessage'] = $err->toText();
113 $apiResult = $this->getResult();
114 ApiResult
::setIndexedTagName( $result, 'page' );
115 $apiResult->addValue( null, $this->getModuleName(), $result );
117 $this->setContinuationManager( null );
118 $continuationManager->setContinuationIntoResult( $apiResult );
122 * Get a cached instance of an ApiPageSet object
125 private function getPageSet() {
126 if ( $this->mPageSet
=== null ) {
127 $this->mPageSet
= new ApiPageSet( $this, 0, NS_FILE
);
130 return $this->mPageSet
;
134 * Checks that the user has permissions to perform rotations.
135 * @param User $user The user to check
136 * @param Title $title
137 * @return string|null Permission error message, or null if there is no error
139 protected function checkPermissions( $user, $title ) {
140 $permissionErrors = array_merge(
141 $title->getUserPermissionsErrors( 'edit', $user ),
142 $title->getUserPermissionsErrors( 'upload', $user )
145 if ( $permissionErrors ) {
146 // Just return the first error
147 $msg = $this->parseMsg( $permissionErrors[0] );
155 public function mustBePosted() {
159 public function isWriteMode() {
163 public function getAllowedParams( $flags = 0 ) {
166 ApiBase
::PARAM_TYPE
=> [ '90', '180', '270' ],
167 ApiBase
::PARAM_REQUIRED
=> true
170 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
174 $result +
= $this->getPageSet()->getFinalParams( $flags );
180 public function needsToken() {
184 protected function getExamplesMessages() {
186 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
187 => 'apihelp-imagerotate-example-simple',
188 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
189 'rotation=180&token=123ABC'
190 => 'apihelp-imagerotate-example-generator',