forked from wikimedia/mediawiki-extensions-Math
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MathWikidataHook.php
81 lines (71 loc) · 2.32 KB
/
MathWikidataHook.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
use ValueFormatters\FormatterOptions;
use ValueParsers\StringParser;
use Wikibase\Rdf\DedupeBag;
use Wikibase\Rdf\EntityMentionListener;
use Wikibase\Rdf\RdfVocabulary;
use Wikibase\Repo\Parsers\WikibaseStringValueNormalizer;
use Wikibase\Repo\WikibaseRepo;
use Wikimedia\Purtle\RdfWriter;
class MathWikidataHook {
/**
* Add Datatype "Math" to the Wikibase Repository
*/
public static function onWikibaseRepoDataTypes( array &$dataTypeDefinitions ) {
global $wgMathEnableWikibaseDataType;
if ( !$wgMathEnableWikibaseDataType ) {
return;
}
$dataTypeDefinitions['PT:math'] = [
'value-type' => 'string',
'validator-factory-callback' => function() {
// load validator builders
$factory = WikibaseRepo::getDefaultValidatorBuilders();
// initialize an array with string validators
// returns an array of validators
// that add basic string validation such as preventing empty strings
$validators = $factory->buildStringValidators();
$validators[] = new MathValidator();
return $validators;
},
'parser-factory-callback' => function( ParserOptions $options ) {
$repo = WikibaseRepo::getDefaultInstance();
$normalizer = new WikibaseStringValueNormalizer( $repo->getStringNormalizer() );
return new StringParser( $normalizer );
},
'formatter-factory-callback' => function( $format, FormatterOptions $options ) {
global $wgOut;
$styles = [ 'ext.math.scripts', 'ext.math.styles' ];
$wgOut->addModuleStyles( $styles );
return new MathFormatter( $format );
},
'rdf-builder-factory-callback' => function (
$mode,
RdfVocabulary $vocab,
RdfWriter $writer,
EntityMentionListener $tracker,
DedupeBag $dedupe
) {
return new MathMLRdfBuilder();
},
];
}
/*
* Add Datatype "Math" to the Wikibase Client
*/
public static function onWikibaseClientDataTypes( array &$dataTypeDefinitions ) {
global $wgMathEnableWikibaseDataType;
if ( !$wgMathEnableWikibaseDataType ) {
return;
}
$dataTypeDefinitions['PT:math'] = [
'value-type' => 'string',
'formatter-factory-callback' => function( $format, FormatterOptions $options ) {
global $wgOut;
$styles = [ 'ext.math.scripts', 'ext.math.styles' ];
$wgOut->addModuleStyles( $styles );
return new MathFormatter( $format );
},
];
}
}