UpgradePaliToc.php 4.1 KB

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