ExportOffline.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. //删除全部的旧文件
  51. foreach (scandir($exportDir) as $key => $file) {
  52. if(is_file($exportDir.'/'.$file)){
  53. unlink($exportDir.'/'.$file);
  54. }
  55. }
  56. //添加 .stop
  57. $exportStop = $exportDir.'/.stop';
  58. $file = fopen($exportStop,'w');
  59. fclose($file);
  60. //建表
  61. $this->info('create db');
  62. $this->call('export:create.db');
  63. //term
  64. $this->info('export term start');
  65. $this->call('export:term');
  66. //导出channel
  67. $this->info('export channel start');
  68. $this->call('export:channel',['db'=>'wikipali-offline']);
  69. $this->call('export:channel',['db'=>'wikipali-offline-index']);
  70. if(!$this->option('shortcut')){
  71. //tag
  72. $this->info('export tag start');
  73. $this->call('export:tag',['db'=>'wikipali-offline']);
  74. $this->call('export:tag.map',['db'=>'wikipali-offline']);
  75. //
  76. $this->info('export pali text start');
  77. $this->call('export:pali.text');
  78. //导出章节索引
  79. $this->info('export chapter start');
  80. $this->call('export:chapter.index',['db'=>'wikipali-offline']);
  81. $this->call('export:chapter.index',['db'=>'wikipali-offline-index']);
  82. //导出译文
  83. $this->info('export sentence start');
  84. $this->call('export:sentence',['--type'=>'translation','--driver'=>$this->option('driver')]);
  85. $this->call('export:sentence',['--type'=>'nissaya','--driver'=>$this->option('driver')]);
  86. //导出原文
  87. $this->call('export:sentence',['--type'=>'original','--driver'=>$this->option('driver')]);
  88. }
  89. $this->info('zip');
  90. Log::info('export offline: db写入完毕 开始压缩');
  91. sleep(5);
  92. $this->call('export:zip',[
  93. 'db'=>'wikipali-offline-index',
  94. 'format'=>$this->argument('format'),
  95. ]);
  96. $this->call('export:zip',[
  97. 'db'=>'wikipali-offline',
  98. 'format'=>$this->argument('format'),
  99. ]);
  100. unlink($exportStop);
  101. return 0;
  102. }
  103. }