2015-12-14 20:26:29 +00:00
|
|
|
<?php
|
|
|
|
|
2021-04-07 22:22:05 +00:00
|
|
|
namespace MediaWiki\Extension\Math;
|
|
|
|
|
2016-04-12 20:53:25 +00:00
|
|
|
use DataValues\StringValue;
|
2021-04-07 22:22:05 +00:00
|
|
|
use InvalidArgumentException;
|
2021-02-09 18:39:18 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2016-04-12 20:53:25 +00:00
|
|
|
use ValueValidators\Error;
|
2015-12-14 20:26:29 +00:00
|
|
|
use ValueValidators\Result;
|
|
|
|
use ValueValidators\ValueValidator;
|
|
|
|
|
2018-05-18 18:01:05 +00:00
|
|
|
/**
|
|
|
|
* @author Duc Linh Tran
|
|
|
|
* @author Julian Hilbig
|
|
|
|
* @author Moritz Schubotz
|
|
|
|
*/
|
2015-12-14 20:26:29 +00:00
|
|
|
class MathValidator implements ValueValidator {
|
|
|
|
|
|
|
|
/**
|
2020-05-12 16:41:04 +00:00
|
|
|
* Validates a value with MediaWiki\Extension\Math\InputCheck\RestbaseChecker
|
2015-12-14 20:26:29 +00:00
|
|
|
*
|
2018-05-18 18:01:05 +00:00
|
|
|
* @param StringValue $value The value to validate
|
2015-12-14 20:26:29 +00:00
|
|
|
*
|
2021-04-07 22:22:05 +00:00
|
|
|
* @return Result
|
2018-05-18 18:01:05 +00:00
|
|
|
* @throws InvalidArgumentException if not called with a StringValue
|
2015-12-14 20:26:29 +00:00
|
|
|
*/
|
|
|
|
public function validate( $value ) {
|
|
|
|
if ( !( $value instanceof StringValue ) ) {
|
2018-05-18 18:01:05 +00:00
|
|
|
throw new InvalidArgumentException( '$value must be a StringValue' );
|
2015-12-14 20:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get input String from value
|
|
|
|
$tex = $value->getValue();
|
2021-05-14 12:39:25 +00:00
|
|
|
$checker = MediaWikiServices::getInstance()
|
|
|
|
->getService( 'Math.CheckerFactory' )
|
|
|
|
->newMathoidChecker( $tex, 'tex' );
|
|
|
|
|
2017-06-20 07:11:57 +00:00
|
|
|
if ( $checker->isValid() ) {
|
2015-12-14 20:26:29 +00:00
|
|
|
return Result::newSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TeX string is not valid
|
|
|
|
return Result::newError(
|
2016-04-12 20:53:25 +00:00
|
|
|
[
|
2019-12-21 15:51:10 +00:00
|
|
|
Error::newError( '', null, 'malformed-value', [ $checker->getError() ] )
|
2016-04-12 20:53:25 +00:00
|
|
|
]
|
2015-12-14 20:26:29 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see ValueValidator::setOptions()
|
|
|
|
*
|
|
|
|
* @param array $options
|
|
|
|
*/
|
|
|
|
public function setOptions( array $options ) {
|
|
|
|
// Do nothing. This method shouldn't even be in the interface.
|
|
|
|
}
|
|
|
|
}
|