Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiSupportedProgrammingLanguages
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 3
56
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
 run
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 1
20
 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 GuzzleHttp\Exception\ClientException;
14use GuzzleHttp\Exception\ConnectException;
15use GuzzleHttp\Exception\ServerException;
16use MediaWiki\Api\ApiMain;
17use MediaWiki\Extension\WikiLambda\HttpStatus;
18use MediaWiki\Extension\WikiLambda\Registry\ZErrorTypeRegistry;
19use MediaWiki\Extension\WikiLambda\ZErrorFactory;
20use MediaWiki\Extension\WikiLambda\ZObjects\ZResponseEnvelope;
21use MediaWiki\PoolCounter\PoolCounterWorkViaCallback;
22use MediaWiki\Status\Status;
23
24class ApiSupportedProgrammingLanguages extends WikiLambdaApiBase {
25
26    public function __construct( ApiMain $mainModule, string $moduleName ) {
27        parent::__construct( $mainModule, $moduleName, 'wikilambda_supported_programming_languages_' );
28
29        $this->setUp();
30    }
31
32    /**
33     * TODO (T338251): Factor out some commonality with WikiLambdaApiBase::executeFunctionCall()
34     * rather than rolling our own. (But note different end-point and error messages.)
35     *
36     * @inheritDoc
37     */
38    protected function run() {
39        // Exit if we're running in non-repo mode (e.g. on a client wiki)
40        if ( !$this->getConfig()->get( 'WikiLambdaEnableRepoMode' ) ) {
41            WikiLambdaApiBase::dieWithZError(
42                ZErrorFactory::createZErrorInstance(
43                    ZErrorTypeRegistry::Z_ERROR_USER_CANNOT_RUN,
44                    []
45                ),
46                HttpStatus::BAD_REQUEST
47            );
48        }
49
50        $pageResult = $this->getResult();
51
52        $work = new PoolCounterWorkViaCallback(
53            'WikiLambdaSupportedProgrammingLanguages',
54            $this->getUser()->getName(),
55            [
56                'doWork' => function () {
57                    return $this->orchestrator->getSupportedProgrammingLanguages();
58                },
59                'error' => function ( Status $status ): never {
60                    $this->dieWithError(
61                        [ "apierror-wikilambda_supported_programming_languages-concurrency-limit" ],
62                        null, null, HttpStatus::TOO_MANY_REQUESTS
63                    );
64                } ] );
65
66        try {
67            $response = $work->execute();
68            $result = [ 'success' => true, 'data' => $response->getBody() ];
69        } catch ( ConnectException ) {
70            $this->dieWithError(
71                [
72                    "apierror-wikilambda_supported_programming_languages-not-connected",
73                    $this->orchestratorHost
74                ],
75                null, null, HttpStatus::INTERNAL_SERVER_ERROR
76            );
77        } catch ( ClientException | ServerException $exception ) {
78            $zError = ZErrorFactory::createEvaluationError( $exception->getResponse()->getReasonPhrase(), '' );
79            $zResponseMap = ZResponseEnvelope::wrapErrorInResponseMap( $zError );
80            $zResponseObject = new ZResponseEnvelope( null, $zResponseMap );
81            $result = [ 'data' => $zResponseObject->getSerialized() ];
82        }
83        $pageResult->addValue( [ 'query' ], $this->getModuleName(), $result );
84    }
85
86    /**
87     * @inheritDoc
88     * @codeCoverageIgnore
89     */
90    protected function getAllowedParams(): array {
91        return [];
92    }
93
94    /**
95     * Mark as internal. This isn't meant to be user-facing, and can change at any time.
96     * @return bool
97     */
98    public function isInternal() {
99        return true;
100    }
101
102}