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