Merge "Add CollationFa"
[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 foreach ( $pageSet->getTitles() as $title ) {
46 $r = [];
47 $r['id'] = $title->getArticleID();
48 ApiQueryBase::addTitleInfo( $r, $title );
49 if ( !$title->exists() ) {
50 $r['missing'] = true;
51 if ( $title->isKnown() ) {
52 $r['known'] = true;
53 }
54 }
55
56 $file = wfFindFile( $title, [ 'latest' => true ] );
57 if ( !$file ) {
58 $r['result'] = 'Failure';
59 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
60 Status::newFatal( 'apierror-filedoesnotexist' )
61 );
62 $result[] = $r;
63 continue;
64 }
65 $handler = $file->getHandler();
66 if ( !$handler || !$handler->canRotate() ) {
67 $r['result'] = 'Failure';
68 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
69 Status::newFatal( 'apierror-filetypecannotberotated' )
70 );
71 $result[] = $r;
72 continue;
73 }
74
75 // Check whether we're allowed to rotate this file
76 $permError = $this->checkTitleUserPermissions( $file->getTitle(), [ 'edit', 'upload' ] );
77 if ( $permError ) {
78 $r['result'] = 'Failure';
79 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
80 $this->errorArrayToStatus( $permError )
81 );
82 $result[] = $r;
83 continue;
84 }
85
86 $srcPath = $file->getLocalRefPath();
87 if ( $srcPath === false ) {
88 $r['result'] = 'Failure';
89 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
90 Status::newFatal( 'apierror-filenopath' )
91 );
92 $result[] = $r;
93 continue;
94 }
95 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION ) );
96 $tmpFile = TempFSFile::factory( 'rotate_', $ext, wfTempDir() );
97 $dstPath = $tmpFile->getPath();
98 $err = $handler->rotate( $file, [
99 'srcPath' => $srcPath,
100 'dstPath' => $dstPath,
101 'rotation' => $rotation
102 ] );
103 if ( !$err ) {
104 $comment = wfMessage(
105 'rotate-comment'
106 )->numParams( $rotation )->inContentLanguage()->text();
107 $status = $file->upload( $dstPath,
108 $comment, $comment, 0, false, false, $this->getUser() );
109 if ( $status->isGood() ) {
110 $r['result'] = 'Success';
111 } else {
112 $r['result'] = 'Failure';
113 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
114 }
115 } else {
116 $r['result'] = 'Failure';
117 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
118 Status::newFatal( ApiMessage::create( $err->getMsg() ) )
119 );
120 }
121 $result[] = $r;
122 }
123 $apiResult = $this->getResult();
124 ApiResult::setIndexedTagName( $result, 'page' );
125 $apiResult->addValue( null, $this->getModuleName(), $result );
126
127 $this->setContinuationManager( null );
128 $continuationManager->setContinuationIntoResult( $apiResult );
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
140 return $this->mPageSet;
141 }
142
143 public function mustBePosted() {
144 return true;
145 }
146
147 public function isWriteMode() {
148 return true;
149 }
150
151 public function getAllowedParams( $flags = 0 ) {
152 $result = [
153 'rotation' => [
154 ApiBase::PARAM_TYPE => [ '90', '180', '270' ],
155 ApiBase::PARAM_REQUIRED => true
156 ],
157 'continue' => [
158 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
159 ],
160 ];
161 if ( $flags ) {
162 $result += $this->getPageSet()->getFinalParams( $flags );
163 }
164
165 return $result;
166 }
167
168 public function needsToken() {
169 return 'csrf';
170 }
171
172 protected function getExamplesMessages() {
173 return [
174 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
175 => 'apihelp-imagerotate-example-simple',
176 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
177 'rotation=180&token=123ABC'
178 => 'apihelp-imagerotate-example-generator',
179 ];
180 }
181 }