ExportOffline.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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? : zip file format 7z,lzma,gz }';
  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. if(\App\Tools\Tools::isStop()){
  37. return 0;
  38. }
  39. $exportDir = storage_path('app/public/export/offline');
  40. if(!is_dir($exportDir)){
  41. $res = mkdir($exportDir,0700,true);
  42. if(!$res){
  43. Log::error('mkdir fail path='.$exportDir);
  44. return 1;
  45. }
  46. }
  47. $exportStop = $exportDir.'/.stop';
  48. $file = fopen($exportStop,'w');
  49. fclose($file);
  50. //建表
  51. $this->info('create db');
  52. $this->call('export:create.db');
  53. //term
  54. $this->info('term');
  55. $this->call('export:term');
  56. //导出channel
  57. $this->info('channel');
  58. $this->call('export:channel');
  59. //tag
  60. $this->info('tag');
  61. $this->call('export:tag');
  62. $this->call('export:tag.map');
  63. //
  64. $this->info('pali text');
  65. $this->call('export:pali.text');
  66. //导出章节索引
  67. $this->info('chapter');
  68. $this->call('export:chapter.index');
  69. //导出译文
  70. $this->info('sentence');
  71. $this->call('export:sentence',['--type'=>'translation']);
  72. $this->call('export:sentence',['--type'=>'nissaya']);
  73. //导出原文
  74. $this->call('export:sentence',['--type'=>'original']);
  75. $this->info('zip');
  76. $exportPath = 'app/public/export/offline';
  77. $exportFile = 'wikipali-offline-'.date("Y-m-d").'.db3';
  78. switch ($this->argument('format')) {
  79. case '7z':
  80. $zipFile = $exportFile . ".7z";
  81. break;
  82. case 'lzma':
  83. $zipFile = $exportFile . ".lzma";
  84. break;
  85. default:
  86. $zipFile = $exportFile . ".gz";
  87. break;
  88. }
  89. //
  90. $exportFullFileName = storage_path($exportPath.'/'.$exportFile);
  91. $zipFullFileName = storage_path($exportPath.'/'.$zipFile);
  92. shell_exec("cd ".storage_path($exportPath));
  93. shell_exec("chmod 600 {$exportFullFileName}");
  94. if($this->argument('format')==='7z'){
  95. $command = "7z a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on {$zipFullFileName} {$exportFullFileName}";
  96. }else if($this->argument('format')==='lzma'){
  97. $command = "xz -k -9 --format=lzma {$exportFullFileName}";
  98. }else{
  99. $command = "gzip -k -q --best -c {$exportFullFileName} > {$zipFullFileName}";
  100. }
  101. $this->info($command);
  102. shell_exec($command);
  103. $info = array();
  104. $info[] = ['filename'=>$zipFile,
  105. 'create_at'=>date("Y-m-d H:i:s"),
  106. 'chapter'=>Cache::get("/export/chapter/count"),
  107. 'filesize'=>filesize($zipFullFileName),
  108. 'min_app_ver'=>'1.3',
  109. ];
  110. Cache::put('/offline/index',$info);
  111. unlink($exportStop);
  112. return 0;
  113. }
  114. }