ExportChannel.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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';
  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. Log::debug('task export offline channel-table start');
  41. $exportFile = storage_path('app/public/export/offline/wikipali-offline-'.date("Y-m-d").'.db3');
  42. $dbh = new \PDO('sqlite:'.$exportFile, "", "", array(\PDO::ATTR_PERSISTENT => true));
  43. $dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
  44. $dbh->beginTransaction();
  45. $query = "INSERT INTO channel ( id , name , type , language ,
  46. summary , owner_id , setting,created_at )
  47. VALUES ( ? , ? , ? , ? , ? , ? , ? , ? )";
  48. try{
  49. $stmt = $dbh->prepare($query);
  50. }catch(PDOException $e){
  51. Log::info($e);
  52. return 1;
  53. }
  54. $bar = $this->output->createProgressBar(Channel::where('status',30)->count());
  55. foreach (Channel::where('status',30)
  56. ->select(['uid','name','type','lang',
  57. 'summary','owner_uid','setting','created_at'])
  58. ->cursor() as $row) {
  59. $currData = array(
  60. $row->uid,
  61. $row->name,
  62. $row->type,
  63. $row->lang,
  64. $row->summary,
  65. $row->owner_uid,
  66. $row->setting,
  67. $row->created_at,
  68. );
  69. $stmt->execute($currData);
  70. $bar->advance();
  71. }
  72. $dbh->commit();
  73. $bar->finish();
  74. Log::debug('task export offline channel-table finished');
  75. return 0;
  76. }
  77. }