ExportSentence.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. if(\App\Tools\Tools::isStop()){
  42. return 0;
  43. }
  44. $channels = [];
  45. $channel_id = $this->option('channel');
  46. if($channel_id){
  47. $file_suf = $channel_id;
  48. $channels[] = $channel_id;
  49. }else{
  50. $channel_type = $this->option('type');
  51. $file_suf = $channel_type;
  52. if($channel_type === "original"){
  53. $pali_channel = ChannelApi::getSysChannel("_System_Pali_VRI_");
  54. if($pali_channel === false){
  55. return 0;
  56. }
  57. $channels[] = $pali_channel;
  58. }else{
  59. $nissaya_channel = Channel::where('type',$channel_type)->where('status',30)->select('uid')->get();
  60. foreach ($nissaya_channel as $key => $value) {
  61. # code...
  62. $channels[] = $value->uid;
  63. }
  64. }
  65. }
  66. $exportFile = storage_path('app/public/export/offline/sentence-'.date("Y-m-d").'.db3');
  67. $dbh = new \PDO('sqlite:'.$exportFile, "", "", array(\PDO::ATTR_PERSISTENT => true));
  68. $dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
  69. $dbh->beginTransaction();
  70. if($channel_type === "original"){
  71. $table = 'sentence';
  72. }else{
  73. $table = 'sentence_translation';
  74. }
  75. $query = "INSERT INTO {$table} ( book , paragraph ,
  76. word_start , word_end , content , channel_id )
  77. VALUES ( ? , ? , ? , ? , ? , ? )";
  78. try{
  79. $stmt = $dbh->prepare($query);
  80. }catch(PDOException $e){
  81. Log::info($e);
  82. return 1;
  83. }
  84. $db = Sentence::whereIn('channel_uid',$channels);
  85. $bar = $this->output->createProgressBar($db->count());
  86. $srcDb = $db->select(['uid','book_id','paragraph',
  87. 'word_start','word_end',
  88. 'content','content_type','channel_uid',
  89. 'editor_uid','language','updated_at'])->cursor();
  90. foreach ($srcDb as $sent) {
  91. if(Str::isUuid($sent->channel_uid)){
  92. $channel = ChannelApi::getById($sent->channel_uid);
  93. $currData = array(
  94. $sent->book_id,
  95. $sent->paragraph,
  96. $sent->word_start,
  97. $sent->word_end,
  98. MdRender::render($sent->content,
  99. [$sent->channel_uid],
  100. null,
  101. 'read',
  102. $channel['type'],
  103. $sent->content_type,
  104. 'unity',
  105. ),
  106. $sent->channel_uid,
  107. );
  108. $stmt->execute($currData);
  109. }
  110. $bar->advance();
  111. }
  112. $dbh->commit();
  113. $bar->finish();
  114. return 0;
  115. }
  116. }