Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 59 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
JCDataContent | |
0.00% |
0 / 59 |
|
0.00% |
0 / 8 |
462 | |
0.00% |
0 / 1 |
validateContent | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
isValidLicense | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
getLocalizedData | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
localizeData | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
renderDescription | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
renderLicense | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
getLicenseObject | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
renderSources | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | namespace JsonConfig; |
4 | |
5 | use Language; |
6 | use MediaWiki\Html\Html; |
7 | use MediaWiki\MediaWikiServices; |
8 | use MediaWiki\Page\PageReference; |
9 | use Parser; |
10 | use ParserOptions; |
11 | use stdClass; |
12 | |
13 | /** |
14 | * @package JsonConfig |
15 | */ |
16 | abstract class JCDataContent extends JCObjContent { |
17 | |
18 | /** |
19 | * Derived classes must implement this method to perform custom validation |
20 | * using the check(...) calls |
21 | */ |
22 | public function validateContent() { |
23 | if ( !$this->thorough() ) { |
24 | // We are not doing any modifications to the original, so no need to validate it |
25 | return; |
26 | } |
27 | |
28 | $this->test( 'license', JCValidators::isStringLine(), self::isValidLicense() ); |
29 | $this->testOptional( 'description', [ 'en' => '' ], JCValidators::isLocalizedString() ); |
30 | $this->testOptional( 'sources', '', JCValidators::isString() ); |
31 | } |
32 | |
33 | /** Returns a validator function to check if the value is a valid string |
34 | * @return callable |
35 | */ |
36 | public static function isValidLicense() { |
37 | return static function ( JCValue $v, array $path ) { |
38 | global $wgLang; |
39 | $allowedLicenses = MediaWikiServices::getInstance()->getMainConfig()->get( 'JsonConfigAllowedLicenses' ); |
40 | if ( !in_array( $v->getValue(), $allowedLicenses, true ) ) { |
41 | $v->error( 'jsonconfig-err-license', $path, |
42 | $wgLang->commaList( $allowedLicenses ) ); |
43 | return false; |
44 | } |
45 | return true; |
46 | }; |
47 | } |
48 | |
49 | /** |
50 | * Get data as localized for the given language |
51 | * @param Language $lang |
52 | * @return mixed |
53 | */ |
54 | public function getLocalizedData( Language $lang ) { |
55 | if ( !$this->isValid() ) { |
56 | return null; |
57 | } |
58 | $result = (object)[]; |
59 | $this->localizeData( $result, $lang ); |
60 | return $result; |
61 | } |
62 | |
63 | /** |
64 | * Resolve any override-specific localizations, and add it to $result |
65 | * @param stdClass $result |
66 | * @param Language $lang |
67 | */ |
68 | protected function localizeData( $result, Language $lang ) { |
69 | $data = $this->getData(); |
70 | if ( property_exists( $data, 'description' ) ) { |
71 | // @phan-suppress-next-line PhanTypeMismatchArgument |
72 | $result->description = JCUtils::pickLocalizedString( $data->description, $lang ); |
73 | } |
74 | $license = $this->getLicenseObject(); |
75 | if ( $license ) { |
76 | $text = $license['text']->inLanguage( $lang )->plain(); |
77 | $result->license = (object)[ |
78 | 'code' => $license['code'], |
79 | 'text' => $text, |
80 | 'url' => $license['url']->inLanguage( $lang )->plain(), |
81 | ]; |
82 | } |
83 | if ( property_exists( $data, 'sources' ) ) { |
84 | $result->sources = $data->sources; |
85 | } |
86 | } |
87 | |
88 | public function renderDescription( $lang ) { |
89 | $description = $this->getField( 'description' ); |
90 | if ( !$description || $description->error() ) { |
91 | return ''; |
92 | } |
93 | |
94 | $description = JCUtils::pickLocalizedString( $description->getValue(), $lang ); |
95 | return Html::element( 'p', [ 'class' => 'mw-jsonconfig-description' ], $description ); |
96 | } |
97 | |
98 | /** |
99 | * Renders license HTML, including optional "or later version" clause |
100 | * <a href="...">Creative Commons 1.0</a>, or later version |
101 | * @return string |
102 | */ |
103 | public function renderLicense() { |
104 | $license = $this->getLicenseObject(); |
105 | if ( !$license ) { |
106 | return ''; |
107 | } |
108 | |
109 | $text = Html::element( 'a', [ |
110 | 'href' => $license['url']->plain() |
111 | ], $license['text']->plain() ); |
112 | $text = wfMessage( 'jsonconfig-license' )->rawParams( $text )->parse(); |
113 | return Html::rawElement( 'p', [ 'class' => 'mw-jsonconfig-license' ], $text ); |
114 | } |
115 | |
116 | /** |
117 | * Get the license object of the content. |
118 | * license code is identifier from https://spdx.org/licenses/ |
119 | * @return array|false code=>license code text=>license name url=>license URL |
120 | */ |
121 | public function getLicenseObject() { |
122 | $license = $this->getField( 'license' ); |
123 | if ( !$license || $license->error() ) { |
124 | return false; |
125 | } |
126 | |
127 | // should be a valid license identifier as in https://spdx.org/licenses/ |
128 | $code = $license->getValue(); |
129 | return [ |
130 | 'code' => $code, |
131 | 'text' => wfMessage( 'jsonconfig-license-name-' . $code ), |
132 | 'url' => wfMessage( 'jsonconfig-license-url-' . $code ), |
133 | ]; |
134 | } |
135 | |
136 | public function renderSources( Parser $parser, PageReference $page, $revId, ParserOptions $options ) { |
137 | $sources = $this->getField( 'sources' ); |
138 | if ( !$sources || $sources->error() ) { |
139 | return ''; |
140 | } |
141 | |
142 | $markup = $sources->getValue(); |
143 | return Html::rawElement( 'p', [ 'class' => 'mw-jsonconfig-sources' ], |
144 | $parser->parse( $markup, $page, $options, true, true, $revId )->getRawText() ); |
145 | } |
146 | } |