ExportPalitext.php 2.4 KB

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