Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
JsonSchemaContentHandler | |
0.00% |
0 / 21 |
|
0.00% |
0 / 4 |
56 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
canBeUsedOn | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
fillParserOutput | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
20 | |||
getContentClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * JSON Schema Content Handler |
4 | * |
5 | * @file |
6 | * @ingroup Extensions |
7 | * @ingroup EventLogging |
8 | * |
9 | * @author Ori Livneh <ori@wikimedia.org> |
10 | */ |
11 | |
12 | namespace MediaWiki\Extension\EventLogging; |
13 | |
14 | use MediaWiki\Content\Content; |
15 | use MediaWiki\Content\JsonContentHandler; |
16 | use MediaWiki\Content\Renderer\ContentParseParams; |
17 | use MediaWiki\Html\Html; |
18 | use MediaWiki\Parser\ParserOutput; |
19 | use MediaWiki\Registration\ExtensionRegistry; |
20 | use MediaWiki\SyntaxHighlight\SyntaxHighlight; |
21 | use MediaWiki\Title\Title; |
22 | use MediaWiki\Xml\Xml; |
23 | |
24 | class JsonSchemaContentHandler extends JsonContentHandler { |
25 | |
26 | /** @inheritDoc */ |
27 | public function __construct( $modelId = 'JsonSchema' ) { |
28 | parent::__construct( $modelId ); |
29 | } |
30 | |
31 | /** @inheritDoc */ |
32 | public function canBeUsedOn( Title $title ) { |
33 | return $title->inNamespace( NS_SCHEMA ); |
34 | } |
35 | |
36 | /** |
37 | * Wraps HTML representation of content. |
38 | * |
39 | * If the schema already exists and if the SyntaxHighlight |
40 | * extension is installed, use it to render code snippets |
41 | * showing how to use schema. |
42 | * |
43 | * @see https://mediawiki.org/wiki/Extension:SyntaxHighlight_GeSHi |
44 | * |
45 | * @param Content $content |
46 | * @param ContentParseParams $cpoParams |
47 | * @param ParserOutput &$parserOutput The output object to fill (reference). |
48 | */ |
49 | protected function fillParserOutput( |
50 | Content $content, |
51 | ContentParseParams $cpoParams, |
52 | ParserOutput &$parserOutput |
53 | ) { |
54 | '@phan-var JsonSchemaContent $content'; |
55 | $page = $cpoParams->getPage(); |
56 | $revId = $cpoParams->getRevId(); |
57 | parent::fillParserOutput( $content, $cpoParams, $parserOutput ); |
58 | if ( $revId !== null && ExtensionRegistry::getInstance()->isLoaded( 'SyntaxHighlight' ) ) { |
59 | $html = ''; |
60 | foreach ( $content->getCodeSamples( $page->getDBkey(), $revId ) as $sample ) { |
61 | $lang = $sample['language']; |
62 | $code = $sample['code']; |
63 | $highlighted = SyntaxHighlight::highlight( $code, $lang )->getValue(); |
64 | $html .= Html::element( 'h2', |
65 | [], |
66 | wfMessage( $sample['header'] )->text() |
67 | ) . $highlighted; |
68 | } |
69 | // The glyph is '< >' from the icon font 'Entypo' (see ../modules). |
70 | $html = Xml::tags( 'div', [ 'class' => 'mw-json-schema-code-glyph' ], '' ) . |
71 | Xml::tags( 'div', [ 'class' => 'mw-json-schema-code-samples' ], $html ); |
72 | $parserOutput->setIndicator( 'schema-code-samples', $html ); |
73 | $parserOutput->addModules( [ 'ext.eventLogging.jsonSchema', 'ext.pygments' ] ); |
74 | $parserOutput->addModuleStyles( [ 'ext.eventLogging.jsonSchema.styles' ] ); |
75 | } |
76 | } |
77 | |
78 | /** @inheritDoc */ |
79 | protected function getContentClass() { |
80 | return JsonSchemaContent::class; |
81 | } |
82 | } |