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