Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiQueryFileRepoInfo.php
blob00fd63d8aa03e45613586922fd7fe8eec35c15c4
1 <?php
2 /**
3 * Copyright © 2013 Mark Holmquist <mtraceur@member.fsf.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @since 1.22
24 namespace MediaWiki\Api;
26 use FileRepo;
27 use MediaWiki\MainConfigNames;
28 use RepoGroup;
29 use Wikimedia\ParamValidator\ParamValidator;
31 /**
32 * A query action to return meta information about the foreign file repos
33 * configured on the wiki.
35 * @ingroup API
37 class ApiQueryFileRepoInfo extends ApiQueryBase {
39 private RepoGroup $repoGroup;
41 public function __construct(
42 ApiQuery $query,
43 string $moduleName,
44 RepoGroup $repoGroup
45 ) {
46 parent::__construct( $query, $moduleName, 'fri' );
47 $this->repoGroup = $repoGroup;
50 public function execute() {
51 $conf = $this->getConfig();
53 $params = $this->extractRequestParams();
54 $props = array_fill_keys( $params['prop'], true );
56 $repos = [];
58 $foreignTargets = $conf->get( MainConfigNames::ForeignUploadTargets );
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 );
65 $repos[] = array_intersect_key( $repoProps, $props );
69 $localInfo = $this->repoGroup->getLocalRepo()->getInfo();
70 $localInfo['canUpload'] = $conf->get( MainConfigNames::EnableUploads );
71 $repos[] = array_intersect_key( $localInfo, $props );
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 );
80 public function getCacheMode( $params ) {
81 return 'public';
84 public function getAllowedParams() {
85 $props = $this->getProps();
87 return [
88 'prop' => [
89 ParamValidator::PARAM_DEFAULT => implode( '|', $props ),
90 ParamValidator::PARAM_ISMULTI => true,
91 ParamValidator::PARAM_TYPE => $props,
92 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
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 } );
103 $propValues = array_values( array_unique( array_merge(
104 $props,
105 array_keys( $this->repoGroup->getLocalRepo()->getInfo() )
106 ) ) );
108 $propValues[] = 'canUpload';
110 sort( $propValues );
111 return $propValues;
114 protected function getExamplesMessages() {
115 $examples = [];
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';
123 return $examples;
126 public function getHelpUrls() {
127 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Filerepoinfo';
131 /** @deprecated class alias since 1.43 */
132 class_alias( ApiQueryFileRepoInfo::class, 'ApiQueryFileRepoInfo' );