Merge "Cleanup for ApiImageRotate"
[lhc/web/wiklou.git] / includes / api / ApiImageRotate.php
1 <?php
2 /**
3 *
4 * Created on January 3rd, 2013
5 *
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.
10 *
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.
15 *
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
20 *
21 * @file
22 */
23
24 class ApiImageRotate extends ApiBase {
25
26 private $mPageSet = null;
27
28 public function __construct( $main, $action ) {
29 parent::__construct( $main, $action );
30 }
31
32 /**
33 * Add all items from $values into the result
34 * @param array $result output
35 * @param array $values values to add
36 * @param string $flag the name of the boolean flag to mark this element
37 * @param string $name if given, name of the value
38 */
39 private static function addValues( array &$result, $values, $flag = null, $name = null ) {
40 foreach ( $values as $val ) {
41 if( $val instanceof Title ) {
42 $v = array();
43 ApiQueryBase::addTitleInfo( $v, $val );
44 } elseif( $name !== null ) {
45 $v = array( $name => $val );
46 } else {
47 $v = $val;
48 }
49 if( $flag !== null ) {
50 $v[$flag] = '';
51 }
52 $result[] = $v;
53 }
54 }
55
56
57 public function execute() {
58 $params = $this->extractRequestParams();
59 $rotation = $params[ 'rotation' ];
60 $user = $this->getUser();
61
62 $pageSet = $this->getPageSet();
63 $pageSet->execute();
64
65 $result = array();
66 $result = array();
67
68 self::addValues( $result, $pageSet->getInvalidTitles(), 'invalid', 'title' );
69 self::addValues( $result, $pageSet->getSpecialTitles(), 'special', 'title' );
70 self::addValues( $result, $pageSet->getMissingPageIDs(), 'missing', 'pageid' );
71 self::addValues( $result, $pageSet->getMissingRevisionIDs(), 'missing', 'revid' );
72 self::addValues( $result, $pageSet->getInterwikiTitlesAsResult() );
73
74 foreach ( $pageSet->getTitles() as $title ) {
75 $r = array();
76 $r['id'] = $title->getArticleID();
77 ApiQueryBase::addTitleInfo( $r, $title );
78 if ( !$title->exists() ) {
79 $r['missing'] = '';
80 }
81
82 $file = wfFindFile( $title );
83 if ( !$file ) {
84 $r['result'] = 'Failure';
85 $r['errormessage'] = 'File does not exist';
86 $result[] = $r;
87 continue;
88 }
89 $handler = $file->getHandler();
90 if ( !$handler || !$handler->canRotate() ) {
91 $r['result'] = 'Failure';
92 $r['errormessage'] = 'File type cannot be rotated';
93 $result[] = $r;
94 continue;
95 }
96
97 // Check whether we're allowed to rotate this file
98 $permError = $this->checkPermissions( $this->getUser(), $file->getTitle() );
99 if ( $permError !== null ) {
100 $r['result'] = 'Failure';
101 $r['errormessage'] = $permError;
102 $result[] = $r;
103 continue;
104 }
105
106 $srcPath = $file->getLocalRefPath();
107 if ( $srcPath === false ) {
108 $r['result'] = 'Failure';
109 $r['errormessage'] = 'Cannot get local file path';
110 $result[] = $r;
111 continue;
112 }
113 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION ) );
114 $tmpFile = TempFSFile::factory( 'rotate_', $ext);
115 $dstPath = $tmpFile->getPath();
116 $err = $handler->rotate( $file, array(
117 "srcPath" => $srcPath,
118 "dstPath" => $dstPath,
119 "rotation"=> $rotation
120 ) );
121 if ( !$err ) {
122 $comment = wfMessage( 'rotate-comment' )->numParams( $rotation )->text();
123 $status = $file->upload( $dstPath,
124 $comment, $comment, 0, false, false, $this->getUser() );
125 if ( $status->isGood() ) {
126 $r['result'] = 'Success';
127 } else {
128 $r['result'] = 'Failure';
129 $r['errormessage'] = $this->getResult()->convertStatusToArray( $status );
130 }
131 } else {
132 $r['result'] = 'Failure';
133 $r['errormessage'] = $err->toText();
134 }
135 $result[] = $r;
136 }
137 $apiResult = $this->getResult();
138 $apiResult->setIndexedTagName( $result, 'page' );
139 $apiResult->addValue( null, $this->getModuleName(), $result );
140 }
141
142 /**
143 * Get a cached instance of an ApiPageSet object
144 * @return ApiPageSet
145 */
146 private function getPageSet() {
147 if ( $this->mPageSet === null ) {
148 $this->mPageSet = new ApiPageSet( $this, 0, NS_FILE );
149 }
150 return $this->mPageSet;
151 }
152
153 /**
154 * Checks that the user has permissions to perform rotations.
155 * @param $user User The user to check.
156 * @return string|null Permission error message, or null if there is no error
157 */
158 protected function checkPermissions( $user, $title ) {
159 $permissionErrors = array_merge(
160 $title->getUserPermissionsErrors( 'edit' , $user ),
161 $title->getUserPermissionsErrors( 'upload' , $user )
162 );
163
164 if ( $permissionErrors ) {
165 // Just return the first error
166 $msg = $this->parseMsg( $permissionErrors[0] );
167 return $msg['info'];
168 }
169
170 return null;
171 }
172
173 public function mustBePosted() {
174 return true;
175 }
176
177 public function isWriteMode() {
178 return true;
179 }
180
181 public function getAllowedParams( $flags = 0 ) {
182 $pageSet = $this->getPageSet();
183 $result = array(
184 'rotation' => array(
185 ApiBase::PARAM_TYPE => array( '90', '180', '270' ),
186 ApiBase::PARAM_REQUIRED => true
187 ),
188 'token' => array(
189 ApiBase::PARAM_TYPE => 'string',
190 ApiBase::PARAM_REQUIRED => true
191 ),
192 );
193 if ( $flags ) {
194 $result += $this->getPageSet()->getFinalParams( $flags );
195 }
196 return $result;
197 }
198
199 public function getParamDescription() {
200 $pageSet = $this->getPageSet();
201 return $pageSet->getParamDescription() + array(
202 'rotation' => 'Degrees to rotate image clockwise',
203 'token' => 'Edit token. You can get one of these through action=tokens',
204 );
205 }
206
207 public function getDescription() {
208 return 'Rotate one or more images';
209 }
210
211 public function needsToken() {
212 return true;
213 }
214
215 public function getTokenSalt() {
216 return '';
217 }
218
219 public function getPossibleErrors() {
220 $pageSet = $this->getPageSet();
221 return array_merge(
222 parent::getPossibleErrors(),
223 $pageSet->getPossibleErrors()
224 );
225 }
226
227 public function getExamples() {
228 return array(
229 'api.php?action=imagerotate&titles=Example.jpg&rotation=90&token=123ABC',
230 );
231 }
232 }