MediaWiki master
ApiQueryFileRepoInfo.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Api;
25
26use FileRepo;
28use RepoGroup;
30
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
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,
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
132class_alias( ApiQueryFileRepoInfo::class, 'ApiQueryFileRepoInfo' );
array $params
The job parameters.
Base class for file repositories.
Definition FileRepo.php:57
getInfo()
Return information about the repository.
getResult()
Get the result object.
Definition ApiBase.php:710
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:224
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:851
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.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getExamplesMessages()
Returns usage examples for this module.
__construct(ApiQuery $query, string $moduleName, RepoGroup $repoGroup)
getCacheMode( $params)
Get the cache mode for the data generated by this module.
This is the main query class.
Definition ApiQuery.php:48
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.
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()
Prioritized list of file repositories.
Definition RepoGroup.php:32
Service for formatting and validating API parameters.