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