MediaWiki master
ApiQueryFileRepoInfo.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\Api;
11
16
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
67 public function getCacheMode( $params ) {
68 return 'public';
69 }
70
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,
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
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
116 public function getHelpUrls() {
117 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Filerepoinfo';
118 }
119}
120
122class_alias( ApiQueryFileRepoInfo::class, 'ApiQueryFileRepoInfo' );
getResult()
Get the result object.
Definition ApiBase.php:682
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:207
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:823
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...
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...
__construct(ApiQuery $query, string $moduleName, RepoGroup $repoGroup)
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:52
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.