Merge "Improve docs for Title::getInternalURL/getCanonicalURL"
[lhc/web/wiklou.git] / includes / api / ApiImageRotate.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 class ApiImageRotate extends ApiBase {
22 private $mPageSet = null;
23
24 public function execute() {
25 $this->useTransactionalTimeLimit();
26
27 $params = $this->extractRequestParams();
28 $rotation = $params['rotation'];
29
30 $continuationManager = new ApiContinuationManager( $this, [], [] );
31 $this->setContinuationManager( $continuationManager );
32
33 $pageSet = $this->getPageSet();
34 $pageSet->execute();
35
36 $result = $pageSet->getInvalidTitlesAndRevisions( [
37 'invalidTitles', 'special', 'missingIds', 'missingRevIds', 'interwikiTitles',
38 ] );
39
40 // Check if user can add tags
41 if ( $params['tags'] ) {
42 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getUser() );
43 if ( !$ableToTag->isOK() ) {
44 $this->dieStatus( $ableToTag );
45 }
46 }
47
48 foreach ( $pageSet->getTitles() as $title ) {
49 $r = [];
50 $r['id'] = $title->getArticleID();
51 ApiQueryBase::addTitleInfo( $r, $title );
52 if ( !$title->exists() ) {
53 $r['missing'] = true;
54 if ( $title->isKnown() ) {
55 $r['known'] = true;
56 }
57 }
58
59 $file = wfFindFile( $title, [ 'latest' => true ] );
60 if ( !$file ) {
61 $r['result'] = 'Failure';
62 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
63 Status::newFatal( 'apierror-filedoesnotexist' )
64 );
65 $result[] = $r;
66 continue;
67 }
68 $handler = $file->getHandler();
69 if ( !$handler || !$handler->canRotate() ) {
70 $r['result'] = 'Failure';
71 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
72 Status::newFatal( 'apierror-filetypecannotberotated' )
73 );
74 $result[] = $r;
75 continue;
76 }
77
78 // Check whether we're allowed to rotate this file
79 $permError = $this->checkTitleUserPermissions( $file->getTitle(), [ 'edit', 'upload' ] );
80 if ( $permError ) {
81 $r['result'] = 'Failure';
82 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
83 $this->errorArrayToStatus( $permError )
84 );
85 $result[] = $r;
86 continue;
87 }
88
89 $srcPath = $file->getLocalRefPath();
90 if ( $srcPath === false ) {
91 $r['result'] = 'Failure';
92 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
93 Status::newFatal( 'apierror-filenopath' )
94 );
95 $result[] = $r;
96 continue;
97 }
98 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION ) );
99 $tmpFile = TempFSFile::factory( 'rotate_', $ext, wfTempDir() );
100 $dstPath = $tmpFile->getPath();
101 $err = $handler->rotate( $file, [
102 'srcPath' => $srcPath,
103 'dstPath' => $dstPath,
104 'rotation' => $rotation
105 ] );
106 if ( !$err ) {
107 $comment = wfMessage(
108 'rotate-comment'
109 )->numParams( $rotation )->inContentLanguage()->text();
110 $status = $file->upload(
111 $dstPath,
112 $comment,
113 $comment,
114 0,
115 false,
116 false,
117 $this->getUser(),
118 $params['tags'] ?: []
119 );
120 if ( $status->isGood() ) {
121 $r['result'] = 'Success';
122 } else {
123 $r['result'] = 'Failure';
124 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
125 }
126 } else {
127 $r['result'] = 'Failure';
128 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
129 Status::newFatal( ApiMessage::create( $err->getMsg() ) )
130 );
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 public function mustBePosted() {
155 return true;
156 }
157
158 public function isWriteMode() {
159 return true;
160 }
161
162 public function getAllowedParams( $flags = 0 ) {
163 $result = [
164 'rotation' => [
165 ApiBase::PARAM_TYPE => [ '90', '180', '270' ],
166 ApiBase::PARAM_REQUIRED => true
167 ],
168 'continue' => [
169 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
170 ],
171 'tags' => [
172 ApiBase::PARAM_TYPE => 'tags',
173 ApiBase::PARAM_ISMULTI => true,
174 ],
175 ];
176 if ( $flags ) {
177 $result += $this->getPageSet()->getFinalParams( $flags );
178 }
179
180 return $result;
181 }
182
183 public function needsToken() {
184 return 'csrf';
185 }
186
187 protected function getExamplesMessages() {
188 return [
189 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
190 => 'apihelp-imagerotate-example-simple',
191 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
192 'rotation=180&token=123ABC'
193 => 'apihelp-imagerotate-example-generator',
194 ];
195 }
196 }