| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Api; |
| 22 |
|
| 23 |
use ChangeTags; |
| 24 |
use MediaWiki\FileBackend\FSFile\TempFSFileFactory; |
| 25 |
use MediaWiki\Status\Status; |
| 26 |
use MediaWiki\Title\TitleFactory; |
| 27 |
use RepoGroup; |
| 28 |
use Wikimedia\ParamValidator\ParamValidator; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class ApiImageRotate extends ApiBase { |
| 34 |
|
| 35 |
private $mPageSet = null; |
| 36 |
|
| 37 |
private RepoGroup $repoGroup; |
| 38 |
private TempFSFileFactory $tempFSFileFactory; |
| 39 |
private TitleFactory $titleFactory; |
| 40 |
|
| 41 |
public function __construct( |
| 42 |
ApiMain $mainModule, |
| 43 |
string $moduleName, |
| 44 |
RepoGroup $repoGroup, |
| 45 |
TempFSFileFactory $tempFSFileFactory, |
| 46 |
TitleFactory $titleFactory |
| 47 |
) { |
| 48 |
parent::__construct( $mainModule, $moduleName ); |
| 49 |
$this->repoGroup = $repoGroup; |
| 50 |
$this->tempFSFileFactory = $tempFSFileFactory; |
| 51 |
$this->titleFactory = $titleFactory; |
| 52 |
} |
| 53 |
|
| 54 |
public function execute() { |
| 55 |
$this->useTransactionalTimeLimit(); |
| 56 |
|
| 57 |
$params = $this->extractRequestParams(); |
| 58 |
$rotation = $params['rotation']; |
| 59 |
|
| 60 |
$continuationManager = new ApiContinuationManager( $this, [], [] ); |
| 61 |
$this->setContinuationManager( $continuationManager ); |
| 62 |
|
| 63 |
$pageSet = $this->getPageSet(); |
| 64 |
$pageSet->execute(); |
| 65 |
|
| 66 |
$result = $pageSet->getInvalidTitlesAndRevisions( [ |
| 67 |
'invalidTitles', 'special', 'missingIds', 'missingRevIds', 'interwikiTitles', |
| 68 |
] ); |
| 69 |
|
| 70 |
|
| 71 |
if ( $params['tags'] ) { |
| 72 |
$ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() ); |
| 73 |
if ( !$ableToTag->isOK() ) { |
| 74 |
$this->dieStatus( $ableToTag ); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
foreach ( $pageSet->getPages() as $page ) { |
| 79 |
$title = $this->titleFactory->newFromPageIdentity( $page ); |
| 80 |
$r = []; |
| 81 |
$r['id'] = $title->getArticleID(); |
| 82 |
ApiQueryBase::addTitleInfo( $r, $title ); |
| 83 |
if ( !$title->exists() ) { |
| 84 |
$r['missing'] = true; |
| 85 |
if ( $title->isKnown() ) { |
| 86 |
$r['known'] = true; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
$file = $this->repoGroup->findFile( $title, [ 'latest' => true ] ); |
| 91 |
if ( !$file ) { |
| 92 |
$r['result'] = 'Failure'; |
| 93 |
$r['errors'] = $this->getErrorFormatter()->arrayFromStatus( |
| 94 |
Status::newFatal( 'apierror-filedoesnotexist' ) |
| 95 |
); |
| 96 |
$result[] = $r; |
| 97 |
continue; |
| 98 |
} |
| 99 |
$handler = $file->getHandler(); |
| 100 |
if ( !$handler || !$handler->canRotate() ) { |
| 101 |
$r['result'] = 'Failure'; |
| 102 |
$r['errors'] = $this->getErrorFormatter()->arrayFromStatus( |
| 103 |
Status::newFatal( 'apierror-filetypecannotberotated' ) |
| 104 |
); |
| 105 |
$result[] = $r; |
| 106 |
continue; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
$this->checkTitleUserPermissions( $file->getTitle(), [ 'edit', 'upload' ] ); |
| 111 |
|
| 112 |
$srcPath = $file->getLocalRefPath(); |
| 113 |
if ( $srcPath === false ) { |
| 114 |
$r['result'] = 'Failure'; |
| 115 |
$r['errors'] = $this->getErrorFormatter()->arrayFromStatus( |
| 116 |
Status::newFatal( 'apierror-filenopath' ) |
| 117 |
); |
| 118 |
$result[] = $r; |
| 119 |
continue; |
| 120 |
} |
| 121 |
$ext = strtolower( pathinfo( "$srcPath", PATHINFO_EXTENSION ) ); |
| 122 |
$tmpFile = $this->tempFSFileFactory->newTempFSFile( 'rotate_', $ext ); |
| 123 |
$dstPath = $tmpFile->getPath(); |
| 124 |
|
| 125 |
$err = $handler->rotate( $file, [ |
| 126 |
'srcPath' => $srcPath, |
| 127 |
'dstPath' => $dstPath, |
| 128 |
'rotation' => $rotation |
| 129 |
] ); |
| 130 |
if ( !$err ) { |
| 131 |
$comment = $this->msg( |
| 132 |
'rotate-comment' |
| 133 |
)->numParams( $rotation )->inContentLanguage()->text(); |
| 134 |
|
| 135 |
$status = $file->upload( |
| 136 |
$dstPath, |
| 137 |
$comment, |
| 138 |
$comment, |
| 139 |
0, |
| 140 |
false, |
| 141 |
false, |
| 142 |
$this->getAuthority(), |
| 143 |
$params['tags'] ?: [] |
| 144 |
); |
| 145 |
if ( $status->isGood() ) { |
| 146 |
$r['result'] = 'Success'; |
| 147 |
} else { |
| 148 |
$r['result'] = 'Failure'; |
| 149 |
$r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status ); |
| 150 |
} |
| 151 |
} else { |
| 152 |
$r['result'] = 'Failure'; |
| 153 |
$r['errors'] = $this->getErrorFormatter()->arrayFromStatus( |
| 154 |
Status::newFatal( ApiMessage::create( $err->getMsg() ) ) |
| 155 |
); |
| 156 |
} |
| 157 |
$result[] = $r; |
| 158 |
} |
| 159 |
$apiResult = $this->getResult(); |
| 160 |
ApiResult::setIndexedTagName( $result, 'page' ); |
| 161 |
$apiResult->addValue( null, $this->getModuleName(), $result ); |
| 162 |
|
| 163 |
$this->setContinuationManager( null ); |
| 164 |
$continuationManager->setContinuationIntoResult( $apiResult ); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
private function getPageSet() { |
| 172 |
$this->mPageSet ??= new ApiPageSet( $this, 0, NS_FILE ); |
| 173 |
|
| 174 |
return $this->mPageSet; |
| 175 |
} |
| 176 |
|
| 177 |
public function mustBePosted() { |
| 178 |
return true; |
| 179 |
} |
| 180 |
|
| 181 |
public function isWriteMode() { |
| 182 |
return true; |
| 183 |
} |
| 184 |
|
| 185 |
public function getAllowedParams( $flags = 0 ) { |
| 186 |
$result = [ |
| 187 |
'rotation' => [ |
| 188 |
ParamValidator::PARAM_TYPE => [ '90', '180', '270' ], |
| 189 |
ParamValidator::PARAM_REQUIRED => true |
| 190 |
], |
| 191 |
'continue' => [ |
| 192 |
ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
| 193 |
], |
| 194 |
'tags' => [ |
| 195 |
ParamValidator::PARAM_TYPE => 'tags', |
| 196 |
ParamValidator::PARAM_ISMULTI => true, |
| 197 |
], |
| 198 |
]; |
| 199 |
if ( $flags ) { |
| 200 |
$result += $this->getPageSet()->getFinalParams( $flags ); |
| 201 |
} |
| 202 |
|
| 203 |
return $result; |
| 204 |
} |
| 205 |
|
| 206 |
public function needsToken() { |
| 207 |
return 'csrf'; |
| 208 |
} |
| 209 |
|
| 210 |
protected function getExamplesMessages() { |
| 211 |
return [ |
| 212 |
'action=imagerotate&titles=File:Example.jpg&rotation=90&token=123ABC' |
| 213 |
=> 'apihelp-imagerotate-example-simple', |
| 214 |
'action=imagerotate&generator=categorymembers&gcmtitle=Category:Flip&gcmtype=file&' . |
| 215 |
'rotation=180&token=123ABC' |
| 216 |
=> 'apihelp-imagerotate-example-generator', |
| 217 |
]; |
| 218 |
} |
| 219 |
|
| 220 |
public function getHelpUrls() { |
| 221 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Imagerotate'; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
class_alias( ApiImageRotate::class, 'ApiImageRotate' ); |