Merge "set url used in createaccount-text to main page canonical url"
[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 public function __construct( $main, $action ) {
28 parent::__construct( $main, $action );
29 }
30
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
37 */
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;
47 }
48 if ( $flag !== null ) {
49 $v[$flag] = '';
50 }
51 $result[] = $v;
52 }
53 }
54
55 public function execute() {
56 $params = $this->extractRequestParams();
57 $rotation = $params['rotation'];
58
59 $pageSet = $this->getPageSet();
60 $pageSet->execute();
61
62 $result = array();
63
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() );
69
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'] = '';
76 }
77
78 $file = wfFindFile( $title );
79 if ( !$file ) {
80 $r['result'] = 'Failure';
81 $r['errormessage'] = 'File does not exist';
82 $result[] = $r;
83 continue;
84 }
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;
91 }
92
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;
100 }
101
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;
108 }
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 );
128 }
129 } else {
130 $r['result'] = 'Failure';
131 $r['errormessage'] = $err->toText();
132 }
133 $result[] = $r;
134 }
135 $apiResult = $this->getResult();
136 $apiResult->setIndexedTagName( $result, 'page' );
137 $apiResult->addValue( null, $this->getModuleName(), $result );
138 }
139
140 /**
141 * Get a cached instance of an ApiPageSet object
142 * @return ApiPageSet
143 */
144 private function getPageSet() {
145 if ( $this->mPageSet === null ) {
146 $this->mPageSet = new ApiPageSet( $this, 0, NS_FILE );
147 }
148
149 return $this->mPageSet;
150 }
151
152 /**
153 * Checks that the user has permissions to perform rotations.
154 * @param User $user The user to check
155 * @param Title $title
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
168 return $msg['info'];
169 }
170
171 return null;
172 }
173
174 public function mustBePosted() {
175 return true;
176 }
177
178 public function isWriteMode() {
179 return true;
180 }
181
182 public function getAllowedParams( $flags = 0 ) {
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
197 return $result;
198 }
199
200 public function getParamDescription() {
201 $pageSet = $this->getPageSet();
202
203 return $pageSet->getFinalParamDescription() + array(
204 'rotation' => 'Degrees to rotate image clockwise',
205 'token' => 'Edit token. You can get one of these through action=tokens',
206 );
207 }
208
209 public function getDescription() {
210 return 'Rotate one or more images';
211 }
212
213 public function needsToken() {
214 return true;
215 }
216
217 public function getTokenSalt() {
218 return '';
219 }
220
221 public function getPossibleErrors() {
222 $pageSet = $this->getPageSet();
223
224 return array_merge(
225 parent::getPossibleErrors(),
226 $pageSet->getFinalPossibleErrors()
227 );
228 }
229
230 public function getExamples() {
231 return array(
232 'api.php?action=imagerotate&titles=Example.jpg&rotation=90&token=123ABC',
233 );
234 }
235 }