ExportChapterIndex.php 2.9 KB

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