InstallPaliText.php 3.8 KB

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