ExportOffline.php 4.1 KB

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