ExportSentence.php 4.1 KB

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