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