Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 69 |
|
0.00% |
0 / 16 |
CRAP | |
0.00% |
0 / 1 |
MathoidDriver | |
0.00% |
0 / 69 |
|
0.00% |
0 / 16 |
930 | |
0.00% |
0 / 1 |
getVersion | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getError | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getSuccess | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getChecked | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getIdentifiers | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getRequiredPackages | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
texvcInfo | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
processResults | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
42 | |||
doPost | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
getBackendUrl | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getPostData | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
checkBackend | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
56 | |||
getSpeech | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getSvg | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFormat | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | use MediaWiki\Logger\LoggerFactory; |
4 | use MediaWiki\MediaWikiServices; |
5 | |
6 | class MathoidDriver { |
7 | |
8 | private $success; |
9 | private $checked; |
10 | private $identifiers; |
11 | private $requiredPackages; |
12 | private $q; |
13 | private $type; |
14 | private $version; |
15 | private $error; |
16 | |
17 | /** |
18 | * @return mixed |
19 | */ |
20 | public function getVersion() { |
21 | return $this->version; |
22 | } |
23 | |
24 | /** |
25 | * @return mixed |
26 | */ |
27 | public function getError() { |
28 | return $this->error; |
29 | } |
30 | |
31 | /** |
32 | * @param string $q |
33 | * @param string $type |
34 | */ |
35 | function __construct( $q = '', $type = 'tex' ) { |
36 | $this->q = $q; |
37 | if ( $type === 'pmml' ) { |
38 | $type = 'mml'; |
39 | } |
40 | $this->type = $type; |
41 | } |
42 | |
43 | /** |
44 | * @return mixed |
45 | */ |
46 | public function getSuccess() { |
47 | return $this->success; |
48 | } |
49 | |
50 | /** |
51 | * @return mixed |
52 | */ |
53 | public function getChecked() { |
54 | return $this->checked; |
55 | } |
56 | |
57 | /** |
58 | * @return mixed |
59 | */ |
60 | public function getIdentifiers() { |
61 | return $this->identifiers; |
62 | } |
63 | |
64 | /** |
65 | * @return mixed |
66 | */ |
67 | public function getRequiredPackages() { |
68 | return $this->requiredPackages; |
69 | } |
70 | |
71 | public function texvcInfo() { |
72 | $url = trim( $this->getBackendUrl(), " \n\t/" ) . "/texvcinfo"; |
73 | return $this->processResults( self::doPost( $url, $this->getPostData() ) ); |
74 | } |
75 | |
76 | protected function processResults( $res ) { |
77 | $jsonResult = json_decode( $res ); |
78 | if ( $jsonResult && json_last_error() === JSON_ERROR_NONE ) { |
79 | if ( isset( $jsonResult->texvcinfo ) ) { |
80 | // mathoid 0.2.9 |
81 | $texvcinfo = $jsonResult->texvcinfo; |
82 | } else { |
83 | // mathoid 0.2.10 |
84 | $texvcinfo = $jsonResult; |
85 | } |
86 | $this->success = $texvcinfo->success; |
87 | if ( $this->success ) { |
88 | $this->checked = $texvcinfo->checked; |
89 | $this->identifiers = $texvcinfo->identifiers; |
90 | $this->requiredPackages = $texvcinfo->requiredPackages; |
91 | } else { |
92 | if ( isset( $texvcinfo->error->error ) ) { |
93 | // mathoid 0.2.9 |
94 | $this->error = $texvcinfo->error; |
95 | } else { |
96 | // mathoid 0.2.10 |
97 | $this->error = $texvcinfo->detail->error; |
98 | } |
99 | } |
100 | return true; |
101 | } else { |
102 | return false; |
103 | } |
104 | } |
105 | |
106 | protected static function doPost( $url, $postData ) { |
107 | $options = [ |
108 | "postData" => $postData, |
109 | "timeout" => 60, |
110 | "method" => 'POST' |
111 | ]; |
112 | $req = MediaWikiServices::getInstance()->getHttpRequestFactory() |
113 | ->create( $url, $options, __METHOD__ ); |
114 | $status = $req->execute(); |
115 | |
116 | if ( $status->isOK() || $req->getStatus() === 400 ) { |
117 | return $req->getContent(); |
118 | } else { |
119 | $errors = $status->getErrorsByType( 'error' ); |
120 | $logger = LoggerFactory::getInstance( 'http' ); |
121 | $logger->warning( $status->getWikiText(), |
122 | [ 'error' => $errors, 'caller' => __METHOD__, 'content' => $req->getContent() ] ); |
123 | return false; |
124 | } |
125 | } |
126 | |
127 | /** |
128 | * @return string |
129 | */ |
130 | public function getBackendUrl() { |
131 | $config = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'main' ); |
132 | return $config->get( "MathMathMLUrl" ); |
133 | } |
134 | |
135 | protected function getPostData() { |
136 | $post = [ |
137 | 'q' => $this->q, |
138 | 'type' => $this->type |
139 | ]; |
140 | return wfArrayToCgi( $post ); |
141 | } |
142 | |
143 | public function checkBackend() { |
144 | $res = MediaWikiServices::getInstance() |
145 | ->getHttpRequestFactory() |
146 | ->get( ( $this->getBackendUrl() . '/_info' ) ); |
147 | if ( $res ) { |
148 | $res = json_decode( $res ); |
149 | if ( $res && json_last_error() === JSON_ERROR_NONE ) { |
150 | if ( isset( $res->name ) && $res->name === 'mathoid' ) { |
151 | $this->version = $res->version; |
152 | if ( preg_match( "/(0\\.2\\.(9|10)|1.0.0.*|0.6.1)/", $this->version ) ) { |
153 | return true; |
154 | } else { |
155 | echo $this->version; |
156 | return false; |
157 | } |
158 | } |
159 | } |
160 | } |
161 | $logger = LoggerFactory::getInstance( 'MathSearch' ); |
162 | $logger->warning( "Mathoid server backend does not point to mathoid.", [ |
163 | 'detail' => $res |
164 | ] ); |
165 | return false; |
166 | } |
167 | |
168 | public function getSpeech() { |
169 | return $this->getFormat( 'speech' ); |
170 | } |
171 | |
172 | public function getSvg() { |
173 | return $this->getFormat( 'svg' ); |
174 | } |
175 | |
176 | /** |
177 | * @param string $format |
178 | * @return bool|string |
179 | */ |
180 | private function getFormat( $format ) { |
181 | return self::doPost( $this->getBackendUrl() . '/' . $format, $this->getPostData() ); |
182 | } |
183 | } |