MediaWiki
REL1_21
|
00001 <?php 00024 class ApiImageRotate extends ApiBase { 00025 00026 private $mPageSet = null; 00027 00028 public function __construct( $main, $action ) { 00029 parent::__construct( $main, $action ); 00030 } 00031 00039 private static function addValues( array &$result, $values, $flag = null, $name = null ) { 00040 foreach ( $values as $val ) { 00041 if( $val instanceof Title ) { 00042 $v = array(); 00043 ApiQueryBase::addTitleInfo( $v, $val ); 00044 } elseif( $name !== null ) { 00045 $v = array( $name => $val ); 00046 } else { 00047 $v = $val; 00048 } 00049 if( $flag !== null ) { 00050 $v[$flag] = ''; 00051 } 00052 $result[] = $v; 00053 } 00054 } 00055 00056 00057 public function execute() { 00058 $params = $this->extractRequestParams(); 00059 $rotation = $params[ 'rotation' ]; 00060 $user = $this->getUser(); 00061 00062 $pageSet = $this->getPageSet(); 00063 $pageSet->execute(); 00064 00065 $result = array(); 00066 $result = array(); 00067 00068 self::addValues( $result, $pageSet->getInvalidTitles(), 'invalid', 'title' ); 00069 self::addValues( $result, $pageSet->getSpecialTitles(), 'special', 'title' ); 00070 self::addValues( $result, $pageSet->getMissingPageIDs(), 'missing', 'pageid' ); 00071 self::addValues( $result, $pageSet->getMissingRevisionIDs(), 'missing', 'revid' ); 00072 self::addValues( $result, $pageSet->getInterwikiTitlesAsResult() ); 00073 00074 foreach ( $pageSet->getTitles() as $title ) { 00075 $r = array(); 00076 $r['id'] = $title->getArticleID(); 00077 ApiQueryBase::addTitleInfo( $r, $title ); 00078 if ( !$title->exists() ) { 00079 $r['missing'] = ''; 00080 } 00081 00082 $file = wfFindFile( $title ); 00083 if ( !$file ) { 00084 $r['result'] = 'Failure'; 00085 $r['errormessage'] = 'File does not exist'; 00086 $result[] = $r; 00087 continue; 00088 } 00089 $handler = $file->getHandler(); 00090 if ( !$handler || !$handler->canRotate() ) { 00091 $r['result'] = 'Failure'; 00092 $r['errormessage'] = 'File type cannot be rotated'; 00093 $result[] = $r; 00094 continue; 00095 } 00096 00097 // Check whether we're allowed to rotate this file 00098 $permError = $this->checkPermissions( $this->getUser(), $file->getTitle() ); 00099 if ( $permError !== null ) { 00100 $r['result'] = 'Failure'; 00101 $r['errormessage'] = $permError; 00102 $result[] = $r; 00103 continue; 00104 } 00105 00106 $srcPath = $file->getLocalRefPath(); 00107 if ( $srcPath === false ) { 00108 $r['result'] = 'Failure'; 00109 $r['errormessage'] = 'Cannot get local file path'; 00110 $result[] = $r; 00111 continue; 00112 } 00113 $ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION ) ); 00114 $tmpFile = TempFSFile::factory( 'rotate_', $ext); 00115 $dstPath = $tmpFile->getPath(); 00116 $err = $handler->rotate( $file, array( 00117 "srcPath" => $srcPath, 00118 "dstPath" => $dstPath, 00119 "rotation"=> $rotation 00120 ) ); 00121 if ( !$err ) { 00122 $comment = wfMessage( 'rotate-comment' )->numParams( $rotation )->text(); 00123 $status = $file->upload( $dstPath, 00124 $comment, $comment, 0, false, false, $this->getUser() ); 00125 if ( $status->isGood() ) { 00126 $r['result'] = 'Success'; 00127 } else { 00128 $r['result'] = 'Failure'; 00129 $r['errormessage'] = $this->getResult()->convertStatusToArray( $status ); 00130 } 00131 } else { 00132 $r['result'] = 'Failure'; 00133 $r['errormessage'] = $err->toText(); 00134 } 00135 $result[] = $r; 00136 } 00137 $apiResult = $this->getResult(); 00138 $apiResult->setIndexedTagName( $result, 'page' ); 00139 $apiResult->addValue( null, $this->getModuleName(), $result ); 00140 } 00141 00146 private function getPageSet() { 00147 if ( $this->mPageSet === null ) { 00148 $this->mPageSet = new ApiPageSet( $this, 0, NS_FILE ); 00149 } 00150 return $this->mPageSet; 00151 } 00152 00158 protected function checkPermissions( $user, $title ) { 00159 $permissionErrors = array_merge( 00160 $title->getUserPermissionsErrors( 'edit' , $user ), 00161 $title->getUserPermissionsErrors( 'upload' , $user ) 00162 ); 00163 00164 if ( $permissionErrors ) { 00165 // Just return the first error 00166 $msg = $this->parseMsg( $permissionErrors[0] ); 00167 return $msg['info']; 00168 } 00169 00170 return null; 00171 } 00172 00173 public function mustBePosted() { 00174 return true; 00175 } 00176 00177 public function isWriteMode() { 00178 return true; 00179 } 00180 00181 public function getAllowedParams( $flags = 0 ) { 00182 $pageSet = $this->getPageSet(); 00183 $result = array( 00184 'rotation' => array( 00185 ApiBase::PARAM_TYPE => array( '90', '180', '270' ), 00186 ApiBase::PARAM_REQUIRED => true 00187 ), 00188 'token' => array( 00189 ApiBase::PARAM_TYPE => 'string', 00190 ApiBase::PARAM_REQUIRED => true 00191 ), 00192 ); 00193 if ( $flags ) { 00194 $result += $this->getPageSet()->getFinalParams( $flags ); 00195 } 00196 return $result; 00197 } 00198 00199 public function getParamDescription() { 00200 $pageSet = $this->getPageSet(); 00201 return $pageSet->getParamDescription() + array( 00202 'rotation' => 'Degrees to rotate image clockwise', 00203 'token' => 'Edit token. You can get one of these through action=tokens', 00204 ); 00205 } 00206 00207 public function getDescription() { 00208 return 'Rotate one or more images'; 00209 } 00210 00211 public function needsToken() { 00212 return true; 00213 } 00214 00215 public function getTokenSalt() { 00216 return ''; 00217 } 00218 00219 public function getPossibleErrors() { 00220 $pageSet = $this->getPageSet(); 00221 return array_merge( 00222 parent::getPossibleErrors(), 00223 $pageSet->getPossibleErrors() 00224 ); 00225 } 00226 00227 public function getExamples() { 00228 return array( 00229 'api.php?action=imagerotate&titles=Example.jpg&rotation=90&token=123ABC', 00230 ); 00231 } 00232 }