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