2
0

ExportOffline.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Storage;
  5. use Illuminate\Support\Facades\Cache;
  6. use Illuminate\Support\Facades\Log;
  7. use Illuminate\Support\Facades\Redis;
  8. class ExportOffline extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. * php artisan export:offline lzma
  13. * @var string
  14. */
  15. protected $signature = 'export:offline {format? : zip file format 7z,lzma,gz } {--shortcut} {--driver=morus}';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'export offline data for app';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return int
  35. */
  36. public function handle()
  37. {
  38. if (\App\Tools\Tools::isStop()) {
  39. return 0;
  40. }
  41. $exportDir = storage_path('app/public/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. }
  48. }
  49. //清空redis
  50. Cache::put('/offline/index', []);
  51. //删除全部的旧文件
  52. foreach (scandir($exportDir) as $key => $file) {
  53. if (is_file($exportDir . '/' . $file)) {
  54. unlink($exportDir . '/' . $file);
  55. }
  56. }
  57. //添加 .stop
  58. $exportStop = $exportDir . '/.stop';
  59. $file = fopen($exportStop, 'w');
  60. fclose($file);
  61. //建表
  62. $this->info('create db');
  63. $this->call('export:create.db');
  64. //term
  65. $this->info('export term start');
  66. $this->call('export:term');
  67. //导出channel
  68. $this->info('export channel start');
  69. $this->call('export:channel', ['db' => 'wikipali-offline']);
  70. $this->call('export:channel', ['db' => 'wikipali-offline-index']);
  71. if (!$this->option('shortcut')) {
  72. //tag
  73. $this->info('export tag start');
  74. $this->call('export:tag', ['db' => 'wikipali-offline']);
  75. $this->call('export:tag.map', ['db' => 'wikipali-offline']);
  76. //
  77. $this->info('export pali text start');
  78. $this->call('export:pali.text');
  79. //导出章节索引
  80. $this->info('export chapter start');
  81. $this->call('export:chapter.index', ['db' => 'wikipali-offline']);
  82. $this->call('export:chapter.index', ['db' => 'wikipali-offline-index']);
  83. //导出译文
  84. $this->info('export sentence start');
  85. $this->call('export:sentence', ['--type' => 'translation', '--driver' => $this->option('driver')]);
  86. $this->call('export:sentence', ['--type' => 'nissaya', '--driver' => $this->option('driver')]);
  87. //导出原文
  88. $this->call('export:sentence', ['--type' => 'original', '--driver' => $this->option('driver')]);
  89. }
  90. $this->info('zip');
  91. Log::info('export offline: db写入完毕 开始压缩');
  92. sleep(5);
  93. $this->call('export:zip', [
  94. 'id' => 'index',
  95. 'filename' => 'wikipali-offline-index' . '-' . date("Y-m-d") . '.db3',
  96. 'title' => 'wikipali 离线包索引',
  97. 'format' => $this->argument('format'),
  98. ]);
  99. $this->call('export:zip', [
  100. 'id' => 'date-package',
  101. 'filename' => 'wikipali-offline' . '-' . date("Y-m-d") . '.db3',
  102. 'title' => 'wikipali 离线包',
  103. 'format' => $this->argument('format'),
  104. ]);
  105. $this->call('export:ai.training.data');
  106. $this->call('export:ai.pali.word.token');
  107. unlink($exportStop);
  108. return 0;
  109. }
  110. }