Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiRevisionUpdate
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 6
156
0.00% covered (danger)
0.00%
0 / 1
 execute
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
56
 mustBePosted
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isWriteMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 needsToken
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 2
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\CodeComment;
7use MediaWiki\Extension\CodeReview\Backend\CodeRepository;
8use MediaWiki\Extension\CodeReview\Backend\CodeRevision;
9use MediaWiki\Extension\CodeReview\UI\CodeRevisionView;
10use Wikimedia\ParamValidator\ParamValidator;
11use Wikimedia\ParamValidator\TypeDef\IntegerDef;
12
13/**
14 * Created on Nov 18, 2010
15 *
16 * Copyright © 2010 Sam Reed
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License along
29 * with this program; if not, write to the Free Software Foundation, Inc.,
30 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 * http://www.gnu.org/copyleft/gpl.html
32 */
33
34class ApiRevisionUpdate extends ApiBase {
35
36    public function execute() {
37        $user = $this->getUser();
38
39        $this->checkUserRightsAny( 'codereview-use' );
40
41        $params = $this->extractRequestParams();
42
43        if ( $params['comment'] ) {
44            $this->checkUserRightsAny( 'codereview-post-comment' );
45        }
46
47        if (
48            !$this->getConfig()->get( 'CodeReviewInlineComments' )
49            && isset( $params['patchline'] )
50        ) {
51            $this->dieWithError(
52                'apierror-codereview-inlinecommentingdisabled', 'inlinecommentingdisabled' );
53        }
54
55        $repo = CodeRepository::newFromName( $params['repo'] );
56        if ( !$repo ) {
57            $this->dieWithError(
58                [ 'apierror-invalidrepo', wfEscapeWikiText( $params['repo'] ) ] );
59        }
60
61        $rev = $repo->getRevision( $params['rev'] );
62
63        if ( !$rev ) {
64            $this->dieWithError( [ 'apierror-nosuchrevid', $params['rev'] ] );
65
66        }
67
68        $revisionCommitter = new CodeRevisionCommitterApi( $repo, $user, $rev );
69
70        $commentID = $revisionCommitter->revisionUpdate(
71            $params['status'],
72            $params['addtags'],
73            $params['removetags'],
74            $params['addflags'],
75            $params['removeflags'],
76            $params['addreferences'],
77            $params['removereferences'],
78            $params['comment'],
79            null,
80            $params['addreferenced'],
81            $params['removereferenced'],
82            $user
83        );
84
85        // Forge a response object
86        $r = [ 'result' => 'Success' ];
87
88        if ( $commentID !== 0 ) {
89            // id inserted
90            $r['commentid'] = intval( $commentID );
91            // HTML Formatted comment
92            $view = new CodeRevisionView( $repo, $rev, $user );
93            $comment = CodeComment::newFromID( $commentID, $rev );
94            $r['HTML'] = $view->formatComment( $comment, $user );
95        }
96
97        $this->getResult()->addValue( null, $this->getModuleName(), $r );
98    }
99
100    public function mustBePosted() {
101        return true;
102    }
103
104    public function isWriteMode() {
105        return true;
106    }
107
108    public function needsToken() {
109        return 'csrf';
110    }
111
112    public function getAllowedParams() {
113        $flags = CodeRevision::getPossibleFlags();
114        return [
115            'repo' => [
116                ParamValidator::PARAM_TYPE => 'string',
117                ParamValidator::PARAM_REQUIRED => true,
118            ],
119            'rev' => [
120                ParamValidator::PARAM_TYPE => 'integer',
121                IntegerDef::PARAM_MIN => 1,
122                ParamValidator::PARAM_REQUIRED => true,
123            ],
124            'comment' => null,
125            'status' => [
126                ParamValidator::PARAM_TYPE => CodeRevision::getPossibleStates()
127            ],
128            'addtags' => [
129                ParamValidator::PARAM_TYPE => 'string',
130                ParamValidator::PARAM_ISMULTI => true,
131            ],
132            'removetags' => [
133                ParamValidator::PARAM_TYPE => 'string',
134                ParamValidator::PARAM_ISMULTI => true,
135            ],
136            'addflags' => [
137                ParamValidator::PARAM_TYPE => $flags,
138                ParamValidator::PARAM_ISMULTI => true,
139            ],
140            'removeflags' => [
141                ParamValidator::PARAM_TYPE => $flags,
142                ParamValidator::PARAM_ISMULTI => true,
143            ],
144            'addreferences' => [
145                ParamValidator::PARAM_TYPE => 'integer',
146                ParamValidator::PARAM_ISMULTI => true,
147            ],
148            'removereferences' => [
149                ParamValidator::PARAM_TYPE => 'integer',
150                ParamValidator::PARAM_ISMULTI => true,
151            ],
152            'addreferenced' => [
153                ParamValidator::PARAM_TYPE => 'integer',
154                ParamValidator::PARAM_ISMULTI => true,
155            ],
156            'removereferenced' => [
157                ParamValidator::PARAM_TYPE => 'integer',
158                ParamValidator::PARAM_ISMULTI => true,
159            ],
160            'token' => null,
161        ];
162    }
163
164    /**
165     * @inheritDoc
166     */
167    protected function getExamplesMessages() {
168        return [
169            'action=coderevisionupdate&repo=MediaWiki&rev=1&status=fixme'
170                => 'apihelp-coderevisionupdate-example-1',
171        ];
172    }
173}