ExportSentence.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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::error($e->getMessage(), ['exception' => $e]);
  84. return 1;
  85. }
  86. $db = Sentence::whereIn('channel_uid', $channels);
  87. $bar = $this->output->createProgressBar($db->count());
  88. $srcDb = $db->select([
  89. 'uid',
  90. 'book_id',
  91. 'paragraph',
  92. 'word_start',
  93. 'word_end',
  94. 'content',
  95. 'content_type',
  96. 'channel_uid',
  97. 'editor_uid',
  98. 'language',
  99. 'updated_at'
  100. ])->cursor();
  101. foreach ($srcDb as $sent) {
  102. if (Str::isUuid($sent->channel_uid)) {
  103. $channel = ChannelApi::getById($sent->channel_uid);
  104. $currData = array(
  105. $sent->book_id,
  106. $sent->paragraph,
  107. $sent->word_start,
  108. $sent->word_end,
  109. MdRender::render(
  110. $sent->content,
  111. [$sent->channel_uid],
  112. null,
  113. 'read',
  114. $channel['type'],
  115. $sent->content_type,
  116. 'unity',
  117. ),
  118. $sent->channel_uid,
  119. );
  120. $stmt->execute($currData);
  121. }
  122. $bar->advance();
  123. }
  124. $dbh->commit();
  125. $bar->finish();
  126. Log::debug('task export sentence finished');
  127. return 0;
  128. }
  129. }