ExportTerm.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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::error($e->getMessage(), ['exception' => $e]);
  58. return 1;
  59. }
  60. $bar = $this->output->createProgressBar(DhammaTerm::count());
  61. foreach (
  62. DhammaTerm::select([
  63. 'guid',
  64. 'word',
  65. 'word_en',
  66. 'meaning',
  67. 'other_meaning',
  68. 'note',
  69. 'tag',
  70. 'channal',
  71. 'language',
  72. "owner",
  73. "editor_id",
  74. "created_at",
  75. "updated_at",
  76. "deleted_at"
  77. ])
  78. ->cursor() as $row
  79. ) {
  80. $currData = array(
  81. $row->guid,
  82. $row->word,
  83. $row->word_en,
  84. $row->meaning,
  85. $row->other_meaning,
  86. $row->note,
  87. $row->tag,
  88. $row->channal,
  89. $row->language,
  90. $row->owner,
  91. $row->editor_id,
  92. $row->created_at,
  93. $row->updated_at,
  94. $row->deleted_at,
  95. );
  96. $stmt->execute($currData);
  97. $bar->advance();
  98. }
  99. $dbh->commit();
  100. $bar->finish();
  101. $this->info(' time=' . (time() - $startAt) . 's');
  102. Log::info('task export offline term-table finished');
  103. return 0;
  104. }
  105. }