Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
MathEngineBaseX
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 5
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
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 / 18
0.00% covered (danger)
0.00%
0 / 1
56
 processMathResults
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
 getPostData
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 update
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3use MediaWiki\Logger\LoggerFactory;
4
5/**
6 * MediaWiki MathSearch extension
7 *
8 * (c) 2014 Moritz Schubotz
9 * GPLv2 license; info in main package.
10 *
11 * @file
12 * @ingroup extensions
13 */
14class MathEngineBaseX extends MathEngineRest {
15
16    /** @var string */
17    protected $type = "mws";
18
19    function __construct( $query = null ) {
20        global $wgMathSearchBaseXBackendUrl;
21        parent::__construct( $query, $wgMathSearchBaseXBackendUrl . 'mwsquery' );
22    }
23
24    /**
25     * TODO: Add error handling.
26     * @param string $res
27     * @param int $numProcess
28     * @return bool
29     */
30    protected function processResults( $res, $numProcess ) {
31        $jsonResult = json_decode( $res ?? '' );
32        if ( $jsonResult && json_last_error() === JSON_ERROR_NONE ) {
33            if ( $jsonResult->success && $jsonResult->response ) {
34                // $xmlObject = new XmlTypeCheck( $jsonResult->response, null, false );
35                try {
36                    $xRes = new SimpleXMLElement( $jsonResult->response );
37                } catch ( Exception $e ) {
38                    global $wgOut;
39                    $wgOut->addWikiTextAsInterface( "invalid XML <code>{$jsonResult->response}</code>" );
40                    return false;
41                }
42                if ( $xRes->run->result ) {
43                    $this->processMathResults( $xRes );
44                    return true;
45                } else {
46                    global $wgOut;
47                    $wgOut->addWikiTextAsInterface( "Result was empty." );
48                    return false;
49                }
50            } else {
51                global $wgOut;
52                $wgOut->addWikiTextAsInterface( "<code>{$jsonResult->response}</code>" );
53                return false;
54            }
55        } else {
56            return false;
57        }
58    }
59
60    /**
61     * @param SimpleXMLElement $xmlRoot
62     */
63    function processMathResults( $xmlRoot ) {
64        foreach ( $xmlRoot->run->result->children() as $page ) {
65            $attrs = $page->attributes();
66            $uri = explode( "#", $attrs["id"] );
67            if ( count( $uri ) != 2 ) {
68                LoggerFactory::getInstance( 'MathSearch' )->error( 'Can not parse' . $attrs['id'] );
69                continue;
70            }
71            $revisionID = $uri[0];
72            $AnchorID = $uri[1];
73            $this->relevanceMap[] = $revisionID;
74            $substarr = [];
75            // TODO: Add hit support.
76            $this->resultSet[(string)$revisionID][(string)$AnchorID][] =
77                [ "xpath" => (string)$attrs["xpath"], "mappings" => $substarr ];
78        }
79        $this->relevanceMap = array_unique( $this->relevanceMap );
80    }
81
82    function getPostData( $numProcess ) {
83        return json_encode( [ "type" => $this->type, "query" => $this->query->getCQuery() ] );
84    }
85
86    function update( $harvest = "", array $delte = [] ) {
87        global $wgMathSearchBaseXBackendUrl;
88        $json_payload = json_encode( [ "harvest" => $harvest, "delete" => $delte ] );
89        try {
90            $res = self::doPost( $wgMathSearchBaseXBackendUrl . 'update', $json_payload );
91            if ( $res ) {
92                $resJson = json_decode( $res );
93                if ( $resJson->success == true ) {
94                    return true;
95                } else {
96                    LoggerFactory::getInstance( 'MathSearch' )->warning( 'harvest update failed' .
97                        var_export( $resJson, true ) );
98                }
99            }
100        } catch ( Exception $e ) {
101            LoggerFactory::getInstance( 'MathSearch' )
102                ->warning( 'Harvest update failed: {exception}',
103                    [ 'exception' => $e->getMessage() ] );
104        }
105        return false;
106    }
107}