ExportOffline.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. *
  11. * @var string
  12. */
  13. protected $signature = 'export:offline {format?}';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Command description';
  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. //建表
  37. $this->info('create db');
  38. $this->call('export:create.db');
  39. //term
  40. $this->info('term');
  41. $this->call('export:term');
  42. //导出channel
  43. $this->info('channel');
  44. $this->call('export:channel');
  45. //tag
  46. $this->info('tag');
  47. $this->call('export:tag');
  48. $this->call('export:tag.map');
  49. //
  50. $this->info('pali text');
  51. $this->call('export:pali.text');
  52. //导出章节索引
  53. $this->info('chapter');
  54. $this->call('export:chapter.index');
  55. //导出译文
  56. $this->info('sentence');
  57. $this->call('export:sentence',['--type'=>'translation']);
  58. $this->call('export:sentence',['--type'=>'nissaya']);
  59. //导出原文
  60. $this->call('export:sentence',['--type'=>'original']);
  61. $this->info('zip');
  62. $exportPath = 'app/public/export/offline';
  63. $exportFile = 'wikipali-offline-'.date("Y-m-d").'.db3';
  64. switch ($this->argument('format')) {
  65. case '7z':
  66. $zipFile = $exportFile . ".7z";
  67. break;
  68. case 'lzma':
  69. $zipFile = $exportFile . ".lzma";
  70. break;
  71. default:
  72. $zipFile = $exportFile . ".gz";
  73. break;
  74. }
  75. //
  76. $exportFullFileName = storage_path($exportPath.'/'.$exportFile);
  77. $zipFullFileName = storage_path($exportPath.'/'.$zipFile);
  78. shell_exec("cd ".storage_path($exportPath));
  79. shell_exec("chmod 600 {$exportFullFileName}");
  80. if($this->argument('format')==='7z'){
  81. $command = "7z a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on {$zipFullFileName} {$exportFullFileName}";
  82. }else if($this->argument('format')==='lzma'){
  83. $command = "xz -k -9 --format=lzma {$exportFullFileName}";
  84. }else{
  85. $command = "gzip -k -q --best -c {$exportFullFileName} > {$zipFullFileName}";
  86. }
  87. $this->info($command);
  88. shell_exec($command);
  89. $info = array();
  90. $info[] = ['filename'=>$zipFile,
  91. 'create_at'=>date("Y-m-d H:i:s"),
  92. 'chapter'=>Cache::get("/export/chapter/count"),
  93. 'filesize'=>filesize($zipFullFileName),
  94. 'min_app_ver'=>'1.3',
  95. ];
  96. Cache::put('/offline/index',$info);
  97. return 0;
  98. }
  99. }