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