Merge "Handle missing namespace prefix in XML dumps more gracefully"
[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 execute() {
28 $this->useTransactionalTimeLimit();
29
30 $params = $this->extractRequestParams();
31 $rotation = $params['rotation'];
32
33 $continuationManager = new ApiContinuationManager( $this, [], [] );
34 $this->setContinuationManager( $continuationManager );
35
36 $pageSet = $this->getPageSet();
37 $pageSet->execute();
38
39 $result = [];
40
41 $result = $pageSet->getInvalidTitlesAndRevisions( [
42 'invalidTitles', 'special', 'missingIds', 'missingRevIds', 'interwikiTitles',
43 ] );
44
45 // Check if user can add tags
46 if ( count( $params['tags'] ) ) {
47 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getUser() );
48 if ( !$ableToTag->isOK() ) {
49 $this->dieStatus( $ableToTag );
50 }
51 }
52
53 foreach ( $pageSet->getTitles() as $title ) {
54 $r = [];
55 $r['id'] = $title->getArticleID();
56 ApiQueryBase::addTitleInfo( $r, $title );
57 if ( !$title->exists() ) {
58 $r['missing'] = true;
59 if ( $title->isKnown() ) {
60 $r['known'] = true;
61 }
62 }
63
64 $file = wfFindFile( $title, [ 'latest' => true ] );
65 if ( !$file ) {
66 $r['result'] = 'Failure';
67 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
68 Status::newFatal( 'apierror-filedoesnotexist' )
69 );
70 $result[] = $r;
71 continue;
72 }
73 $handler = $file->getHandler();
74 if ( !$handler || !$handler->canRotate() ) {
75 $r['result'] = 'Failure';
76 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
77 Status::newFatal( 'apierror-filetypecannotberotated' )
78 );
79 $result[] = $r;
80 continue;
81 }
82
83 // Check whether we're allowed to rotate this file
84 $permError = $this->checkTitleUserPermissions( $file->getTitle(), [ 'edit', 'upload' ] );
85 if ( $permError ) {
86 $r['result'] = 'Failure';
87 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
88 $this->errorArrayToStatus( $permError )
89 );
90 $result[] = $r;
91 continue;
92 }
93
94 $srcPath = $file->getLocalRefPath();
95 if ( $srcPath === false ) {
96 $r['result'] = 'Failure';
97 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
98 Status::newFatal( 'apierror-filenopath' )
99 );
100 $result[] = $r;
101 continue;
102 }
103 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION ) );
104 $tmpFile = TempFSFile::factory( 'rotate_', $ext, wfTempDir() );
105 $dstPath = $tmpFile->getPath();
106 $err = $handler->rotate( $file, [
107 'srcPath' => $srcPath,
108 'dstPath' => $dstPath,
109 'rotation' => $rotation
110 ] );
111 if ( !$err ) {
112 $comment = wfMessage(
113 'rotate-comment'
114 )->numParams( $rotation )->inContentLanguage()->text();
115 $status = $file->upload(
116 $dstPath,
117 $comment,
118 $comment,
119 0,
120 false,
121 false,
122 $this->getUser(),
123 $params['tags'] ?: []
124 );
125 if ( $status->isGood() ) {
126 $r['result'] = 'Success';
127 } else {
128 $r['result'] = 'Failure';
129 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
130 }
131 } else {
132 $r['result'] = 'Failure';
133 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
134 Status::newFatal( ApiMessage::create( $err->getMsg() ) )
135 );
136 }
137 $result[] = $r;
138 }
139 $apiResult = $this->getResult();
140 ApiResult::setIndexedTagName( $result, 'page' );
141 $apiResult->addValue( null, $this->getModuleName(), $result );
142
143 $this->setContinuationManager( null );
144 $continuationManager->setContinuationIntoResult( $apiResult );
145 }
146
147 /**
148 * Get a cached instance of an ApiPageSet object
149 * @return ApiPageSet
150 */
151 private function getPageSet() {
152 if ( $this->mPageSet === null ) {
153 $this->mPageSet = new ApiPageSet( $this, 0, NS_FILE );
154 }
155
156 return $this->mPageSet;
157 }
158
159 public function mustBePosted() {
160 return true;
161 }
162
163 public function isWriteMode() {
164 return true;
165 }
166
167 public function getAllowedParams( $flags = 0 ) {
168 $result = [
169 'rotation' => [
170 ApiBase::PARAM_TYPE => [ '90', '180', '270' ],
171 ApiBase::PARAM_REQUIRED => true
172 ],
173 'continue' => [
174 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
175 ],
176 'tags' => [
177 ApiBase::PARAM_TYPE => 'tags',
178 ApiBase::PARAM_ISMULTI => true,
179 ],
180 ];
181 if ( $flags ) {
182 $result += $this->getPageSet()->getFinalParams( $flags );
183 }
184
185 return $result;
186 }
187
188 public function needsToken() {
189 return 'csrf';
190 }
191
192 protected function getExamplesMessages() {
193 return [
194 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
195 => 'apihelp-imagerotate-example-simple',
196 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
197 'rotation=180&token=123ABC'
198 => 'apihelp-imagerotate-example-generator',
199 ];
200 }
201 }