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
24 namespace MediaWiki\Api
;
27 use MediaWiki\MainConfigNames
;
29 use Wikimedia\ParamValidator\ParamValidator
;
32 * A query action to return meta information about the foreign file repos
33 * configured on the wiki.
37 class ApiQueryFileRepoInfo
extends ApiQueryBase
{
39 private RepoGroup
$repoGroup;
41 public function __construct(
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 );
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 ) {
84 public function getAllowedParams() {
85 $props = $this->getProps();
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() {
99 $this->repoGroup
->forEachForeignRepo( static function ( FileRepo
$repo ) use ( &$props ) {
100 $props = array_merge( $props, array_keys( $repo->getInfo() ) );
103 $propValues = array_values( array_unique( array_merge(
105 array_keys( $this->repoGroup
->getLocalRepo()->getInfo() )
108 $propValues[] = 'canUpload';
114 protected function getExamplesMessages() {
117 $props = array_intersect( [ 'apiurl', 'name', 'displayname' ], $this->getProps() );
119 $examples['action=query&meta=filerepoinfo&friprop=' . implode( '|', $props )] =
120 'apihelp-query+filerepoinfo-example-simple';
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' );