ExportTerm.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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::debug('task export offline term-table start');
  38. if(\App\Tools\Tools::isStop()){
  39. return 0;
  40. }
  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 dhamma_terms ( uuid , word , word_en , meaning ,
  46. other_meaning , note , tag , channel_id,
  47. language, owner, editor_id,
  48. created_at,updated_at,deleted_at)
  49. VALUES ( ? , ? , ? , ? ,
  50. ? , ? , ? , ? ,
  51. ?, ?, ?,
  52. ?, ?, ? )";
  53. try{
  54. $stmt = $dbh->prepare($query);
  55. }catch(PDOException $e){
  56. Log::info($e);
  57. return 1;
  58. }
  59. $bar = $this->output->createProgressBar(DhammaTerm::count());
  60. foreach (DhammaTerm::select(['guid','word','word_en','meaning',
  61. 'other_meaning','note','tag','channal',
  62. 'language',"owner","editor_id",
  63. "created_at","updated_at","deleted_at"
  64. ])
  65. ->cursor() as $row) {
  66. $currData = array(
  67. $row->guid,
  68. $row->word,
  69. $row->word_en,
  70. $row->meaning,
  71. $row->other_meaning,
  72. $row->note,
  73. $row->tag,
  74. $row->channal,
  75. $row->language,
  76. $row->owner,
  77. $row->editor_id,
  78. $row->created_at,
  79. $row->updated_at,
  80. $row->deleted_at,
  81. );
  82. $stmt->execute($currData);
  83. $bar->advance();
  84. }
  85. $dbh->commit();
  86. $bar->finish();
  87. Log::debug('task export offline term-table finished');
  88. return 0;
  89. }
  90. }