Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryCodeComments
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 5
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
42
 formatRow
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
56
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\CodeReview\Api;
4
5use ApiBase;
6use ApiQueryBase;
7use ApiResult;
8use MediaWiki\Extension\CodeReview\UI\CodeCommentsListView;
9use Wikimedia\ParamValidator\ParamValidator;
10use Wikimedia\ParamValidator\TypeDef\IntegerDef;
11
12/**
13 * Created on Oct 29, 2008
14 *
15 * Copyright © 2008 Bryan Tong Minh <Bryan.TongMinh@Gmail.com>
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 * http://www.gnu.org/copyleft/gpl.html
31 */
32
33class ApiQueryCodeComments extends ApiQueryBase {
34    /**
35     * @var array
36     */
37    private $props;
38
39    public function __construct( $query, $moduleName ) {
40        parent::__construct( $query, $moduleName, 'cc' );
41    }
42
43    public function execute() {
44        $this->checkUserRightsAny( 'codereview-use' );
45
46        $params = $this->extractRequestParams();
47
48        $this->props = array_flip( $params['prop'] );
49        if ( isset( $this->props['revision'] ) ) {
50            $this->addDeprecation(
51                [ 'apiwarn-deprecation-withreplacement', 'ccprop=revision', 'ccprop=status' ],
52                'action=query&list=codecomments&ccprop=revision'
53            );
54        }
55
56        $listview = new CodeCommentsListView( $params['repo'] );
57        if ( $listview->getRepo() === null ) {
58            $this->dieWithError( [ 'apierror-invalidrepo', wfEscapeWikiText( $params['repo'] ) ] );
59        }
60        $pager = $listview->getPager();
61
62        if ( $params['start'] !== null ) {
63            $pager->setOffset( $this->getDB()->timestamp( $params['start'] ) );
64        }
65        $limit = $params['limit'];
66        $pager->setLimit( $limit );
67
68        $pager->doQuery();
69
70        $comments = $pager->getResult();
71        $data = [];
72
73        $count = 0;
74        $lastTimestamp = 0;
75        foreach ( $comments as $row ) {
76            if ( $count == $limit ) {
77                $this->setContinueEnumParameter( 'start',
78                    wfTimestamp( TS_ISO_8601, $lastTimestamp ) );
79                break;
80            }
81
82            $data[] = $this->formatRow( $row );
83            $lastTimestamp = $row->cc_timestamp;
84            $count++;
85        }
86        $comments->free();
87
88        $result = $this->getResult();
89        ApiResult::setIndexedTagName( $data, 'comment' );
90        $result->addValue( 'query', $this->getModuleName(), $data );
91    }
92
93    private function formatRow( $row ) {
94        $item = [];
95        if ( isset( $this->props['revid'] ) ) {
96            $item['revid'] = $row->cc_rev_id;
97        }
98        if ( isset( $this->props['timestamp'] ) ) {
99            $item['timestamp'] = wfTimestamp( TS_ISO_8601, $row->cc_timestamp );
100        }
101        if ( isset( $this->props['user'] ) ) {
102            $item['user'] = $row->cc_user_text;
103        }
104        if ( isset( $this->props['revision'] ) || isset( $this->props['status'] ) ) {
105            $item['status'] = $row->cr_status;
106        }
107        if ( isset( $this->props['text'] ) ) {
108            ApiResult::setContentValue( $item, 'text', $row->cc_text );
109        }
110        return $item;
111    }
112
113    public function getAllowedParams() {
114        return [
115            'repo' => [
116                ParamValidator::PARAM_TYPE => 'string',
117                ParamValidator::PARAM_REQUIRED => true,
118            ],
119            'limit' => [
120                ParamValidator::PARAM_DEFAULT => 10,
121                ParamValidator::PARAM_TYPE => 'limit',
122                IntegerDef::PARAM_MIN => 1,
123                IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
124                IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
125            ],
126            'start' => [
127                ParamValidator::PARAM_TYPE => 'timestamp'
128            ],
129            'prop' => [
130                ParamValidator::PARAM_ISMULTI => true,
131                ParamValidator::PARAM_DEFAULT => 'timestamp|user|status|revid',
132                ParamValidator::PARAM_TYPE => [
133                    'timestamp',
134                    'user',
135                    'status',
136                    'text',
137                    'revid',
138                    'revision',
139                ],
140            ],
141        ];
142    }
143
144    /**
145     * @inheritDoc
146     */
147    protected function getExamplesMessages() {
148        return [
149            'action=query&list=codecomments&ccrepo=MediaWiki'
150                => 'apihelp-query+codecomments-example-1',
151            'action=query&list=codecomments&ccrepo=MediaWiki&ccprop=timestamp|user|status|text'
152                => 'apihelp-query+codecomments-example-2',
153        ];
154    }
155}