Merge "Typo fix"
[mediawiki.git] / includes / api / ApiImageRotate.php
blob7a60e831b303881c3bd020577da1bd62d75c35d2
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 __construct( $main, $action ) {
28 parent::__construct( $main, $action );
31 /**
32 * Add all items from $values into the result
33 * @param array $result output
34 * @param array $values values to add
35 * @param string $flag the name of the boolean flag to mark this element
36 * @param string $name if given, name of the value
38 private static function addValues( array &$result, $values, $flag = null, $name = null ) {
39 foreach ( $values as $val ) {
40 if ( $val instanceof Title ) {
41 $v = array();
42 ApiQueryBase::addTitleInfo( $v, $val );
43 } elseif ( $name !== null ) {
44 $v = array( $name => $val );
45 } else {
46 $v = $val;
48 if ( $flag !== null ) {
49 $v[$flag] = '';
51 $result[] = $v;
55 public function execute() {
56 $params = $this->extractRequestParams();
57 $rotation = $params['rotation'];
59 $pageSet = $this->getPageSet();
60 $pageSet->execute();
62 $result = array();
64 self::addValues( $result, $pageSet->getInvalidTitles(), 'invalid', 'title' );
65 self::addValues( $result, $pageSet->getSpecialTitles(), 'special', 'title' );
66 self::addValues( $result, $pageSet->getMissingPageIDs(), 'missing', 'pageid' );
67 self::addValues( $result, $pageSet->getMissingRevisionIDs(), 'missing', 'revid' );
68 self::addValues( $result, $pageSet->getInterwikiTitlesAsResult() );
70 foreach ( $pageSet->getTitles() as $title ) {
71 $r = array();
72 $r['id'] = $title->getArticleID();
73 ApiQueryBase::addTitleInfo( $r, $title );
74 if ( !$title->exists() ) {
75 $r['missing'] = '';
78 $file = wfFindFile( $title );
79 if ( !$file ) {
80 $r['result'] = 'Failure';
81 $r['errormessage'] = 'File does not exist';
82 $result[] = $r;
83 continue;
85 $handler = $file->getHandler();
86 if ( !$handler || !$handler->canRotate() ) {
87 $r['result'] = 'Failure';
88 $r['errormessage'] = 'File type cannot be rotated';
89 $result[] = $r;
90 continue;
93 // Check whether we're allowed to rotate this file
94 $permError = $this->checkPermissions( $this->getUser(), $file->getTitle() );
95 if ( $permError !== null ) {
96 $r['result'] = 'Failure';
97 $r['errormessage'] = $permError;
98 $result[] = $r;
99 continue;
102 $srcPath = $file->getLocalRefPath();
103 if ( $srcPath === false ) {
104 $r['result'] = 'Failure';
105 $r['errormessage'] = 'Cannot get local file path';
106 $result[] = $r;
107 continue;
109 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION ) );
110 $tmpFile = TempFSFile::factory( 'rotate_', $ext );
111 $dstPath = $tmpFile->getPath();
112 $err = $handler->rotate( $file, array(
113 "srcPath" => $srcPath,
114 "dstPath" => $dstPath,
115 "rotation" => $rotation
116 ) );
117 if ( !$err ) {
118 $comment = wfMessage(
119 'rotate-comment'
120 )->numParams( $rotation )->inContentLanguage()->text();
121 $status = $file->upload( $dstPath,
122 $comment, $comment, 0, false, false, $this->getUser() );
123 if ( $status->isGood() ) {
124 $r['result'] = 'Success';
125 } else {
126 $r['result'] = 'Failure';
127 $r['errormessage'] = $this->getResult()->convertStatusToArray( $status );
129 } else {
130 $r['result'] = 'Failure';
131 $r['errormessage'] = $err->toText();
133 $result[] = $r;
135 $apiResult = $this->getResult();
136 $apiResult->setIndexedTagName( $result, 'page' );
137 $apiResult->addValue( null, $this->getModuleName(), $result );
141 * Get a cached instance of an ApiPageSet object
142 * @return ApiPageSet
144 private function getPageSet() {
145 if ( $this->mPageSet === null ) {
146 $this->mPageSet = new ApiPageSet( $this, 0, NS_FILE );
148 return $this->mPageSet;
152 * Checks that the user has permissions to perform rotations.
153 * @param User $user The user to check
154 * @param Title $title
155 * @return string|null Permission error message, or null if there is no error
157 protected function checkPermissions( $user, $title ) {
158 $permissionErrors = array_merge(
159 $title->getUserPermissionsErrors( 'edit', $user ),
160 $title->getUserPermissionsErrors( 'upload', $user )
163 if ( $permissionErrors ) {
164 // Just return the first error
165 $msg = $this->parseMsg( $permissionErrors[0] );
166 return $msg['info'];
169 return null;
172 public function mustBePosted() {
173 return true;
176 public function isWriteMode() {
177 return true;
180 public function getAllowedParams( $flags = 0 ) {
181 $result = array(
182 'rotation' => array(
183 ApiBase::PARAM_TYPE => array( '90', '180', '270' ),
184 ApiBase::PARAM_REQUIRED => true
186 'token' => array(
187 ApiBase::PARAM_TYPE => 'string',
188 ApiBase::PARAM_REQUIRED => true
191 if ( $flags ) {
192 $result += $this->getPageSet()->getFinalParams( $flags );
194 return $result;
197 public function getParamDescription() {
198 $pageSet = $this->getPageSet();
199 return $pageSet->getFinalParamDescription() + array(
200 'rotation' => 'Degrees to rotate image clockwise',
201 'token' => 'Edit token. You can get one of these through action=tokens',
205 public function getDescription() {
206 return 'Rotate one or more images';
209 public function needsToken() {
210 return true;
213 public function getTokenSalt() {
214 return '';
217 public function getPossibleErrors() {
218 $pageSet = $this->getPageSet();
219 return array_merge(
220 parent::getPossibleErrors(),
221 $pageSet->getFinalPossibleErrors()
225 public function getExamples() {
226 return array(
227 'api.php?action=imagerotate&titles=Example.jpg&rotation=90&token=123ABC',