Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
JCDataApi
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 4
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
20
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 isInternal
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace JsonConfig;
3
4use ApiBase;
5use ApiResult;
6use MediaWiki\Title\Title;
7use Wikimedia\ParamValidator\ParamValidator;
8
9/**
10 * Get localized json data, similar to Lua's mw.data.get() function
11 */
12class JCDataApi extends ApiBase {
13
14    public function execute() {
15        $params = $this->extractRequestParams();
16        $jct = JCSingleton::parseTitle( $params['title'], NS_DATA );
17        if ( !$jct ) {
18            $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
19        }
20
21        $data = JCSingleton::getContent( $jct );
22        if ( !$data ) {
23            $this->dieWithError(
24                [
25                    'apierror-invalidtitle',
26                    wfEscapeWikiText( Title::newFromLinkTarget( $jct )->getPrefixedText() )
27                ]
28            );
29        } elseif ( !method_exists( $data, 'getLocalizedData' ) ) {
30            $data = $data->getData();
31        } else {
32            /** @var JCDataContent $data */
33            // @phan-suppress-next-line PhanUndeclaredMethod
34            $data = $data->getSafeData( $data->getLocalizedData( $this->getLanguage() ) );
35        }
36
37        // Armor any API metadata in $data
38        $data = ApiResult::addMetadataToResultVars( (array)$data, is_object( $data ) );
39
40        $this->getResult()->addValue( null, $this->getModuleName(), $data );
41
42        $this->getMain()->setCacheMaxAge( 24 * 60 * 60 ); // seconds
43        $this->getMain()->setCacheMode( 'public' );
44    }
45
46    public function getAllowedParams() {
47        return [
48            'title' => [
49                ParamValidator::PARAM_TYPE => 'string',
50                ParamValidator::PARAM_REQUIRED => true,
51            ],
52        ];
53    }
54
55    protected function getExamplesMessages() {
56        return [
57            'action=jsondata&formatversion=2&format=jsonfm&title=Sample.tab'
58                => 'apihelp-jsondata-example-1',
59            'action=jsondata&formatversion=2&format=jsonfm&title=Sample.tab&uselang=fr'
60                => 'apihelp-jsondata-example-2',
61        ];
62    }
63
64    public function isInternal() {
65        return true;
66    }
67}