ExportAiPaliWordToken.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\Api\DictApi;
  4. use App\Models\UserDict;
  5. use App\Tools\Tools;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\Log;
  8. class ExportAiPaliWordToken extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. * php artisan export:ai.pali.word.token
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'export:ai.pali.word.token {--format=gz : zip file format 7z,lzma,gz }';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'export ai pali word token';
  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. Log::debug('export ai pali word token');
  40. if (Tools::isStop()) {
  41. return 0;
  42. }
  43. $exportDir = storage_path('app/tmp/export/offline');
  44. if (! is_dir($exportDir)) {
  45. $res = mkdir($exportDir, 0755, true);
  46. if (! $res) {
  47. Log::error('mkdir fail path='.$exportDir);
  48. return 1;
  49. } else {
  50. Log::info('make dir successful '.$exportDir);
  51. }
  52. }
  53. $dict_id = DictApi::getSysDict('system_preference');
  54. if (! $dict_id) {
  55. Log::error('没有找到 system_preference 字典');
  56. return 1;
  57. }
  58. $filename = 'ai-pali-word-token-'.date('Y-m-d').'.tsv';
  59. $exportFile = $exportDir.'/'.$filename;
  60. $fp = fopen($exportFile, 'w');
  61. if ($fp === false) {
  62. Log::error('无法创建文件');
  63. return 1;
  64. }
  65. $start = time();
  66. $total = UserDict::where('dict_id', $dict_id)->count();
  67. $words = UserDict::where('dict_id', $dict_id)
  68. ->select([
  69. 'word',
  70. 'factors',
  71. ])->cursor();
  72. foreach ($words as $key => $word) {
  73. $output = [$word->word, $word->factors];
  74. fwrite($fp, implode("\t", $output)."\n");
  75. if ($key % 100 === 0) {
  76. $present = (int) ($key * 100 / $total);
  77. $this->info("[{$present}%]-{$key}");
  78. }
  79. }
  80. fclose($fp);
  81. Log::info((time() - $start).' seconds');
  82. $this->call('export:zip', [
  83. 'id' => 'ai-pali-word-token',
  84. 'filename' => $exportFile,
  85. 'title' => 'ai pali word token',
  86. 'format' => $this->option('format'),
  87. ]);
  88. return 0;
  89. }
  90. }