ExportPalitext.php 2.3 KB

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