Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ContentMathWikidataHook
0.00% covered (danger)
0.00%
0 / 35
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 / 25
0.00% covered (danger)
0.00%
0 / 1
6
 onWikibaseClientDataTypes
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace MediaWiki\Extension\MathSearch\Wikidata\Content;
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 ContentMathWikidataHook {
13
14    /**
15     * Add Datatype "ContentMath" 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:contentmath'] = [
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 ContentMathValidator();
36
37                return $validators;
38            },
39            'formatter-factory-callback' => static function ( $format, FormatterOptions $options ) {
40                global $wgOut;
41                $styles = [ 'ext.math.desktop.styles', 'ext.math.scripts', 'ext.math.styles' ];
42                $wgOut->addModuleStyles( $styles );
43
44                return new ContentMathFormatter( $format );
45            },
46            'rdf-builder-factory-callback' => static function (
47                $mode,
48                RdfVocabulary $vocab,
49                RdfWriter $writer,
50                EntityMentionListener $tracker,
51                DedupeBag $dedupe
52            ) {
53                return new ContentMathMLRdfBuilder();
54            },
55        ];
56    }
57
58    /**
59     * Add Datatype "ContentMath" to the Wikibase Client
60     *
61     * @param array[] &$dataTypeDefinitions
62     */
63    public static function onWikibaseClientDataTypes( array &$dataTypeDefinitions ) {
64        global $wgContentMathEnableWikibaseDataType;
65
66        if ( !$wgContentMathEnableWikibaseDataType ) {
67            return;
68        }
69
70        $dataTypeDefinitions['PT:contentmath'] = [
71
72            'value-type'                 => 'string',
73            'formatter-factory-callback' => static function ( $format, FormatterOptions $options ) {
74                global $wgOut;
75                $styles = [ 'ext.math.desktop.styles', 'ext.math.scripts', 'ext.math.styles' ];
76                $wgOut->addModuleStyles( $styles );
77
78                return new ContentMathFormatter( $format );
79            },
80        ];
81    }
82
83}