| 1 |
<?php |
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Api; |
| 24 |
|
| 25 |
use LocalFile; |
| 26 |
use MediaWiki\Title\Title; |
| 27 |
use RepoGroup; |
| 28 |
use Wikimedia\ParamValidator\ParamValidator; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class ApiFileRevert extends ApiBase { |
| 34 |
|
| 35 |
protected $file; |
| 36 |
|
| 37 |
|
| 38 |
protected $archiveName; |
| 39 |
|
| 40 |
|
| 41 |
protected $params; |
| 42 |
|
| 43 |
|
| 44 |
private $repoGroup; |
| 45 |
|
| 46 |
public function __construct( |
| 47 |
ApiMain $main, |
| 48 |
string $action, |
| 49 |
RepoGroup $repoGroup |
| 50 |
) { |
| 51 |
parent::__construct( $main, $action ); |
| 52 |
$this->repoGroup = $repoGroup; |
| 53 |
} |
| 54 |
|
| 55 |
public function execute() { |
| 56 |
$this->useTransactionalTimeLimit(); |
| 57 |
|
| 58 |
$this->params = $this->extractRequestParams(); |
| 59 |
|
| 60 |
$this->validateParameters(); |
| 61 |
|
| 62 |
|
| 63 |
$this->checkTitleUserPermissions( $this->file->getTitle(), [ 'edit', 'upload' ] ); |
| 64 |
$rights = [ 'reupload' ]; |
| 65 |
if ( $this->getUser()->equals( $this->file->getUploader() ) ) { |
| 66 |
|
| 67 |
array_unshift( $rights, 'reupload-own' ); |
| 68 |
} |
| 69 |
$this->checkUserRightsAny( $rights ); |
| 70 |
|
| 71 |
$sourceUrl = $this->file->getArchiveVirtualUrl( $this->archiveName ); |
| 72 |
$status = $this->file->upload( |
| 73 |
$sourceUrl, |
| 74 |
$this->params['comment'], |
| 75 |
$this->params['comment'], |
| 76 |
0, |
| 77 |
false, |
| 78 |
false, |
| 79 |
$this->getAuthority() |
| 80 |
); |
| 81 |
|
| 82 |
if ( $status->isGood() ) { |
| 83 |
$result = [ 'result' => 'Success' ]; |
| 84 |
} else { |
| 85 |
$result = [ |
| 86 |
'result' => 'Failure', |
| 87 |
'errors' => $this->getErrorFormatter()->arrayFromStatus( $status ), |
| 88 |
]; |
| 89 |
} |
| 90 |
|
| 91 |
$this->getResult()->addValue( null, $this->getModuleName(), $result ); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
protected function validateParameters() { |
| 99 |
|
| 100 |
$title = Title::makeTitleSafe( NS_FILE, $this->params['filename'] ); |
| 101 |
if ( $title === null ) { |
| 102 |
$this->dieWithError( |
| 103 |
[ 'apierror-invalidtitle', wfEscapeWikiText( $this->params['filename'] ) ] |
| 104 |
); |
| 105 |
} |
| 106 |
$localRepo = $this->repoGroup->getLocalRepo(); |
| 107 |
|
| 108 |
|
| 109 |
$this->file = $localRepo->newFile( $title ); |
| 110 |
if ( !$this->file->exists() ) { |
| 111 |
$this->dieWithError( 'apierror-missingtitle' ); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
$this->archiveName = $this->params['archivename']; |
| 116 |
|
| 117 |
$oldFile = $localRepo->newFromArchiveName( $title, $this->archiveName ); |
| 118 |
if ( !$oldFile->exists() ) { |
| 119 |
$this->dieWithError( 'filerevert-badversion' ); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
public function mustBePosted() { |
| 124 |
return true; |
| 125 |
} |
| 126 |
|
| 127 |
public function isWriteMode() { |
| 128 |
return true; |
| 129 |
} |
| 130 |
|
| 131 |
public function getAllowedParams() { |
| 132 |
return [ |
| 133 |
'filename' => [ |
| 134 |
ParamValidator::PARAM_TYPE => 'string', |
| 135 |
ParamValidator::PARAM_REQUIRED => true, |
| 136 |
], |
| 137 |
'comment' => [ |
| 138 |
ParamValidator::PARAM_DEFAULT => '', |
| 139 |
], |
| 140 |
'archivename' => [ |
| 141 |
ParamValidator::PARAM_TYPE => 'string', |
| 142 |
ParamValidator::PARAM_REQUIRED => true, |
| 143 |
], |
| 144 |
]; |
| 145 |
} |
| 146 |
|
| 147 |
public function needsToken() { |
| 148 |
return 'csrf'; |
| 149 |
} |
| 150 |
|
| 151 |
protected function getExamplesMessages() { |
| 152 |
return [ |
| 153 |
'action=filerevert&filename=Wiki.png&comment=Revert&' . |
| 154 |
'archivename=20110305152740!Wiki.png&token=123ABC' |
| 155 |
=> 'apihelp-filerevert-example-revert', |
| 156 |
]; |
| 157 |
} |
| 158 |
|
| 159 |
public function getHelpUrls() { |
| 160 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Filerevert'; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
class_alias( ApiFileRevert::class, 'ApiFileRevert' ); |