Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
35 / 35 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
InputCheckFactory | |
100.00% |
35 / 35 |
|
100.00% |
5 / 5 |
9 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
newMathoidChecker | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
newRestbaseChecker | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
newLocalChecker | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
newDefaultChecker | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
5 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\Math\InputCheck; |
4 | |
5 | use MediaWiki\Config\ServiceOptions; |
6 | use MediaWiki\Extension\Math\MathRestbaseInterface; |
7 | use MediaWiki\Http\HttpRequestFactory; |
8 | use Psr\Log\LoggerInterface; |
9 | use Wikimedia\ObjectCache\WANObjectCache; |
10 | |
11 | class InputCheckFactory { |
12 | |
13 | public const CONSTRUCTOR_OPTIONS = [ |
14 | 'MathMathMLUrl', |
15 | 'MathLaTeXMLTimeout', |
16 | 'MathTexVCService' |
17 | ]; |
18 | /** @var string */ |
19 | private $url; |
20 | /** @var int */ |
21 | private $timeout; |
22 | /** @var WANObjectCache */ |
23 | private $cache; |
24 | /** @var HttpRequestFactory */ |
25 | private $httpFactory; |
26 | /** @var LoggerInterface */ |
27 | private $logger; |
28 | /** @var string */ |
29 | private $texVCmode; |
30 | |
31 | /** |
32 | * @param ServiceOptions $options |
33 | * @param WANObjectCache $cache |
34 | * @param HttpRequestFactory $httpFactory |
35 | * @param LoggerInterface $logger |
36 | */ |
37 | public function __construct( |
38 | ServiceOptions $options, |
39 | WANObjectCache $cache, |
40 | HttpRequestFactory $httpFactory, |
41 | LoggerInterface $logger |
42 | ) { |
43 | $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
44 | $this->url = $options->get( 'MathMathMLUrl' ); |
45 | $this->timeout = $options->get( 'MathLaTeXMLTimeout' ); |
46 | $this->texVCmode = $options->get( 'MathTexVCService' ); |
47 | $this->cache = $cache; |
48 | $this->httpFactory = $httpFactory; |
49 | $this->logger = $logger; |
50 | } |
51 | |
52 | /** |
53 | * @param string $input input string to be checked |
54 | * @param string $type type of input |
55 | * @param bool $purge whether to purge the cache |
56 | * @return MathoidChecker checker based on mathoid |
57 | */ |
58 | public function newMathoidChecker( string $input, string $type, bool $purge ): MathoidChecker { |
59 | return new MathoidChecker( |
60 | $this->cache, |
61 | $this->httpFactory, |
62 | $this->logger, |
63 | $this->url, |
64 | $this->timeout, |
65 | $input, |
66 | $type, |
67 | $purge |
68 | ); |
69 | } |
70 | |
71 | /** |
72 | * @param string $input input string to be checked |
73 | * @param string $type type of input |
74 | * @param MathRestbaseInterface|null &$restbaseInterface restbase interface which is used for remote communication |
75 | * @return RestbaseChecker checker based on communication with restbase interface |
76 | */ |
77 | public function newRestbaseChecker( string $input, string $type, |
78 | ?MathRestbaseInterface &$restbaseInterface = null ): RestbaseChecker { |
79 | return new RestbaseChecker( |
80 | $input, |
81 | $type, |
82 | $restbaseInterface |
83 | ); |
84 | } |
85 | |
86 | /** |
87 | * @param string $input input string to be checked |
88 | * @param string $type type of input (only 'tex') |
89 | * @param bool $purge whether to purge the cache |
90 | * @return LocalChecker checker based on php implementation of WikiTexVC within Math-extension |
91 | */ |
92 | public function newLocalChecker( string $input, string $type, bool $purge = false ): LocalChecker { |
93 | return new LocalChecker( |
94 | $this->cache, |
95 | $input, |
96 | $type, |
97 | $purge |
98 | ); |
99 | } |
100 | |
101 | /** |
102 | * Creates an instance of BaseChecker based on the configuration parameter for the texVC Service. |
103 | * By default, this sets the checker to the local PHP variant of WikiTexVC. |
104 | * |
105 | * @param string $input input string which is checked |
106 | * @param string $type input type, for some configurations this has to be 'tex' |
107 | * @param MathRestbaseInterface|null &$restbaseInterface restbase interface, |
108 | * only necessary when using 'restbase' configuration |
109 | * @param bool $purge whether to purge the cache |
110 | * @return BaseChecker a checker object which has the results of the check. |
111 | */ |
112 | public function newDefaultChecker( string $input, |
113 | string $type, |
114 | ?MathRestbaseInterface &$restbaseInterface = null, |
115 | bool $purge = false ): BaseChecker { |
116 | switch ( $this->texVCmode ) { |
117 | case "mathoid": |
118 | return $this->newMathoidChecker( $input, $type, $purge ); |
119 | case "local": |
120 | return $this->newLocalChecker( $input, $type, $purge ); |
121 | case "restbase": |
122 | default: |
123 | return $this->newRestbaseChecker( $input, $type, $restbaseInterface ); |
124 | } |
125 | } |
126 | } |