ExportSentence.php 4.1 KB

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