Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.88% covered (success)
96.88%
31 / 32
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryZLanguages
96.88% covered (success)
96.88%
31 / 32
50.00% covered (danger)
50.00%
1 / 2
12
0.00% covered (danger)
0.00%
0 / 1
 __construct
n/a
0 / 0
n/a
0 / 0
1
 run
95.45% covered (success)
95.45%
21 / 22
0.00% covered (danger)
0.00%
0 / 1
5
 getLabelsForZids
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
 getAllowedParams
n/a
0 / 0
n/a
0 / 0
1
1<?php
2/**
3 * WikiLambda API to map language codes to ZLanguage ZIDs via the query 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 MediaWiki\Api\ApiQuery;
14use MediaWiki\Extension\WikiLambda\Registry\ZLangRegistry;
15use MediaWiki\Extension\WikiLambda\WikiLambdaServices;
16use MediaWiki\Extension\WikiLambda\ZObjectStore;
17use MediaWiki\Logger\LoggerFactory;
18use Wikimedia\ParamValidator\ParamValidator;
19
20class ApiQueryZLanguages extends WikiLambdaApiQueryGeneratorBase {
21
22    private ZLangRegistry $langRegistry;
23    private ZObjectStore $zObjectStore;
24
25    /**
26     * @codeCoverageIgnore
27     */
28    public function __construct( ApiQuery $query, string $moduleName ) {
29        parent::__construct( $query, $moduleName, 'wikilambdaload_zlanguages_' );
30
31        $this->langRegistry = ZLangRegistry::singleton();
32        $this->zObjectStore = WikiLambdaServices::getZObjectStore();
33        $this->setLogger( LoggerFactory::getInstance( 'WikiLambda' ) );
34    }
35
36    /**
37     * @inheritDoc
38     */
39    protected function run( $resultPageSet = null ) {
40        $params = $this->extractRequestParams();
41
42        $codes = $params[ 'codes' ] ?? [];
43        if ( !is_array( $codes ) ) {
44            $codes = [ $codes ];
45        }
46
47        $zidMap = $this->langRegistry->getLanguageZidsFromCodes( $codes );
48        $withLabels = $params['withlabels'] ?? false;
49        $labelsByZid = $withLabels ? $this->getLabelsForZids( $zidMap ) : [];
50
51        $items = array_map(
52            static function ( string $code ) use ( $zidMap, $withLabels, $labelsByZid ) {
53                $zid = $zidMap[ $code ] ?? null;
54                $item = [
55                    'code' => $code,
56                    'zid' => $zid,
57                ];
58                // If requested, add label to the item
59                if ( $withLabels ) {
60                    $item['label'] = is_string( $zid ) ? ( $labelsByZid[ $zid ] ?? null ) : null;
61                }
62                return $item;
63            },
64            $codes
65        );
66
67        $result = $this->getResult();
68        $result->addValue( [ 'query' ], $this->getModuleName(), $items );
69    }
70
71    /**
72     * Returns labels for all valid ZIDs from the provided code=>zid map.
73     *
74     * @param array<string,?string> $zidMap
75     * @return array<string,?string>
76     */
77    private function getLabelsForZids( array $zidMap ): array {
78        $zids = [];
79        foreach ( $zidMap as $zid ) {
80            if ( is_string( $zid ) && $zid !== '' ) {
81                $zids[$zid] = true;
82            }
83        }
84
85        if ( !$zids ) {
86            return [];
87        }
88
89        return $this->zObjectStore->fetchZObjectLabels(
90            array_keys( $zids ),
91            $this->getLanguage()->getCode()
92        );
93    }
94
95    /**
96     * @inheritDoc
97     * @codeCoverageIgnore
98     */
99    protected function getAllowedParams(): array {
100        return [
101            'codes' => [
102                ParamValidator::PARAM_TYPE => 'string',
103                ParamValidator::PARAM_ISMULTI => true,
104                ParamValidator::PARAM_REQUIRED => true,
105            ],
106            'withlabels' => [
107                ParamValidator::PARAM_TYPE => 'boolean',
108                ParamValidator::PARAM_DEFAULT => false,
109            ],
110        ];
111    }
112}