2
0

ExportTerm.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\DhammaTerm;
  4. use App\Tools\Tools;
  5. use Illuminate\Console\Command;
  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 (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, '', '', [\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. $total = DhammaTerm::count();
  61. $channels = DhammaTerm::select([
  62. 'guid',
  63. 'word',
  64. 'word_en',
  65. 'meaning',
  66. 'other_meaning',
  67. 'note',
  68. 'tag',
  69. 'channal',
  70. 'language',
  71. 'owner',
  72. 'editor_id',
  73. 'created_at',
  74. 'updated_at',
  75. 'deleted_at',
  76. ])
  77. ->cursor();
  78. foreach ($channels as $key => $row) {
  79. $currData = [
  80. $row->guid,
  81. $row->word,
  82. $row->word_en,
  83. $row->meaning,
  84. $row->other_meaning,
  85. $row->note,
  86. $row->tag,
  87. $row->channal,
  88. $row->language,
  89. $row->owner,
  90. $row->editor_id,
  91. $row->created_at,
  92. $row->updated_at,
  93. $row->deleted_at,
  94. ];
  95. $stmt->execute($currData);
  96. if ($key % 1000 === 0) {
  97. $precent = (int) ($key * 100 / $total);
  98. $this->line("[{$precent}%]");
  99. }
  100. }
  101. $dbh->commit();
  102. $bar->finish();
  103. $this->info(' time='.(time() - $startAt).'s');
  104. Log::info('task export offline term-table finished');
  105. return 0;
  106. }
  107. }