Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
48.65% covered (danger)
48.65%
18 / 37
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiSupportedProgrammingLanguages
48.65% covered (danger)
48.65%
18 / 37
40.00% covered (danger)
40.00%
2 / 5
16.67
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 executeGenerator
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 run
43.33% covered (danger)
43.33%
13 / 30
0.00% covered (danger)
0.00%
0 / 1
4.64
 getAllowedParams
n/a
0 / 0
n/a
0 / 0
1
 isInternal
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * WikiLambda function call API
4 *
5 * @file
6 * @ingroup Extensions
7 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
8 * @license MIT
9 */
10
11namespace MediaWiki\Extension\WikiLambda\ActionAPI;
12
13use ApiPageSet;
14use GuzzleHttp\Exception\ClientException;
15use GuzzleHttp\Exception\ConnectException;
16use GuzzleHttp\Exception\ServerException;
17use MediaWiki\Extension\WikiLambda\ZErrorFactory;
18use MediaWiki\Extension\WikiLambda\ZObjects\ZResponseEnvelope;
19use MediaWiki\PoolCounter\PoolCounterWorkViaCallback;
20use MediaWiki\Status\Status;
21
22class ApiSupportedProgrammingLanguages extends WikiLambdaApiBase {
23
24    /**
25     * @inheritDoc
26     */
27    public function __construct( $query, $moduleName ) {
28        parent::__construct( $query, $moduleName, 'wikilambda_supported_programming_languages_' );
29
30        $this->setUp();
31    }
32
33    /**
34     * @inheritDoc
35     */
36    public function execute() {
37        // (T362271) Emit appropriate cache headers for a 24 hour TTL
38        // NOTE (T362273): MediaWiki out-guesses us and assumes we don't know what we're doing; to fix so it works
39        $this->getMain()->setCacheMode( 'public' );
40        $this->getMain()->setCacheMaxAge( 60 * 60 * 24 );
41
42        $this->run();
43    }
44
45    /**
46     * @inheritDoc
47     */
48    public function executeGenerator( $resultPageSet ) {
49        $this->run( $resultPageSet );
50    }
51
52    /**
53     * TODO (T338251): Factor out some commonality with WikiLambdaApiBase::executeFunctionCall()
54     * rather than rolling our own. (But note different end-point and error messages.)
55     *
56     * @param ApiPageSet|null $resultPageSet
57     */
58    private function run( $resultPageSet = null ) {
59        $pageResult = $this->getResult();
60
61        $work = new PoolCounterWorkViaCallback(
62            'WikiLambdaSupportedProgrammingLanguages',
63            $this->getUser()->getName(),
64            [
65                'doWork' => function () {
66                    return $this->orchestrator->getSupportedProgrammingLanguages();
67                },
68                'error' => function ( Status $status ) {
69                    $this->dieWithError(
70                        [ "apierror-wikilambda_supported_programming_languages-concurrency-limit" ],
71                        null, null, 429
72                    );
73                } ] );
74
75        try {
76            $response = $work->execute();
77            $result = [ 'success' => true, 'data' => $response->getBody() ];
78        } catch ( ConnectException $exception ) {
79            $this->dieWithError(
80                [
81                    "apierror-wikilambda_supported_programming_languages-not-connected",
82                    $this->orchestratorHost
83                ],
84                null, null, 500
85            );
86        } catch ( ClientException | ServerException $exception ) {
87            $zError = ZErrorFactory::wrapMessageInZError( $exception->getResponse()->getReasonPhrase(), '' );
88            $zResponseMap = ZResponseEnvelope::wrapErrorInResponseMap( $zError );
89            $zResponseObject = new ZResponseEnvelope( null, $zResponseMap );
90            $result = [ 'data' => $zResponseObject->getSerialized() ];
91        }
92        $pageResult->addValue( [ 'query' ], $this->getModuleName(), $result );
93    }
94
95    /**
96     * @inheritDoc
97     * @codeCoverageIgnore
98     */
99    protected function getAllowedParams(): array {
100        return [];
101    }
102
103    /**
104     * Mark as internal. This isn't meant to be user-facing, and can change at any time.
105     * @return bool
106     */
107    public function isInternal() {
108        return true;
109    }
110
111}