ExportSentence.php 4.0 KB

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