Implement extension registration from an extension.json file
[mediawiki.git] / includes / api / ApiImageRotate.php
blobaba6921a1c4562b191235fbd8908b91908bbcdad
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] = '';
47 $result[] = $v;
51 public function execute() {
52 $params = $this->extractRequestParams();
53 $rotation = $params['rotation'];
55 $this->getResult()->beginContinuation( $params['continue'], array(), array() );
57 $pageSet = $this->getPageSet();
58 $pageSet->execute();
60 $result = array();
62 self::addValues( $result, $pageSet->getInvalidTitles(), 'invalid', 'title' );
63 self::addValues( $result, $pageSet->getSpecialTitles(), 'special', 'title' );
64 self::addValues( $result, $pageSet->getMissingPageIDs(), 'missing', 'pageid' );
65 self::addValues( $result, $pageSet->getMissingRevisionIDs(), 'missing', 'revid' );
66 self::addValues( $result, $pageSet->getInterwikiTitlesAsResult() );
68 foreach ( $pageSet->getTitles() as $title ) {
69 $r = array();
70 $r['id'] = $title->getArticleID();
71 ApiQueryBase::addTitleInfo( $r, $title );
72 if ( !$title->exists() ) {
73 $r['missing'] = '';
76 $file = wfFindFile( $title );
77 if ( !$file ) {
78 $r['result'] = 'Failure';
79 $r['errormessage'] = 'File does not exist';
80 $result[] = $r;
81 continue;
83 $handler = $file->getHandler();
84 if ( !$handler || !$handler->canRotate() ) {
85 $r['result'] = 'Failure';
86 $r['errormessage'] = 'File type cannot be rotated';
87 $result[] = $r;
88 continue;
91 // Check whether we're allowed to rotate this file
92 $permError = $this->checkPermissions( $this->getUser(), $file->getTitle() );
93 if ( $permError !== null ) {
94 $r['result'] = 'Failure';
95 $r['errormessage'] = $permError;
96 $result[] = $r;
97 continue;
100 $srcPath = $file->getLocalRefPath();
101 if ( $srcPath === false ) {
102 $r['result'] = 'Failure';
103 $r['errormessage'] = 'Cannot get local file path';
104 $result[] = $r;
105 continue;
107 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION ) );
108 $tmpFile = TempFSFile::factory( 'rotate_', $ext );
109 $dstPath = $tmpFile->getPath();
110 $err = $handler->rotate( $file, array(
111 "srcPath" => $srcPath,
112 "dstPath" => $dstPath,
113 "rotation" => $rotation
114 ) );
115 if ( !$err ) {
116 $comment = wfMessage(
117 'rotate-comment'
118 )->numParams( $rotation )->inContentLanguage()->text();
119 $status = $file->upload( $dstPath,
120 $comment, $comment, 0, false, false, $this->getUser() );
121 if ( $status->isGood() ) {
122 $r['result'] = 'Success';
123 } else {
124 $r['result'] = 'Failure';
125 $r['errormessage'] = $this->getResult()->convertStatusToArray( $status );
127 } else {
128 $r['result'] = 'Failure';
129 $r['errormessage'] = $err->toText();
131 $result[] = $r;
133 $apiResult = $this->getResult();
134 $apiResult->setIndexedTagName( $result, 'page' );
135 $apiResult->addValue( null, $this->getModuleName(), $result );
136 $apiResult->endContinuation();
140 * Get a cached instance of an ApiPageSet object
141 * @return ApiPageSet
143 private function getPageSet() {
144 if ( $this->mPageSet === null ) {
145 $this->mPageSet = new ApiPageSet( $this, 0, NS_FILE );
148 return $this->mPageSet;
152 * Checks that the user has permissions to perform rotations.
153 * @param User $user The user to check
154 * @param Title $title
155 * @return string|null Permission error message, or null if there is no error
157 protected function checkPermissions( $user, $title ) {
158 $permissionErrors = array_merge(
159 $title->getUserPermissionsErrors( 'edit', $user ),
160 $title->getUserPermissionsErrors( 'upload', $user )
163 if ( $permissionErrors ) {
164 // Just return the first error
165 $msg = $this->parseMsg( $permissionErrors[0] );
167 return $msg['info'];
170 return null;
173 public function mustBePosted() {
174 return true;
177 public function isWriteMode() {
178 return true;
181 public function getAllowedParams( $flags = 0 ) {
182 $result = array(
183 'rotation' => array(
184 ApiBase::PARAM_TYPE => array( '90', '180', '270' ),
185 ApiBase::PARAM_REQUIRED => true
187 'continue' => array(
188 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
191 if ( $flags ) {
192 $result += $this->getPageSet()->getFinalParams( $flags );
195 return $result;
198 public function needsToken() {
199 return 'csrf';
202 protected function getExamplesMessages() {
203 return array(
204 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
205 => 'apihelp-imagerotate-example-simple',
206 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
207 'rotation=180&token=123ABC'
208 => 'apihelp-imagerotate-example-generator',