ExportPalitext.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. Log::debug('task export offline palitext-table start');
  38. if(\App\Tools\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, "", "", array(\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::info($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 = array(
  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. }