ExportPalitext.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Storage;
  5. use App\Models\PaliText;
  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. $exportFile = storage_path('app/public/export/offline/sentence-'.date("Y-m-d").'.db3');
  38. $dbh = new \PDO('sqlite:'.$exportFile, "", "", array(\PDO::ATTR_PERSISTENT => true));
  39. $dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
  40. $dbh->beginTransaction();
  41. $query = "INSERT INTO pali_text ( id , book , paragraph, level, toc,
  42. chapter_len , parent )
  43. VALUES ( ? , ? , ? , ? , ? , ? , ? )";
  44. try{
  45. $stmt = $dbh->prepare($query);
  46. }catch(PDOException $e){
  47. Log::info($e);
  48. return 1;
  49. }
  50. $bar = $this->output->createProgressBar(PaliText::count());
  51. foreach (PaliText::select(['uid','book','paragraph',
  52. 'level','toc','lenght','chapter_len',
  53. 'next_chapter','prev_chapter','parent','chapter_strlen'])
  54. ->orderBy('book')
  55. ->orderBy('paragraph')
  56. ->cursor() as $chapter) {
  57. $currData = array(
  58. $chapter->uid,
  59. $chapter->book,
  60. $chapter->paragraph,
  61. $chapter->level,
  62. $chapter->toc,
  63. $chapter->chapter_len,
  64. $chapter->parent,
  65. );
  66. $stmt->execute($currData);
  67. $bar->advance();
  68. }
  69. $dbh->commit();
  70. $bar->finish();
  71. return 0;
  72. }
  73. }