Merge "Add math related styles and script via appropriate hook"
This commit is contained in:
commit
2eb412663b
|
@ -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"
|
||||
|
|
|
@ -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' ] );
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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 );
|
||||
},
|
||||
];
|
||||
|
|
|
@ -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';
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue