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;
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
) {
38 ApiQueryBase
::addTitleInfo( $v, $val );
39 } elseif ( $name !== null ) {
40 $v = array( $name => $val );
44 if ( $flag !== null ) {
51 public function execute() {
52 $params = $this->extractRequestParams();
53 $rotation = $params['rotation'];
55 $pageSet = $this->getPageSet();
60 self
::addValues( $result, $pageSet->getInvalidTitles(), 'invalid', 'title' );
61 self
::addValues( $result, $pageSet->getSpecialTitles(), 'special', 'title' );
62 self
::addValues( $result, $pageSet->getMissingPageIDs(), 'missing', 'pageid' );
63 self
::addValues( $result, $pageSet->getMissingRevisionIDs(), 'missing', 'revid' );
64 self
::addValues( $result, $pageSet->getInterwikiTitlesAsResult() );
66 foreach ( $pageSet->getTitles() as $title ) {
68 $r['id'] = $title->getArticleID();
69 ApiQueryBase
::addTitleInfo( $r, $title );
70 if ( !$title->exists() ) {
74 $file = wfFindFile( $title );
76 $r['result'] = 'Failure';
77 $r['errormessage'] = 'File does not exist';
81 $handler = $file->getHandler();
82 if ( !$handler ||
!$handler->canRotate() ) {
83 $r['result'] = 'Failure';
84 $r['errormessage'] = 'File type cannot be rotated';
89 // Check whether we're allowed to rotate this file
90 $permError = $this->checkPermissions( $this->getUser(), $file->getTitle() );
91 if ( $permError !== null ) {
92 $r['result'] = 'Failure';
93 $r['errormessage'] = $permError;
98 $srcPath = $file->getLocalRefPath();
99 if ( $srcPath === false ) {
100 $r['result'] = 'Failure';
101 $r['errormessage'] = 'Cannot get local file path';
105 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION
) );
106 $tmpFile = TempFSFile
::factory( 'rotate_', $ext );
107 $dstPath = $tmpFile->getPath();
108 $err = $handler->rotate( $file, array(
109 "srcPath" => $srcPath,
110 "dstPath" => $dstPath,
111 "rotation" => $rotation
114 $comment = wfMessage(
116 )->numParams( $rotation )->inContentLanguage()->text();
117 $status = $file->upload( $dstPath,
118 $comment, $comment, 0, false, false, $this->getUser() );
119 if ( $status->isGood() ) {
120 $r['result'] = 'Success';
122 $r['result'] = 'Failure';
123 $r['errormessage'] = $this->getResult()->convertStatusToArray( $status );
126 $r['result'] = 'Failure';
127 $r['errormessage'] = $err->toText();
131 $apiResult = $this->getResult();
132 $apiResult->setIndexedTagName( $result, 'page' );
133 $apiResult->addValue( null, $this->getModuleName(), $result );
137 * Get a cached instance of an ApiPageSet object
140 private function getPageSet() {
141 if ( $this->mPageSet
=== null ) {
142 $this->mPageSet
= new ApiPageSet( $this, 0, NS_FILE
);
145 return $this->mPageSet
;
149 * Checks that the user has permissions to perform rotations.
150 * @param User $user The user to check
151 * @param Title $title
152 * @return string|null Permission error message, or null if there is no error
154 protected function checkPermissions( $user, $title ) {
155 $permissionErrors = array_merge(
156 $title->getUserPermissionsErrors( 'edit', $user ),
157 $title->getUserPermissionsErrors( 'upload', $user )
160 if ( $permissionErrors ) {
161 // Just return the first error
162 $msg = $this->parseMsg( $permissionErrors[0] );
170 public function mustBePosted() {
174 public function isWriteMode() {
178 public function getAllowedParams( $flags = 0 ) {
181 ApiBase
::PARAM_TYPE
=> array( '90', '180', '270' ),
182 ApiBase
::PARAM_REQUIRED
=> true
185 ApiBase
::PARAM_TYPE
=> 'string',
186 ApiBase
::PARAM_REQUIRED
=> true
190 $result +
= $this->getPageSet()->getFinalParams( $flags );
196 public function getParamDescription() {
197 $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();
221 parent
::getPossibleErrors(),
222 $pageSet->getFinalPossibleErrors()
226 public function getExamples() {
228 'api.php?action=imagerotate&titles=Example.jpg&rotation=90&token=123ABC',