ExportSentence.php 4.2 KB

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