ExportChannel.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * 导出离线用的channel数据
  4. */
  5. namespace App\Console\Commands;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\Storage;
  8. use App\Models\Channel;
  9. use Illuminate\Support\Facades\Log;
  10. class ExportChannel extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'export:channel {db}';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = '导出离线用的channel数据';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return int
  37. */
  38. public function handle()
  39. {
  40. if(\App\Tools\Tools::isStop()){
  41. return 0;
  42. }
  43. Log::debug('task export offline channel-table start');
  44. $exportFile = storage_path('app/public/export/offline/'.$this->argument('db').'-'.date("Y-m-d").'.db3');
  45. $dbh = new \PDO('sqlite:'.$exportFile, "", "", array(\PDO::ATTR_PERSISTENT => true));
  46. $dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
  47. $dbh->beginTransaction();
  48. $query = "INSERT INTO channel ( id , name , type , language ,
  49. summary , owner_id , setting,created_at )
  50. VALUES ( ? , ? , ? , ? , ? , ? , ? , ? )";
  51. try{
  52. $stmt = $dbh->prepare($query);
  53. }catch(PDOException $e){
  54. Log::info($e);
  55. return 1;
  56. }
  57. $bar = $this->output->createProgressBar(Channel::where('status',30)->count());
  58. foreach (Channel::where('status',30)
  59. ->select(['uid','name','type','lang',
  60. 'summary','owner_uid','setting','created_at'])
  61. ->cursor() as $row) {
  62. $currData = array(
  63. $row->uid,
  64. $row->name,
  65. $row->type,
  66. $row->lang,
  67. $row->summary,
  68. $row->owner_uid,
  69. $row->setting,
  70. $row->created_at,
  71. );
  72. $stmt->execute($currData);
  73. $bar->advance();
  74. }
  75. $dbh->commit();
  76. $bar->finish();
  77. Log::debug('task export offline channel-table finished');
  78. return 0;
  79. }
  80. }