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 $this->getResult()->beginContinuation( $params['continue'], array(), array() );
57 $pageSet = $this->getPageSet();
62 self
::addValues( $result, $pageSet->getInvalidTitles(), 'invalid', 'title' );
63 self
::addValues( $result, $pageSet->getSpecialTitles(), 'special', 'title' );
64 self
::addValues( $result, $pageSet->getMissingPageIDs(), 'missing', 'pageid' );
65 self
::addValues( $result, $pageSet->getMissingRevisionIDs(), 'missing', 'revid' );
66 self
::addValues( $result, $pageSet->getInterwikiTitlesAsResult() );
68 foreach ( $pageSet->getTitles() as $title ) {
70 $r['id'] = $title->getArticleID();
71 ApiQueryBase
::addTitleInfo( $r, $title );
72 if ( !$title->exists() ) {
76 $file = wfFindFile( $title );
78 $r['result'] = 'Failure';
79 $r['errormessage'] = 'File does not exist';
83 $handler = $file->getHandler();
84 if ( !$handler ||
!$handler->canRotate() ) {
85 $r['result'] = 'Failure';
86 $r['errormessage'] = 'File type cannot be rotated';
91 // Check whether we're allowed to rotate this file
92 $permError = $this->checkPermissions( $this->getUser(), $file->getTitle() );
93 if ( $permError !== null ) {
94 $r['result'] = 'Failure';
95 $r['errormessage'] = $permError;
100 $srcPath = $file->getLocalRefPath();
101 if ( $srcPath === false ) {
102 $r['result'] = 'Failure';
103 $r['errormessage'] = 'Cannot get local file path';
107 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION
) );
108 $tmpFile = TempFSFile
::factory( 'rotate_', $ext );
109 $dstPath = $tmpFile->getPath();
110 $err = $handler->rotate( $file, array(
111 "srcPath" => $srcPath,
112 "dstPath" => $dstPath,
113 "rotation" => $rotation
116 $comment = wfMessage(
118 )->numParams( $rotation )->inContentLanguage()->text();
119 $status = $file->upload( $dstPath,
120 $comment, $comment, 0, false, false, $this->getUser() );
121 if ( $status->isGood() ) {
122 $r['result'] = 'Success';
124 $r['result'] = 'Failure';
125 $r['errormessage'] = $this->getResult()->convertStatusToArray( $status );
128 $r['result'] = 'Failure';
129 $r['errormessage'] = $err->toText();
133 $apiResult = $this->getResult();
134 $apiResult->setIndexedTagName( $result, 'page' );
135 $apiResult->addValue( null, $this->getModuleName(), $result );
136 $apiResult->endContinuation();
140 * Get a cached instance of an ApiPageSet object
143 private function getPageSet() {
144 if ( $this->mPageSet
=== null ) {
145 $this->mPageSet
= new ApiPageSet( $this, 0, NS_FILE
);
148 return $this->mPageSet
;
152 * Checks that the user has permissions to perform rotations.
153 * @param User $user The user to check
154 * @param Title $title
155 * @return string|null Permission error message, or null if there is no error
157 protected function checkPermissions( $user, $title ) {
158 $permissionErrors = array_merge(
159 $title->getUserPermissionsErrors( 'edit', $user ),
160 $title->getUserPermissionsErrors( 'upload', $user )
163 if ( $permissionErrors ) {
164 // Just return the first error
165 $msg = $this->parseMsg( $permissionErrors[0] );
173 public function mustBePosted() {
177 public function isWriteMode() {
181 public function getAllowedParams( $flags = 0 ) {
184 ApiBase
::PARAM_TYPE
=> array( '90', '180', '270' ),
185 ApiBase
::PARAM_REQUIRED
=> true
188 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
192 $result +
= $this->getPageSet()->getFinalParams( $flags );
198 public function needsToken() {
202 protected function getExamplesMessages() {
204 'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC'
205 => 'apihelp-imagerotate-example-simple',
206 'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' .
207 'rotation=180&token=123ABC'
208 => 'apihelp-imagerotate-example-generator',