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 if( is_null( $rotation ) ||
$rotation %
90 ) {
63 $this->dieUsage( "Rotation: {$rotation}", 'rotation must be multiple of 90 degrees' );
66 $pageSet = $this->getPageSet();
72 self
::addValues( $result, $pageSet->getInvalidTitles(), 'invalid', 'title' );
73 self
::addValues( $result, $pageSet->getSpecialTitles(), 'special', 'title' );
74 self
::addValues( $result, $pageSet->getMissingPageIDs(), 'missing', 'pageid' );
75 self
::addValues( $result, $pageSet->getMissingRevisionIDs(), 'missing', 'revid' );
76 self
::addValues( $result, $pageSet->getMissingTitles(), 'missing' );
77 self
::addValues( $result, $pageSet->getInterwikiTitlesAsResult() );
79 foreach ( $pageSet->getTitles() as $title ) {
80 $file = wfFindFile( $title );
83 $r[ 'title' ] = $title->getFullText();
86 $r['result'] = 'Failure';
90 $handler = $file->getHandler();
91 if ( !$handler ||
!$handler->canRotate() ) {
93 $r['result'] = 'Failure';
98 // Check whether we're allowed to rotate this file
99 $this->checkPermissions( $this->getUser(), $file->getTitle() );
101 $srcPath = $file->getLocalRefPath();
102 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION
) );
103 $tmpFile = TempFSFile
::factory( 'rotate_', $ext);
104 $dstPath = $tmpFile->getPath();
105 $err = $handler->rotate( $file, array(
106 "srcPath" => $srcPath,
107 "dstPath" => $dstPath,
108 "rotation"=> $rotation
111 $comment = wfMessage( 'rotate-comment' )->numParams( $rotation )->text();
112 $status = $file->upload( $dstPath,
113 $comment, $comment, 0, false, false, $this->getUser() );
114 if ( $status->isGood() ) {
115 $r['result'] = 'Success';
117 $r['result'] = 'Failure';
118 $r['errormessage'] = $this->getResult()->convertStatusToArray( $status );
121 $r['result'] = 'Failure';
122 $r['errormessage'] = $err->toText();
126 $apiResult = $this->getResult();
127 $apiResult->setIndexedTagName( $result, 'page' );
128 $apiResult->addValue( null, $this->getModuleName(), $result );
132 * Get a cached instance of an ApiPageSet object
135 private function getPageSet() {
136 if ( $this->mPageSet
=== null ) {
137 $this->mPageSet
= new ApiPageSet( $this, 0, NS_FILE
);
139 return $this->mPageSet
;
143 * Checks that the user has permissions to perform rotations.
144 * Dies with usage message on inadequate permissions.
145 * @param $user User The user to check.
147 protected function checkPermissions( $user, $title ) {
148 $permissionErrors = array_merge(
149 $title->getUserPermissionsErrors( 'edit' , $user ),
150 $title->getUserPermissionsErrors( 'upload' , $user )
153 if ( $permissionErrors ) {
154 $this->dieUsageMsg( $permissionErrors[0] );
158 public function mustBePosted() {
162 public function isWriteMode() {
166 public function getAllowedParams( $flags = 0 ) {
167 $pageSet = $this->getPageSet();
170 ApiBase
::PARAM_DFLT
=> 0,
173 ApiBase
::PARAM_TYPE
=> 'string',
174 ApiBase
::PARAM_REQUIRED
=> true
178 $result +
= $this->getPageSet()->getFinalParams( $flags );
183 public function getParamDescription() {
184 $pageSet = $this->getPageSet();
185 return $pageSet->getParamDescription() +
array(
186 'rotation' => 'Degrees to rotate image, values can be 0, 90, 180 or 270',
187 'token' => 'Edit token. You can get one of these through prop=info',
191 public function getDescription() {
192 return 'Rotate one or more images';
195 public function needsToken() {
199 public function getTokenSalt() {
203 public function getPossibleErrors() {
204 $pageSet = $this->getPageSet();
206 parent
::getPossibleErrors(),
207 $pageSet->getPossibleErrors()
211 public function getExamples() {
213 'api.php?action=imagerotate&titles=Example.jpg&rotation=90&token=+\\',