ExportSentence.php 3.9 KB

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