Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.29% covered (warning)
85.29%
29 / 34
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiCentralNoticeChoiceData
85.29% covered (warning)
85.29%
29 / 34
50.00% covered (danger)
50.00%
2 / 4
5.08
0.00% covered (danger)
0.00%
0 / 1
 execute
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
1
 getAllowedParams
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 sanitizeText
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
1<?php
2
3/**
4 * Module for the centralnoticechoicedata Web API.
5 *
6 * This is provided as a fallback mechanism for getting banner choice data
7 * from an infrastructure wiki, for cases in which direct cross-wiki DB
8 * queries are not possible.
9 */
10class ApiCentralNoticeChoiceData extends ApiBase {
11
12    private const LANG_FILTER = '/[a-zA-Z0-9\-]+/';
13
14    private const PROJECT_FILTER = '/[a-zA-Z0-9_\-]+/';
15
16    public function execute() {
17        // Extract, sanitize and munge the parameters
18        $params = $this->extractRequestParams();
19
20        $project = self::sanitizeText(
21                $params['project'],
22                self::PROJECT_FILTER
23        );
24
25        $lang = self::sanitizeText(
26                $params['language'],
27                self::LANG_FILTER
28        );
29
30        $choices = ChoiceDataProvider::getChoices( $project, $lang );
31
32        // Get the result object for creating the output
33        $apiResult = $this->getResult();
34
35        $apiResult->addValue(
36            null,
37            'choices',
38            $choices
39        );
40    }
41
42    public function getAllowedParams() {
43        return [
44            'project' => [
45                    ApiBase::PARAM_TYPE => 'string',
46                    ApiBase::PARAM_REQUIRED => true
47            ],
48            'language' => [
49                    ApiBase::PARAM_TYPE => 'string',
50                    ApiBase::PARAM_REQUIRED => true
51            ]
52        ];
53    }
54
55    protected function getExamplesMessages() {
56        return [
57            'action=centralnoticechoicedata&project=wikipedia&language=en'
58            => 'apihelp-centralnoticechoicedata-example-1'
59        ];
60    }
61
62    /**
63     * Obtains the parameter $param, sanitizes by returning the first match to $regex or
64     * $default if there was no match.
65     *
66     * @param string $param Name of GET/POST parameter
67     * @param string $regex Sanitization regular expression
68     * @param string|null $default Default value to return on error
69     *
70     * @return string The sanitized value
71     */
72    private static function sanitizeText( $param, $regex, $default = null ) {
73        $matches = [];
74
75        if ( preg_match( $regex, $param, $matches ) ) {
76            return $matches[ 0 ];
77        } else {
78            return $default;
79        }
80    }
81}