wfMsgForContentNoTrans() was removed
[mediawiki.git] / includes / api / ApiImageRotate.php
blob7a544ec0e250989762d4382210a6cf6ed3b83d4d
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 /**
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
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;
44 if ( $flag !== null ) {
45 $v[$flag] = true;
47 $result[] = $v;
51 public function execute() {
52 $this->useTransactionalTimeLimit();
54 $params = $this->extractRequestParams();
55 $rotation = $params['rotation'];
57 $continuationManager = new ApiContinuationManager( $this, array(), array() );
58 $this->setContinuationManager( $continuationManager );
60 $pageSet = $this->getPageSet();
61 $pageSet->execute();
63 $result = array();
65 self::addValues( $result, $pageSet->getInvalidTitlesAndReasons(), 'invalid' );
66 self::addValues( $result, $pageSet->getSpecialTitles(), 'special', 'title' );
67 self::addValues( $result, $pageSet->getMissingPageIDs(), 'missing', 'pageid' );
68 self::addValues( $result, $pageSet->getMissingRevisionIDs(), 'missing', 'revid' );
69 self::addValues( $result, $pageSet->getInterwikiTitlesAsResult() );
71 foreach ( $pageSet->getTitles() as $title ) {
72 $r = array();
73 $r['id'] = $title->getArticleID();
74 ApiQueryBase::addTitleInfo( $r, $title );
75 if ( !$title->exists() ) {
76 $r['missing'] = true;
79 $file = wfFindFile( $title, array( 'latest' => true ) );
80 if ( !$file ) {
81 $r['result'] = 'Failure';
82 $r['errormessage'] = 'File does not exist';
83 $result[] = $r;
84 continue;
86 $handler = $file->getHandler();
87 if ( !$handler || !$handler->canRotate() ) {
88 $r['result'] = 'Failure';
89 $r['errormessage'] = 'File type cannot be rotated';
90 $result[] = $r;
91 continue;
94 // Check whether we're allowed to rotate this file
95 $permError = $this->checkPermissions( $this->getUser(), $file->getTitle() );
96 if ( $permError !== null ) {
97 $r['result'] = 'Failure';
98 $r['errormessage'] = $permError;
99 $result[] = $r;
100 continue;
103 $srcPath = $file->getLocalRefPath();
104 if ( $srcPath === false ) {
105 $r['result'] = 'Failure';
106 $r['errormessage'] = 'Cannot get local file path';
107 $result[] = $r;
108 continue;
110 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION ) );
111 $tmpFile = TempFSFile::factory( 'rotate_', $ext );
112 $dstPath = $tmpFile->getPath();
113 $err = $handler->rotate( $file, array(
114 "srcPath" => $srcPath,
115 "dstPath" => $dstPath,
116 "rotation" => $rotation
117 ) );
118 if ( !$err ) {
119 $comment = wfMessage(
120 'rotate-comment'
121 )->numParams( $rotation )->inContentLanguage()->text();
122 $status = $file->upload( $dstPath,
123 $comment, $comment, 0, false, false, $this->getUser() );
124 if ( $status->isGood() ) {
125 $r['result'] = 'Success';
126 } else {
127 $r['result'] = 'Failure';
128 $r['errormessage'] = $this->getErrorFormatter()->arrayFromStatus( $status );
130 } else {
131 $r['result'] = 'Failure';
132 $r['errormessage'] = $err->toText();
134 $result[] = $r;
136 $apiResult = $this->getResult();
137 ApiResult::setIndexedTagName( $result, 'page' );
138 $apiResult->addValue( null, $this->getModuleName(), $result );
140 $this->setContinuationManager( null );
141 $continuationManager->setContinuationIntoResult( $apiResult );
145 * Get a cached instance of an ApiPageSet object
146 * @return ApiPageSet
148 private function getPageSet() {
149 if ( $this->mPageSet === null ) {
150 $this->mPageSet = new ApiPageSet( $this, 0, NS_FILE );
153 return $this->mPageSet;
157 * Checks that the user has permissions to perform rotations.
158 * @param User $user The user to check
159 * @param Title $title
160 * @return string|null Permission error message, or null if there is no error
162 protected function checkPermissions( $user, $title ) {
163 $permissionErrors = array_merge(
164 $title->getUserPermissionsErrors( 'edit', $user ),
165 $title->getUserPermissionsErrors( 'upload', $user )
168 if ( $permissionErrors ) {
169 // Just return the first error
170 $msg = $this->parseMsg( $permissionErrors[0] );
172 return $msg['info'];
175 return null;
178 public function mustBePosted() {
179 return true;
182 public function isWriteMode() {
183 return true;
186 public function getAllowedParams( $flags = 0 ) {
187 $result = array(
188 'rotation' => array(
189 ApiBase::PARAM_TYPE => array( '90', '180', '270' ),
190 ApiBase::PARAM_REQUIRED => true
192 'continue' => array(
193 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
196 if ( $flags ) {
197 $result += $this->getPageSet()->getFinalParams( $flags );
200 return $result;
203 public function needsToken() {
204 return 'csrf';
207 protected function getExamplesMessages() {
208 return array(
209 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
210 => 'apihelp-imagerotate-example-simple',
211 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
212 'rotation=180&token=123ABC'
213 => 'apihelp-imagerotate-example-generator',