ExportChapterIndex.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Storage;
  5. use App\Models\ProgressChapter;
  6. use App\Models\Channel;
  7. use Illuminate\Support\Facades\Cache;
  8. use Illuminate\Support\Facades\Log;
  9. class ExportChapterIndex extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'export:chapter.index {db : db file name wikipali-offline or wikipali-offline-index}';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'export chapter index';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return int
  36. */
  37. public function handle()
  38. {
  39. Log::debug('task export offline chapter-index-table start');
  40. if (\App\Tools\Tools::isStop()) {
  41. return 0;
  42. }
  43. $exportFile = storage_path('app/public/export/offline/' . $this->argument('db') . '-' . date("Y-m-d") . '.db3');
  44. $dbh = new \PDO('sqlite:' . $exportFile, "", "", array(\PDO::ATTR_PERSISTENT => true));
  45. $dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
  46. $dbh->beginTransaction();
  47. $query = "INSERT INTO chapter ( id , book , paragraph,
  48. language , title , channel_id , progress,updated_at )
  49. VALUES ( ? , ? , ? , ? , ? , ? , ? , ? )";
  50. try {
  51. $stmt = $dbh->prepare($query);
  52. } catch (\PDOException $e) {
  53. Log::info($e);
  54. return 1;
  55. }
  56. $publicChannels = Channel::where('status', 30)->select('uid')->get();
  57. $rows = ProgressChapter::whereIn('channel_id', $publicChannels)->count();
  58. Cache::put("/export/chapter/count", $rows, 3600 * 10);
  59. $bar = $this->output->createProgressBar($rows);
  60. foreach (
  61. ProgressChapter::whereIn('channel_id', $publicChannels)
  62. ->select([
  63. 'uid',
  64. 'book',
  65. 'para',
  66. 'lang',
  67. 'title',
  68. 'channel_id',
  69. 'progress',
  70. 'updated_at'
  71. ])->cursor() as $row
  72. ) {
  73. $currData = array(
  74. $row->uid,
  75. $row->book,
  76. $row->para,
  77. $row->lang,
  78. $row->title,
  79. $row->channel_id,
  80. $row->progress,
  81. $row->updated_at,
  82. );
  83. $stmt->execute($currData);
  84. $bar->advance();
  85. }
  86. $dbh->commit();
  87. $bar->finish();
  88. Log::debug('task export offline chapter-index-table finished');
  89. return 0;
  90. }
  91. }