Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
MathSearchTerm
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 13
462
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setRel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getExpr
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setExpr
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 doSearch
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
90
 getResultSet
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRelevanceMap
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRevisionResult
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3use MediaWiki\MediaWikiServices;
4
5class MathSearchTerm {
6
7    public const TYPE_TEXT = 0;
8    public const TYPE_MATH = 1;
9    public const TYPE_XMATH = 2;
10    public const REL_AND = 0;
11    public const REL_OR = 1;
12    public const REL_NAND = 2;
13    public const REL_NOR = 3;
14
15    /** @var int */
16    private $key = 0;
17    /** @var int */
18    private $rel = 0;
19    /** @var int */
20    private $type = 0;
21    /** @var string */
22    private $expr = '';
23    /** @var int[] */
24    private $relevanceMap = [];
25    /** @var array<int,SearchResult|array<string,array[]>> */
26    private $resultSet = [];
27
28    /**
29     * @param int $i
30     * @param int $rel
31     * @param int $type
32     * @param string $expr
33     */
34    function __construct( $i, $rel, $type, $expr ) {
35        $this->key  = $i;
36        $this->rel  = $rel;
37        $this->type = $type;
38        $this->expr = $expr;
39    }
40
41    /**
42     * @return int
43     */
44    public function getKey() {
45        return $this->key;
46    }
47
48    /**
49     * @param int $key
50     */
51    public function setKey( $key ) {
52        $this->key = $key;
53    }
54
55    /**
56     * @return int
57     */
58    public function getRel() {
59        return $this->rel;
60    }
61
62    /**
63     * @param int $rel
64     */
65    public function setRel( $rel ) {
66        $this->rel = $rel;
67    }
68
69    /**
70     * @return int
71     */
72    public function getType() {
73        return $this->type;
74    }
75
76    /**
77     * @param int $type
78     */
79    public function setType( $type ) {
80        $this->type = $type;
81    }
82
83    /**
84     * @return string
85     */
86    public function getExpr() {
87        return $this->expr;
88    }
89
90    /**
91     * @param string $expr
92     */
93    public function setExpr( $expr ) {
94        $this->expr = $expr;
95    }
96
97    public function doSearch( MathEngineRest $backend ) {
98        $backend->resetResults();
99        switch ( $this->getType() ) {
100            case self::TYPE_TEXT:
101                $search = MediaWikiServices::getInstance()->getSearchEngineFactory()->create( "CirrusSearch" );
102                $search->setLimitOffset( 10000 );
103                $sres = $search->searchText( $this->getExpr() );
104                if ( $sres ) {
105                    /** @var SearchResult $tres */
106                    foreach ( $sres as $tres ) {
107                        $revisionID = $tres->getTitle()->getLatestRevID();
108                        $this->resultSet[(string)$revisionID] = $tres;
109                        $this->relevanceMap[] = $revisionID;
110                    }
111                    return true;
112                }
113
114                return false;
115
116            case self::TYPE_MATH:
117                $query = new MathQueryObject( $this->getExpr() );
118                $cQuery = $query->getCQuery();
119                if ( $cQuery ) {
120                    $backend->setQuery( $query );
121                    if ( !$backend->postQuery() ) {
122                        return false;
123                    }
124                    $this->relevanceMap = $backend->getRelevanceMap();
125                    $this->resultSet = $backend->getResultSet();
126                } else {
127                    return false;
128                }
129                break;
130            case self::TYPE_XMATH:
131                $query = new MathQueryObject( '' );
132                $query->setXQuery( $this->getExpr() );
133                $backend = new MathEngineBaseX( $query );
134                $backend->setType( 'xquery' );
135                if ( !$backend->postQuery() ) {
136                    return false;
137                }
138                $this->relevanceMap = $backend->getRelevanceMap();
139                $this->resultSet = $backend->getResultSet();
140        }
141    }
142
143    /**
144     * @return array<int,SearchResult|array<string,array[]>>
145     */
146    public function getResultSet() {
147        return $this->resultSet;
148    }
149
150    /**
151     * @return int[]
152     */
153    public function getRelevanceMap() {
154        return $this->relevanceMap;
155    }
156
157    /**
158     * @param int $revisionId
159     * @return SearchResult|array<string,array[]>
160     */
161    public function getRevisionResult( $revisionId ) {
162        return $this->resultSet[$revisionId] ?? [];
163    }
164
165}