2015-12-14 20:26:29 +00:00
|
|
|
<?php
|
|
|
|
|
2021-04-07 22:22:05 +00:00
|
|
|
namespace MediaWiki\Extension\Math;
|
|
|
|
|
|
|
|
use ParserOptions;
|
2015-12-14 20:26:29 +00:00
|
|
|
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
|
|
|
|
2021-04-07 22:22:05 +00:00
|
|
|
class WikibaseHook {
|
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',
|
2021-05-04 08:06:42 +00:00
|
|
|
'validator-factory-callback' => static 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;
|
|
|
|
},
|
2021-05-04 08:06:42 +00:00
|
|
|
'parser-factory-callback' => static 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 );
|
|
|
|
},
|
2021-05-04 08:06:42 +00:00
|
|
|
'formatter-factory-callback' => static function ( $format, FormatterOptions $options ) {
|
2015-12-14 20:26:29 +00:00
|
|
|
return new MathFormatter( $format );
|
|
|
|
},
|
2021-05-04 08:06:42 +00:00
|
|
|
'rdf-builder-factory-callback' => static function (
|
2016-02-09 19:53:51 +00:00
|
|
|
$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',
|
2021-05-04 08:06:42 +00:00
|
|
|
'formatter-factory-callback' => static 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
|
|
|
}
|
|
|
|
|
|
|
|
}
|