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
{
26 private $mPageSet = null;
28 public function __construct( $main, $action ) {
29 parent
::__construct( $main, $action );
33 * Add all items from $values into the result
34 * @param array $result output
35 * @param array $values values to add
36 * @param string $flag the name of the boolean flag to mark this element
37 * @param string $name if given, name of the value
39 private static function addValues( array &$result, $values, $flag = null, $name = null ) {
40 foreach ( $values as $val ) {
41 if( $val instanceof Title
) {
43 ApiQueryBase
::addTitleInfo( $v, $val );
44 } elseif( $name !== null ) {
45 $v = array( $name => $val );
49 if( $flag !== null ) {
57 public function execute() {
58 $params = $this->extractRequestParams();
59 $rotation = $params['rotation'];
60 $user = $this->getUser();
62 $pageSet = $this->getPageSet();
68 self
::addValues( $result, $pageSet->getInvalidTitles(), 'invalid', 'title' );
69 self
::addValues( $result, $pageSet->getSpecialTitles(), 'special', 'title' );
70 self
::addValues( $result, $pageSet->getMissingPageIDs(), 'missing', 'pageid' );
71 self
::addValues( $result, $pageSet->getMissingRevisionIDs(), 'missing', 'revid' );
72 self
::addValues( $result, $pageSet->getInterwikiTitlesAsResult() );
74 foreach ( $pageSet->getTitles() as $title ) {
76 $r['id'] = $title->getArticleID();
77 ApiQueryBase
::addTitleInfo( $r, $title );
78 if ( !$title->exists() ) {
82 $file = wfFindFile( $title );
84 $r['result'] = 'Failure';
85 $r['errormessage'] = 'File does not exist';
89 $handler = $file->getHandler();
90 if ( !$handler ||
!$handler->canRotate() ) {
91 $r['result'] = 'Failure';
92 $r['errormessage'] = 'File type cannot be rotated';
97 // Check whether we're allowed to rotate this file
98 $permError = $this->checkPermissions( $this->getUser(), $file->getTitle() );
99 if ( $permError !== null ) {
100 $r['result'] = 'Failure';
101 $r['errormessage'] = $permError;
106 $srcPath = $file->getLocalRefPath();
107 if ( $srcPath === false ) {
108 $r['result'] = 'Failure';
109 $r['errormessage'] = 'Cannot get local file path';
113 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION
) );
114 $tmpFile = TempFSFile
::factory( 'rotate_', $ext );
115 $dstPath = $tmpFile->getPath();
116 $err = $handler->rotate( $file, array(
117 "srcPath" => $srcPath,
118 "dstPath" => $dstPath,
119 "rotation" => $rotation
122 $comment = wfMessage( 'rotate-comment' )->numParams( $rotation )->text();
123 $status = $file->upload( $dstPath,
124 $comment, $comment, 0, false, false, $this->getUser() );
125 if ( $status->isGood() ) {
126 $r['result'] = 'Success';
128 $r['result'] = 'Failure';
129 $r['errormessage'] = $this->getResult()->convertStatusToArray( $status );
132 $r['result'] = 'Failure';
133 $r['errormessage'] = $err->toText();
137 $apiResult = $this->getResult();
138 $apiResult->setIndexedTagName( $result, 'page' );
139 $apiResult->addValue( null, $this->getModuleName(), $result );
143 * Get a cached instance of an ApiPageSet object
146 private function getPageSet() {
147 if ( $this->mPageSet
=== null ) {
148 $this->mPageSet
= new ApiPageSet( $this, 0, NS_FILE
);
150 return $this->mPageSet
;
154 * Checks that the user has permissions to perform rotations.
155 * @param $user User The user to check.
156 * @return string|null Permission error message, or null if there is no error
158 protected function checkPermissions( $user, $title ) {
159 $permissionErrors = array_merge(
160 $title->getUserPermissionsErrors( 'edit', $user ),
161 $title->getUserPermissionsErrors( 'upload', $user )
164 if ( $permissionErrors ) {
165 // Just return the first error
166 $msg = $this->parseMsg( $permissionErrors[0] );
173 public function mustBePosted() {
177 public function isWriteMode() {
181 public function getAllowedParams( $flags = 0 ) {
182 $pageSet = $this->getPageSet();
185 ApiBase
::PARAM_TYPE
=> array( '90', '180', '270' ),
186 ApiBase
::PARAM_REQUIRED
=> true
189 ApiBase
::PARAM_TYPE
=> 'string',
190 ApiBase
::PARAM_REQUIRED
=> true
194 $result +
= $this->getPageSet()->getFinalParams( $flags );
199 public function getParamDescription() {
200 $pageSet = $this->getPageSet();
201 return $pageSet->getParamDescription() +
array(
202 'rotation' => 'Degrees to rotate image clockwise',
203 'token' => 'Edit token. You can get one of these through action=tokens',
207 public function getDescription() {
208 return 'Rotate one or more images';
211 public function needsToken() {
215 public function getTokenSalt() {
219 public function getPossibleErrors() {
220 $pageSet = $this->getPageSet();
222 parent
::getPossibleErrors(),
223 $pageSet->getPossibleErrors()
227 public function getExamples() {
229 'api.php?action=imagerotate&titles=Example.jpg&rotation=90&token=123ABC',