| 1 |
<?php |
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace MediaWiki\Api; |
| 25 |
|
| 26 |
use FileRepo; |
| 27 |
use MediaWiki\MainConfigNames; |
| 28 |
use RepoGroup; |
| 29 |
use Wikimedia\ParamValidator\ParamValidator; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class ApiQueryFileRepoInfo extends ApiQueryBase { |
| 38 |
|
| 39 |
private RepoGroup $repoGroup; |
| 40 |
|
| 41 |
public function __construct( |
| 42 |
ApiQuery $query, |
| 43 |
string $moduleName, |
| 44 |
RepoGroup $repoGroup |
| 45 |
) { |
| 46 |
parent::__construct( $query, $moduleName, 'fri' ); |
| 47 |
$this->repoGroup = $repoGroup; |
| 48 |
} |
| 49 |
|
| 50 |
public function execute() { |
| 51 |
$conf = $this->getConfig(); |
| 52 |
|
| 53 |
$params = $this->extractRequestParams(); |
| 54 |
$props = array_fill_keys( $params['prop'], true ); |
| 55 |
|
| 56 |
$repos = []; |
| 57 |
|
| 58 |
$foreignTargets = $conf->get( MainConfigNames::ForeignUploadTargets ); |
| 59 |
|
| 60 |
$this->repoGroup->forEachForeignRepo( |
| 61 |
static function ( FileRepo $repo ) use ( &$repos, $props, $foreignTargets ) { |
| 62 |
$repoProps = $repo->getInfo(); |
| 63 |
$repoProps['canUpload'] = in_array( $repoProps['name'], $foreignTargets ); |
| 64 |
|
| 65 |
$repos[] = array_intersect_key( $repoProps, $props ); |
| 66 |
} |
| 67 |
); |
| 68 |
|
| 69 |
$localInfo = $this->repoGroup->getLocalRepo()->getInfo(); |
| 70 |
$localInfo['canUpload'] = $conf->get( MainConfigNames::EnableUploads ); |
| 71 |
$repos[] = array_intersect_key( $localInfo, $props ); |
| 72 |
|
| 73 |
$result = $this->getResult(); |
| 74 |
ApiResult::setIndexedTagName( $repos, 'repo' ); |
| 75 |
ApiResult::setArrayTypeRecursive( $repos, 'assoc' ); |
| 76 |
ApiResult::setArrayType( $repos, 'array' ); |
| 77 |
$result->addValue( [ 'query' ], 'repos', $repos ); |
| 78 |
} |
| 79 |
|
| 80 |
public function getCacheMode( $params ) { |
| 81 |
return 'public'; |
| 82 |
} |
| 83 |
|
| 84 |
public function getAllowedParams() { |
| 85 |
$props = $this->getProps(); |
| 86 |
|
| 87 |
return [ |
| 88 |
'prop' => [ |
| 89 |
ParamValidator::PARAM_DEFAULT => implode( '|', $props ), |
| 90 |
ParamValidator::PARAM_ISMULTI => true, |
| 91 |
ParamValidator::PARAM_TYPE => $props, |
| 92 |
ApiBase::PARAM_HELP_MSG_PER_VALUE => [], |
| 93 |
], |
| 94 |
]; |
| 95 |
} |
| 96 |
|
| 97 |
public function getProps() { |
| 98 |
$props = []; |
| 99 |
$this->repoGroup->forEachForeignRepo( static function ( FileRepo $repo ) use ( &$props ) { |
| 100 |
$props = array_merge( $props, array_keys( $repo->getInfo() ) ); |
| 101 |
} ); |
| 102 |
|
| 103 |
$propValues = array_values( array_unique( array_merge( |
| 104 |
$props, |
| 105 |
array_keys( $this->repoGroup->getLocalRepo()->getInfo() ) |
| 106 |
) ) ); |
| 107 |
|
| 108 |
$propValues[] = 'canUpload'; |
| 109 |
|
| 110 |
sort( $propValues ); |
| 111 |
return $propValues; |
| 112 |
} |
| 113 |
|
| 114 |
protected function getExamplesMessages() { |
| 115 |
$examples = []; |
| 116 |
|
| 117 |
$props = array_intersect( [ 'apiurl', 'name', 'displayname' ], $this->getProps() ); |
| 118 |
if ( $props ) { |
| 119 |
$examples['action=query&meta=filerepoinfo&friprop=' . implode( '|', $props )] = |
| 120 |
'apihelp-query+filerepoinfo-example-simple'; |
| 121 |
} |
| 122 |
|
| 123 |
return $examples; |
| 124 |
} |
| 125 |
|
| 126 |
public function getHelpUrls() { |
| 127 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Filerepoinfo'; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
class_alias( ApiQueryFileRepoInfo::class, 'ApiQueryFileRepoInfo' ); |