-
-
Notifications
You must be signed in to change notification settings - Fork 292
/
EngineTrait.php
177 lines (152 loc) · 4.09 KB
/
EngineTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
namespace TeamTNT\TNTSearch\Engines;
use Exception;
use TeamTNT\TNTSearch\Connectors\FileSystemConnector;
use TeamTNT\TNTSearch\Connectors\MySqlConnector;
use TeamTNT\TNTSearch\Connectors\PostgresConnector;
use TeamTNT\TNTSearch\Connectors\SQLiteConnector;
use TeamTNT\TNTSearch\Connectors\SqlServerConnector;
use TeamTNT\TNTSearch\Support\Collection;
use TeamTNT\TNTSearch\Support\TokenizerInterface;
trait EngineTrait
{
/**
* @return string
*/
public function getStoragePath()
{
return $this->config['storage'];
}
/**
* @param array $config
*
* @return FileSystemConnector|MySqlConnector|PostgresConnector|SQLiteConnector|SqlServerConnector
* @throws Exception
*/
public function createConnector(array $config)
{
if (!isset($config['driver'])) {
throw new Exception('A driver must be specified.');
}
switch ($config['driver']) {
case 'mysql':
return new MySqlConnector;
case 'pgsql':
return new PostgresConnector;
case 'sqlite':
return new SQLiteConnector;
case 'sqlsrv':
return new SqlServerConnector;
case 'filesystem':
return new FileSystemConnector;
}
throw new Exception("Unsupported driver [{$config['driver']}]");
}
public function query($query)
{
$this->query = $query;
}
public function disableOutput($value)
{
$this->disableOutput = $value;
}
public function setStemmer($stemmer)
{
$this->stemmer = $stemmer;
$this->updateInfoTable('stemmer', get_class($stemmer));
}
public function getPrimaryKey()
{
if (isset($this->primaryKey)) {
return $this->primaryKey;
}
return 'id';
}
public function stemText($text)
{
$stemmer = $this->getStemmer();
$words = $this->breakIntoTokens($text);
$stems = [];
foreach ($words as $word) {
$stems[] = $stemmer->stem($word);
}
return $stems;
}
public function getStemmer()
{
return $this->stemmer;
}
public function breakIntoTokens($text)
{
if ($this->decodeHTMLEntities) {
$text = html_entity_decode($text);
}
return $this->tokenizer->tokenize($text, $this->stopWords);
}
public function info($text)
{
if (!$this->disableOutput) {
echo $text . PHP_EOL;
}
}
public function setInMemory($value)
{
$this->inMemory = $value;
}
public function setIndex($index)
{
$this->index = $index;
}
/**
* @param TokenizerInterface $tokenizer
*/
public function setTokenizer(TokenizerInterface $tokenizer)
{
$this->tokenizer = $tokenizer;
$this->updateInfoTable('tokenizer', get_class($tokenizer));
}
public function update($id, $document)
{
$this->delete($id);
$this->insert($document);
}
public function insert($document)
{
$this->processDocument(new Collection($document));
$total = $this->totalDocumentsInCollection() + 1;
$this->updateInfoTable('total_documents', $total);
}
public function includePrimaryKey()
{
$this->excludePrimaryKey = false;
}
public function setPrimaryKey($primaryKey)
{
$this->primaryKey = $primaryKey;
}
public function countWordInWordList($word)
{
$res = $this->getWordFromWordList($word);
if ($res) {
return $res['num_hits'];
}
return 0;
}
public function asYouType($value)
{
$this->asYouType = $value;
}
public function fuzziness($value)
{
$this->fuzziness = $value;
}
public function setLanguage($language = 'no')
{
$class = 'TeamTNT\\TNTSearch\\Stemmer\\' . ucfirst(strtolower($language)) . 'Stemmer';
$this->setStemmer(new $class);
}
public function setDatabaseHandle($dbh)
{
$this->dbh = $dbh;
}
}