UpgradePaliToc.php 3.0 KB

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