MediaWiki  master
ApiQueryFileRepoInfo.php
Go to the documentation of this file.
1 <?php
31 
33  private $repoGroup;
34 
40  public function __construct(
41  ApiQuery $query,
42  $moduleName,
44  ) {
45  parent::__construct( $query, $moduleName, 'fri' );
46  $this->repoGroup = $repoGroup;
47  }
48 
49  public function execute() {
50  $conf = $this->getConfig();
51 
52  $params = $this->extractRequestParams();
53  $props = array_fill_keys( $params['prop'], true );
54 
55  $repos = [];
56 
57  $foreignTargets = $conf->get( 'ForeignUploadTargets' );
58 
59  $this->repoGroup->forEachForeignRepo(
60  static function ( FileRepo $repo ) use ( &$repos, $props, $foreignTargets ) {
61  $repoProps = $repo->getInfo();
62  $repoProps['canUpload'] = in_array( $repoProps['name'], $foreignTargets );
63 
64  $repos[] = array_intersect_key( $repoProps, $props );
65  }
66  );
67 
68  $localInfo = $this->repoGroup->getLocalRepo()->getInfo();
69  $localInfo['canUpload'] = $conf->get( 'EnableUploads' );
70  $repos[] = array_intersect_key( $localInfo, $props );
71 
72  $result = $this->getResult();
73  ApiResult::setIndexedTagName( $repos, 'repo' );
74  ApiResult::setArrayTypeRecursive( $repos, 'assoc' );
75  ApiResult::setArrayType( $repos, 'array' );
76  $result->addValue( [ 'query' ], 'repos', $repos );
77  }
78 
79  public function getCacheMode( $params ) {
80  return 'public';
81  }
82 
83  public function getAllowedParams() {
84  $props = $this->getProps();
85 
86  return [
87  'prop' => [
88  ApiBase::PARAM_DFLT => implode( '|', $props ),
89  ApiBase::PARAM_ISMULTI => true,
90  ApiBase::PARAM_TYPE => $props,
92  ],
93  ];
94  }
95 
96  public function getProps() {
97  $props = [];
98  $this->repoGroup->forEachForeignRepo( static function ( FileRepo $repo ) use ( &$props ) {
99  $props = array_merge( $props, array_keys( $repo->getInfo() ) );
100  } );
101 
102  $propValues = array_values( array_unique( array_merge(
103  $props,
104  array_keys( $this->repoGroup->getLocalRepo()->getInfo() )
105  ) ) );
106 
107  $propValues[] = 'canUpload';
108 
109  sort( $propValues );
110  return $propValues;
111  }
112 
113  protected function getExamplesMessages() {
114  $examples = [];
115 
116  $props = array_intersect( [ 'apiurl', 'name', 'displayname' ], $this->getProps() );
117  if ( $props ) {
118  $examples['action=query&meta=filerepoinfo&friprop=' . implode( '|', $props )] =
119  'apihelp-query+filerepoinfo-example-simple';
120  }
121 
122  return $examples;
123  }
124 
125  public function getHelpUrls() {
126  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Filerepoinfo';
127  }
128 }
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:72
ApiQuery
This is the main query class.
Definition: ApiQuery.php:39
ApiQueryFileRepoInfo\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiQueryFileRepoInfo.php:49
ApiQueryFileRepoInfo\getProps
getProps()
Definition: ApiQueryFileRepoInfo.php:96
FileRepo\getInfo
getInfo()
Return information about the repository.
Definition: FileRepo.php:1974
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:81
ApiResult\setArrayTypeRecursive
static setArrayTypeRecursive(array &$arr, $type, $kvpKeyName=null)
Set the array data type recursively.
Definition: ApiResult.php:746
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:628
FileRepo
Base class for file repositories.
Definition: FileRepo.php:45
ApiResult\setArrayType
static setArrayType(array &$arr, $type, $kvpKeyName=null)
Set the array data type.
Definition: ApiResult.php:715
ApiQueryBase
This is a base class for all Query modules.
Definition: ApiQueryBase.php:37
ApiQueryFileRepoInfo\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiQueryFileRepoInfo.php:113
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:764
ApiQueryFileRepoInfo\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiQueryFileRepoInfo.php:83
ApiResult\setIndexedTagName
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
Definition: ApiResult.php:603
ApiQueryFileRepoInfo
A query action to return meta information about the foreign file repos configured on the wiki.
Definition: ApiQueryFileRepoInfo.php:30
ApiQueryFileRepoInfo\__construct
__construct(ApiQuery $query, $moduleName, RepoGroup $repoGroup)
Definition: ApiQueryFileRepoInfo.php:40
ApiBase\PARAM_DFLT
const PARAM_DFLT
Definition: ApiBase.php:73
ApiQueryFileRepoInfo\$repoGroup
RepoGroup $repoGroup
Definition: ApiQueryFileRepoInfo.php:33
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
Definition: ApiBase.php:77
RepoGroup
Prioritized list of file repositories.
Definition: RepoGroup.php:32
ApiQueryFileRepoInfo\getCacheMode
getCacheMode( $params)
Get the cache mode for the data generated by this module.
Definition: ApiQueryFileRepoInfo.php:79
ApiBase\PARAM_HELP_MSG_PER_VALUE
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:195
ApiQueryFileRepoInfo\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiQueryFileRepoInfo.php:125