Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
MathMLWikidataHook
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 2
20
0.00% covered (danger)
0.00%
0 / 1
 onWikibaseRepoDataTypes
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
6
 onWikibaseClientDataTypes
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace MediaWiki\Extension\MathSearch\Wikidata\MathML;
4
5use ValueFormatters\FormatterOptions;
6use Wikibase\Repo\Rdf\DedupeBag;
7use Wikibase\Repo\Rdf\EntityMentionListener;
8use Wikibase\Repo\Rdf\RdfVocabulary;
9use Wikibase\Repo\WikibaseRepo;
10use Wikimedia\Purtle\RdfWriter;
11
12class MathMLWikidataHook {
13
14    /**
15     * Add Datatype "MathML" to the Wikibase Repository
16     * @param array &$dataTypeDefinitions
17     */
18    public static function onWikibaseRepoDataTypes( array &$dataTypeDefinitions ) {
19        global $wgContentMathEnableWikibaseDataType;
20        if ( !$wgContentMathEnableWikibaseDataType ) {
21            return;
22        }
23
24        $dataTypeDefinitions['PT:mathml'] = [
25            'value-type' => 'string',
26            'validator-factory-callback' => static function () {
27                global $wgMathSearchContentTexMaxLength;
28                // load validator builders
29                $factory = WikibaseRepo::getDefaultValidatorBuilders();
30
31                // initialize an array with string validators
32                // returns an array of validators
33                // that add basic string validation such as preventing empty strings
34                $validators = $factory->buildStringValidators( $wgMathSearchContentTexMaxLength );
35                $validators[] = new MathMLValidator();
36
37                return $validators;
38            },
39            'formatter-factory-callback' => static function ( $format, FormatterOptions $options ) {
40                return new MathMLFormatter( $format );
41            },
42            'rdf-builder-factory-callback' => static function (
43                $mode, RdfVocabulary $vocab, RdfWriter $writer, EntityMentionListener $tracker,
44                DedupeBag $dedupe
45            ) {
46                return new MathMLRdfBuilder();
47            },
48        ];
49    }
50
51    /**
52     * Add Datatype "MathML" to the Wikibase Client
53     *
54     * @param array[] &$dataTypeDefinitions
55     */
56    public static function onWikibaseClientDataTypes( array &$dataTypeDefinitions ) {
57        global $wgContentMathEnableWikibaseDataType;
58
59        if ( !$wgContentMathEnableWikibaseDataType ) {
60            return;
61        }
62
63        $dataTypeDefinitions['PT:mathml'] = [
64            'value-type' => 'string',
65            'formatter-factory-callback' => static function ( $format, FormatterOptions $options ) {
66                return new MathMLFormatter( $format );
67            },
68        ];
69    }
70
71}