Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryFileRepoInfo
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 7
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
2
 getCacheMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 getProps
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Copyright © 2013 Mark Holmquist <mtraceur@member.fsf.org>
4 *
5 * @license GPL-2.0-or-later
6 * @file
7 * @since 1.22
8 */
9
10namespace MediaWiki\Api;
11
12use MediaWiki\FileRepo\FileRepo;
13use MediaWiki\FileRepo\RepoGroup;
14use MediaWiki\MainConfigNames;
15use Wikimedia\ParamValidator\ParamValidator;
16
17/**
18 * A query action to return meta information about the foreign file repos
19 * configured on the wiki.
20 *
21 * @ingroup API
22 */
23class ApiQueryFileRepoInfo extends ApiQueryBase {
24
25    public function __construct(
26        ApiQuery $query,
27        string $moduleName,
28        private readonly RepoGroup $repoGroup,
29    ) {
30        parent::__construct( $query, $moduleName, 'fri' );
31    }
32
33    public function execute() {
34        $conf = $this->getConfig();
35
36        $params = $this->extractRequestParams();
37        $props = array_fill_keys( $params['prop'], true );
38
39        $repos = [];
40
41        $foreignTargets = $conf->get( MainConfigNames::ForeignUploadTargets );
42
43        $this->repoGroup->forEachForeignRepo(
44            static function ( FileRepo $repo ) use ( &$repos, $props, $foreignTargets ) {
45                $repoProps = $repo->getInfo();
46                $repoProps['canUpload'] = in_array( $repoProps['name'], $foreignTargets );
47
48                $repos[] = array_intersect_key( $repoProps, $props );
49            }
50        );
51
52        $localInfo = $this->repoGroup->getLocalRepo()->getInfo();
53        $localInfo['canUpload'] = $conf->get( MainConfigNames::EnableUploads );
54        $repos[] = array_intersect_key( $localInfo, $props );
55
56        $result = $this->getResult();
57        ApiResult::setIndexedTagName( $repos, 'repo' );
58        ApiResult::setArrayTypeRecursive( $repos, 'assoc' );
59        ApiResult::setArrayType( $repos, 'array' );
60        $result->addValue( [ 'query' ], 'repos', $repos );
61    }
62
63    /** @inheritDoc */
64    public function getCacheMode( $params ) {
65        return 'public';
66    }
67
68    /** @inheritDoc */
69    public function getAllowedParams() {
70        $props = $this->getProps();
71
72        return [
73            'prop' => [
74                ParamValidator::PARAM_DEFAULT => implode( '|', $props ),
75                ParamValidator::PARAM_ISMULTI => true,
76                ParamValidator::PARAM_TYPE => $props,
77                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
78            ],
79        ];
80    }
81
82    public function getProps(): array {
83        $props = [];
84        $this->repoGroup->forEachForeignRepo( static function ( FileRepo $repo ) use ( &$props ) {
85            $props = array_merge( $props, array_keys( $repo->getInfo() ) );
86        } );
87
88        $propValues = array_values( array_unique( array_merge(
89            $props,
90            array_keys( $this->repoGroup->getLocalRepo()->getInfo() )
91        ) ) );
92
93        $propValues[] = 'canUpload';
94
95        sort( $propValues );
96        return $propValues;
97    }
98
99    /** @inheritDoc */
100    protected function getExamplesMessages() {
101        $examples = [];
102
103        $props = array_intersect( [ 'apiurl', 'name', 'displayname' ], $this->getProps() );
104        if ( $props ) {
105            $examples['action=query&meta=filerepoinfo&friprop=' . implode( '|', $props )] =
106                'apihelp-query+filerepoinfo-example-simple';
107        }
108
109        return $examples;
110    }
111
112    /** @inheritDoc */
113    public function getHelpUrls() {
114        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Filerepoinfo';
115    }
116}
117
118/** @deprecated class alias since 1.43 */
119class_alias( ApiQueryFileRepoInfo::class, 'ApiQueryFileRepoInfo' );