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 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiCentralNoticeLogs
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 4
30
0.00% covered (danger)
0.00%
0 / 1
 execute
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 sanitizeText
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3use MediaWiki\Api\ApiBase;
4use MediaWiki\Api\ApiQueryBase;
5use Wikimedia\ParamValidator\ParamValidator;
6use Wikimedia\ParamValidator\TypeDef\NumericDef;
7
8# TODO: bannerlogs
9
10/** @todo This needs some major cleanup to work more like the rest of the API. */
11class ApiCentralNoticeLogs extends ApiQueryBase {
12
13    private const USER_FILTER = '/[a-zA-Z0-9_.]+/';
14    private const CAMPAIGNS_FILTER = '/[a-zA-Z0-9_|\-]+/';
15
16    public function execute() {
17        $params = $this->extractRequestParams();
18
19        $start = $params['start'];
20        $end = $params['end'];
21        $limit = $params['limit'];
22        $offset = $params['offset'];
23
24        $user = self::sanitizeText( $params['user'], self::USER_FILTER );
25        # TODO: multiple
26        $campaign = self::sanitizeText( $params['campaign'], self::CAMPAIGNS_FILTER );
27
28        $logs = Campaign::campaignLogs( $campaign, $user, $start, $end, $limit, $offset );
29
30        $this->getResult()->addValue( [ 'query', $this->getModuleName() ], 'logs', $logs );
31    }
32
33    /** @inheritDoc */
34    public function getAllowedParams() {
35        return [
36            'campaign' => [
37                ParamValidator::PARAM_TYPE => 'string',
38            ],
39            'user' => [
40                ParamValidator::PARAM_TYPE => 'string',
41            ],
42            'limit' => [
43                ParamValidator::PARAM_DEFAULT => 50,
44                ParamValidator::PARAM_TYPE => 'limit',
45                NumericDef::PARAM_MIN  => 1,
46                NumericDef::PARAM_MAX  => ApiBase::LIMIT_BIG1,
47                NumericDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2,
48            ],
49            'offset' => [
50                ParamValidator::PARAM_DEFAULT => 0,
51                ParamValidator::PARAM_TYPE => 'integer',
52            ],
53            'start' => [
54                ParamValidator::PARAM_TYPE => 'timestamp',
55            ],
56            'end' => [
57                ParamValidator::PARAM_TYPE => 'timestamp',
58            ],
59        ];
60    }
61
62    /**
63     * @inheritDoc
64     */
65    protected function getExamplesMessages() {
66        return [
67            'action=query&list=centralnoticelogs&format=json'
68                => 'apihelp-query+centralnoticelogs-example-1',
69        ];
70    }
71
72    /**
73     * Obtains the parameter $param, sanitizes by returning the first match to $regex or
74     * null if there was no match.
75     *
76     * @param ?string $value Incoming value
77     * @param string $regex Sanitization regular expression
78     * @return string|null The sanitized value
79     */
80    private static function sanitizeText( ?string $value, string $regex ): ?string {
81        if ( preg_match( $regex, $value ?? '', $matches ) ) {
82            return $matches[ 0 ];
83        }
84        return null;
85    }
86}