CreateMyHanCrop.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Storage;
  5. class CreateMyHanCrop extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. * php artisan create:my.han.crop --page=10
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'create:my.han.crop {--page=}';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $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. $csvFile = config('mint.path.dict_text').'/zh/my-han/index.csv';
  37. if (($fp = fopen($csvFile, 'r')) !== false) {
  38. $row = 0;
  39. $currPage = 0;
  40. $currPageWords = [];
  41. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  42. $row++;
  43. if ($row === 1) {
  44. continue;
  45. }
  46. if ($this->option('page')) {
  47. if ($currPage >= (int) $this->option('page')) {
  48. break;
  49. }
  50. }
  51. $page = (int) $data[1];
  52. $word = $data[2];
  53. if ($page !== $currPage) {
  54. // 保存上一页数据
  55. $this->save($currPage, $currPageWords);
  56. $currPage = $page;
  57. // 清空单词缓存
  58. $currPageWords = [];
  59. }
  60. $currPageWords[] = $word;
  61. }
  62. fclose($fp);
  63. $this->save($currPage, $currPageWords);
  64. }
  65. $this->info('done');
  66. return 0;
  67. }
  68. private function save($page, $words)
  69. {
  70. $basicUrl = 'https://ftp.wikipali.org/kosalla/%E7%BC%85%E6%96%87%E8%AF%8D%E5%85%B8/';
  71. if (count($words) > 0) {
  72. $m = new \Mustache_Engine([
  73. 'entity_flags' => ENT_QUOTES,
  74. 'escape' => function ($value) {
  75. return $value;
  76. },
  77. ]);
  78. $tplFile = resource_path('/mustache/my_han_crop.tpl');
  79. $tpl = file_get_contents($tplFile);
  80. $wordWithIndex = [];
  81. foreach ($words as $key => $value) {
  82. $wordWithIndex[] = [
  83. 'index' => $key + 1,
  84. 'word' => trim($value),
  85. ];
  86. }
  87. $data = [
  88. 'dict' => [
  89. ['index' => 'a', 'img' => "{$basicUrl}{$page}A.jpg"],
  90. ['index' => 'b', 'img' => "{$basicUrl}{$page}B.jpg"],
  91. ['index' => 'a', 'img' => "{$basicUrl}".($page + 1).'A.jpg'],
  92. ],
  93. 'words' => $wordWithIndex,
  94. ];
  95. $content = $m->render($tpl, $data);
  96. // 保存到临时文件夹
  97. // 使用本地磁盘
  98. // 创建目录]
  99. $dir = '/tmp/export/myhan_crop/'.$page;
  100. Storage::disk('local')->makeDirectory($dir);
  101. Storage::disk('local')->makeDirectory($dir.'/img');
  102. Storage::disk('local')->put($dir.'/index.html', $content);
  103. Storage::disk('local')->put($dir."/img/{$page}", $page);
  104. $this->info("page={$page} word=".count($words));
  105. } else {
  106. $this->error('page'.$page.'no words');
  107. }
  108. }
  109. }