2
0

InstallPaliText.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\PaliText;
  4. use App\Services\V3\PaliTextService;
  5. use App\Tools\Tools;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Log;
  9. class InstallPaliText extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'install:palitext {from?} {to?}';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'Command description';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return int
  36. */
  37. public function handle()
  38. {
  39. if (Tools::isStop()) {
  40. return 0;
  41. }
  42. $this->info('instert pali text');
  43. $startTime = time();
  44. $_from = $this->argument('from');
  45. $_to = $this->argument('to');
  46. if (empty($_from) && empty($_to)) {
  47. $_from = 1;
  48. $_to = 217;
  49. } elseif (empty($_to)) {
  50. $_to = $_from;
  51. }
  52. $fileListFileName = config('mint.path.palitext_filelist');
  53. $filelist = [];
  54. if (($handle = fopen($fileListFileName, 'r')) !== false) {
  55. while (($filelist[] = fgetcsv($handle, 0, ',')) !== false) {
  56. }
  57. }
  58. $bar = $this->output->createProgressBar($_to - $_from + 1);
  59. for ($from = $_from; $from <= $_to; $from++) {
  60. // code...
  61. $fileSn = $from - 1;
  62. $FileName = $filelist[$fileSn][1];
  63. $dirXmlBase = config('mint.path.palicsv').'/';
  64. $GLOBALS['data'] = [];
  65. // 打开vri html文件并读取数据
  66. $pali_text_array = [];
  67. $htmlFile = config('mint.path.palitext').'/'.$FileName.'.htm';
  68. if (($fpPaliText = fopen($htmlFile, 'r')) !== false) {
  69. while (($data = fgets($fpPaliText)) !== false) {
  70. if (substr($data, 0, 2) === '<p') {
  71. array_push($pali_text_array, $data);
  72. }
  73. }
  74. fclose($fpPaliText);
  75. // $this->info("pali text load:" . $htmlFile . PHP_EOL);
  76. } else {
  77. $this->error('can not pali text file. filename='.$htmlFile.PHP_EOL);
  78. Log::error('can not pali text file. filename='.$htmlFile.PHP_EOL);
  79. }
  80. $inputRow = 0;
  81. $csvFile = config('mint.path.palicsv').'/'.$FileName.'/'.$FileName.'_pali.csv';
  82. if (($fp = fopen($csvFile, 'r')) !== false) {
  83. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  84. if ($inputRow > 0) {
  85. if (($inputRow - 1) < count($pali_text_array)) {
  86. $data[5] = $pali_text_array[$inputRow - 1];
  87. }
  88. $data[1] = mb_substr($data[1], 1, null, 'UTF-8');
  89. $GLOBALS['data'][] = $data;
  90. }
  91. $inputRow++;
  92. }
  93. fclose($fp);
  94. // $this->info("单词表load:" . $csvFile.PHP_EOL);
  95. } else {
  96. $this->error('can not open csv file. filename='.$csvFile.PHP_EOL);
  97. Log::error('can not open csv file. filename='.$csvFile.PHP_EOL);
  98. continue;
  99. }
  100. if (($inputRow - 1) != count($pali_text_array)) {
  101. $this->error("line count error $FileName ".PHP_EOL);
  102. Log::error("line count error $FileName ".PHP_EOL);
  103. }
  104. // 删除目标数据库中数据
  105. PaliText::where('book', $from)->delete();
  106. // 打开文件并读取数据
  107. DB::transaction(function () {
  108. foreach ($GLOBALS['data'] as $oneParam) {
  109. if ($oneParam[3] < 100) {
  110. $toc = $oneParam[6];
  111. } else {
  112. $toc = '';
  113. }
  114. $params = [
  115. 'book' => $oneParam[1],
  116. 'paragraph' => $oneParam[2],
  117. 'level' => $oneParam[3],
  118. 'class' => $oneParam[4],
  119. 'toc' => $toc,
  120. 'text' => $oneParam[6],
  121. 'html' => $oneParam[5],
  122. 'lenght' => mb_strlen($oneParam[6], 'UTF-8'),
  123. ];
  124. PaliText::create($params);
  125. }
  126. });
  127. // 阅读、导出缓存的 pali_texts(按书缓存一个月)
  128. app(PaliTextService::class)->forgetBook((int) $from);
  129. $bar->advance();
  130. }
  131. $bar->finish();
  132. $this->info('instert pali text finished. in '.time() - $startTime.'s'.PHP_EOL);
  133. return 0;
  134. }
  135. }