Merge "Drop unused FormatMetadata::flattenArray method"
[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] = true;
46 }
47 $result[] = $v;
48 }
49 }
50
51 public function execute() {
52 $params = $this->extractRequestParams();
53 $rotation = $params['rotation'];
54
55 $continuationManager = new ApiContinuationManager( $this, array(), array() );
56 $this->setContinuationManager( $continuationManager );
57
58 $pageSet = $this->getPageSet();
59 $pageSet->execute();
60
61 $result = array();
62
63 self::addValues( $result, $pageSet->getInvalidTitles(), 'invalid', 'title' );
64 self::addValues( $result, $pageSet->getSpecialTitles(), 'special', 'title' );
65 self::addValues( $result, $pageSet->getMissingPageIDs(), 'missing', 'pageid' );
66 self::addValues( $result, $pageSet->getMissingRevisionIDs(), 'missing', 'revid' );
67 self::addValues( $result, $pageSet->getInterwikiTitlesAsResult() );
68
69 foreach ( $pageSet->getTitles() as $title ) {
70 $r = array();
71 $r['id'] = $title->getArticleID();
72 ApiQueryBase::addTitleInfo( $r, $title );
73 if ( !$title->exists() ) {
74 $r['missing'] = true;
75 }
76
77 $file = wfFindFile( $title, array( 'latest' => true ) );
78 if ( !$file ) {
79 $r['result'] = 'Failure';
80 $r['errormessage'] = 'File does not exist';
81 $result[] = $r;
82 continue;
83 }
84 $handler = $file->getHandler();
85 if ( !$handler || !$handler->canRotate() ) {
86 $r['result'] = 'Failure';
87 $r['errormessage'] = 'File type cannot be rotated';
88 $result[] = $r;
89 continue;
90 }
91
92 // Check whether we're allowed to rotate this file
93 $permError = $this->checkPermissions( $this->getUser(), $file->getTitle() );
94 if ( $permError !== null ) {
95 $r['result'] = 'Failure';
96 $r['errormessage'] = $permError;
97 $result[] = $r;
98 continue;
99 }
100
101 $srcPath = $file->getLocalRefPath();
102 if ( $srcPath === false ) {
103 $r['result'] = 'Failure';
104 $r['errormessage'] = 'Cannot get local file path';
105 $result[] = $r;
106 continue;
107 }
108 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION ) );
109 $tmpFile = TempFSFile::factory( 'rotate_', $ext );
110 $dstPath = $tmpFile->getPath();
111 $err = $handler->rotate( $file, array(
112 "srcPath" => $srcPath,
113 "dstPath" => $dstPath,
114 "rotation" => $rotation
115 ) );
116 if ( !$err ) {
117 $comment = wfMessage(
118 'rotate-comment'
119 )->numParams( $rotation )->inContentLanguage()->text();
120 $status = $file->upload( $dstPath,
121 $comment, $comment, 0, false, false, $this->getUser() );
122 if ( $status->isGood() ) {
123 $r['result'] = 'Success';
124 } else {
125 $r['result'] = 'Failure';
126 $r['errormessage'] = $this->getErrorFormatter()->arrayFromStatus( $status );
127 }
128 } else {
129 $r['result'] = 'Failure';
130 $r['errormessage'] = $err->toText();
131 }
132 $result[] = $r;
133 }
134 $apiResult = $this->getResult();
135 ApiResult::setIndexedTagName( $result, 'page' );
136 $apiResult->addValue( null, $this->getModuleName(), $result );
137
138 $this->setContinuationManager( null );
139 $continuationManager->setContinuationIntoResult( $apiResult );
140 }
141
142 /**
143 * Get a cached instance of an ApiPageSet object
144 * @return ApiPageSet
145 */
146 private function getPageSet() {
147 if ( $this->mPageSet === null ) {
148 $this->mPageSet = new ApiPageSet( $this, 0, NS_FILE );
149 }
150
151 return $this->mPageSet;
152 }
153
154 /**
155 * Checks that the user has permissions to perform rotations.
156 * @param User $user The user to check
157 * @param Title $title
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
170 return $msg['info'];
171 }
172
173 return null;
174 }
175
176 public function mustBePosted() {
177 return true;
178 }
179
180 public function isWriteMode() {
181 return true;
182 }
183
184 public function getAllowedParams( $flags = 0 ) {
185 $result = array(
186 'rotation' => array(
187 ApiBase::PARAM_TYPE => array( '90', '180', '270' ),
188 ApiBase::PARAM_REQUIRED => true
189 ),
190 'continue' => array(
191 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
192 ),
193 );
194 if ( $flags ) {
195 $result += $this->getPageSet()->getFinalParams( $flags );
196 }
197
198 return $result;
199 }
200
201 public function needsToken() {
202 return 'csrf';
203 }
204
205 protected function getExamplesMessages() {
206 return array(
207 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
208 => 'apihelp-imagerotate-example-simple',
209 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
210 'rotation=180&token=123ABC'
211 => 'apihelp-imagerotate-example-generator',
212 );
213 }
214 }