ExportOffline.php 3.9 KB

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