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