Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
FuzzyLikeThis | |
0.00% |
0 / 27 |
|
0.00% |
0 / 8 |
132 | |
0.00% |
0 / 1 |
addFieldNames | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
setLikeText | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
setIgnoreTermFrequency | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
setFuzziness | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
setPrefixLength | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
setMaxQueryTerms | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
setAnalyzer | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
toArray | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\TtmServer; |
5 | |
6 | use Elastica\Query\AbstractQuery; |
7 | |
8 | /** |
9 | * NOTE: the following class has been copied from elastica 2.3.1 : |
10 | * https://github.com/ruflin/Elastica/blob/2.3.1/lib/Elastica/Query/FuzzyLikeThis.php |
11 | * (few modifications have been made to comply with phpcs rules used by this extension) |
12 | * It is intended to be used as a temporary workaround with the wmf extra |
13 | * elasticsearch plugin with elasticsearch 2.x. |
14 | * |
15 | * The MIT License (MIT) |
16 | * |
17 | * Copyright (c) 2014 Nicolas Ruflin |
18 | * |
19 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
20 | * of this software and associated documentation files (the "Software"), to deal |
21 | * in the Software without restriction, including without limitation the rights |
22 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
23 | * copies of the Software, and to permit persons to whom the Software is |
24 | * furnished to do so, subject to the following conditions: |
25 | * |
26 | * The above copyright notice and this permission notice shall be included in |
27 | * all copies or substantial portions of the Software. |
28 | * |
29 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
31 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
32 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
33 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
34 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
35 | * THE SOFTWARE. |
36 | * |
37 | * (c.f. https://github.com/ruflin/Elastica/blob/2.3.1/LICENSE.txt) |
38 | * Fuzzy Like This query. |
39 | * |
40 | * @author Raul Martinez, Jr <juneym@gmail.com> |
41 | * @license MIT |
42 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/1.7/query-dsl-flt-query.html * |
43 | * @since 2016.05 |
44 | * @ingroup TTMServer |
45 | */ |
46 | class FuzzyLikeThis extends AbstractQuery { |
47 | private array $fieldNames = []; |
48 | private string $likeText = ''; |
49 | private bool $ignoreTermFrequency = false; |
50 | private int $maxQueryTerms = 25; |
51 | private int $fuzziness = 2; |
52 | private int $prefixLength = 0; |
53 | private ?string $analyzer = null; |
54 | |
55 | public function addFieldNames( array $fieldNames ): self { |
56 | $this->fieldNames = $fieldNames; |
57 | return $this; |
58 | } |
59 | |
60 | public function setLikeText( string $text ): self { |
61 | $this->likeText = trim( $text ); |
62 | |
63 | return $this; |
64 | } |
65 | |
66 | public function setIgnoreTermFrequency( bool $ignoreTermFrequency ): self { |
67 | $this->ignoreTermFrequency = $ignoreTermFrequency; |
68 | |
69 | return $this; |
70 | } |
71 | |
72 | public function setFuzziness( int $value ): self { |
73 | $this->fuzziness = $value; |
74 | |
75 | return $this; |
76 | } |
77 | |
78 | public function setPrefixLength( int $value ): self { |
79 | $this->prefixLength = $value; |
80 | |
81 | return $this; |
82 | } |
83 | |
84 | public function setMaxQueryTerms( int $value ): self { |
85 | $this->maxQueryTerms = $value; |
86 | |
87 | return $this; |
88 | } |
89 | |
90 | public function setAnalyzer( string $text ): self { |
91 | $this->analyzer = trim( $text ); |
92 | |
93 | return $this; |
94 | } |
95 | |
96 | /** |
97 | * Converts fuzzy like this query to array. |
98 | * @return array Query array |
99 | * @see \Elastica\Query\AbstractQuery::toArray() |
100 | */ |
101 | public function toArray(): array { |
102 | $args = []; |
103 | if ( $this->fieldNames !== [] ) { |
104 | $args['fields'] = $this->fieldNames; |
105 | } |
106 | |
107 | if ( $this->analyzer ) { |
108 | $args['analyzer'] = $this->analyzer; |
109 | } |
110 | |
111 | $args['fuzziness'] = ( $this->fuzziness > 0 ) ? $this->fuzziness : 0; |
112 | |
113 | $args['like_text'] = $this->likeText; |
114 | $args['prefix_length'] = $this->prefixLength; |
115 | $args['ignore_tf'] = $this->ignoreTermFrequency; |
116 | $args['max_query_terms'] = $this->maxQueryTerms; |
117 | |
118 | $data = parent::toArray(); |
119 | $args = array_merge( $args, $data['fuzzy_like_this'] ); |
120 | |
121 | return [ 'fuzzy_like_this' => $args ]; |
122 | } |
123 | } |