ExportChapterIndex.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. $exportFile = storage_path('app/public/export/offline/wikipali-offline-'.date("Y-m-d").'.db3');
  41. $dbh = new \PDO('sqlite:'.$exportFile, "", "", array(\PDO::ATTR_PERSISTENT => true));
  42. $dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
  43. $dbh->beginTransaction();
  44. $query = "INSERT INTO chapter ( id , book , paragraph,
  45. language , title , channel_id , progress,updated_at )
  46. VALUES ( ? , ? , ? , ? , ? , ? , ? , ? )";
  47. try{
  48. $stmt = $dbh->prepare($query);
  49. }catch(PDOException $e){
  50. Log::info($e);
  51. return 1;
  52. }
  53. $publicChannels = Channel::where('status',30)->select('uid')->get();
  54. $rows = ProgressChapter::whereIn('channel_id',$publicChannels)->count();
  55. Cache::put("/export/chapter/count",$rows,3600*10);
  56. $bar = $this->output->createProgressBar($rows);
  57. foreach (ProgressChapter::whereIn('channel_id',$publicChannels)
  58. ->select(['uid','book','para',
  59. 'lang','title','channel_id',
  60. 'progress','updated_at'])->cursor() as $row) {
  61. $currData = array(
  62. $row->uid,
  63. $row->book,
  64. $row->para,
  65. $row->lang,
  66. $row->title,
  67. $row->channel_id,
  68. $row->progress,
  69. $row->updated_at,
  70. );
  71. $stmt->execute($currData);
  72. $bar->advance();
  73. }
  74. $dbh->commit();
  75. $bar->finish();
  76. Log::debug('task export offline chapter-index-table finished');
  77. return 0;
  78. }
  79. }