ExportChapterIndex.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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';
  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/wikipali-offline-'.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 (ProgressChapter::whereIn('channel_id',$publicChannels)
  61. ->select(['uid','book','para',
  62. 'lang','title','channel_id',
  63. 'progress','updated_at'])->cursor() as $row) {
  64. $currData = array(
  65. $row->uid,
  66. $row->book,
  67. $row->para,
  68. $row->lang,
  69. $row->title,
  70. $row->channel_id,
  71. $row->progress,
  72. $row->updated_at,
  73. );
  74. $stmt->execute($currData);
  75. $bar->advance();
  76. }
  77. $dbh->commit();
  78. $bar->finish();
  79. Log::debug('task export offline chapter-index-table finished');
  80. return 0;
  81. }
  82. }