MediaWiki master
ApiQueryFileRepoInfo.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\Api;
11
16
24
25 public function __construct(
26 ApiQuery $query,
27 string $moduleName,
28 private readonly RepoGroup $repoGroup,
29 ) {
30 parent::__construct( $query, $moduleName, 'fri' );
31 }
32
33 public function execute() {
34 $conf = $this->getConfig();
35
36 $params = $this->extractRequestParams();
37 $props = array_fill_keys( $params['prop'], true );
38
39 $repos = [];
40
41 $foreignTargets = $conf->get( MainConfigNames::ForeignUploadTargets );
42
43 $this->repoGroup->forEachForeignRepo(
44 static function ( FileRepo $repo ) use ( &$repos, $props, $foreignTargets ) {
45 $repoProps = $repo->getInfo();
46 $repoProps['canUpload'] = in_array( $repoProps['name'], $foreignTargets );
47
48 $repos[] = array_intersect_key( $repoProps, $props );
49 }
50 );
51
52 $localInfo = $this->repoGroup->getLocalRepo()->getInfo();
53 $localInfo['canUpload'] = $conf->get( MainConfigNames::EnableUploads );
54 $repos[] = array_intersect_key( $localInfo, $props );
55
56 $result = $this->getResult();
57 ApiResult::setIndexedTagName( $repos, 'repo' );
58 ApiResult::setArrayTypeRecursive( $repos, 'assoc' );
59 ApiResult::setArrayType( $repos, 'array' );
60 $result->addValue( [ 'query' ], 'repos', $repos );
61 }
62
64 public function getCacheMode( $params ) {
65 return 'public';
66 }
67
69 public function getAllowedParams() {
70 $props = $this->getProps();
71
72 return [
73 'prop' => [
74 ParamValidator::PARAM_DEFAULT => implode( '|', $props ),
75 ParamValidator::PARAM_ISMULTI => true,
76 ParamValidator::PARAM_TYPE => $props,
78 ],
79 ];
80 }
81
82 public function getProps(): array {
83 $props = [];
84 $this->repoGroup->forEachForeignRepo( static function ( FileRepo $repo ) use ( &$props ) {
85 $props = array_merge( $props, array_keys( $repo->getInfo() ) );
86 } );
87
88 $propValues = array_values( array_unique( array_merge(
89 $props,
90 array_keys( $this->repoGroup->getLocalRepo()->getInfo() )
91 ) ) );
92
93 $propValues[] = 'canUpload';
94
95 sort( $propValues );
96 return $propValues;
97 }
98
100 protected function getExamplesMessages() {
101 $examples = [];
102
103 $props = array_intersect( [ 'apiurl', 'name', 'displayname' ], $this->getProps() );
104 if ( $props ) {
105 $examples['action=query&meta=filerepoinfo&friprop=' . implode( '|', $props )] =
106 'apihelp-query+filerepoinfo-example-simple';
107 }
108
109 return $examples;
110 }
111
113 public function getHelpUrls() {
114 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Filerepoinfo';
115 }
116}
117
119class_alias( ApiQueryFileRepoInfo::class, 'ApiQueryFileRepoInfo' );
getResult()
Get the result object.
Definition ApiBase.php:696
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:206
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:837
This is a base class for all Query modules.
A query action to return meta information about the foreign file repos configured on the wiki.
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
__construct(ApiQuery $query, string $moduleName, private readonly RepoGroup $repoGroup,)
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
getCacheMode( $params)
Get the cache mode for the data generated by this module.Override this in the module subclass....
This is the main query class.
Definition ApiQuery.php:36
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
static setArrayType(array &$arr, $type, $kvpKeyName=null)
Set the array data type.
static setArrayTypeRecursive(array &$arr, $type, $kvpKeyName=null)
Set the array data type recursively.
Base class for file repositories.
Definition FileRepo.php:51
getInfo()
Return information about the repository.
Prioritized list of file repositories.
Definition RepoGroup.php:30
A class containing constants representing the names of configuration variables.
const ForeignUploadTargets
Name constant for the ForeignUploadTargets setting, for use with Config::get()
const EnableUploads
Name constant for the EnableUploads setting, for use with Config::get()
Service for formatting and validating API parameters.