15
15
use Foolz \SphinxQL \Exception \DatabaseException ;
16
16
use Foolz \SphinxQL \Helper ;
17
17
use Foolz \SphinxQL \SphinxQL ;
18
+ use Psr \Log \LoggerInterface ;
18
19
19
20
class SphinxToolkit implements SphinxToolkitMysqliInterface, SphinxToolkitFoolzInterface
20
21
{
@@ -35,16 +36,22 @@ class SphinxToolkit implements SphinxToolkitMysqliInterface, SphinxToolkitFoolzI
35
36
* @var PDO
36
37
*/
37
38
private $ sphinx_connection ;
39
+
40
+ /**
41
+ * @var LoggerInterface
42
+ */
43
+ private $ logger ;
38
44
39
- public function __construct (PDO $ mysql_connection , PDO $ sphinx_connection )
45
+ public function __construct (PDO $ mysql_connection , PDO $ sphinx_connection, LoggerInterface $ logger = null )
40
46
{
41
47
$ this ->mysql_connection = $ mysql_connection ;
42
48
$ this ->sphinx_connection = $ sphinx_connection ;
49
+ $ this ->logger = $ logger ;
43
50
}
44
51
45
52
public function setRebuildIndexOptions (array $ options = []):array
46
53
{
47
- // на самом деле разворачиваем опции с установкой дефолтов
54
+ // разворачиваем опции с установкой дефолтов
48
55
$ this ->rai_options ['chunk_length ' ] = SphinxToolkitHelper::setOption ($ options , 'chunk_length ' , 500 );
49
56
50
57
$ this ->rai_options ['log_rows_inside_chunk ' ] = SphinxToolkitHelper::setOption ($ options , 'log_rows_inside_chunk ' , true );
@@ -287,54 +294,14 @@ public function checkIndexExist(string $sphinx_index)
287
294
return count ($ index_definition ) > 0 ;
288
295
}
289
296
290
-
291
-
292
- /* =========================== СТАТИЧЕСКИЕ МЕТОДЫ ==================================== */
293
-
294
- public static function EmulateBuildExcerpts ($ source , $ needle , $ options )
295
- {
296
- $ opts = [
297
- // Строка, вставляемая перед ключевым словом. По умолчанию "<strong>".
298
- 'before_match ' => SphinxToolkitHelper::setOption ($ options , 'before_match ' , '<strong> ' ),
299
-
300
- // Строка, вставляемая после ключевого слова. По умолчанию "</strong>".
301
- 'after_match ' => SphinxToolkitHelper::setOption ($ options , 'after_match ' , '</strong> ' ),
302
- // Строка, вставляемая между частями фрагмента. по умолчанию "...".
303
- 'chunk_separator ' => '... ' ,
304
-
305
- // НЕ РЕАЛИЗОВАНО: Максимальный размер фрагмента в символах. Integer, по умолчанию 256.
306
- 'limit ' => SphinxToolkitHelper::setOption ($ options , 'limit ' , 256 ),
307
-
308
- // НЕ РЕАЛИЗОВАНО: Сколько слов необходимо выбрать вокруг каждого совпадающего с ключевыми словами блока. Integer, по умолчанию 5.
309
- 'around ' => SphinxToolkitHelper::setOption ($ options , 'around ' , 5 ),
310
-
311
- // НЕ РЕАЛИЗОВАНО: Необходимо ли подсвечивать только точное совпадение с поисковой фразой, а не отдельные ключевые слова. Boolean, по умолчанию FALSE.
312
- 'exact_phrase ' => SphinxToolkitHelper::setOption ($ options , 'around ' , null ),
313
-
314
- // НЕ РЕАЛИЗОВАНО: Необходимо ли извлечь только единичный наиболее подходящий фрагмент. Boolean, по умолчанию FALSE.
315
- 'single_passage ' => SphinxToolkitHelper::setOption ($ options , 'single_passage ' , null ),
316
-
317
- ];
318
-
319
- $ target = strip_tags ($ source );
320
-
321
- $ target = SphinxToolkitHelper::mb_str_replace ($ needle , $ opts ['before_match ' ] . $ needle . $ opts ['after_match ' ], $ target );
322
-
323
- if (($ opts ['limit ' ] > 0 ) && ( mb_strlen ($ source ) > $ opts ['limit ' ] )) {
324
- $ target = SphinxToolkitHelper::mb_trim_text ($ target , $ opts ['limit ' ] ,true ,false , $ opts ['chunk_separator ' ]);
325
- }
326
-
327
- return $ target ;
328
- } // EmulateBuildExcerpts
329
-
330
297
/* =========================== STATIC IMPLEMENTATION ================================= */
331
298
332
299
/**
333
300
* rebuild_logging_options
334
301
*
335
302
* @var array
336
303
*/
337
- private static $ rlo = [];
304
+ private static $ spql_options = [];
338
305
339
306
/**
340
307
* @var Connection
@@ -349,58 +316,82 @@ public static function EmulateBuildExcerpts($source, $needle, $options)
349
316
* @var Connection
350
317
*/
351
318
private static $ spql_connection ;
352
-
353
- public static function init (string $ sphinx_connection_host , string $ sphinx_connection_port , $ options = [])
319
+
320
+ /**
321
+ * @var SphinxQL
322
+ */
323
+ private static $ spql_instance ;
324
+
325
+ /**
326
+ * @var LoggerInterface
327
+ */
328
+ private static $ spql_logger ;
329
+
330
+ /**
331
+ * @inheritDoc
332
+ */
333
+ public static function init (string $ sphinx_connection_host , string $ sphinx_connection_port , $ options = [], LoggerInterface $ logger = null )
354
334
{
355
335
self ::$ spql_connection_host = $ sphinx_connection_host ;
356
336
self ::$ spql_connection_port = $ sphinx_connection_port ;
357
337
358
- self ::$ rlo ['chunk_length ' ] = SphinxToolkitHelper::setOption ($ options , 'chunk_length ' , 500 );
338
+ self ::$ spql_options ['chunk_length ' ] = SphinxToolkitHelper::setOption ($ options , 'chunk_length ' , 500 );
359
339
360
- self ::$ rlo ['log_rows_inside_chunk ' ] = SphinxToolkitHelper::setOption ($ options , 'log_rows_inside_chunk ' , true );
361
- self ::$ rlo ['log_total_rows_found ' ] = SphinxToolkitHelper::setOption ($ options , 'log_total_rows_found ' , true );
340
+ self ::$ spql_options ['log_rows_inside_chunk ' ] = SphinxToolkitHelper::setOption ($ options , 'log_rows_inside_chunk ' , true );
341
+ self ::$ spql_options ['log_total_rows_found ' ] = SphinxToolkitHelper::setOption ($ options , 'log_total_rows_found ' , true );
362
342
363
- self ::$ rlo ['log_before_chunk ' ] = SphinxToolkitHelper::setOption ($ options , 'log_before_chunk ' , true );
364
- self ::$ rlo ['log_after_chunk ' ] = SphinxToolkitHelper::setOption ($ options , 'log_after_chunk ' , true );
343
+ self ::$ spql_options ['log_before_chunk ' ] = SphinxToolkitHelper::setOption ($ options , 'log_before_chunk ' , true );
344
+ self ::$ spql_options ['log_after_chunk ' ] = SphinxToolkitHelper::setOption ($ options , 'log_after_chunk ' , true );
365
345
366
- self ::$ rlo ['sleep_after_chunk ' ] = SphinxToolkitHelper::setOption ($ options , 'sleep_after_chunk ' , true );
346
+ self ::$ spql_options ['sleep_after_chunk ' ] = SphinxToolkitHelper::setOption ($ options , 'sleep_after_chunk ' , true );
367
347
368
- self ::$ rlo ['sleep_time ' ] = SphinxToolkitHelper::setOption ($ options , 'sleep_time ' , 1 );
369
- if (self ::$ rlo ['sleep_time ' ] == 0 ) {
370
- self ::$ rlo ['sleep_after_chunk ' ] = false ;
348
+ self ::$ spql_options ['sleep_time ' ] = SphinxToolkitHelper::setOption ($ options , 'sleep_time ' , 1 );
349
+ if (self ::$ spql_options ['sleep_time ' ] == 0 ) {
350
+ self ::$ spql_options ['sleep_after_chunk ' ] = false ;
371
351
}
372
352
373
- self ::$ rlo ['log_before_index ' ] = SphinxToolkitHelper::setOption ($ options , 'log_before_index ' , true );
374
- self ::$ rlo ['log_after_index ' ] = SphinxToolkitHelper::setOption ($ options , 'log_after_index ' , true );
353
+ self ::$ spql_options ['log_before_index ' ] = SphinxToolkitHelper::setOption ($ options , 'log_before_index ' , true );
354
+ self ::$ spql_options ['log_after_index ' ] = SphinxToolkitHelper::setOption ($ options , 'log_after_index ' , true );
355
+
356
+ self ::$ spql_logger = $ logger ;
375
357
}
376
-
358
+
359
+ /**
360
+ * @inheritDoc
361
+ */
377
362
public static function initConnection ()
378
363
{
379
- $ conn = new Connection ();
380
- $ conn ->setParams ([
364
+ $ connection = new Connection ();
365
+ $ connection ->setParams ([
381
366
'host ' => self ::$ spql_connection_host ,
382
367
'port ' => self ::$ spql_connection_port
383
368
]);
384
369
385
- self :: $ spql_connection = $ conn ;
370
+ return $ connection ;
386
371
}
387
-
372
+
373
+ /**
374
+ * @inheritDoc
375
+ */
388
376
public static function getInstance ()
389
377
{
390
378
return (new SphinxQL (self ::$ spql_connection ));
391
379
}
392
-
380
+
381
+ /**
382
+ * @inheritDoc
383
+ */
393
384
public static function createInstance ()
394
385
{
395
- $ conn = new Connection ();
396
- $ conn ->setParams ([
397
- 'host ' => self ::$ spql_connection_host ,
398
- 'port ' => self ::$ spql_connection_port
399
- ]);
400
-
401
- return (new SphinxQL ($ conn ));
386
+ self ::$ spql_connection = self ::initConnection ();
387
+ self ::$ spql_instance = self ::getInstance ();
388
+
389
+ return self ::$ spql_instance ;
402
390
}
403
-
391
+
392
+ /**
393
+ * @inheritDoc
394
+ */
404
395
public static function rt_ReplaceIndex (string $ index_name , array $ updateset )
405
396
{
406
397
if (empty ($ updateset )) return null ;
@@ -411,7 +402,10 @@ public static function rt_ReplaceIndex(string $index_name, array $updateset)
411
402
->set ($ updateset )
412
403
->execute ();
413
404
}
414
-
405
+
406
+ /**
407
+ * @inheritDoc
408
+ */
415
409
public static function rt_UpdateIndex (string $ index_name , array $ updateset )
416
410
{
417
411
if (empty ($ updateset )) return null ;
@@ -422,7 +416,10 @@ public static function rt_UpdateIndex(string $index_name, array $updateset)
422
416
->set ($ updateset )
423
417
->execute ();
424
418
}
425
-
419
+
420
+ /**
421
+ * @inheritDoc
422
+ */
426
423
public static function rt_DeleteIndex (string $ index_name , string $ field , $ field_value = null )
427
424
{
428
425
if (is_null ($ field_value )) return null ;
@@ -449,11 +446,7 @@ public static function rt_DeleteIndexMatch(string $index_name, string $field, $f
449
446
}
450
447
451
448
/**
452
- * Делает truncate index с реконфигурацией по умолчанию
453
- *
454
- * @param string $index_name
455
- * @param bool $is_reconfigure
456
- * @return bool
449
+ * @inheritDoc
457
450
*/
458
451
public static function rt_TruncateIndex (string $ index_name , bool $ is_reconfigure = true )
459
452
{
@@ -462,10 +455,13 @@ public static function rt_TruncateIndex(string $index_name, bool $is_reconfigure
462
455
463
456
return (bool )self ::createInstance ()->query ("TRUNCATE RTINDEX {$ index_name } {$ with }" );
464
457
}
465
-
458
+
459
+ /**
460
+ * @inheritDoc
461
+ */
466
462
public static function rt_RebuildAbstractIndex (PDO $ pdo_connection , string $ sql_source_table , string $ sphinx_index , Closure $ make_updateset_method , string $ condition = '' )
467
463
{
468
- $ chunk_size = self ::$ rlo ['chunk_length ' ];
464
+ $ chunk_size = self ::$ spql_options ['chunk_length ' ];
469
465
470
466
self ::rt_TruncateIndex ($ sphinx_index );
471
467
@@ -475,17 +471,17 @@ public static function rt_RebuildAbstractIndex(PDO $pdo_connection, string $sql_
475
471
->fetchColumn ();
476
472
$ total_updated = 0 ;
477
473
478
- if (self ::$ rlo ['log_before_index ' ])
474
+ if (self ::$ spql_options ['log_before_index ' ])
479
475
CLIConsole::say ("<font color='yellow'>[ {$ sphinx_index }]</font> index : " , false );
480
476
481
- if (self ::$ rlo ['log_total_rows_found ' ])
477
+ if (self ::$ spql_options ['log_total_rows_found ' ])
482
478
CLIConsole::say ("<font color='green'> {$ total_count }</font> elements found for rebuild. " );
483
479
484
480
// iterate chunks
485
481
for ($ i = 0 ; $ i < ceil ($ total_count / $ chunk_size ); $ i ++) {
486
482
$ offset = $ i * $ chunk_size ;
487
483
488
- if (self ::$ rlo ['log_before_chunk ' ]) CLIConsole::say ("Rebuilding elements from <font color='green'> {$ offset }</font>, <font color='yellow'> {$ chunk_size }</font> count... " , false );
484
+ 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 );
489
485
490
486
$ query_chunk_data = "SELECT * FROM {$ sql_source_table } " ;
491
487
$ query_chunk_data .= $ condition != '' ? " WHERE {$ condition } " : ' ' ;
@@ -495,7 +491,7 @@ public static function rt_RebuildAbstractIndex(PDO $pdo_connection, string $sql_
495
491
496
492
// iterate inside chunk
497
493
while ($ item = $ sth ->fetch ()) {
498
- if (self ::$ rlo ['log_rows_inside_chunk ' ])
494
+ if (self ::$ spql_options ['log_rows_inside_chunk ' ])
499
495
CLIConsole::say ("{$ sql_source_table }: {$ item ['id ' ]}" );
500
496
501
497
$ update_set = $ make_updateset_method ($ item );
@@ -505,25 +501,30 @@ public static function rt_RebuildAbstractIndex(PDO $pdo_connection, string $sql_
505
501
$ total_updated ++;
506
502
} // while
507
503
508
- $ breakline_after_chunk = !self ::$ rlo ['sleep_after_chunk ' ];
504
+ $ breakline_after_chunk = !self ::$ spql_options ['sleep_after_chunk ' ];
509
505
510
- if (self ::$ rlo ['log_after_chunk ' ]) {
506
+ if (self ::$ spql_options ['log_after_chunk ' ]) {
511
507
CLIConsole::say ("Updated RT-index <font color='yellow'> {$ sphinx_index }</font>. " , $ breakline_after_chunk );
512
508
} else {
513
509
CLIConsole::say ("<strong>Ok</strong> " , $ breakline_after_chunk );
514
510
}
515
511
516
- if (self ::$ rlo ['sleep_after_chunk ' ]) {
517
- CLIConsole::say (" ZZZZzzz for " . self ::$ rlo ['sleep_time ' ] . " second(s)... " , FALSE );
518
- sleep (self ::$ rlo ['sleep_time ' ]);
512
+ if (self ::$ spql_options ['sleep_after_chunk ' ]) {
513
+ CLIConsole::say (" ZZZZzzz for " . self ::$ spql_options ['sleep_time ' ] . " second(s)... " , FALSE );
514
+ sleep (self ::$ spql_options ['sleep_time ' ]);
519
515
CLIConsole::say ("I woke up! " );
520
516
}
521
517
} // for
522
- if (self ::$ rlo ['log_after_index ' ])
518
+ if (self ::$ spql_options ['log_after_index ' ])
523
519
CLIConsole::say ("Total updated <strong> {$ total_updated }</strong> elements for <font color='yellow'> {$ sphinx_index }</font> RT-index. <br> " );
524
520
525
521
return $ total_updated ;
526
522
}
523
+
524
+ public static function showMeta ()
525
+ {
526
+ (new Helper (self ::$ spql_connection ))->showMeta ()->execute ()->fetchAllAssoc ();
527
+ }
527
528
528
529
/**
529
530
*
0 commit comments