2018-07-05 10:43:55 +00:00
|
|
|
<?php
|
|
|
|
|
2021-04-07 22:22:05 +00:00
|
|
|
namespace MediaWiki\Extension\Math;
|
|
|
|
|
|
|
|
use ParserOutput;
|
2018-07-05 10:43:55 +00:00
|
|
|
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 {
|
|
|
|
|
2020-12-17 20:53:28 +00:00
|
|
|
/** @var bool */
|
2018-07-05 10:43:55 +00:00
|
|
|
private $hasMath = false;
|
2021-04-07 22:22:05 +00:00
|
|
|
|
2018-07-05 10:43:55 +00:00
|
|
|
/**
|
|
|
|
* @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' ] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|