Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 69 |
|
0.00% |
0 / 18 |
CRAP | |
0.00% |
0 / 1 |
MathosphereDriver | |
0.00% |
0 / 69 |
|
0.00% |
0 / 18 |
992 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
newFromWikitext | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
setWikiText | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getWikiText | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getLanguage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setLanguage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getTitle | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setTitle | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getVersion | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getError | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
analyze | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
processResults | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
42 | |||
addIdentifierDefinitionTuple | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
doPost | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
6 | |||
getBackendUrl | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getPostData | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
checkBackend | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
42 | |||
getRelations | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | use MediaWiki\Logger\LoggerFactory; |
4 | use MediaWiki\MediaWikiServices; |
5 | use MediaWiki\Revision\SlotRecord; |
6 | |
7 | class MathosphereDriver { |
8 | |
9 | /** @var string */ |
10 | private $wikiText; |
11 | /** @var string */ |
12 | private $language = "en"; |
13 | /** @var string */ |
14 | private $title = ""; |
15 | /** @var string|null */ |
16 | private $version; |
17 | private $error; |
18 | /** @var bool|null */ |
19 | private $success; |
20 | private $relations = []; |
21 | private $identifiers = []; |
22 | |
23 | function __construct( $revisionId = null ) { |
24 | if ( $revisionId !== null ) { |
25 | $revisionRecord = MediaWikiServices::getInstance() |
26 | ->getRevisionLookup() |
27 | ->getRevisionById( $revisionId ); |
28 | /** @var TextContent $content */ |
29 | '@phan-var TextContent $content'; |
30 | $content = $revisionRecord->getContent( SlotRecord::MAIN ); |
31 | $this->wikiText = $content->getText(); |
32 | } |
33 | } |
34 | |
35 | public static function newFromWikitext( $wikiText ) { |
36 | $instance = new MathosphereDriver(); |
37 | $instance->setWikiText( $wikiText ); |
38 | return $instance; |
39 | } |
40 | |
41 | public function setWikiText( $wikiText ) { |
42 | $this->wikiText = $wikiText; |
43 | } |
44 | |
45 | public function getWikiText() { |
46 | return $this->wikiText; |
47 | } |
48 | |
49 | /** |
50 | * @return string |
51 | */ |
52 | public function getLanguage() { |
53 | return $this->language; |
54 | } |
55 | |
56 | /** |
57 | * @param string $language |
58 | */ |
59 | public function setLanguage( $language ) { |
60 | $this->language = $language; |
61 | } |
62 | |
63 | /** |
64 | * @return string |
65 | */ |
66 | public function getTitle() { |
67 | return $this->title; |
68 | } |
69 | |
70 | /** |
71 | * @param string $title |
72 | */ |
73 | public function setTitle( $title ) { |
74 | $this->title = $title; |
75 | } |
76 | |
77 | /** |
78 | * @return string|null |
79 | */ |
80 | public function getVersion() { |
81 | return $this->version; |
82 | } |
83 | |
84 | /** |
85 | * @return mixed |
86 | */ |
87 | public function getError() { |
88 | return $this->error; |
89 | } |
90 | |
91 | public function analyze() { |
92 | return $this->processResults( self::doPost( $this->getBackendUrl() . "/AnalyzeWikiText", |
93 | $this->getPostData() ) ); |
94 | } |
95 | |
96 | protected function processResults( $res ) { |
97 | $jsonResult = json_decode( $res ); |
98 | if ( $jsonResult && |
99 | json_last_error() === JSON_ERROR_NONE && |
100 | isset( $jsonResult->success ) |
101 | ) { |
102 | $this->success = $jsonResult->success; |
103 | if ( $this->success ) { |
104 | foreach ( $jsonResult->relations as $r ) { |
105 | $this->addIdentifierDefinitionTuple( $r ); |
106 | } |
107 | return true; |
108 | } |
109 | } |
110 | // TODO: Implement error handling |
111 | return false; |
112 | } |
113 | |
114 | private function addIdentifierDefinitionTuple( $r ) { |
115 | if ( !isset( $this->relations[$r->identifier] ) ) { |
116 | $this->relations[$r->identifier] = []; |
117 | } |
118 | $this->relations[$r->identifier][] = $r; |
119 | } |
120 | |
121 | protected static function doPost( $url, $postData ) { |
122 | $options = [ |
123 | "postData" => $postData, |
124 | "timeout" => 60, |
125 | "method" => 'POST' |
126 | ]; |
127 | $req = MMediaWikiServices::getInstance()->getHttpRequestFactory() |
128 | ->create( $url, $options, __METHOD__ ); |
129 | $status = $req->execute(); |
130 | |
131 | if ( $status->isOK() ) { |
132 | return $req->getContent(); |
133 | } else { |
134 | $errors = $status->getErrorsByType( 'error' ); |
135 | $logger = LoggerFactory::getInstance( 'http' ); |
136 | $logger->warning( $status->getWikiText(), |
137 | [ 'error' => $errors, 'caller' => __METHOD__, 'content' => $req->getContent() ] ); |
138 | return false; |
139 | } |
140 | } |
141 | |
142 | /** |
143 | * @return string |
144 | */ |
145 | public function getBackendUrl() { |
146 | $config = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'main' ); |
147 | return $config->get( "MathSearchBaseXBackendUrl" ) . 'api'; |
148 | } |
149 | |
150 | protected function getPostData() { |
151 | $post = [ |
152 | "wikitext" => $this->wikiText, |
153 | "title" => $this->title |
154 | ]; |
155 | return json_encode( $post ); |
156 | } |
157 | |
158 | public function checkBackend() { |
159 | $res = MediaWikiServices::getInstance()->getHttpRequestFactory() |
160 | ->get( $this->getBackendUrl() . '/_info' ); |
161 | if ( $res ) { |
162 | $res = json_decode( $res ); |
163 | if ( $res && json_last_error() === JSON_ERROR_NONE ) { |
164 | if ( isset( $res->name ) && $res->name === 'mathosphere' ) { |
165 | $this->version = $res->version; |
166 | // Mathosphere version 3.0.0-SNAPSHOT is only version currently supported |
167 | return ( $this->version === "3.0.0-SNAPSHOT" ); |
168 | } |
169 | } |
170 | } |
171 | $logger = LoggerFactory::getInstance( 'MathSearch' ); |
172 | $logger->warning( "Mathosphere Backend server backend does not point to mathosphere.", [ |
173 | 'detail' => $res |
174 | ] ); |
175 | return false; |
176 | } |
177 | |
178 | /** |
179 | * @return array[] |
180 | */ |
181 | public function getRelations() { |
182 | return $this->relations; |
183 | } |
184 | } |