ExportOffline.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. class ExportOffline extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. * php artisan export:offline lzma
  11. * @var string
  12. */
  13. protected $signature = 'export:offline {format?}';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'export offline data for app';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return int
  33. */
  34. public function handle()
  35. {
  36. $exportStop = storage_path('app/public/export/offline/.stop');
  37. $file = fopen($exportStop,'w');
  38. fclose($file);
  39. //建表
  40. $this->info('create db');
  41. $this->call('export:create.db');
  42. //term
  43. $this->info('term');
  44. $this->call('export:term');
  45. //导出channel
  46. $this->info('channel');
  47. $this->call('export:channel');
  48. //tag
  49. $this->info('tag');
  50. $this->call('export:tag');
  51. $this->call('export:tag.map');
  52. //
  53. $this->info('pali text');
  54. $this->call('export:pali.text');
  55. //导出章节索引
  56. $this->info('chapter');
  57. $this->call('export:chapter.index');
  58. //导出译文
  59. $this->info('sentence');
  60. $this->call('export:sentence',['--type'=>'translation']);
  61. $this->call('export:sentence',['--type'=>'nissaya']);
  62. //导出原文
  63. $this->call('export:sentence',['--type'=>'original']);
  64. $this->info('zip');
  65. $exportPath = 'app/public/export/offline';
  66. $exportFile = 'wikipali-offline-'.date("Y-m-d").'.db3';
  67. switch ($this->argument('format')) {
  68. case '7z':
  69. $zipFile = $exportFile . ".7z";
  70. break;
  71. case 'lzma':
  72. $zipFile = $exportFile . ".lzma";
  73. break;
  74. default:
  75. $zipFile = $exportFile . ".gz";
  76. break;
  77. }
  78. //
  79. $exportFullFileName = storage_path($exportPath.'/'.$exportFile);
  80. $zipFullFileName = storage_path($exportPath.'/'.$zipFile);
  81. shell_exec("cd ".storage_path($exportPath));
  82. shell_exec("chmod 600 {$exportFullFileName}");
  83. if($this->argument('format')==='7z'){
  84. $command = "7z a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on {$zipFullFileName} {$exportFullFileName}";
  85. }else if($this->argument('format')==='lzma'){
  86. $command = "xz -k -9 --format=lzma {$exportFullFileName}";
  87. }else{
  88. $command = "gzip -k -q --best -c {$exportFullFileName} > {$zipFullFileName}";
  89. }
  90. $this->info($command);
  91. shell_exec($command);
  92. $info = array();
  93. $info[] = ['filename'=>$zipFile,
  94. 'create_at'=>date("Y-m-d H:i:s"),
  95. 'chapter'=>Cache::get("/export/chapter/count"),
  96. 'filesize'=>filesize($zipFullFileName),
  97. 'min_app_ver'=>'1.3',
  98. ];
  99. Cache::put('/offline/index',$info);
  100. unlink($exportStop);
  101. return 0;
  102. }
  103. }