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