ExportOffline.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 App\Tools\RedisClusters;
  8. use Illuminate\Support\Facades\Redis;
  9. class ExportOffline extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. * php artisan export:offline lzma
  14. * @var string
  15. */
  16. protected $signature = 'export:offline {format? : zip file format 7z,lzma,gz } {--shortcut} {--driver=morus}';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'export offline data for app';
  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. if(\App\Tools\Tools::isStop()){
  40. return 0;
  41. }
  42. $exportDir = storage_path('app/public/export/offline');
  43. if(!is_dir($exportDir)){
  44. $res = mkdir($exportDir,0755,true);
  45. if(!$res){
  46. Log::error('mkdir fail path='.$exportDir);
  47. return 1;
  48. }
  49. }
  50. //清空redis
  51. RedisClusters::put('/offline/index',[]);
  52. //删除全部的旧文件
  53. foreach (scandir($exportDir) as $key => $file) {
  54. if(is_file($exportDir.'/'.$file)){
  55. unlink($exportDir.'/'.$file);
  56. }
  57. }
  58. //添加 .stop
  59. $exportStop = $exportDir.'/.stop';
  60. $file = fopen($exportStop,'w');
  61. fclose($file);
  62. //建表
  63. $this->info('create db');
  64. $this->call('export:create.db');
  65. //term
  66. $this->info('export term start');
  67. $this->call('export:term');
  68. //导出channel
  69. $this->info('export channel start');
  70. $this->call('export:channel',['db'=>'wikipali-offline']);
  71. $this->call('export:channel',['db'=>'wikipali-offline-index']);
  72. if(!$this->option('shortcut')){
  73. //tag
  74. $this->info('export tag start');
  75. $this->call('export:tag',['db'=>'wikipali-offline']);
  76. $this->call('export:tag.map',['db'=>'wikipali-offline']);
  77. //
  78. $this->info('export pali text start');
  79. $this->call('export:pali.text');
  80. //导出章节索引
  81. $this->info('export chapter start');
  82. $this->call('export:chapter.index',['db'=>'wikipali-offline']);
  83. $this->call('export:chapter.index',['db'=>'wikipali-offline-index']);
  84. //导出译文
  85. $this->info('export sentence start');
  86. $this->call('export:sentence',['--type'=>'translation','--driver'=>$this->option('driver')]);
  87. $this->call('export:sentence',['--type'=>'nissaya','--driver'=>$this->option('driver')]);
  88. //导出原文
  89. $this->call('export:sentence',['--type'=>'original','--driver'=>$this->option('driver')]);
  90. }
  91. $this->info('zip');
  92. Log::info('export offline: db写入完毕 开始压缩');
  93. sleep(5);
  94. $this->call('export:zip',[
  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. 'filename'=>'wikipali-offline'.'-'.date("Y-m-d").'.db3',
  101. 'title' => 'wikipali 离线包',
  102. 'format'=>$this->argument('format'),
  103. ]);
  104. $this->call('export:ai.training.data');
  105. unlink($exportStop);
  106. return 0;
  107. }
  108. }