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 $continuationManager = new ApiContinuationManager( $this, array(), array() );
56 $this->setContinuationManager( $continuationManager );
58 $pageSet = $this->getPageSet();
63 self
::addValues( $result, $pageSet->getInvalidTitlesAndReasons(), 'invalid' );
64 self
::addValues( $result, $pageSet->getSpecialTitles(), 'special', 'title' );
65 self
::addValues( $result, $pageSet->getMissingPageIDs(), 'missing', 'pageid' );
66 self
::addValues( $result, $pageSet->getMissingRevisionIDs(), 'missing', 'revid' );
67 self
::addValues( $result, $pageSet->getInterwikiTitlesAsResult() );
69 foreach ( $pageSet->getTitles() as $title ) {
71 $r['id'] = $title->getArticleID();
72 ApiQueryBase
::addTitleInfo( $r, $title );
73 if ( !$title->exists() ) {
77 $file = wfFindFile( $title, array( 'latest' => true ) );
79 $r['result'] = 'Failure';
80 $r['errormessage'] = 'File does not exist';
84 $handler = $file->getHandler();
85 if ( !$handler ||
!$handler->canRotate() ) {
86 $r['result'] = 'Failure';
87 $r['errormessage'] = 'File type cannot be rotated';
92 // Check whether we're allowed to rotate this file
93 $permError = $this->checkPermissions( $this->getUser(), $file->getTitle() );
94 if ( $permError !== null ) {
95 $r['result'] = 'Failure';
96 $r['errormessage'] = $permError;
101 $srcPath = $file->getLocalRefPath();
102 if ( $srcPath === false ) {
103 $r['result'] = 'Failure';
104 $r['errormessage'] = 'Cannot get local file path';
108 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION
) );
109 $tmpFile = TempFSFile
::factory( 'rotate_', $ext );
110 $dstPath = $tmpFile->getPath();
111 $err = $handler->rotate( $file, array(
112 "srcPath" => $srcPath,
113 "dstPath" => $dstPath,
114 "rotation" => $rotation
117 $comment = wfMessage(
119 )->numParams( $rotation )->inContentLanguage()->text();
120 $status = $file->upload( $dstPath,
121 $comment, $comment, 0, false, false, $this->getUser() );
122 if ( $status->isGood() ) {
123 $r['result'] = 'Success';
125 $r['result'] = 'Failure';
126 $r['errormessage'] = $this->getErrorFormatter()->arrayFromStatus( $status );
129 $r['result'] = 'Failure';
130 $r['errormessage'] = $err->toText();
134 $apiResult = $this->getResult();
135 ApiResult
::setIndexedTagName( $result, 'page' );
136 $apiResult->addValue( null, $this->getModuleName(), $result );
138 $this->setContinuationManager( null );
139 $continuationManager->setContinuationIntoResult( $apiResult );
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
);
151 return $this->mPageSet
;
155 * Checks that the user has permissions to perform rotations.
156 * @param User $user The user to check
157 * @param Title $title
158 * @return string|null Permission error message, or null if there is no error
160 protected function checkPermissions( $user, $title ) {
161 $permissionErrors = array_merge(
162 $title->getUserPermissionsErrors( 'edit', $user ),
163 $title->getUserPermissionsErrors( 'upload', $user )
166 if ( $permissionErrors ) {
167 // Just return the first error
168 $msg = $this->parseMsg( $permissionErrors[0] );
176 public function mustBePosted() {
180 public function isWriteMode() {
184 public function getAllowedParams( $flags = 0 ) {
187 ApiBase
::PARAM_TYPE
=> array( '90', '180', '270' ),
188 ApiBase
::PARAM_REQUIRED
=> true
191 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
195 $result +
= $this->getPageSet()->getFinalParams( $flags );
201 public function needsToken() {
205 protected function getExamplesMessages() {
207 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
208 => 'apihelp-imagerotate-example-simple',
209 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
210 'rotation=180&token=123ABC'
211 => 'apihelp-imagerotate-example-generator',