ExportAiPaliWordToken.php 2.6 KB

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