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
24 class ApiImageRotate
extends ApiBase
{
25 private $mPageSet = null;
27 public function __construct( $main, $action ) {
28 parent
::__construct( $main, $action );
32 * Add all items from $values into the result
33 * @param array $result output
34 * @param array $values values to add
35 * @param string $flag the name of the boolean flag to mark this element
36 * @param string $name if given, name of the value
38 private static function addValues( array &$result, $values, $flag = null, $name = null ) {
39 foreach ( $values as $val ) {
40 if ( $val instanceof Title
) {
42 ApiQueryBase
::addTitleInfo( $v, $val );
43 } elseif ( $name !== null ) {
44 $v = array( $name => $val );
48 if ( $flag !== null ) {
55 public function execute() {
56 $params = $this->extractRequestParams();
57 $rotation = $params['rotation'];
59 $pageSet = $this->getPageSet();
64 self
::addValues( $result, $pageSet->getInvalidTitles(), 'invalid', 'title' );
65 self
::addValues( $result, $pageSet->getSpecialTitles(), 'special', 'title' );
66 self
::addValues( $result, $pageSet->getMissingPageIDs(), 'missing', 'pageid' );
67 self
::addValues( $result, $pageSet->getMissingRevisionIDs(), 'missing', 'revid' );
68 self
::addValues( $result, $pageSet->getInterwikiTitlesAsResult() );
70 foreach ( $pageSet->getTitles() as $title ) {
72 $r['id'] = $title->getArticleID();
73 ApiQueryBase
::addTitleInfo( $r, $title );
74 if ( !$title->exists() ) {
78 $file = wfFindFile( $title );
80 $r['result'] = 'Failure';
81 $r['errormessage'] = 'File does not exist';
85 $handler = $file->getHandler();
86 if ( !$handler ||
!$handler->canRotate() ) {
87 $r['result'] = 'Failure';
88 $r['errormessage'] = 'File type cannot be rotated';
93 // Check whether we're allowed to rotate this file
94 $permError = $this->checkPermissions( $this->getUser(), $file->getTitle() );
95 if ( $permError !== null ) {
96 $r['result'] = 'Failure';
97 $r['errormessage'] = $permError;
102 $srcPath = $file->getLocalRefPath();
103 if ( $srcPath === false ) {
104 $r['result'] = 'Failure';
105 $r['errormessage'] = 'Cannot get local file path';
109 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION
) );
110 $tmpFile = TempFSFile
::factory( 'rotate_', $ext );
111 $dstPath = $tmpFile->getPath();
112 $err = $handler->rotate( $file, array(
113 "srcPath" => $srcPath,
114 "dstPath" => $dstPath,
115 "rotation" => $rotation
118 $comment = wfMessage(
120 )->numParams( $rotation )->inContentLanguage()->text();
121 $status = $file->upload( $dstPath,
122 $comment, $comment, 0, false, false, $this->getUser() );
123 if ( $status->isGood() ) {
124 $r['result'] = 'Success';
126 $r['result'] = 'Failure';
127 $r['errormessage'] = $this->getResult()->convertStatusToArray( $status );
130 $r['result'] = 'Failure';
131 $r['errormessage'] = $err->toText();
135 $apiResult = $this->getResult();
136 $apiResult->setIndexedTagName( $result, 'page' );
137 $apiResult->addValue( null, $this->getModuleName(), $result );
141 * Get a cached instance of an ApiPageSet object
144 private function getPageSet() {
145 if ( $this->mPageSet
=== null ) {
146 $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] );
172 public function mustBePosted() {
176 public function isWriteMode() {
180 public function getAllowedParams( $flags = 0 ) {
183 ApiBase
::PARAM_TYPE
=> array( '90', '180', '270' ),
184 ApiBase
::PARAM_REQUIRED
=> true
187 ApiBase
::PARAM_TYPE
=> 'string',
188 ApiBase
::PARAM_REQUIRED
=> true
192 $result +
= $this->getPageSet()->getFinalParams( $flags );
197 public function getParamDescription() {
198 $pageSet = $this->getPageSet();
199 return $pageSet->getFinalParamDescription() +
array(
200 'rotation' => 'Degrees to rotate image clockwise',
201 'token' => 'Edit token. You can get one of these through action=tokens',
205 public function getDescription() {
206 return 'Rotate one or more images';
209 public function needsToken() {
213 public function getTokenSalt() {
217 public function getPossibleErrors() {
218 $pageSet = $this->getPageSet();
220 parent
::getPossibleErrors(),
221 $pageSet->getFinalPossibleErrors()
225 public function getExamples() {
227 'api.php?action=imagerotate&titles=Example.jpg&rotation=90&token=123ABC',