ExportSentence.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Storage;
  5. use App\Models\Sentence;
  6. use App\Models\Channel;
  7. use App\Http\Api\ChannelApi;
  8. use Illuminate\Support\Facades\Log;
  9. class ExportSentence extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'export:sentence {--channel=} {--type=translation}';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'Command description';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return int
  36. */
  37. public function handle()
  38. {
  39. $channels = [];
  40. $channel_id = $this->option('channel');
  41. if($channel_id){
  42. $file_suf = $channel_id;
  43. $channels[] = $channel_id;
  44. }else{
  45. $channel_type = $this->option('type');
  46. $file_suf = $channel_type;
  47. if($channel_type === "original"){
  48. $pali_channel = ChannelApi::getSysChannel("_System_Pali_VRI_");
  49. if($pali_channel === false){
  50. return 0;
  51. }
  52. $channels[] = $pali_channel;
  53. }else{
  54. $nissaya_channel = Channel::where('type',$channel_type)->where('status',30)->select('uid')->get();
  55. foreach ($nissaya_channel as $key => $value) {
  56. # code...
  57. $channels[] = $value->uid;
  58. }
  59. }
  60. }
  61. $exportFile = storage_path('app/public/export/offline/sentence-'.date("Y-m-d").'.db3');
  62. $dbh = new \PDO('sqlite:'.$exportFile, "", "", array(\PDO::ATTR_PERSISTENT => true));
  63. $dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
  64. $dbh->beginTransaction();
  65. if($channel_type === "original"){
  66. $table = 'sentence';
  67. }else{
  68. $table = 'sentence_translation';
  69. }
  70. $query = "INSERT INTO {$table} ( book , paragraph ,
  71. word_start , word_end , content , channel_id )
  72. VALUES ( ? , ? , ? , ? , ? , ? )";
  73. try{
  74. $stmt = $dbh->prepare($query);
  75. }catch(PDOException $e){
  76. Log::info($e);
  77. return 1;
  78. }
  79. $db = Sentence::whereIn('channel_uid',$channels);
  80. $bar = $this->output->createProgressBar($db->count());
  81. $srcDb = $db->select(['uid','book_id','paragraph',
  82. 'word_start','word_end',
  83. 'content','content_type','channel_uid',
  84. 'editor_uid','language','updated_at'])->cursor();
  85. foreach ($srcDb as $sent) {
  86. $currData = array(
  87. $sent->book_id,
  88. $sent->paragraph,
  89. $sent->word_start,
  90. $sent->word_end,
  91. $sent->content,
  92. $sent->channel_uid,
  93. );
  94. $stmt->execute($currData);
  95. $bar->advance();
  96. }
  97. $dbh->commit();
  98. $bar->finish();
  99. return 0;
  100. }
  101. }