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