Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiCodeDiff
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 3
56
0.00% covered (danger)
0.00%
0 / 1
 execute
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
30
 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 MediaWiki\Extension\CodeReview\Backend\CodeDiffHighlighter;
7use MediaWiki\Extension\CodeReview\Backend\CodeRepository;
8use Wikimedia\ParamValidator\ParamValidator;
9use Wikimedia\ParamValidator\TypeDef\IntegerDef;
10
11/**
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * http://www.gnu.org/copyleft/gpl.html
26 */
27
28class ApiCodeDiff extends ApiBase {
29
30    public function execute() {
31        $this->checkUserRightsAny( 'codereview-use' );
32
33        $params = $this->extractRequestParams();
34
35        $repo = CodeRepository::newFromName( $params['repo'] );
36        if ( !$repo ) {
37            $this->dieWithError( [ 'apierror-invalidrepo', wfEscapeWikiText( $params['repo'] ) ] );
38        }
39
40        $lastStoredRev = $repo->getLastStoredRev();
41
42        if ( $params['rev'] > $lastStoredRev ) {
43            $this->dieWithError( [ 'apierror-nosuchrevid', $params['rev'] ] );
44        }
45
46        $diff = $repo->getDiff( $params['rev'] );
47
48        if ( !is_string( $diff ) ) {
49            // FIXME: Are we sure we don't want to throw an error here?
50            $html = 'Failed to load diff. Error message: ' . CodeRepository::getDiffErrorMessage( $diff );
51        } elseif ( strlen( $diff ) > $this->getConfig()->get( 'CodeReviewMaxDiffSize' ) ) {
52            $html = 'Diff too large.';
53        } else {
54            $hilite = new CodeDiffHighlighter();
55            $html = $hilite->render( $diff );
56        }
57
58        $data = [
59            'repo' => $params['repo'],
60            'id' => $params['rev'],
61            'diff' => $html
62        ];
63        $this->getResult()->addValue( 'code', 'rev', $data );
64    }
65
66    public function getAllowedParams() {
67        return [
68            'repo' => [
69                ParamValidator::PARAM_TYPE => 'string',
70                ParamValidator::PARAM_REQUIRED => true,
71            ],
72            'rev' => [
73                ParamValidator::PARAM_TYPE => 'integer',
74                IntegerDef::PARAM_MIN => 1,
75                ParamValidator::PARAM_REQUIRED => true,
76            ]
77        ];
78    }
79
80    /**
81     * @inheritDoc
82     */
83    protected function getExamplesMessages() {
84        return [
85            'action=codediff&repo=MediaWiki&rev=42080'
86                => 'apihelp-codediff-example-1',
87        ];
88    }
89}