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