ExportTerm.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Storage;
  5. use App\Models\DhammaTerm;
  6. use Illuminate\Support\Facades\Log;
  7. class ExportTerm extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'export:term';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Command description';
  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. Log::info('task export offline term-table start');
  38. $startAt = time();
  39. if(\App\Tools\Tools::isStop()){
  40. return 0;
  41. }
  42. $exportFile = storage_path('app/public/export/offline/wikipali-offline-'.date("Y-m-d").'.db3');
  43. $dbh = new \PDO('sqlite:'.$exportFile, "", "", array(\PDO::ATTR_PERSISTENT => true));
  44. $dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
  45. $dbh->beginTransaction();
  46. $query = "INSERT INTO dhamma_terms ( uuid , word , word_en , meaning ,
  47. other_meaning , note , tag , channel_id,
  48. language, owner, editor_id,
  49. created_at,updated_at,deleted_at)
  50. VALUES ( ? , ? , ? , ? ,
  51. ? , ? , ? , ? ,
  52. ?, ?, ?,
  53. ?, ?, ? )";
  54. try{
  55. $stmt = $dbh->prepare($query);
  56. }catch(PDOException $e){
  57. Log::info($e);
  58. return 1;
  59. }
  60. $bar = $this->output->createProgressBar(DhammaTerm::count());
  61. foreach (DhammaTerm::select(['guid','word','word_en','meaning',
  62. 'other_meaning','note','tag','channal',
  63. 'language',"owner","editor_id",
  64. "created_at","updated_at","deleted_at"
  65. ])
  66. ->cursor() as $row) {
  67. $currData = array(
  68. $row->guid,
  69. $row->word,
  70. $row->word_en,
  71. $row->meaning,
  72. $row->other_meaning,
  73. $row->note,
  74. $row->tag,
  75. $row->channal,
  76. $row->language,
  77. $row->owner,
  78. $row->editor_id,
  79. $row->created_at,
  80. $row->updated_at,
  81. $row->deleted_at,
  82. );
  83. $stmt->execute($currData);
  84. $bar->advance();
  85. }
  86. $dbh->commit();
  87. $bar->finish();
  88. $this->info(' time='.(time()-$startAt).'s');
  89. Log::info('task export offline term-table finished');
  90. return 0;
  91. }
  92. }