CreateMyHanCrop.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. * @var string
  11. */
  12. protected $signature = 'create:my.han.crop {--page=}';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = '建立缅汉字典切图工作文件';
  19. /**
  20. * Create a new command instance.
  21. *
  22. * @return void
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. }
  28. /**
  29. * Execute the console command.
  30. *
  31. * @return int
  32. */
  33. public function handle()
  34. {
  35. $csvFile = config("mint.path.dict_text") . '/zh/my-han/index.csv';
  36. if (($fp = fopen($csvFile, "r")) !== false) {
  37. $row = 0;
  38. $currPage = 0;
  39. $currPageWords = [];
  40. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  41. $row++;
  42. if ($row === 1) {
  43. continue;
  44. }
  45. if ($this->option('page')) {
  46. if ($currPage >= (int)$this->option('page')) {
  47. break;
  48. }
  49. }
  50. $page = (int)$data[1];
  51. $word = $data[2];
  52. if ($page !== $currPage) {
  53. //保存上一页数据
  54. $this->save($currPage, $currPageWords);
  55. $currPage = $page;
  56. //清空单词缓存
  57. $currPageWords = [];
  58. }
  59. $currPageWords[] = $word;
  60. }
  61. fclose($fp);
  62. $this->save($currPage, $currPageWords);
  63. }
  64. $this->info('done');
  65. return 0;
  66. }
  67. private function save($page, $words)
  68. {
  69. $basicUrl = 'https://ftp.wikipali.org/kosalla/%E7%BC%85%E6%96%87%E8%AF%8D%E5%85%B8/';
  70. if (count($words) > 0) {
  71. $m = new \Mustache_Engine(array(
  72. 'entity_flags' => ENT_QUOTES,
  73. 'escape' => function ($value) {
  74. return $value;
  75. }
  76. ));
  77. $tplFile = resource_path("/mustache/my_han_crop.tpl");
  78. $tpl = file_get_contents($tplFile);
  79. $wordWithIndex = [];
  80. foreach ($words as $key => $value) {
  81. $wordWithIndex[] = [
  82. 'index' => $key + 1,
  83. 'word' => trim($value),
  84. ];
  85. }
  86. $data = [
  87. 'dict' => [
  88. ['index' => 'a', 'img' => "{$basicUrl}{$page}A.jpg"],
  89. ['index' => 'b', 'img' => "{$basicUrl}{$page}B.jpg"],
  90. ['index' => 'a', 'img' => "{$basicUrl}" . ($page + 1) . "A.jpg"],
  91. ],
  92. 'words' => $wordWithIndex
  93. ];
  94. $content = $m->render($tpl, $data);
  95. //保存到临时文件夹
  96. // 使用本地磁盘
  97. // 创建目录]
  98. $dir = '/tmp/export/myhan_crop/' . $page;
  99. Storage::disk('local')->makeDirectory($dir);
  100. Storage::disk('local')->makeDirectory($dir . '/img');
  101. Storage::disk('local')->put($dir . "/index.html", $content);
  102. Storage::disk('local')->put($dir . "/img/{$page}", $page);
  103. $this->info("page={$page} word=" . count($words));
  104. } else {
  105. $this->error('page' . $page . 'no words');
  106. }
  107. }
  108. }