Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiListExtDistBranches
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 8
272
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
 getProvider
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getCacheMode
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 / 17
0.00% covered (danger)
0.00%
0 / 1
12
 getInfo
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
 isInternal
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 / 16
0.00% covered (danger)
0.00%
0 / 1
12
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\ExtensionDistributor\Api;
4
5use MediaWiki\Api\ApiBase;
6use MediaWiki\Api\ApiQuery;
7use MediaWiki\Api\ApiQueryBase;
8use MediaWiki\Api\ApiResult;
9use MediaWiki\Extension\ExtensionDistributor\Providers\ExtDistProvider;
10use MediaWiki\Logger\LoggerFactory;
11use Wikimedia\ParamValidator\ParamValidator;
12
13/**
14 * @author Legoktm
15 */
16class ApiListExtDistBranches extends ApiQueryBase {
17
18    /** @var (ExtDistProvider|null)[] */
19    private $providers = [];
20
21    public function __construct( ApiQuery $query, string $moduleName ) {
22        parent::__construct( $query, $moduleName, 'edb' );
23    }
24
25    /**
26     * @param string $type
27     * @return ExtDistProvider|null
28     */
29    protected function getProvider( $type ) {
30        if ( !isset( $this->providers[$type] ) ) {
31            $this->providers[$type] = ExtDistProvider::getProviderFor( $type );
32        }
33
34        return $this->providers[$type];
35    }
36
37    /** @inheritDoc */
38    public function getCacheMode( $params ) {
39        return 'public';
40    }
41
42    public function execute() {
43        $logger = LoggerFactory::getInstance( 'ExtensionDistributor' );
44        $extProvider = ExtDistProvider::getProviderFor( ExtDistProvider::EXTENSIONS );
45        $extProvider->setLogger( $logger );
46        $skinProvider = ExtDistProvider::getProviderFor( ExtDistProvider::SKINS );
47        $skinProvider->setLogger( $logger );
48        $info = [];
49        ApiResult::setArrayType( $info, 'assoc' );
50        $params = $this->extractRequestParams();
51        if ( $params['exts'] ) {
52            $info['extensions'] = $this->getInfo( $extProvider, $params['exts'] );
53        }
54        if ( $params['skins'] ) {
55            $info['skins'] = $this->getInfo( $skinProvider, $params['skins'] );
56        }
57        $this->getResult()->addValue(
58            'query',
59            $this->getModuleName(),
60            $info
61        );
62    }
63
64    /**
65     * Get the info to output for a given provider
66     *
67     * @param ExtDistProvider $provider
68     * @param array $repos
69     * @return array
70     */
71    private function getInfo( ExtDistProvider $provider, array $repos ) {
72        $out = [];
73        foreach ( $repos as $repo ) {
74            $out[$repo] = [];
75            $branches = $provider->getBranches( $repo );
76            foreach ( $branches as $branch => $sha1 ) {
77                $out[$repo][$branch] = $provider->getTarballLocation( $repo, $branch );
78            }
79            $source = $provider->getSourceURL( $repo );
80            if ( $source !== false ) {
81                // As long as 'source' is never added to $wgExtDistSnapshotRefs,
82                // we'll be totally fine.
83                $out[$repo]['source'] = $source;
84            }
85            ApiResult::setArrayType( $out[$repo], 'assoc' );
86        }
87
88        return $out;
89    }
90
91    /** @inheritDoc */
92    public function isInternal() {
93        return true;
94    }
95
96    /** @inheritDoc */
97    public function getAllowedParams() {
98        $extensionsProvider = $this->getProvider( ExtDistProvider::EXTENSIONS );
99        $skinsProvider = $this->getProvider( ExtDistProvider::SKINS );
100        return [
101            'exts' => [
102                ParamValidator::PARAM_ISMULTI => true,
103                ParamValidator::PARAM_TYPE => $extensionsProvider
104                    ? $extensionsProvider->getRepositoryList()
105                    : [],
106            ],
107            'skins' => [
108                ParamValidator::PARAM_ISMULTI => true,
109                ParamValidator::PARAM_TYPE => $skinsProvider
110                    ? $skinsProvider->getRepositoryList()
111                    : [],
112            ]
113        ];
114    }
115
116    /**
117     * @see ApiBase::getExamplesMessages()
118     * @return array
119     */
120    protected function getExamplesMessages() {
121        return [
122            'action=query&list=extdistbranches&edbexts=ExtensionDistributor'
123                => 'apihelp-query+extdistbranches-example-1',
124        ];
125    }
126}