Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiCodeUpdate
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 5
132
0.00% covered (danger)
0.00%
0 / 1
 execute
0.00% covered (danger)
0.00%
0 / 29
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
 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\CodeRepository;
7use MediaWiki\Extension\CodeReview\Backend\CodeRevision;
8use MediaWiki\Extension\CodeReview\Backend\SubversionAdaptor;
9use Wikimedia\ParamValidator\ParamValidator;
10use Wikimedia\ParamValidator\TypeDef\IntegerDef;
11
12/**
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 * http://www.gnu.org/copyleft/gpl.html
28 */
29
30class ApiCodeUpdate extends ApiBase {
31
32    public function execute() {
33        $this->checkUserRightsAny( 'codereview-use' );
34
35        $params = $this->extractRequestParams();
36
37        $repo = CodeRepository::newFromName( $params['repo'] );
38        if ( !$repo ) {
39            $this->dieWithError( [ 'apierror-invalidrepo', wfEscapeWikiText( $params['repo'] ) ] );
40        }
41
42        $svn = SubversionAdaptor::newFromRepo( $repo->getPath() );
43        $lastStoredRev = $repo->getLastStoredRev();
44
45        if ( $lastStoredRev >= $params['rev'] ) {
46            // Nothing to do, we're up to date.
47            // Return an empty result
48            $this->getResult()->addValue( null, $this->getModuleName(), [] );
49            return;
50        }
51
52        // FIXME: this could be a lot?
53        $log = $svn->getLog( '', $lastStoredRev + 1, $params['rev'] );
54        if ( !$log ) {
55            // FIXME: When and how often does this happen?
56            // Should we use dieWithError() here instead?
57            ApiBase::dieDebug( __METHOD__, 'Something awry...' );
58        }
59
60        $result = [];
61        $revs = [];
62        foreach ( $log as $data ) {
63            $codeRev = CodeRevision::newFromSvn( $repo, $data );
64            $codeRev->save();
65            $result[] = [
66                'id' => $codeRev->getId(),
67                'author' => $codeRev->getAuthor(),
68                'timestamp' => wfTimestamp( TS_ISO_8601, $codeRev->getTimestamp() ),
69                'message' => $codeRev->getMessage()
70            ];
71            $revs[] = $codeRev;
72        }
73        // Cache the diffs if there are a only a few.
74        // Mainly for WMF post-commit ping hook...
75        if ( count( $revs ) <= 2 ) {
76            foreach ( $revs as $codeRev ) {
77                // trigger caching
78                $repo->setDiffCache( $codeRev );
79            }
80        }
81        $this->getResult()->setIndexedTagName( $result, 'rev' );
82        $this->getResult()->addValue( null, $this->getModuleName(), $result );
83    }
84
85    public function mustBePosted() {
86        // Discourage casual browsing :)
87        return true;
88    }
89
90    public function isWriteMode() {
91        return true;
92    }
93
94    public function getAllowedParams() {
95        return [
96            'repo' => [
97                ParamValidator::PARAM_TYPE => 'string',
98                ParamValidator::PARAM_REQUIRED => true,
99            ],
100            'rev' => [
101                ParamValidator::PARAM_TYPE => 'integer',
102                IntegerDef::PARAM_MIN => 1,
103                ParamValidator::PARAM_REQUIRED => true,
104            ]
105        ];
106    }
107
108    /**
109     * @inheritDoc
110     */
111    protected function getExamplesMessages() {
112        return [
113            'action=codeupdate&repo=MediaWiki&rev=42080'
114                => 'apihelp-codeupdate-example-1',
115        ];
116    }
117}