ExportCreateDb.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Storage;
  5. use Illuminate\Support\Facades\Log;
  6. class ExportCreateDb extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'export:create.db';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Command description';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return int
  33. */
  34. public function handle()
  35. {
  36. Log::debug('task export offline create-db start');
  37. if(\App\Tools\Tools::isStop()){
  38. return 0;
  39. }
  40. $sqlPath = database_path('export/sentence.sql');
  41. $exportDir = storage_path('app/public/export/offline');
  42. $exportFile = $exportDir.'/wikipali-offline-'.date("Y-m-d").'.db3';
  43. $file = fopen($exportFile,'w');
  44. fclose($file);
  45. $dbh = new \PDO('sqlite:'.$exportFile, "", "", array(\PDO::ATTR_PERSISTENT => true));
  46. $dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
  47. //建立数据库
  48. $_sql = file_get_contents($sqlPath);
  49. $_arr = explode(';', $_sql);
  50. //执行sql语句
  51. foreach ($_arr as $_value) {
  52. $dbh->query($_value . ';');
  53. }
  54. Log::debug('task export offline create-db finished');
  55. return 0;
  56. }
  57. }