UpgradePaliToc.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\ResIndex;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Log;
  7. class UpgradePaliToc extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'upgrade:palitoc {lang} {from?} {to?}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'upgrade pali toc from csv';
  21. protected $usage = 'upgrade:palitoc lang from to';
  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(\App\Tools\Tools::isStop()){
  39. return 0;
  40. }
  41. $this->info("upgrade pali text");
  42. $startTime = time();
  43. $_lang = $this->argument('lang');
  44. $_from = $this->argument('from');
  45. $_to = $this->argument('to');
  46. if(empty($_from) && empty($_to)){
  47. $_from = 1;
  48. $_to = 217;
  49. }else if(empty($_to)){
  50. $_to = $_from;
  51. }
  52. if ($_lang == "pali") {
  53. $type = 1;
  54. } else {
  55. $type = 2;
  56. }
  57. $bar = $this->output->createProgressBar($_to-$_from+1);
  58. for ($from=$_from; $from <= $_to; $from++) {
  59. // 打开csv文件并读取数据
  60. $strFileName = config("mint.path.pali_title") . "/{$from}_{$_lang}.csv";
  61. if(!file_exists($strFileName)){
  62. continue;
  63. }
  64. #删除目标数据库中数据
  65. ResIndex::where('book', $from)
  66. ->where('language', $_lang)
  67. ->delete();
  68. DB::transaction(function ()use($from,$strFileName,$type,$_lang) {
  69. $inputRow = 0;
  70. if (($fp = fopen($strFileName, "r")) !== false) {
  71. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  72. if ($inputRow > 0 && $data[3] != 100 && !empty($data[6])) {
  73. if (isset($data[7])) {
  74. $author = $data[7];
  75. }else {
  76. $author = "cscd4";
  77. }
  78. $data[6] = mb_substr($data[6],0,1024);
  79. $newData = [
  80. 'book'=>$from,
  81. 'paragraph'=>$data[2],
  82. 'title'=>$data[6],
  83. 'title_en'=>$this->getWordEn($data[6]),
  84. 'level'=>$data[3],
  85. 'type'=>$type,
  86. 'language'=>$_lang,
  87. 'author'=>$author,
  88. 'share'=>1,
  89. 'create_time'=>time()*1000,
  90. 'update_time'=>time()*1000,
  91. ];
  92. ResIndex::create($newData);
  93. }
  94. $inputRow++;
  95. }
  96. fclose($fp);
  97. Log::info("res load:" .$strFileName);
  98. } else {
  99. $this->error("can not open csv $strFileName");
  100. Log::error("can not open csv $strFileName");
  101. }
  102. });
  103. $bar->advance();
  104. }
  105. $bar->finish();
  106. $msg = "upgrade pali toc finished. in ". time()-$startTime . "s";
  107. $this->info($msg .PHP_EOL);
  108. Log::info($msg);
  109. return 0;
  110. }
  111. private function getWordEn($strIn)
  112. {
  113. $strIn = strtolower($strIn);
  114. $search = array('ā', 'ī', 'ū', 'ṅ', 'ñ', 'ṭ', 'ḍ', 'ṇ', 'ḷ', 'ṃ');
  115. $replace = array('a', 'i', 'u', 'n', 'n', 't', 'd', 'n', 'l', 'm');
  116. return (str_replace($search, $replace, $strIn));
  117. }
  118. }