Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 51 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
ApiQueryFileRepoInfo | |
0.00% |
0 / 50 |
|
0.00% |
0 / 7 |
72 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
2 | |||
getCacheMode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getAllowedParams | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
getProps | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
getExamplesMessages | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
getHelpUrls | |
0.00% |
0 / 1 |
|
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 | |
24 | namespace MediaWiki\Api; |
25 | |
26 | use FileRepo; |
27 | use MediaWiki\MainConfigNames; |
28 | use RepoGroup; |
29 | use Wikimedia\ParamValidator\ParamValidator; |
30 | |
31 | /** |
32 | * A query action to return meta information about the foreign file repos |
33 | * configured on the wiki. |
34 | * |
35 | * @ingroup API |
36 | */ |
37 | class ApiQueryFileRepoInfo extends ApiQueryBase { |
38 | |
39 | private RepoGroup $repoGroup; |
40 | |
41 | public function __construct( |
42 | ApiQuery $query, |
43 | string $moduleName, |
44 | RepoGroup $repoGroup |
45 | ) { |
46 | parent::__construct( $query, $moduleName, 'fri' ); |
47 | $this->repoGroup = $repoGroup; |
48 | } |
49 | |
50 | public function execute() { |
51 | $conf = $this->getConfig(); |
52 | |
53 | $params = $this->extractRequestParams(); |
54 | $props = array_fill_keys( $params['prop'], true ); |
55 | |
56 | $repos = []; |
57 | |
58 | $foreignTargets = $conf->get( MainConfigNames::ForeignUploadTargets ); |
59 | |
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 ); |
64 | |
65 | $repos[] = array_intersect_key( $repoProps, $props ); |
66 | } |
67 | ); |
68 | |
69 | $localInfo = $this->repoGroup->getLocalRepo()->getInfo(); |
70 | $localInfo['canUpload'] = $conf->get( MainConfigNames::EnableUploads ); |
71 | $repos[] = array_intersect_key( $localInfo, $props ); |
72 | |
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 ); |
78 | } |
79 | |
80 | public function getCacheMode( $params ) { |
81 | return 'public'; |
82 | } |
83 | |
84 | public function getAllowedParams() { |
85 | $props = $this->getProps(); |
86 | |
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 => [], |
93 | ], |
94 | ]; |
95 | } |
96 | |
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 | } ); |
102 | |
103 | $propValues = array_values( array_unique( array_merge( |
104 | $props, |
105 | array_keys( $this->repoGroup->getLocalRepo()->getInfo() ) |
106 | ) ) ); |
107 | |
108 | $propValues[] = 'canUpload'; |
109 | |
110 | sort( $propValues ); |
111 | return $propValues; |
112 | } |
113 | |
114 | protected function getExamplesMessages() { |
115 | $examples = []; |
116 | |
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'; |
121 | } |
122 | |
123 | return $examples; |
124 | } |
125 | |
126 | public function getHelpUrls() { |
127 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Filerepoinfo'; |
128 | } |
129 | } |
130 | |
131 | /** @deprecated class alias since 1.43 */ |
132 | class_alias( ApiQueryFileRepoInfo::class, 'ApiQueryFileRepoInfo' ); |