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 $this->useTransactionalTimeLimit();
54 $params = $this->extractRequestParams();
55 $rotation = $params['rotation'];
57 $continuationManager = new ApiContinuationManager( $this, array(), array() );
58 $this->setContinuationManager( $continuationManager );
60 $pageSet = $this->getPageSet();
65 self
::addValues( $result, $pageSet->getInvalidTitlesAndReasons(), 'invalid' );
66 self
::addValues( $result, $pageSet->getSpecialTitles(), 'special', 'title' );
67 self
::addValues( $result, $pageSet->getMissingPageIDs(), 'missing', 'pageid' );
68 self
::addValues( $result, $pageSet->getMissingRevisionIDs(), 'missing', 'revid' );
69 self
::addValues( $result, $pageSet->getInterwikiTitlesAsResult() );
71 foreach ( $pageSet->getTitles() as $title ) {
73 $r['id'] = $title->getArticleID();
74 ApiQueryBase
::addTitleInfo( $r, $title );
75 if ( !$title->exists() ) {
79 $file = wfFindFile( $title, array( 'latest' => true ) );
81 $r['result'] = 'Failure';
82 $r['errormessage'] = 'File does not exist';
86 $handler = $file->getHandler();
87 if ( !$handler ||
!$handler->canRotate() ) {
88 $r['result'] = 'Failure';
89 $r['errormessage'] = 'File type cannot be rotated';
94 // Check whether we're allowed to rotate this file
95 $permError = $this->checkPermissions( $this->getUser(), $file->getTitle() );
96 if ( $permError !== null ) {
97 $r['result'] = 'Failure';
98 $r['errormessage'] = $permError;
103 $srcPath = $file->getLocalRefPath();
104 if ( $srcPath === false ) {
105 $r['result'] = 'Failure';
106 $r['errormessage'] = 'Cannot get local file path';
110 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION
) );
111 $tmpFile = TempFSFile
::factory( 'rotate_', $ext );
112 $dstPath = $tmpFile->getPath();
113 $err = $handler->rotate( $file, array(
114 "srcPath" => $srcPath,
115 "dstPath" => $dstPath,
116 "rotation" => $rotation
119 $comment = wfMessage(
121 )->numParams( $rotation )->inContentLanguage()->text();
122 $status = $file->upload( $dstPath,
123 $comment, $comment, 0, false, false, $this->getUser() );
124 if ( $status->isGood() ) {
125 $r['result'] = 'Success';
127 $r['result'] = 'Failure';
128 $r['errormessage'] = $this->getErrorFormatter()->arrayFromStatus( $status );
131 $r['result'] = 'Failure';
132 $r['errormessage'] = $err->toText();
136 $apiResult = $this->getResult();
137 ApiResult
::setIndexedTagName( $result, 'page' );
138 $apiResult->addValue( null, $this->getModuleName(), $result );
140 $this->setContinuationManager( null );
141 $continuationManager->setContinuationIntoResult( $apiResult );
145 * Get a cached instance of an ApiPageSet object
148 private function getPageSet() {
149 if ( $this->mPageSet
=== null ) {
150 $this->mPageSet
= new ApiPageSet( $this, 0, NS_FILE
);
153 return $this->mPageSet
;
157 * Checks that the user has permissions to perform rotations.
158 * @param User $user The user to check
159 * @param Title $title
160 * @return string|null Permission error message, or null if there is no error
162 protected function checkPermissions( $user, $title ) {
163 $permissionErrors = array_merge(
164 $title->getUserPermissionsErrors( 'edit', $user ),
165 $title->getUserPermissionsErrors( 'upload', $user )
168 if ( $permissionErrors ) {
169 // Just return the first error
170 $msg = $this->parseMsg( $permissionErrors[0] );
178 public function mustBePosted() {
182 public function isWriteMode() {
186 public function getAllowedParams( $flags = 0 ) {
189 ApiBase
::PARAM_TYPE
=> array( '90', '180', '270' ),
190 ApiBase
::PARAM_REQUIRED
=> true
193 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
197 $result +
= $this->getPageSet()->getFinalParams( $flags );
203 public function needsToken() {
207 protected function getExamplesMessages() {
209 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
210 => 'apihelp-imagerotate-example-simple',
211 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
212 'rotation=180&token=123ABC'
213 => 'apihelp-imagerotate-example-generator',