-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQuery.php
402 lines (343 loc) · 11.5 KB
/
Query.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<?php
namespace ArabyPHP\Query;
class Query
{
private $_fields = [];
private $_lexPatterns = [];
private $_lexReplacements = [];
private $_mode = 0;
/**
* Loads initialize values.
*/
public function __construct()
{
$xml = simplexml_load_file(__DIR__.'/data/ArQuery.xml');
foreach ($xml->xpath("//preg_replace[@function='__construct']/pair")
as $pair) {
array_push($this->_lexPatterns, (string) $pair->search);
array_push($this->_lexReplacements, (string) $pair->replace);
}
}
/**
* Setting value for $_fields array.
*
* @param array $arrConfig Name of the fields that SQL statement will search
* them (in array format where items are those
* fields names)
*
* @return object $this to build a fluent interface
*
*/
public function setArrFields($arrConfig)
{
if (is_array($arrConfig)) {
// Get _fields array
$this->_fields = $arrConfig;
}
return $this;
}
/**
* Setting value for $_fields array.
*
* @param string $strConfig Name of the fields that SQL statement will search
* them (in string format using comma as delimated)
*
* @return object $this to build a fluent interface
*
*/
public function setStrFields($strConfig)
{
if (is_string($strConfig)) {
// Get _fields array
$this->_fields = explode(',', $strConfig);
}
return $this;
}
/**
* Setting $mode propority value that refer to search mode
* [0 for OR logic | 1 for AND logic].
*
* @param int $mode Setting value to be saved in the $mode propority
*
* @return object $this to build a fluent interface
*
*/
public function setQueryMode($mode)
{
if (in_array($mode, ['0', '1'])) {
// Set search mode [0 for OR logic | 1 for AND logic]
$this->_mode = $mode;
}
return $this;
}
/**
* Getting $mode propority value that refer to search mode
* [0 for OR logic | 1 for AND logic].
*
* @return int Value of $mode properity
*
*/
public function getQueryMode()
{
// Get search mode value [0 for OR logic | 1 for AND logic]
return $this->_mode;
}
/**
* Getting values of $_fields Array in array format.
*
* @return array Value of $_fields array in Array format
*
*/
public function getArrFields()
{
$fields = $this->_fields;
return $fields;
}
/**
* Getting values of $_fields array in String format (comma delimated).
*
* @return string Values of $_fields array in String format (comma delimated)
*
*/
public function getStrFields()
{
$fields = implode(',', $this->_fields);
return $fields;
}
/**
* Build WHERE section of the SQL statement using defind lex's rules, search
* mode [AND | OR], and handle also phrases (inclosed by "") using normal
* LIKE condition to match it as it is.
*
* @param string $arg String that user search for in the database table
*
* @return string The WHERE section in SQL statement
* (MySQL database engine format)
*
*/
public function getWhereCondition($arg)
{
$sql = '';
//$arg = mysql_real_escape_string($arg);
$search = ['\\', "\x00", "\n", "\r", "'", '"', "\x1a"];
$replace = ['\\\\', '\\0', '\\n', '\\r', "\'", '\"', '\\Z'];
$arg = str_replace($search, $replace, $arg);
// Check if there are phrases in $arg should handle as it is
$phrase = explode('"', $arg);
if (count($phrase) > 2) {
// Re-init $arg variable
// (It will contain the rest of $arg except phrases).
$arg = '';
for ($i = 0; $i < count($phrase); $i++) {
$subPhrase = $phrase[$i];
if ($i % 2 == 0 && $subPhrase != '') {
// Re-build $arg variable after restricting phrases
$arg .= $subPhrase;
} elseif ($i % 2 == 1 && $subPhrase != '') {
// Handle phrases using reqular LIKE matching in MySQL
$this->wordCondition[] = $this->getWordLike($subPhrase);
}
}
}
// Handle normal $arg using lex's and regular expresion
$words = preg_split('/\s+/', trim($arg));
foreach ($words as $word) {
//if (is_numeric($word) || strlen($word) > 2) {
// Take off all the punctuation
//$word = preg_replace("/\p{P}/", '', $word);
$exclude = ['(', ')', '[', ']', '{', '}', ',', ';', ':',
'?', '!', '،', '؛', '؟', ];
$word = str_replace($exclude, '', $word);
$this->wordCondition[] = $this->getWordRegExp($word);
//}
}
if (!empty($this->wordCondition)) {
if ($this->_mode == 0) {
$sql = '('.implode(') OR (', $this->wordCondition).')';
} elseif ($this->_mode == 1) {
$sql = '('.implode(') AND (', $this->wordCondition).')';
}
}
return $sql;
}
/**
* Search condition in SQL format for one word in all defind fields using
* REGEXP clause and lex's rules.
*
* @param string $arg String (one word) that you want to build a condition for
*
* @return string sub SQL condition (for internal use)
*
*/
protected function getWordRegExp($arg)
{
$arg = $this->lex($arg);
//$sql = implode(" REGEXP '$arg' OR ", $this->_fields) . " REGEXP '$arg'";
$sql = ' REPLACE('.
implode(", 'ـ', '') REGEXP '$arg' OR REPLACE(", $this->_fields).
", 'ـ', '') REGEXP '$arg'";
return $sql;
}
/**
* Search condition in SQL format for one word in all defind fields using
* normal LIKE clause.
*
* @param string $arg String (one word) that you want to build a condition for
*
* @return string sub SQL condition (for internal use)
*
*/
protected function getWordLike($arg)
{
$sql = implode(" LIKE '$arg' OR ", $this->_fields)." LIKE '$arg'";
return $sql;
}
/**
* Get more relevant order by section related to the user search keywords.
*
* @param string $arg String that user search for in the database table
*
* @return string sub SQL ORDER BY section
*
*/
public function getOrderBy($arg)
{
// Check if there are phrases in $arg should handle as it is
$phrase = explode('"', $arg);
if (count($phrase) > 2) {
// Re-init $arg variable
// (It will contain the rest of $arg except phrases).
$arg = '';
for ($i = 0; $i < count($phrase); $i++) {
if ($i % 2 == 0 && $phrase[$i] != '') {
// Re-build $arg variable after restricting phrases
$arg .= $phrase[$i];
} elseif ($i % 2 == 1 && $phrase[$i] != '') {
// Handle phrases using reqular LIKE matching in MySQL
$wordOrder[] = $this->getWordLike($phrase[$i]);
}
}
}
// Handle normal $arg using lex's and regular expresion
$words = explode(' ', $arg);
foreach ($words as $word) {
if ($word != '') {
$wordOrder[] = 'CASE WHEN '.
$this->getWordRegExp($word).
' THEN 1 ELSE 0 END';
}
}
$order = '(('.implode(') + (', $wordOrder).')) DESC';
return $order;
}
/**
* This method will implement various regular expressin rules based on
* pre-defined Arabic lexical rules.
*
* @param string $arg String of one word user want to search for
*
* @return string Regular Expression format to be used in MySQL query statement
*
*/
protected function lex($arg)
{
$arg = preg_replace($this->_lexPatterns, $this->_lexReplacements, $arg);
return $arg;
}
/**
* Get most possible Arabic lexical forms for a given word.
*
* @param string $word String that user search for
*
* @return string list of most possible Arabic lexical forms for a given word
*
*/
protected function allWordForms($word)
{
$wordForms = [$word];
$postfix1 = ['كم', 'كن', 'نا', 'ها', 'هم', 'هن'];
$postfix2 = ['ين', 'ون', 'ان', 'ات', 'وا'];
$len = mb_strlen($word);
if (mb_substr($word, 0, 2) == 'ال') {
$word = mb_substr($word, 2);
}
$wordForms[] = $word;
$str1 = mb_substr($word, 0, -1);
$str2 = mb_substr($word, 0, -2);
$str3 = mb_substr($word, 0, -3);
$last1 = mb_substr($word, -1);
$last2 = mb_substr($word, -2);
$last3 = mb_substr($word, -3);
if ($len >= 6 && $last3 == 'تين') {
$wordForms[] = $str3;
$wordForms[] = $str3.'ة';
$wordForms[] = $word.'ة';
}
if ($len >= 6 && ($last3 == 'كما' || $last3 == 'هما')) {
$wordForms[] = $str3;
$wordForms[] = $str3.'كما';
$wordForms[] = $str3.'هما';
}
if ($len >= 5 && in_array($last2, $postfix2)) {
$wordForms[] = $str2;
$wordForms[] = $str2.'ة';
$wordForms[] = $str2.'تين';
foreach ($postfix2 as $postfix) {
$wordForms[] = $str2.$postfix;
}
}
if ($len >= 5 && in_array($last2, $postfix1)) {
$wordForms[] = $str2;
$wordForms[] = $str2.'ي';
$wordForms[] = $str2.'ك';
$wordForms[] = $str2.'كما';
$wordForms[] = $str2.'هما';
foreach ($postfix1 as $postfix) {
$wordForms[] = $str2.$postfix;
}
}
if ($len >= 5 && $last2 == 'ية') {
$wordForms[] = $str1;
$wordForms[] = $str2;
}
if (($len >= 4 && ($last1 == 'ة' || $last1 == 'ه' || $last1 == 'ت'))
|| ($len >= 5 && $last2 == 'ات')
) {
$wordForms[] = $str1;
$wordForms[] = $str1.'ة';
$wordForms[] = $str1.'ه';
$wordForms[] = $str1.'ت';
$wordForms[] = $str1.'ات';
}
if ($len >= 4 && $last1 == 'ى') {
$wordForms[] = $str1.'ا';
}
$trans = ['أ' => 'ا', 'إ' => 'ا', 'آ' => 'ا'];
foreach ($wordForms as $word) {
$normWord = strtr($word, $trans);
if ($normWord != $word) {
$wordForms[] = $normWord;
}
}
$wordForms = array_unique($wordForms);
return $wordForms;
}
/**
* Get most possible Arabic lexical forms of user search keywords.
*
* @param string $arg String that user search for
*
* @return string list of most possible Arabic lexical forms for given keywords
*
*/
public function allForms($arg)
{
$wordForms = [];
$words = explode(' ', $arg);
foreach ($words as $word) {
$wordForms = array_merge($wordForms, $this->allWordForms($word));
}
$str = implode(' ', $wordForms);
return $str;
}
}