Merge "numRows on MySQL no longer propagates unrelated errors"
[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 if( is_null( $rotation ) || $rotation % 90 ) {
63 $this->dieUsage( "Rotation: {$rotation}", 'rotation must be multiple of 90 degrees' );
64 }
65
66 $pageSet = $this->getPageSet();
67 $pageSet->execute();
68
69 $result = array();
70 $result = array();
71
72 self::addValues( $result, $pageSet->getInvalidTitles(), 'invalid', 'title' );
73 self::addValues( $result, $pageSet->getSpecialTitles(), 'special', 'title' );
74 self::addValues( $result, $pageSet->getMissingPageIDs(), 'missing', 'pageid' );
75 self::addValues( $result, $pageSet->getMissingRevisionIDs(), 'missing', 'revid' );
76 self::addValues( $result, $pageSet->getMissingTitles(), 'missing' );
77 self::addValues( $result, $pageSet->getInterwikiTitlesAsResult() );
78
79 foreach ( $pageSet->getTitles() as $title ) {
80 $file = wfFindFile( $title );
81
82 $r = array();
83 $r[ 'title' ] = $title->getFullText();
84 if ( !$file ) {
85 $r['missing'] = '';
86 $r['result'] = 'Failure';
87 $result[] = $r;
88 continue;
89 }
90 $handler = $file->getHandler();
91 if ( !$handler || !$handler->canRotate() ) {
92 $r['invalid'] = '';
93 $r['result'] = 'Failure';
94 $result[] = $r;
95 continue;
96 }
97
98 // Check whether we're allowed to rotate this file
99 $this->checkPermissions( $this->getUser(), $file->getTitle() );
100
101 $srcPath = $file->getLocalRefPath();
102 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION ) );
103 $tmpFile = TempFSFile::factory( 'rotate_', $ext);
104 $dstPath = $tmpFile->getPath();
105 $err = $handler->rotate( $file, array(
106 "srcPath" => $srcPath,
107 "dstPath" => $dstPath,
108 "rotation"=> $rotation
109 ) );
110 if ( !$err ) {
111 $comment = wfMessage( 'rotate-comment' )->numParams( $rotation )->text();
112 $status = $file->upload( $dstPath,
113 $comment, $comment, 0, false, false, $this->getUser() );
114 if ( $status->isGood() ) {
115 $r['result'] = 'Success';
116 } else {
117 $r['result'] = 'Failure';
118 $r['errormessage'] = $this->getResult()->convertStatusToArray( $status );
119 }
120 } else {
121 $r['result'] = 'Failure';
122 $r['errormessage'] = $err->toText();
123 }
124 $result[] = $r;
125 }
126 $apiResult = $this->getResult();
127 $apiResult->setIndexedTagName( $result, 'page' );
128 $apiResult->addValue( null, $this->getModuleName(), $result );
129 }
130
131 /**
132 * Get a cached instance of an ApiPageSet object
133 * @return ApiPageSet
134 */
135 private function getPageSet() {
136 if ( $this->mPageSet === null ) {
137 $this->mPageSet = new ApiPageSet( $this, 0, NS_FILE );
138 }
139 return $this->mPageSet;
140 }
141
142 /**
143 * Checks that the user has permissions to perform rotations.
144 * Dies with usage message on inadequate permissions.
145 * @param $user User The user to check.
146 */
147 protected function checkPermissions( $user, $title ) {
148 $permissionErrors = array_merge(
149 $title->getUserPermissionsErrors( 'edit' , $user ),
150 $title->getUserPermissionsErrors( 'upload' , $user )
151 );
152
153 if ( $permissionErrors ) {
154 $this->dieUsageMsg( $permissionErrors[0] );
155 }
156 }
157
158 public function mustBePosted() {
159 return true;
160 }
161
162 public function isWriteMode() {
163 return true;
164 }
165
166 public function getAllowedParams( $flags = 0 ) {
167 $pageSet = $this->getPageSet();
168 $result = array(
169 'rotation' => array(
170 ApiBase::PARAM_DFLT => 0,
171 ),
172 'token' => array(
173 ApiBase::PARAM_TYPE => 'string',
174 ApiBase::PARAM_REQUIRED => true
175 ),
176 );
177 if ( $flags ) {
178 $result += $this->getPageSet()->getFinalParams( $flags );
179 }
180 return $result;
181 }
182
183 public function getParamDescription() {
184 $pageSet = $this->getPageSet();
185 return $pageSet->getParamDescription() + array(
186 'rotation' => 'Degrees to rotate image, values can be 0, 90, 180 or 270',
187 'token' => 'Edit token. You can get one of these through prop=info',
188 );
189 }
190
191 public function getDescription() {
192 return 'Rotate one or more images';
193 }
194
195 public function needsToken() {
196 return true;
197 }
198
199 public function getTokenSalt() {
200 return '';
201 }
202
203 public function getPossibleErrors() {
204 $pageSet = $this->getPageSet();
205 return array_merge(
206 parent::getPossibleErrors(),
207 $pageSet->getPossibleErrors()
208 );
209 }
210
211 public function getExamples() {
212 return array(
213 'api.php?action=imagerotate&titles=Example.jpg&rotation=90&token=+\\',
214 );
215 }
216 }