InstallPaliText.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. $this->info("instert pali text");
  38. $startTime = time();
  39. $_from = $this->argument('from');
  40. $_to = $this->argument('to');
  41. if(empty($_from) && empty($_to)){
  42. $_from = 1;
  43. $_to = 217;
  44. }else if(empty($_to)){
  45. $_to = $_from;
  46. }
  47. $fileListFileName = config("app.path.palitext_filelist");
  48. $filelist = array();
  49. if (($handle = fopen($fileListFileName, 'r')) !== false) {
  50. while (($filelist[] = fgetcsv($handle, 0, ',')) !== false) {
  51. }
  52. }
  53. $bar = $this->output->createProgressBar($_to-$_from+1);
  54. for ($from=$_from; $from <=$_to ; $from++) {
  55. # code...
  56. $fileSn = $from-1;
  57. $FileName = $filelist[$fileSn][1];
  58. $dirXmlBase = config("app.path.palicsv") . "/";
  59. $GLOBALS['data'] = array();
  60. // 打开vri html文件并读取数据
  61. $pali_text_array = array();
  62. $htmlFile = config("app.path.palitext") .'/'. $FileName.'.htm';
  63. if (($fpPaliText = fopen($htmlFile, "r")) !== false) {
  64. while (($data = fgets($fpPaliText)) !== false) {
  65. if (substr($data, 0, 2) === "<p") {
  66. array_push($pali_text_array, $data);
  67. }
  68. }
  69. fclose($fpPaliText);
  70. //$this->info("pali text load:" . $htmlFile . PHP_EOL);
  71. } else {
  72. $this->error( "can not pali text file. filename=" . $htmlFile . PHP_EOL) ;
  73. Log::error( "can not pali text file. filename=" . $htmlFile . PHP_EOL) ;
  74. }
  75. $inputRow = 0;
  76. $csvFile = config("app.path.palicsv") .'/'. $FileName .'/'. $FileName.'_pali.csv';
  77. if (($fp = fopen($csvFile, "r")) !== false) {
  78. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  79. if ($inputRow > 0) {
  80. if (($inputRow - 1) < count($pali_text_array)) {
  81. $data[5] = $pali_text_array[$inputRow - 1];
  82. }
  83. $data[1] = mb_substr($data[1],1,null,"UTF-8");
  84. $GLOBALS['data'][] = $data;
  85. }
  86. $inputRow++;
  87. }
  88. fclose($fp);
  89. //$this->info("单词表load:" . $csvFile.PHP_EOL);
  90. } else {
  91. $this->error( "can not open csv file. filename=" . $csvFile. PHP_EOL) ;
  92. Log::error( "can not open csv file. filename=" . $csvFile. PHP_EOL) ;
  93. continue;
  94. }
  95. if (($inputRow - 1) != count($pali_text_array)) {
  96. $this->error( "line count error $FileName ".PHP_EOL);
  97. Log::error( "line count error $FileName ".PHP_EOL);
  98. }
  99. #删除目标数据库中数据
  100. PaliText::where('book', $from)->delete();
  101. // 打开文件并读取数据
  102. DB::transaction(function () {
  103. foreach ($GLOBALS['data'] as $oneParam) {
  104. if ($oneParam[3] < 100) {
  105. $toc = $oneParam[6];
  106. } else {
  107. $toc = "";
  108. }
  109. $params = [
  110. 'book'=>$oneParam[1],
  111. 'paragraph'=>$oneParam[2],
  112. 'level'=>$oneParam[3],
  113. 'class'=> $oneParam[4],
  114. 'toc'=>$toc,
  115. 'text'=>$oneParam[6],
  116. 'html'=>$oneParam[5],
  117. 'lenght'=>mb_strlen($oneParam[6], "UTF-8"),
  118. ];
  119. PaliText::create($params);
  120. }
  121. });
  122. $bar->advance();
  123. }
  124. $bar->finish();
  125. $this->info("instert pali text finished. in ". time()-$startTime . "s" .PHP_EOL);
  126. return 0;
  127. }
  128. }