2015-12-14 20:26:29 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use ValueFormatters\FormatterOptions;
|
|
|
|
use ValueParsers\StringParser;
|
|
|
|
use Wikibase\Repo\Parsers\WikibaseStringValueNormalizer;
|
2020-06-11 14:43:40 +00:00
|
|
|
use Wikibase\Repo\Rdf\DedupeBag;
|
|
|
|
use Wikibase\Repo\Rdf\EntityMentionListener;
|
|
|
|
use Wikibase\Repo\Rdf\RdfVocabulary;
|
2015-12-14 20:26:29 +00:00
|
|
|
use Wikibase\Repo\WikibaseRepo;
|
2016-02-09 19:53:51 +00:00
|
|
|
use Wikimedia\Purtle\RdfWriter;
|
2015-12-14 20:26:29 +00:00
|
|
|
|
2019-02-08 18:48:30 +00:00
|
|
|
class MathWikibaseHook {
|
2016-01-12 20:21:46 +00:00
|
|
|
|
|
|
|
/**
|
2015-12-14 20:26:29 +00:00
|
|
|
* Add Datatype "Math" to the Wikibase Repository
|
2018-05-15 15:50:08 +00:00
|
|
|
* @param array[] &$dataTypeDefinitions
|
2015-12-14 20:26:29 +00:00
|
|
|
*/
|
|
|
|
public static function onWikibaseRepoDataTypes( array &$dataTypeDefinitions ) {
|
2016-01-12 20:21:46 +00:00
|
|
|
global $wgMathEnableWikibaseDataType;
|
|
|
|
|
|
|
|
if ( !$wgMathEnableWikibaseDataType ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-12 20:53:25 +00:00
|
|
|
$dataTypeDefinitions['PT:math'] = [
|
2015-12-14 20:26:29 +00:00
|
|
|
'value-type' => 'string',
|
2017-06-20 07:11:57 +00:00
|
|
|
'validator-factory-callback' => function () {
|
2015-12-14 20:26:29 +00:00
|
|
|
// 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;
|
|
|
|
},
|
2017-06-20 07:11:57 +00:00
|
|
|
'parser-factory-callback' => function ( ParserOptions $options ) {
|
2021-03-04 13:37:41 +00:00
|
|
|
$normalizer = new WikibaseStringValueNormalizer( WikibaseRepo::getStringNormalizer() );
|
2015-12-14 20:26:29 +00:00
|
|
|
return new StringParser( $normalizer );
|
|
|
|
},
|
2017-06-20 07:11:57 +00:00
|
|
|
'formatter-factory-callback' => function ( $format, FormatterOptions $options ) {
|
2015-12-14 20:26:29 +00:00
|
|
|
return new MathFormatter( $format );
|
|
|
|
},
|
2016-02-09 19:53:51 +00:00
|
|
|
'rdf-builder-factory-callback' => function (
|
|
|
|
$mode,
|
|
|
|
RdfVocabulary $vocab,
|
|
|
|
RdfWriter $writer,
|
|
|
|
EntityMentionListener $tracker,
|
|
|
|
DedupeBag $dedupe
|
|
|
|
) {
|
|
|
|
return new MathMLRdfBuilder();
|
|
|
|
},
|
2016-04-12 20:53:25 +00:00
|
|
|
];
|
2015-12-14 20:26:29 +00:00
|
|
|
}
|
|
|
|
|
2017-08-09 20:56:07 +00:00
|
|
|
/**
|
2015-12-14 20:26:29 +00:00
|
|
|
* Add Datatype "Math" to the Wikibase Client
|
2018-05-15 15:50:08 +00:00
|
|
|
* @param array[] &$dataTypeDefinitions
|
2015-12-14 20:26:29 +00:00
|
|
|
*/
|
|
|
|
public static function onWikibaseClientDataTypes( array &$dataTypeDefinitions ) {
|
2016-01-12 20:21:46 +00:00
|
|
|
global $wgMathEnableWikibaseDataType;
|
|
|
|
|
|
|
|
if ( !$wgMathEnableWikibaseDataType ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-12 20:53:25 +00:00
|
|
|
$dataTypeDefinitions['PT:math'] = [
|
2015-12-14 20:26:29 +00:00
|
|
|
'value-type' => 'string',
|
2017-06-20 07:11:57 +00:00
|
|
|
'formatter-factory-callback' => function ( $format, FormatterOptions $options ) {
|
2015-12-14 20:26:29 +00:00
|
|
|
return new MathFormatter( $format );
|
|
|
|
},
|
2016-04-12 20:53:25 +00:00
|
|
|
];
|
2015-12-14 20:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|