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