InstallPaliText.php 4.7 KB

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