Don't try to verify XML well-formedness for partial SVG uploads
[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 $this->getResult()->beginContinuation( $params['continue'], array(), array() );
56
57 $pageSet = $this->getPageSet();
58 $pageSet->execute();
59
60 $result = array();
61
62 self::addValues( $result, $pageSet->getInvalidTitles(), 'invalid', 'title' );
63 self::addValues( $result, $pageSet->getSpecialTitles(), 'special', 'title' );
64 self::addValues( $result, $pageSet->getMissingPageIDs(), 'missing', 'pageid' );
65 self::addValues( $result, $pageSet->getMissingRevisionIDs(), 'missing', 'revid' );
66 self::addValues( $result, $pageSet->getInterwikiTitlesAsResult() );
67
68 foreach ( $pageSet->getTitles() as $title ) {
69 $r = array();
70 $r['id'] = $title->getArticleID();
71 ApiQueryBase::addTitleInfo( $r, $title );
72 if ( !$title->exists() ) {
73 $r['missing'] = '';
74 }
75
76 $file = wfFindFile( $title );
77 if ( !$file ) {
78 $r['result'] = 'Failure';
79 $r['errormessage'] = 'File does not exist';
80 $result[] = $r;
81 continue;
82 }
83 $handler = $file->getHandler();
84 if ( !$handler || !$handler->canRotate() ) {
85 $r['result'] = 'Failure';
86 $r['errormessage'] = 'File type cannot be rotated';
87 $result[] = $r;
88 continue;
89 }
90
91 // Check whether we're allowed to rotate this file
92 $permError = $this->checkPermissions( $this->getUser(), $file->getTitle() );
93 if ( $permError !== null ) {
94 $r['result'] = 'Failure';
95 $r['errormessage'] = $permError;
96 $result[] = $r;
97 continue;
98 }
99
100 $srcPath = $file->getLocalRefPath();
101 if ( $srcPath === false ) {
102 $r['result'] = 'Failure';
103 $r['errormessage'] = 'Cannot get local file path';
104 $result[] = $r;
105 continue;
106 }
107 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION ) );
108 $tmpFile = TempFSFile::factory( 'rotate_', $ext );
109 $dstPath = $tmpFile->getPath();
110 $err = $handler->rotate( $file, array(
111 "srcPath" => $srcPath,
112 "dstPath" => $dstPath,
113 "rotation" => $rotation
114 ) );
115 if ( !$err ) {
116 $comment = wfMessage(
117 'rotate-comment'
118 )->numParams( $rotation )->inContentLanguage()->text();
119 $status = $file->upload( $dstPath,
120 $comment, $comment, 0, false, false, $this->getUser() );
121 if ( $status->isGood() ) {
122 $r['result'] = 'Success';
123 } else {
124 $r['result'] = 'Failure';
125 $r['errormessage'] = $this->getResult()->convertStatusToArray( $status );
126 }
127 } else {
128 $r['result'] = 'Failure';
129 $r['errormessage'] = $err->toText();
130 }
131 $result[] = $r;
132 }
133 $apiResult = $this->getResult();
134 $apiResult->setIndexedTagName( $result, 'page' );
135 $apiResult->addValue( null, $this->getModuleName(), $result );
136 $apiResult->endContinuation();
137 }
138
139 /**
140 * Get a cached instance of an ApiPageSet object
141 * @return ApiPageSet
142 */
143 private function getPageSet() {
144 if ( $this->mPageSet === null ) {
145 $this->mPageSet = new ApiPageSet( $this, 0, NS_FILE );
146 }
147
148 return $this->mPageSet;
149 }
150
151 /**
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
156 */
157 protected function checkPermissions( $user, $title ) {
158 $permissionErrors = array_merge(
159 $title->getUserPermissionsErrors( 'edit', $user ),
160 $title->getUserPermissionsErrors( 'upload', $user )
161 );
162
163 if ( $permissionErrors ) {
164 // Just return the first error
165 $msg = $this->parseMsg( $permissionErrors[0] );
166
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 $result = array(
183 'rotation' => array(
184 ApiBase::PARAM_TYPE => array( '90', '180', '270' ),
185 ApiBase::PARAM_REQUIRED => true
186 ),
187 'continue' => '',
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 'continue' => 'When more results are available, use this to continue',
202 );
203 }
204
205 public function getDescription() {
206 return 'Rotate one or more images.';
207 }
208
209 public function needsToken() {
210 return 'csrf';
211 }
212
213 public function getExamples() {
214 return array(
215 'api.php?action=imagerotate&titles=Example.jpg&rotation=90&token=123ABC',
216 );
217 }
218 }