ExportOffline.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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,0755,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. Log::debug('export offline: db写入完毕 开始压缩');
  80. $exportPath = 'app/public/export/offline';
  81. $exportFile = 'wikipali-offline-'.date("Y-m-d").'.db3';
  82. Log::debug('export offline: zip file {filename} {format}',
  83. [
  84. 'filename'=>$exportFile,
  85. 'format'=>$this->argument('format')
  86. ]);
  87. switch ($this->argument('format')) {
  88. case '7z':
  89. $zipFile = $exportFile . ".7z";
  90. break;
  91. case 'lzma':
  92. $zipFile = $exportFile . ".lzma";
  93. break;
  94. default:
  95. $zipFile = $exportFile . ".gz";
  96. break;
  97. }
  98. //
  99. $exportFullFileName = storage_path($exportPath.'/'.$exportFile);
  100. $zipFullFileName = storage_path($exportPath.'/'.$zipFile);
  101. shell_exec("cd ".storage_path($exportPath));
  102. if($this->argument('format')==='7z'){
  103. $command = "7z a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on {$zipFullFileName} {$exportFullFileName}";
  104. }else if($this->argument('format')==='lzma'){
  105. $command = "xz -k -9 --format=lzma {$exportFullFileName}";
  106. }else{
  107. $command = "gzip -k -q --best -c {$exportFullFileName} > {$zipFullFileName}";
  108. }
  109. $this->info($command);
  110. Log::debug('export offline: zip command:'.$command);
  111. shell_exec($command);
  112. Log::debug('zip file {filename} in {format} saved.',
  113. [
  114. 'filename'=>$exportFile,
  115. 'format'=>$this->argument('format')
  116. ]);
  117. $info = array();
  118. $url = array();
  119. foreach (config('mint.server.cdn_urls') as $key => $cdn) {
  120. $url[] = [
  121. 'link' => $cdn . '/' . $zipFile,
  122. 'hostname' =>'cdn-' . $key,
  123. ];
  124. }
  125. //s3
  126. Storage::put($zipFile, file_get_contents($zipFullFileName));
  127. $s3Link = Storage::url($zipFile);
  128. Log::info('export offline: link='.$s3Link);
  129. $url[] = [
  130. 'link'=>$s3Link,
  131. 'hostname'=>'Amazon cloud storage(Hongkong)',
  132. ];
  133. $info[] = ['filename'=>$zipFile,
  134. 'url' => $url,
  135. 'create_at'=>date("Y-m-d H:i:s"),
  136. 'chapter'=>RedisClusters::get("/export/chapter/count"),
  137. 'filesize'=>filesize($zipFullFileName),
  138. 'min_app_ver'=>'1.3',
  139. ];
  140. RedisClusters::put('/offline/index',$info);
  141. unlink($exportStop);
  142. unlink($exportFullFileName);
  143. return 0;
  144. }
  145. }