ExportOffline.php 3.9 KB

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