ExportPalitext.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\PaliText;
  4. use App\Tools\Tools;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\Log;
  7. class ExportPalitext extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'export:pali.text';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '导出离线用的巴利段落数据';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return int
  34. */
  35. public function handle()
  36. {
  37. Log::debug('task export offline palitext-table start');
  38. if (Tools::isStop()) {
  39. return 0;
  40. }
  41. $exportFile = storage_path('app/public/export/offline/wikipali-offline-'.date('Y-m-d').'.db3');
  42. $dbh = new \PDO('sqlite:'.$exportFile, '', '', [\PDO::ATTR_PERSISTENT => true]);
  43. $dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
  44. $dbh->beginTransaction();
  45. $query = 'INSERT INTO pali_text ( id , book , paragraph, level, toc,
  46. chapter_len , parent )
  47. VALUES ( ? , ? , ? , ? , ? , ? , ? )';
  48. try {
  49. $stmt = $dbh->prepare($query);
  50. } catch (PDOException $e) {
  51. Log::error($e->getMessage(), ['exception' => $e]);
  52. return 1;
  53. }
  54. $bar = $this->output->createProgressBar(PaliText::count());
  55. foreach (PaliText::select(['uid', 'book', 'paragraph',
  56. 'level', 'toc', 'lenght', 'chapter_len',
  57. 'next_chapter', 'prev_chapter', 'parent', 'chapter_strlen'])
  58. ->orderBy('book')
  59. ->orderBy('paragraph')
  60. ->cursor() as $chapter) {
  61. $currData = [
  62. $chapter->uid,
  63. $chapter->book,
  64. $chapter->paragraph,
  65. $chapter->level,
  66. $chapter->toc,
  67. $chapter->chapter_len,
  68. $chapter->parent,
  69. ];
  70. $stmt->execute($currData);
  71. $bar->advance();
  72. }
  73. $dbh->commit();
  74. $bar->finish();
  75. Log::debug('task: export offline palitext-table finished');
  76. return 0;
  77. }
  78. }