UpgradeFts.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\BookTitle;
  4. use App\Models\WbwTemplate;
  5. use App\Tools\PaliSearch;
  6. use App\Tools\Tools;
  7. use Illuminate\Console\Command;
  8. class UpgradeFts extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'upgrade:fts {--content : upgrade col content only}
  16. {para?}
  17. {--test : output log only}';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'upgrade full text search table';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return int
  37. */
  38. public function handle()
  39. {
  40. if (Tools::isStop()) {
  41. return 0;
  42. }
  43. if (! empty($this->argument('para'))) {
  44. $para = explode('-', $this->argument('para'));
  45. }
  46. for ($iBook = 1; $iBook <= 217; $iBook++) {
  47. if (isset($para[0]) && $para[0] != $iBook) {
  48. continue;
  49. }
  50. // code...
  51. $this->info('book:'.$iBook);
  52. $maxParagraph = WbwTemplate::where('book', $iBook)->max('paragraph');
  53. $bar = $this->output->createProgressBar($maxParagraph - 1);
  54. for ($iPara = 1; $iPara <= $maxParagraph; $iPara++) {
  55. if (isset($para[1]) && $para[1] != $iPara) {
  56. $bar->advance();
  57. continue;
  58. }
  59. $content = $this->getContent($iBook, $iPara);
  60. // 查找黑体字
  61. $words = WbwTemplate::where('book', $iBook)
  62. ->where('paragraph', $iPara)
  63. ->orderBy('wid')->get();
  64. $bold1 = [];
  65. $bold2 = [];
  66. $bold3 = [];
  67. $currBold = [];
  68. foreach ($words as $word) {
  69. if ($word->style === 'bld') {
  70. $currBold[] = $word->real;
  71. } else {
  72. $countBold = count($currBold);
  73. if ($countBold === 1) {
  74. $bold1[] = $currBold[0];
  75. } elseif ($countBold === 2) {
  76. $bold2 = array_merge($bold2, $currBold);
  77. } elseif ($countBold > 0) {
  78. $bold3 = array_merge($bold3, $currBold);
  79. }
  80. $currBold = [];
  81. }
  82. }
  83. $pcd_book = BookTitle::where('book', $iBook)
  84. ->where('paragraph', '<=', $iPara)
  85. ->orderBy('paragraph', 'desc')
  86. ->first();
  87. if ($pcd_book) {
  88. $pcd_book_id = $pcd_book->sn;
  89. } else {
  90. $pcd_book_id = BookTitle::where('book', $iBook)
  91. ->orderBy('paragraph')
  92. ->value('sn');
  93. }
  94. if ($this->option('test')) {
  95. $this->info($content.
  96. ' pcd_book='.$pcd_book_id.
  97. ' bold1='.implode(' ', $bold1).
  98. ' bold2='.implode(' ', $bold2).
  99. ' bold3='.implode(' ', $bold3).PHP_EOL
  100. );
  101. } else {
  102. $update = PaliSearch::update($iBook,
  103. $iPara,
  104. implode(' ', $bold1),
  105. implode(' ', $bold2),
  106. implode(' ', $bold3),
  107. $content,
  108. $pcd_book_id);
  109. }
  110. $bar->advance();
  111. }
  112. $bar->finish();
  113. $this->info('done');
  114. }
  115. return 0;
  116. }
  117. private function getContent($book, $para)
  118. {
  119. $words = WbwTemplate::where('book', $book)
  120. ->where('paragraph', $para)
  121. ->where('type', '<>', '.ctl.')
  122. ->orderBy('wid')->get();
  123. $content = '';
  124. foreach ($words as $word) {
  125. if ($word->style === 'bld') {
  126. if (strpos($word->word, '{') === false) {
  127. $content .= "**{$word->word}** ";
  128. } else {
  129. $content .= str_replace(['{', '}'], ['**', '** '], $word->word);
  130. }
  131. } elseif ($word->style === 'note') {
  132. $content .= " _{$word->word}_ ";
  133. } else {
  134. $content .= $word->word.' ';
  135. }
  136. }
  137. return $content;
  138. }
  139. }