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 / 50
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 / 2
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 * 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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @since 1.22
22 */
23
24use MediaWiki\MainConfigNames;
25use Wikimedia\ParamValidator\ParamValidator;
26
27/**
28 * A query action to return meta information about the foreign file repos
29 * configured on the wiki.
30 *
31 * @ingroup API
32 */
33class ApiQueryFileRepoInfo extends ApiQueryBase {
34
35    private RepoGroup $repoGroup;
36
37    /**
38     * @param ApiQuery $query
39     * @param string $moduleName
40     * @param RepoGroup $repoGroup
41     */
42    public function __construct(
43        ApiQuery $query,
44        $moduleName,
45        RepoGroup $repoGroup
46    ) {
47        parent::__construct( $query, $moduleName, 'fri' );
48        $this->repoGroup = $repoGroup;
49    }
50
51    public function execute() {
52        $conf = $this->getConfig();
53
54        $params = $this->extractRequestParams();
55        $props = array_fill_keys( $params['prop'], true );
56
57        $repos = [];
58
59        $foreignTargets = $conf->get( MainConfigNames::ForeignUploadTargets );
60
61        $this->repoGroup->forEachForeignRepo(
62            static function ( FileRepo $repo ) use ( &$repos, $props, $foreignTargets ) {
63                $repoProps = $repo->getInfo();
64                $repoProps['canUpload'] = in_array( $repoProps['name'], $foreignTargets );
65
66                $repos[] = array_intersect_key( $repoProps, $props );
67            }
68        );
69
70        $localInfo = $this->repoGroup->getLocalRepo()->getInfo();
71        $localInfo['canUpload'] = $conf->get( MainConfigNames::EnableUploads );
72        $repos[] = array_intersect_key( $localInfo, $props );
73
74        $result = $this->getResult();
75        ApiResult::setIndexedTagName( $repos, 'repo' );
76        ApiResult::setArrayTypeRecursive( $repos, 'assoc' );
77        ApiResult::setArrayType( $repos, 'array' );
78        $result->addValue( [ 'query' ], 'repos', $repos );
79    }
80
81    public function getCacheMode( $params ) {
82        return 'public';
83    }
84
85    public function getAllowedParams() {
86        $props = $this->getProps();
87
88        return [
89            'prop' => [
90                ParamValidator::PARAM_DEFAULT => implode( '|', $props ),
91                ParamValidator::PARAM_ISMULTI => true,
92                ParamValidator::PARAM_TYPE => $props,
93                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
94            ],
95        ];
96    }
97
98    public function getProps() {
99        $props = [];
100        $this->repoGroup->forEachForeignRepo( static function ( FileRepo $repo ) use ( &$props ) {
101            $props = array_merge( $props, array_keys( $repo->getInfo() ) );
102        } );
103
104        $propValues = array_values( array_unique( array_merge(
105            $props,
106            array_keys( $this->repoGroup->getLocalRepo()->getInfo() )
107        ) ) );
108
109        $propValues[] = 'canUpload';
110
111        sort( $propValues );
112        return $propValues;
113    }
114
115    protected function getExamplesMessages() {
116        $examples = [];
117
118        $props = array_intersect( [ 'apiurl', 'name', 'displayname' ], $this->getProps() );
119        if ( $props ) {
120            $examples['action=query&meta=filerepoinfo&friprop=' . implode( '|', $props )] =
121                'apihelp-query+filerepoinfo-example-simple';
122        }
123
124        return $examples;
125    }
126
127    public function getHelpUrls() {
128        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Filerepoinfo';
129    }
130}