7
7
use Foolz \SphinxQL \Drivers \ConnectionInterface ;
8
8
use Foolz \SphinxQL \Exception \ConnectionException ;
9
9
use Foolz \SphinxQL \Exception \SphinxQLException ;
10
- use PDO ;
11
- use PDOException ;
12
10
use Arris \Toolkit \CLIConsole ;
13
11
14
12
use Foolz \SphinxQL \Drivers \Mysqli \Connection ;
@@ -29,12 +27,12 @@ class SphinxToolkit implements SphinxToolkitMysqliInterface, SphinxToolkitFoolzI
29
27
private $ rai_options ;
30
28
31
29
/**
32
- * @var PDO
30
+ * @var
33
31
*/
34
32
private $ mysql_connection ;
35
33
36
34
/**
37
- * @var PDO
35
+ * @var
38
36
*/
39
37
private $ sphinx_connection ;
40
38
@@ -43,7 +41,7 @@ class SphinxToolkit implements SphinxToolkitMysqliInterface, SphinxToolkitFoolzI
43
41
*/
44
42
private $ logger ;
45
43
46
- public function __construct (PDO $ mysql_connection , PDO $ sphinx_connection , LoggerInterface $ logger = null )
44
+ public function __construct ($ mysql_connection , $ sphinx_connection , LoggerInterface $ logger = null )
47
45
{
48
46
$ this ->mysql_connection = $ mysql_connection ;
49
47
$ this ->sphinx_connection = $ sphinx_connection ;
@@ -79,10 +77,20 @@ public function rebuildAbstractIndex(string $mysql_table, string $sphinx_index,
79
77
{
80
78
$ mysql_connection = $ this ->mysql_connection ;
81
79
$ sphinx_connection = $ this ->sphinx_connection ;
80
+
81
+ if (empty ($ sphinx_index )) {
82
+ throw new Exception ("Requested update of undefined index " , 1 );
83
+ }
84
+
85
+ if (empty ($ mysql_table )) {
86
+ throw new Exception ('Not defined source SQL table ' , 1 );
87
+ }
82
88
83
89
// проверяем, существует ли индекс
84
- if (! SphinxToolkitHelper::RTIndexCheckExist ($ this ->sphinx_connection , $ sphinx_index ))
90
+ if (! SphinxToolkitHelper::RTIndexCheckExist ($ this ->sphinx_connection , $ sphinx_index )) {
85
91
throw new Exception ("` {$ sphinx_index }` not present " , 1 );
92
+ }
93
+
86
94
87
95
$ chunk_size = $ this ->rai_options ['chunk_length ' ];
88
96
@@ -93,18 +101,21 @@ public function rebuildAbstractIndex(string $mysql_table, string $sphinx_index,
93
101
$ total_count = SphinxToolkitHelper::MySQL_GetRowsCount ($ mysql_connection , $ mysql_table , $ condition );
94
102
$ total_updated = 0 ;
95
103
96
- if ($ this ->rai_options ['log_before_index ' ])
104
+ if ($ this ->rai_options ['log_before_index ' ]) {
97
105
CLIConsole::say ("<font color='yellow'>[ {$ sphinx_index }]</font> index : " , false );
106
+ }
98
107
99
- if ($ this ->rai_options ['log_total_rows_found ' ])
108
+ if ($ this ->rai_options ['log_total_rows_found ' ]) {
100
109
CLIConsole::say ("<font color='green'> {$ total_count }</font> elements found for rebuild. " );
110
+ }
101
111
102
112
// iterate chunks
103
- for ($ i = 0 ; $ i < ceil ($ total_count / $ chunk_size ); $ i ++) {
113
+ for ($ i = 0 ; $ i < \ ceil ($ total_count / $ chunk_size ); $ i ++) {
104
114
$ offset = $ i * $ chunk_size ;
105
115
106
- if ($ this ->rai_options ['log_before_chunk ' ])
116
+ if ($ this ->rai_options ['log_before_chunk ' ]) {
107
117
CLIConsole::say ("Rebuilding elements from <font color='green'> {$ offset }</font>, <font color='yellow'> {$ chunk_size }</font> count... " , false );
118
+ }
108
119
109
120
$ query_chunk_data = "SELECT * FROM {$ mysql_table } " ;
110
121
$ query_chunk_data .= $ condition != '' ? " WHERE {$ condition } " : '' ;
@@ -114,8 +125,9 @@ public function rebuildAbstractIndex(string $mysql_table, string $sphinx_index,
114
125
115
126
// iterate inside chunk
116
127
while ($ item = $ sth ->fetch ()) {
117
- if ($ this ->rai_options ['log_rows_inside_chunk ' ])
128
+ if ($ this ->rai_options ['log_rows_inside_chunk ' ]) {
118
129
CLIConsole::say ("{$ mysql_table }: {$ item ['id ' ]}" );
130
+ }
119
131
120
132
$ update_set = $ make_updateset_method ($ item );
121
133
@@ -144,8 +156,9 @@ public function rebuildAbstractIndex(string $mysql_table, string $sphinx_index,
144
156
CLIConsole::say ("I woke up! " );
145
157
}
146
158
} // for
147
- if ($ this ->rai_options ['log_after_index ' ])
159
+ if ($ this ->rai_options ['log_after_index ' ]) {
148
160
CLIConsole::say ("Total updated <strong> {$ total_updated }</strong> elements for <font color='yellow'> {$ sphinx_index }</font> RT-index. <br> " );
161
+ }
149
162
150
163
return $ total_updated ;
151
164
} // rebuildAbstractIndex
@@ -158,17 +171,21 @@ public function rebuildAbstractIndexMVA(string $mysql_table, string $sphinx_inde
158
171
159
172
$ chunk_size = $ this ->rai_options ['chunk_length ' ];
160
173
161
- if (empty ($ sphinx_index ))
174
+ if (empty ($ sphinx_index )) {
162
175
throw new Exception ("SearchD index: ` {$ sphinx_index }` not defined " , 1 );
176
+ }
163
177
164
- if (empty ($ mysql_table ))
178
+ if (empty ($ mysql_table )) {
165
179
throw new Exception ("MySQL table: ` {$ mysql_table }` not defined " , 1 );
180
+ }
166
181
167
- if (! SphinxToolkitHelper::RTIndexCheckExist ($ this ->sphinx_connection , $ sphinx_index ))
182
+ if (! SphinxToolkitHelper::RTIndexCheckExist ($ this ->sphinx_connection , $ sphinx_index )) {
168
183
throw new Exception ("SearchD index: ` {$ sphinx_index }` not found " , 1 );
184
+ }
169
185
170
- if (! SphinxToolkitHelper::RTIndexCheckExist ($ this ->mysql_connection , $ mysql_table ))
186
+ if (! SphinxToolkitHelper::RTIndexCheckExist ($ this ->mysql_connection , $ mysql_table )) {
171
187
throw new Exception ("Source mysql table ` {$ mysql_table }` not found " , 1 );
188
+ }
172
189
173
190
// truncate
174
191
SphinxToolkitHelper::RTIndexTruncate ($ sphinx_connection , $ sphinx_index );
@@ -177,18 +194,21 @@ public function rebuildAbstractIndexMVA(string $mysql_table, string $sphinx_inde
177
194
$ total_count = SphinxToolkitHelper::MySQL_GetRowsCount ($ mysql_connection , $ mysql_table , $ condition );
178
195
$ total_updated = 0 ;
179
196
180
- if ($ this ->rai_options ['log_before_index ' ])
197
+ if ($ this ->rai_options ['log_before_index ' ]) {
181
198
CLIConsole::say ("<font color='yellow'>[ {$ sphinx_index }]</font> index : " , false );
199
+ }
182
200
183
- if ($ this ->rai_options ['log_total_rows_found ' ])
201
+ if ($ this ->rai_options ['log_total_rows_found ' ]) {
184
202
CLIConsole::say ("<font color='green'> {$ total_count }</font> elements found for rebuild. " );
203
+ }
185
204
186
205
// iterate chunks
187
206
for ($ i = 0 ; $ i < ceil ($ total_count / $ chunk_size ); $ i ++) {
188
207
$ offset = $ i * $ chunk_size ;
189
208
190
- if ($ this ->rai_options ['log_before_chunk ' ])
209
+ if ($ this ->rai_options ['log_before_chunk ' ]) {
191
210
CLIConsole::say ("Rebuilding elements from <font color='green'> {$ offset }</font>, <font color='yellow'> {$ chunk_size }</font> count... " , false );
211
+ }
192
212
193
213
$ query_chunk_data = " SELECT * FROM {$ mysql_table } " ;
194
214
$ query_chunk_data .= $ condition != '' ? " WHERE {$ condition } " : '' ;
@@ -202,8 +222,9 @@ public function rebuildAbstractIndexMVA(string $mysql_table, string $sphinx_inde
202
222
203
223
// iterate inside chunk
204
224
while ($ item = $ sth ->fetch ()) {
205
- if ($ this ->rai_options ['log_rows_inside_chunk ' ])
225
+ if ($ this ->rai_options ['log_rows_inside_chunk ' ]) {
206
226
CLIConsole::say ("{$ mysql_table }: {$ item ['id ' ]}" );
227
+ }
207
228
208
229
$ update_set = $ make_updateset_method ($ item );
209
230
@@ -228,8 +249,10 @@ public function rebuildAbstractIndexMVA(string $mysql_table, string $sphinx_inde
228
249
CLIConsole::say ("I woke up! " );
229
250
}
230
251
} // for
231
- if ($ this ->rai_options ['log_after_index ' ])
252
+
253
+ if ($ this ->rai_options ['log_after_index ' ]) {
232
254
CLIConsole::say ("Total updated <strong> {$ total_updated }</strong> elements for <font color='yellow'> {$ sphinx_index }</font> RT-index. <br> " );
255
+ }
233
256
234
257
return $ total_updated ;
235
258
} // rebuildAbstractIndexMVA
@@ -251,22 +274,22 @@ private static function buildReplaceQueryMVA(string $table, array $dataset, arra
251
274
{
252
275
$ query = "REPLACE INTO ` {$ table }` ( " ;
253
276
254
- $ dataset_keys = array_keys ($ dataset );
277
+ $ dataset_keys = \ array_keys ($ dataset );
255
278
256
- $ query .= implode (', ' , array_map (function ($ i ){
279
+ $ query .= \ implode (', ' , \ array_map (function ($ i ){
257
280
return "` {$ i }` " ;
258
281
}, $ dataset_keys ));
259
282
260
283
$ query .= " ) VALUES ( " ;
261
284
262
- $ query .= implode (', ' , array_map (function ($ i ) use ($ mva_attributes , $ dataset ){
263
- return in_array ($ i , $ mva_attributes ) ? "( {$ dataset [$ i ]}) " : ": {$ i }" ;
285
+ $ query .= \ implode (', ' , \ array_map (function ($ i ) use ($ mva_attributes , $ dataset ){
286
+ return \ in_array ($ i , $ mva_attributes ) ? "( {$ dataset [$ i ]}) " : ": {$ i }" ;
264
287
}, $ dataset_keys ));
265
288
266
289
$ query .= " ) " ;
267
290
268
- $ new_dataset = array_filter ($ dataset , function ($ value , $ key ) use ($ mva_attributes ) {
269
- return !in_array ($ key , $ mva_attributes );
291
+ $ new_dataset = \ array_filter ($ dataset , function ($ value , $ key ) use ($ mva_attributes ) {
292
+ return !\ in_array ($ key , $ mva_attributes );
270
293
}, ARRAY_FILTER_USE_BOTH );
271
294
272
295
return [
@@ -281,17 +304,17 @@ private static function buildReplaceQueryMVA(string $table, array $dataset, arra
281
304
*/
282
305
private static function buildReplaceQuery (string $ table , array $ dataset ):string
283
306
{
284
- $ dataset_keys = array_keys ($ dataset );
307
+ $ dataset_keys = \ array_keys ($ dataset );
285
308
286
309
$ query = "REPLACE INTO ` {$ table }` ( " ;
287
310
288
- $ query .= implode (', ' , array_map (function ($ i ){
311
+ $ query .= \ implode (', ' , \ array_map (function ($ i ){
289
312
return "` {$ i }` " ;
290
313
}, $ dataset_keys ));
291
314
292
315
$ query .= " ) VALUES ( " ;
293
316
294
- $ query .= implode (', ' , array_map (function ($ i ){
317
+ $ query .= \ implode (', ' , \ array_map (function ($ i ){
295
318
return ": {$ i }" ;
296
319
}, $ dataset_keys ));
297
320
@@ -407,7 +430,9 @@ public static function createInstance()
407
430
*/
408
431
public static function rt_ReplaceIndex (string $ index_name , array $ updateset )
409
432
{
410
- if (empty ($ updateset )) return null ;
433
+ if (empty ($ updateset )) {
434
+ return null ;
435
+ }
411
436
412
437
return self ::createInstance ()
413
438
->replace ()
@@ -421,7 +446,9 @@ public static function rt_ReplaceIndex(string $index_name, array $updateset)
421
446
*/
422
447
public static function rt_UpdateIndex (string $ index_name , array $ updateset )
423
448
{
424
- if (empty ($ updateset )) return null ;
449
+ if (empty ($ updateset )) {
450
+ return null ;
451
+ }
425
452
426
453
return self ::createInstance ()
427
454
->update ($ index_name )
@@ -435,7 +462,9 @@ public static function rt_UpdateIndex(string $index_name, array $updateset)
435
462
*/
436
463
public static function rt_DeleteIndex (string $ index_name , string $ field , $ field_value = null )
437
464
{
438
- if (is_null ($ field_value )) return null ;
465
+ if (is_null ($ field_value )) {
466
+ return null ;
467
+ }
439
468
440
469
return self ::createInstance ()
441
470
->delete ()
@@ -449,7 +478,9 @@ public static function rt_DeleteIndex(string $index_name, string $field, $field_
449
478
*/
450
479
public static function rt_DeleteIndexMatch (string $ index_name , string $ field , $ field_value = '' )
451
480
{
452
- if (is_null ($ field_value )) return null ;
481
+ if (is_null ($ field_value )) {
482
+ return null ;
483
+ }
453
484
454
485
return self ::createInstance ()
455
486
->delete ()
@@ -463,7 +494,10 @@ public static function rt_DeleteIndexMatch(string $index_name, string $field, $f
463
494
*/
464
495
public static function rt_TruncateIndex (string $ index_name , bool $ is_reconfigure = true )
465
496
{
466
- if (empty ($ index_name )) return false ;
497
+ if (empty ($ index_name )) {
498
+ return false ;
499
+ }
500
+
467
501
$ with = $ is_reconfigure ? 'WITH RECONFIGURE ' : '' ;
468
502
469
503
return (bool )self ::createInstance ()->query ("TRUNCATE RTINDEX {$ index_name } {$ with }" );
@@ -472,7 +506,7 @@ public static function rt_TruncateIndex(string $index_name, bool $is_reconfigure
472
506
/**
473
507
* @inheritDoc
474
508
*/
475
- public static function rt_RebuildAbstractIndex (PDO $ pdo_connection , string $ sql_source_table , string $ sphinx_index , Closure $ make_updateset_method , string $ condition = '' )
509
+ public static function rt_RebuildAbstractIndex ($ pdo_connection , string $ sql_source_table , string $ sphinx_index , Closure $ make_updateset_method , string $ condition = '' )
476
510
{
477
511
$ chunk_size = self ::$ spql_options ['chunk_length ' ];
478
512
@@ -485,15 +519,21 @@ public static function rt_RebuildAbstractIndex(PDO $pdo_connection, string $sql_
485
519
486
520
$ total_updated = 0 ;
487
521
488
- if (self ::$ spql_options ['log_before_index ' ]) CLIConsole::say ("<font color='yellow'>[ {$ sphinx_index }]</font> index : " , false );
522
+ if (self ::$ spql_options ['log_before_index ' ]) {
523
+ CLIConsole::say ("<font color='yellow'>[ {$ sphinx_index }]</font> index : " , false );
524
+ }
489
525
490
- if (self ::$ spql_options ['log_total_rows_found ' ]) CLIConsole::say ("<font color='green'> {$ total_count }</font> elements found for rebuild. " );
526
+ if (self ::$ spql_options ['log_total_rows_found ' ]) {
527
+ CLIConsole::say ("<font color='green'> {$ total_count }</font> elements found for rebuild. " );
528
+ }
491
529
492
530
// iterate chunks
493
531
for ($ i = 0 ; $ i < ceil ($ total_count / $ chunk_size ); $ i ++) {
494
532
$ offset = $ i * $ chunk_size ;
495
533
496
- if (self ::$ spql_options ['log_before_chunk ' ]) CLIConsole::say ("Rebuilding elements from <font color='green'> {$ offset }</font>, <font color='yellow'> {$ chunk_size }</font> count... " , false );
534
+ if (self ::$ spql_options ['log_before_chunk ' ]) {
535
+ CLIConsole::say ("Rebuilding elements from <font color='green'> {$ offset }</font>, <font color='yellow'> {$ chunk_size }</font> count... " , false );
536
+ }
497
537
498
538
$ query_chunk_data = "SELECT * FROM {$ sql_source_table } " ;
499
539
$ query_chunk_data .= $ condition != '' ? " WHERE {$ condition } " : ' ' ;
@@ -503,7 +543,9 @@ public static function rt_RebuildAbstractIndex(PDO $pdo_connection, string $sql_
503
543
504
544
// iterate inside chunk
505
545
while ($ item = $ sth ->fetch ()) {
506
- if (self ::$ spql_options ['log_rows_inside_chunk ' ]) CLIConsole::say ("{$ sql_source_table }: {$ item ['id ' ]}" );
546
+ if (self ::$ spql_options ['log_rows_inside_chunk ' ]) {
547
+ CLIConsole::say ("{$ sql_source_table }: {$ item ['id ' ]}" );
548
+ }
507
549
508
550
$ update_set = $ make_updateset_method ($ item ); // call closure
509
551
@@ -527,7 +569,9 @@ public static function rt_RebuildAbstractIndex(PDO $pdo_connection, string $sql_
527
569
}
528
570
} // for
529
571
530
- if (self ::$ spql_options ['log_after_index ' ]) CLIConsole::say ("Total updated <strong> {$ total_updated }</strong> elements for <font color='yellow'> {$ sphinx_index }</font> RT-index. <br> " );
572
+ if (self ::$ spql_options ['log_after_index ' ]) {
573
+ CLIConsole::say ("Total updated <strong> {$ total_updated }</strong> elements for <font color='yellow'> {$ sphinx_index }</font> RT-index. <br> " );
574
+ }
531
575
532
576
return $ total_updated ;
533
577
}
@@ -553,7 +597,9 @@ public static function spql_getDataSet(string $search_query, string $source_inde
553
597
$ found_dataset = [];
554
598
$ compiled_request = '' ;
555
599
556
- if (empty ($ source_index )) return $ found_dataset ;
600
+ if (empty ($ source_index )) {
601
+ return $ found_dataset ;
602
+ }
557
603
558
604
try {
559
605
$ search_request = self ::createInstance ()
@@ -570,12 +616,12 @@ public static function spql_getDataSet(string $search_query, string $source_inde
570
616
->option ('field_weights ' , $ option_weight );
571
617
}
572
618
573
- if (!is_null ($ limit ) && is_numeric ($ limit )) {
619
+ if (!\ is_null ($ limit ) && \ is_numeric ($ limit )) {
574
620
$ search_request = $ search_request
575
621
->limit ($ limit );
576
622
}
577
623
578
- if (strlen ($ search_query ) > 0 ) {
624
+ if (\ strlen ($ search_query ) > 0 ) {
579
625
$ search_request = $ search_request
580
626
->match (['title ' ], $ search_query );
581
627
}
@@ -595,7 +641,7 @@ public static function spql_getDataSet(string $search_query, string $source_inde
595
641
" Error fetching data from ` {$ source_index }` : " . $ e ->getMessage (),
596
642
[
597
643
$ e ->getCode (),
598
- htmlspecialchars (urldecode ($ _SERVER ['HTTP_HOST ' ] . $ _SERVER ['REQUEST_URI ' ])),
644
+ \ htmlspecialchars (\ urldecode ($ _SERVER ['HTTP_HOST ' ] . $ _SERVER ['REQUEST_URI ' ])),
599
645
$ search_request ->getCompiled (),
600
646
$ meta
601
647
]
@@ -616,7 +662,9 @@ public static function spql_getDataSet(string $search_query, string $source_inde
616
662
*/
617
663
private static function internal_ReplaceIndex (string $ index_name , array $ updateset )
618
664
{
619
- if (empty ($ updateset )) return null ;
665
+ if (empty ($ updateset )) {
666
+ return null ;
667
+ }
620
668
621
669
return self ::getInstance (self ::$ spql_connection )
622
670
->replace ()
0 commit comments