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