Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / includes / api / ApiImageRotate.php
blob71bda6d7e498125742299a7ec7d46b5167a5eef2
1 <?php
2 /**
4 * Created on January 3rd, 2013
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.
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.
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
21 * @file
24 class ApiImageRotate extends ApiBase {
25 private $mPageSet = null;
27 public function execute() {
28 $this->useTransactionalTimeLimit();
30 $params = $this->extractRequestParams();
31 $rotation = $params['rotation'];
33 $continuationManager = new ApiContinuationManager( $this, [], [] );
34 $this->setContinuationManager( $continuationManager );
36 $pageSet = $this->getPageSet();
37 $pageSet->execute();
39 $result = [];
41 $result = $pageSet->getInvalidTitlesAndRevisions( [
42 'invalidTitles', 'special', 'missingIds', 'missingRevIds', 'interwikiTitles',
43 ] );
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 );
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;
64 $file = wfFindFile( $title, [ 'latest' => true ] );
65 if ( !$file ) {
66 $r['result'] = 'Failure';
67 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
68 Status::newFatal( 'apierror-filedoesnotexist' )
70 $result[] = $r;
71 continue;
73 $handler = $file->getHandler();
74 if ( !$handler || !$handler->canRotate() ) {
75 $r['result'] = 'Failure';
76 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
77 Status::newFatal( 'apierror-filetypecannotberotated' )
79 $result[] = $r;
80 continue;
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 )
90 $result[] = $r;
91 continue;
94 $srcPath = $file->getLocalRefPath();
95 if ( $srcPath === false ) {
96 $r['result'] = 'Failure';
97 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
98 Status::newFatal( 'apierror-filenopath' )
100 $result[] = $r;
101 continue;
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,
120 false,
121 false,
122 $this->getUser(),
123 $params['tags'] ?: []
125 if ( $status->isGood() ) {
126 $r['result'] = 'Success';
127 } else {
128 $r['result'] = 'Failure';
129 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
131 } else {
132 $r['result'] = 'Failure';
133 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus(
134 Status::newFatal( ApiMessage::create( $err->getMsg() ) )
137 $result[] = $r;
139 $apiResult = $this->getResult();
140 ApiResult::setIndexedTagName( $result, 'page' );
141 $apiResult->addValue( null, $this->getModuleName(), $result );
143 $this->setContinuationManager( null );
144 $continuationManager->setContinuationIntoResult( $apiResult );
148 * Get a cached instance of an ApiPageSet object
149 * @return ApiPageSet
151 private function getPageSet() {
152 if ( $this->mPageSet === null ) {
153 $this->mPageSet = new ApiPageSet( $this, 0, NS_FILE );
156 return $this->mPageSet;
159 public function mustBePosted() {
160 return true;
163 public function isWriteMode() {
164 return true;
167 public function getAllowedParams( $flags = 0 ) {
168 $result = [
169 'rotation' => [
170 ApiBase::PARAM_TYPE => [ '90', '180', '270' ],
171 ApiBase::PARAM_REQUIRED => true
173 'continue' => [
174 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
176 'tags' => [
177 ApiBase::PARAM_TYPE => 'tags',
178 ApiBase::PARAM_ISMULTI => true,
181 if ( $flags ) {
182 $result += $this->getPageSet()->getFinalParams( $flags );
185 return $result;
188 public function needsToken() {
189 return 'csrf';
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',