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