Add math related styles and script via appropriate hook

Create MathDataUpdater class that checks if statements
are using math and adds the stlyes required for math in that case.

This patch has no effect until
I5bc0622ee7338f3215d14e15331a0a1931ca1ae0
is merged.

After both changes are in effect
I0abd6acbfa12fd503d11476f0c8c8c8a8a851cdc
can be merged.

Bug: T173949
Change-Id: I0e24bbb53e6e01d549f534744780ca1afc49fdd7
This commit is contained in:
Moritz Schubotz (physikerwelt) 2018-07-05 12:43:55 +02:00
parent cc33a3092d
commit f0c8840148
No known key found for this signature in database
GPG Key ID: 73D26C61BAB32E94
4 changed files with 153 additions and 7 deletions

View File

@ -30,7 +30,8 @@
"MathFormatter": "src/MathFormatter.php",
"MathWikidataHook": "src/MathWikidataHook.php",
"MathMLRdfBuilder": "src/MathMLRdfBuilder.php",
"MathPng": "src/MathPng.php"
"MathPng": "src/MathPng.php",
"MathDataUpdater": "src/MathDataUpdater.php"
},
"DefaultUserOptions": {
"math": "mathml"

55
src/MathDataUpdater.php Normal file
View File

@ -0,0 +1,55 @@
<?php
use Wikibase\DataModel\Services\Entity\PropertyDataTypeMatcher;
use Wikibase\DataModel\Statement\Statement;
use Wikibase\Repo\ParserOutput\StatementDataUpdater;
/**
* Add required styles for mathematical formulae to the ParserOutput.
*
* @license GPL-2.0-or-later
* @author Moritz Schubotz
*/
class MathDataUpdater implements StatementDataUpdater {
private $hasMath = false;
/**
* @var PropertyDataTypeMatcher
*/
private $propertyDataTypeMatcher;
/**
* @inheritDoc
*/
public function __construct( PropertyDataTypeMatcher $propertyDataTypeMatcher ) {
$this->propertyDataTypeMatcher = $propertyDataTypeMatcher;
}
/**
* Extract some data or do processing on a Statement during parsing.
*
* This method is normally invoked when processing a StatementList
* for all Statements on a StatementListProvider (e.g. an Item).
*
* @param Statement $statement
*/
public function processStatement( Statement $statement ) {
$propertyId = $statement->getPropertyId();
if ( $this->propertyDataTypeMatcher->isMatchingDataType( $propertyId, 'math' ) ) {
$this->hasMath = true;
}
}
/**
* Update extension data, properties or other data in ParserOutput.
* These updates are invoked when EntityContent::getParserOutput is called.
*
* @param ParserOutput $parserOutput
*/
public function updateParserOutput( ParserOutput $parserOutput ) {
if ( $this->hasMath ) {
$parserOutput->addModules( [ 'ext.math.scripts' ] );
$parserOutput->addModuleStyles( [ 'ext.math.styles' ] );
}
}
}

View File

@ -41,9 +41,6 @@ class MathWikidataHook {
return new StringParser( $normalizer );
},
'formatter-factory-callback' => function ( $format, FormatterOptions $options ) {
global $wgOut;
$wgOut->addModuleStyles( [ 'ext.math.styles' ] );
$wgOut->addModules( [ 'ext.math.scripts' ] );
return new MathFormatter( $format );
},
'rdf-builder-factory-callback' => function (
@ -72,9 +69,6 @@ class MathWikidataHook {
$dataTypeDefinitions['PT:math'] = [
'value-type' => 'string',
'formatter-factory-callback' => function ( $format, FormatterOptions $options ) {
global $wgOut;
$wgOut->addModuleStyles( [ 'ext.math.styles' ] );
$wgOut->addModules( [ 'ext.math.scripts' ] );
return new MathFormatter( $format );
},
];

View File

@ -0,0 +1,96 @@
<?php
use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\DataModel\Services\Entity\PropertyDataTypeMatcher;
use Wikibase\DataModel\Services\Lookup\PropertyDataTypeLookup;
/**
* Test the MathDataUpdater for Wikidata
*
* @covers MathDataUpdater
**
* @license GPL-2.0-or-later
*/
class MathDataUpdaterTest extends MediaWikiTestCase {
/**
* @var PropertyId
*/
private $mathProperty;
/**
* @var PropertyId
*/
private $otherProperty;
/**
* @inheritDoc
*/
protected function setUp() {
parent::setUp();
$this->mathProperty = new PropertyId( 'P' . DummyPropertyDataTypeLookup::$mathId );
$this->otherProperty = new PropertyId( 'P' . ( DummyPropertyDataTypeLookup::$mathId + 1 ) );
}
public function testNoMath() {
$matcher = new PropertyDataTypeMatcher( new DummyPropertyDataTypeLookup() );
$updater = new MathDataUpdater( $matcher );
$statement =
$this->getMockBuilder( Wikibase\DataModel\Statement\Statement::class )
->setMethods( [ 'getPropertyId' ] )
->disableOriginalConstructor()
->getMock();
$statement->method( 'getPropertyId' )->willReturn( $this->otherProperty );
/** @var Wikibase\DataModel\Statement\Statement $statement */
$updater->processStatement( $statement );
$parserOutput = $this->getMockBuilder( ParserOutput::class )->setMethods( [
'addModules',
'addModuleStyles',
] )->getMock();
$parserOutput->expects( $this->never() )->method( 'addModules' );
$parserOutput->expects( $this->never() )->method( 'addModuleStyles' );
/** @var ParserOutput $parserOutput */
$updater->updateParserOutput( $parserOutput );
}
public function testMath() {
$matcher = new PropertyDataTypeMatcher( new DummyPropertyDataTypeLookup() );
$updater = new MathDataUpdater( $matcher );
$statement =
$this->getMockBuilder( Wikibase\DataModel\Statement\Statement::class )
->setMethods( [ 'getPropertyId' ] )
->disableOriginalConstructor()
->getMock();
$statement->method( 'getPropertyId' )->willReturn( $this->mathProperty );
/** @var Wikibase\DataModel\Statement\Statement $statement */
$updater->processStatement( $statement );
$parserOutput = $this->getMockBuilder( ParserOutput::class )->setMethods( [
'addModules',
'addModuleStyles',
] )->getMock();
$parserOutput->expects( $this->once() )->method( 'addModules' );
$parserOutput->expects( $this->once() )->method( 'addModuleStyles' );
/** @var ParserOutput $parserOutput */
$updater->updateParserOutput( $parserOutput );
}
}
class DummyPropertyDataTypeLookup implements PropertyDataTypeLookup {
/**
* @var int
*/
public static $mathId = 1;
/**
* Returns the data type for the Property of which the id is given.
*
* @since 2.0
*
* @param \Wikibase\DataModel\Entity\PropertyId $propertyId
*
* @return string
* @throws \Wikibase\DataModel\Services\Lookup\PropertyDataTypeLookupException
*/
public function getDataTypeIdForProperty( \Wikibase\DataModel\Entity\PropertyId $propertyId ) {
return $propertyId->getNumericId() == self::$mathId ? 'math' : 'not-math';
}
}