DatabaseMysqlBase visibility cleanups
[mediawiki.git] / includes / api / ApiImageRotate.php
blob37cb80a9fc292eae28a72bc2bb9a281acb8a0a5f
1 <?php
2 /**
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
21 * @file
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();
37 $pageSet->execute();
39 $result = [];
41 $result = $pageSet->getInvalidTitlesAndRevisions( [
42 'invalidTitles', 'special', 'missingIds', 'missingRevIds', 'interwikiTitles',
43 ] );
45 foreach ( $pageSet->getTitles() as $title ) {
46 $r = [];
47 $r['id'] = $title->getArticleID();
48 ApiQueryBase::addTitleInfo( $r, $title );
49 if ( !$title->exists() ) {
50 $r['missing'] = true;
51 if ( $title->isKnown() ) {
52 $r['known'] = true;
56 $file = wfFindFile( $title, [ 'latest' => true ] );
57 if ( !$file ) {
58 $r['result'] = 'Failure';
59 $r['errormessage'] = 'File does not exist';
60 $result[] = $r;
61 continue;
63 $handler = $file->getHandler();
64 if ( !$handler || !$handler->canRotate() ) {
65 $r['result'] = 'Failure';
66 $r['errormessage'] = 'File type cannot be rotated';
67 $result[] = $r;
68 continue;
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;
76 $result[] = $r;
77 continue;
80 $srcPath = $file->getLocalRefPath();
81 if ( $srcPath === false ) {
82 $r['result'] = 'Failure';
83 $r['errormessage'] = 'Cannot get local file path';
84 $result[] = $r;
85 continue;
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
94 ] );
95 if ( !$err ) {
96 $comment = wfMessage(
97 'rotate-comment'
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';
103 } else {
104 $r['result'] = 'Failure';
105 $r['errormessage'] = $this->getErrorFormatter()->arrayFromStatus( $status );
107 } else {
108 $r['result'] = 'Failure';
109 $r['errormessage'] = $err->toText();
111 $result[] = $r;
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
123 * @return ApiPageSet
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] );
149 return $msg['info'];
152 return null;
155 public function mustBePosted() {
156 return true;
159 public function isWriteMode() {
160 return true;
163 public function getAllowedParams( $flags = 0 ) {
164 $result = [
165 'rotation' => [
166 ApiBase::PARAM_TYPE => [ '90', '180', '270' ],
167 ApiBase::PARAM_REQUIRED => true
169 'continue' => [
170 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
173 if ( $flags ) {
174 $result += $this->getPageSet()->getFinalParams( $flags );
177 return $result;
180 public function needsToken() {
181 return 'csrf';
184 protected function getExamplesMessages() {
185 return [
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',