Merge "(bug 46059) giving #p-personal a higher z-index than #content in monobook"
[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(
123 'rotate-comment'
124 )->numParams( $rotation )->inContentLanguage()->text();
125 $status = $file->upload( $dstPath,
126 $comment, $comment, 0, false, false, $this->getUser() );
127 if ( $status->isGood() ) {
128 $r['result'] = 'Success';
129 } else {
130 $r['result'] = 'Failure';
131 $r['errormessage'] = $this->getResult()->convertStatusToArray( $status );
132 }
133 } else {
134 $r['result'] = 'Failure';
135 $r['errormessage'] = $err->toText();
136 }
137 $result[] = $r;
138 }
139 $apiResult = $this->getResult();
140 $apiResult->setIndexedTagName( $result, 'page' );
141 $apiResult->addValue( null, $this->getModuleName(), $result );
142 }
143
144 /**
145 * Get a cached instance of an ApiPageSet object
146 * @return ApiPageSet
147 */
148 private function getPageSet() {
149 if ( $this->mPageSet === null ) {
150 $this->mPageSet = new ApiPageSet( $this, 0, NS_FILE );
151 }
152 return $this->mPageSet;
153 }
154
155 /**
156 * Checks that the user has permissions to perform rotations.
157 * @param $user User The user to check.
158 * @return string|null Permission error message, or null if there is no error
159 */
160 protected function checkPermissions( $user, $title ) {
161 $permissionErrors = array_merge(
162 $title->getUserPermissionsErrors( 'edit', $user ),
163 $title->getUserPermissionsErrors( 'upload', $user )
164 );
165
166 if ( $permissionErrors ) {
167 // Just return the first error
168 $msg = $this->parseMsg( $permissionErrors[0] );
169 return $msg['info'];
170 }
171
172 return null;
173 }
174
175 public function mustBePosted() {
176 return true;
177 }
178
179 public function isWriteMode() {
180 return true;
181 }
182
183 public function getAllowedParams( $flags = 0 ) {
184 $pageSet = $this->getPageSet();
185 $result = array(
186 'rotation' => array(
187 ApiBase::PARAM_TYPE => array( '90', '180', '270' ),
188 ApiBase::PARAM_REQUIRED => true
189 ),
190 'token' => array(
191 ApiBase::PARAM_TYPE => 'string',
192 ApiBase::PARAM_REQUIRED => true
193 ),
194 );
195 if ( $flags ) {
196 $result += $this->getPageSet()->getFinalParams( $flags );
197 }
198 return $result;
199 }
200
201 public function getParamDescription() {
202 $pageSet = $this->getPageSet();
203 return $pageSet->getParamDescription() + 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 return array_merge(
224 parent::getPossibleErrors(),
225 $pageSet->getPossibleErrors()
226 );
227 }
228
229 public function getExamples() {
230 return array(
231 'api.php?action=imagerotate&titles=Example.jpg&rotation=90&token=123ABC',
232 );
233 }
234 }