MediaWiki master
ApiQueryFileRepoInfo.php
Go to the documentation of this file.
1<?php
26
34
35 private RepoGroup $repoGroup;
36
42 public function __construct(
43 ApiQuery $query,
44 $moduleName,
45 RepoGroup $repoGroup
46 ) {
47 parent::__construct( $query, $moduleName, 'fri' );
48 $this->repoGroup = $repoGroup;
49 }
50
51 public function execute() {
52 $conf = $this->getConfig();
53
55 $props = array_fill_keys( $params['prop'], true );
56
57 $repos = [];
58
59 $foreignTargets = $conf->get( MainConfigNames::ForeignUploadTargets );
60
61 $this->repoGroup->forEachForeignRepo(
62 static function ( FileRepo $repo ) use ( &$repos, $props, $foreignTargets ) {
63 $repoProps = $repo->getInfo();
64 $repoProps['canUpload'] = in_array( $repoProps['name'], $foreignTargets );
65
66 $repos[] = array_intersect_key( $repoProps, $props );
67 }
68 );
69
70 $localInfo = $this->repoGroup->getLocalRepo()->getInfo();
71 $localInfo['canUpload'] = $conf->get( MainConfigNames::EnableUploads );
72 $repos[] = array_intersect_key( $localInfo, $props );
73
74 $result = $this->getResult();
75 ApiResult::setIndexedTagName( $repos, 'repo' );
76 ApiResult::setArrayTypeRecursive( $repos, 'assoc' );
77 ApiResult::setArrayType( $repos, 'array' );
78 $result->addValue( [ 'query' ], 'repos', $repos );
79 }
80
81 public function getCacheMode( $params ) {
82 return 'public';
83 }
84
85 public function getAllowedParams() {
86 $props = $this->getProps();
87
88 return [
89 'prop' => [
90 ParamValidator::PARAM_DEFAULT => implode( '|', $props ),
91 ParamValidator::PARAM_ISMULTI => true,
92 ParamValidator::PARAM_TYPE => $props,
94 ],
95 ];
96 }
97
98 public function getProps() {
99 $props = [];
100 $this->repoGroup->forEachForeignRepo( static function ( FileRepo $repo ) use ( &$props ) {
101 $props = array_merge( $props, array_keys( $repo->getInfo() ) );
102 } );
103
104 $propValues = array_values( array_unique( array_merge(
105 $props,
106 array_keys( $this->repoGroup->getLocalRepo()->getInfo() )
107 ) ) );
108
109 $propValues[] = 'canUpload';
110
111 sort( $propValues );
112 return $propValues;
113 }
114
115 protected function getExamplesMessages() {
116 $examples = [];
117
118 $props = array_intersect( [ 'apiurl', 'name', 'displayname' ], $this->getProps() );
119 if ( $props ) {
120 $examples['action=query&meta=filerepoinfo&friprop=' . implode( '|', $props )] =
121 'apihelp-query+filerepoinfo-example-simple';
122 }
123
124 return $examples;
125 }
126
127 public function getHelpUrls() {
128 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Filerepoinfo';
129 }
130}
array $params
The job parameters.
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:211
getResult()
Get the result object.
Definition ApiBase.php:671
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:811
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.
__construct(ApiQuery $query, $moduleName, RepoGroup $repoGroup)
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getExamplesMessages()
Returns usage examples for this module.
getHelpUrls()
Return links to more detailed help pages about the module.
getCacheMode( $params)
Get the cache mode for the data generated by this module.
This is the main query class.
Definition ApiQuery.php:43
Base class for file repositories.
Definition FileRepo.php:51
getInfo()
Return information about the repository.
A class containing constants representing the names of configuration variables.
Prioritized list of file repositories.
Definition RepoGroup.php:30
Service for formatting and validating API parameters.