Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 25 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
ApiCentralNoticeQueryCampaign | |
0.00% |
0 / 25 |
|
0.00% |
0 / 4 |
56 | |
0.00% |
0 / 1 |
execute | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
12 | |||
getAllowedParams | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getExamplesMessages | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
sanitizeText | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | /** @todo: This needs some major cleanup to work more like the rest of the API. */ |
4 | class ApiCentralNoticeQueryCampaign extends ApiBase { |
5 | |
6 | /** |
7 | * @var string sanitize campaign name |
8 | * FIXME: the string is apparently unrestricted in Special:CentralNotice |
9 | */ |
10 | private const CAMPAIGNS_FILTER = '/^[a-zA-Z0-9 _|\-]+$/'; |
11 | |
12 | public function execute() { |
13 | // Obtain the ApiResults object from the base |
14 | $result = $this->getResult(); |
15 | |
16 | // Get our language/project/country |
17 | $params = $this->extractRequestParams(); |
18 | |
19 | $campaigns = explode( '|', self::sanitizeText( $params['campaign'], self::CAMPAIGNS_FILTER ) ); |
20 | |
21 | foreach ( $campaigns as $campaign ) { |
22 | $settings = Campaign::getCampaignSettings( $campaign ); |
23 | if ( $settings ) { |
24 | $settings['banners'] = json_decode( $settings['banners'] ); |
25 | |
26 | # TODO this should probably be pushed down: |
27 | $settings['projects'] = explode( ', ', $settings['projects'] ); |
28 | $settings['countries'] = explode( ', ', $settings['countries'] ); |
29 | $settings['regions'] = explode( ', ', $settings['regions'] ); |
30 | $settings['languages'] = explode( ', ', $settings['languages'] ); |
31 | |
32 | $settings['enabled'] = (bool)$settings['enabled']; |
33 | $settings['preferred'] = (bool)$settings['preferred']; |
34 | $settings['locked'] = (bool)$settings['locked']; |
35 | $settings['geo'] = (bool)$settings['geo']; |
36 | } |
37 | |
38 | $result->addValue( [ $this->getModuleName() ], $campaign, $settings ); |
39 | } |
40 | } |
41 | |
42 | public function getAllowedParams() { |
43 | return [ 'campaign' => '' ]; |
44 | } |
45 | |
46 | /** |
47 | * @inheritDoc |
48 | */ |
49 | protected function getExamplesMessages() { |
50 | return [ |
51 | 'action=centralnoticequerycampaign&format=json&campaign=Plea_US' |
52 | => 'apihelp-centralnoticequerycampaign-example-1', |
53 | ]; |
54 | } |
55 | |
56 | /** |
57 | * Obtains the parameter $param, sanitizes by returning the first match to $regex or |
58 | * $default if there was no match. |
59 | * |
60 | * @param string $value Incoming value |
61 | * @param string $regex Sanitization regular expression |
62 | * @param string|null $default Default value to return on error |
63 | * |
64 | * @return string The sanitized value |
65 | */ |
66 | private static function sanitizeText( $value, $regex, $default = null ) { |
67 | $matches = []; |
68 | |
69 | if ( preg_match( $regex, $value, $matches ) ) { |
70 | return $matches[ 0 ]; |
71 | } else { |
72 | return $default; |
73 | } |
74 | } |
75 | } |