Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Hooks
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 5
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onParserFirstCallInit
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 onSoftwareInfo
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 onWikibaseRepoDataTypes
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
6
 onWikibaseClientDataTypes
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace MediaWiki\Extension\Score;
4
5use MediaWiki\Config\Config;
6use MediaWiki\Hook\ParserFirstCallInitHook;
7use MediaWiki\Hook\SoftwareInfoHook;
8use MediaWiki\MainConfigNames;
9use MediaWiki\Parser\Parser;
10use ValueFormatters\FormatterOptions;
11use Wikibase\Repo\Rdf\DedupeBag;
12use Wikibase\Repo\Rdf\EntityMentionListener;
13use Wikibase\Repo\Rdf\NullEntityRdfBuilder;
14use Wikibase\Repo\Rdf\RdfVocabulary;
15use Wikibase\Repo\WikibaseRepo;
16use Wikimedia\Purtle\RdfWriter;
17
18class Hooks implements
19    ParserFirstCallInitHook,
20    SoftwareInfoHook
21{
22    private Config $config;
23
24    public function __construct(
25        Config $config
26    ) {
27        $this->config = $config;
28    }
29
30    /**
31     * @param Parser $parser
32     */
33    public function onParserFirstCallInit( $parser ) {
34        global $wgScoreTrim, $wgScoreUseSvg;
35        if ( $wgScoreUseSvg ) {
36            // For SVG, always set true
37            $wgScoreTrim = true;
38        }
39        if ( $wgScoreTrim === null ) {
40            // Default to if we use Image Magick, since it requires Image Magick.
41            $wgScoreTrim = $this->config->get( MainConfigNames::UseImageMagick );
42        }
43        $parser->setHook( 'score', [ Score::class, 'render' ] );
44    }
45
46    /** @inheritDoc */
47    public function onSoftwareInfo( &$software ) {
48        try {
49            $software[ '[https://lilypond.org/ LilyPond]' ] = Score::getLilypondVersion();
50        } catch ( ScoreException $ex ) {
51            // LilyPond executable can't found
52        }
53    }
54
55    /**
56     * Add Datatype "Musical notation" to the Wikibase Repository
57     * @param array[] &$dataTypeDefinitions
58     */
59    public static function onWikibaseRepoDataTypes( array &$dataTypeDefinitions ) {
60        global $wgMusicalNotationEnableWikibaseDataType;
61
62        if ( !$wgMusicalNotationEnableWikibaseDataType ) {
63            return;
64        }
65
66        $dataTypeDefinitions['PT:musical-notation'] = [
67            'value-type' => 'string',
68            'validator-factory-callback' => static function () {
69                global $wgScoreMaxLength;
70                // load validator builders
71                $factory = WikibaseRepo::getDefaultValidatorBuilders();
72                // initialize an array with string validators
73                // returns an array of validators
74                // that add basic string validation such as preventing empty strings
75                $validators = $factory->buildStringValidators( $wgScoreMaxLength );
76                // $validators[] = new ScoreValidator();
77                // TODO: Take out the validation out of Score
78                return $validators;
79            },
80            'formatter-factory-callback' => static function ( $format, FormatterOptions $options ) {
81                return new ScoreFormatter( $format );
82            },
83            'rdf-builder-factory-callback' => static function (
84                $mode,
85                RdfVocabulary $vocab,
86                RdfWriter $writer,
87                EntityMentionListener $tracker,
88                DedupeBag $dedupe
89            ) {
90                // TODO: Implement
91                return new NullEntityRdfBuilder();
92            },
93        ];
94    }
95
96    /**
97     * Add Datatype "Musical notation" to the Wikibase Client
98     * @param array[] &$dataTypeDefinitions
99     */
100    public static function onWikibaseClientDataTypes( array &$dataTypeDefinitions ) {
101        global $wgMusicalNotationEnableWikibaseDataType;
102        if ( !$wgMusicalNotationEnableWikibaseDataType ) {
103            return;
104        }
105        $dataTypeDefinitions['PT:musical-notation'] = [
106            'value-type' => 'string',
107            'formatter-factory-callback' => static function ( $format, FormatterOptions $options ) {
108                return new ScoreFormatter( $format );
109            },
110        ];
111    }
112
113}