CacheDictPreference.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\UserDict;
  4. use App\Tools\Tools;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\Cache;
  7. use Illuminate\Support\Facades\DB;
  8. class CacheDictPreference extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'cache:dict.preference';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '从第三方字典中提取首选项';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return int
  35. */
  36. public function handle()
  37. {
  38. if (Tools::isStop()) {
  39. return 0;
  40. }
  41. $prefix = 'dict-preference';
  42. $words = UserDict::select(['word', 'language'])
  43. ->groupBy(['word', 'language'])
  44. ->cursor();
  45. $wordCount = DB::select('SELECT count(*) from (
  46. SELECT word,language from user_dicts group by word,language) T');
  47. $bar = $this->output->createProgressBar($wordCount[0]->count);
  48. $count = 0;
  49. foreach ($words as $key => $word) {
  50. $meaning = UserDict::where('word', $word->word)
  51. ->where('language', $word->language)
  52. ->where('source', '_PAPER_RICH_')
  53. ->whereNotNull('mean')
  54. ->value('mean');
  55. $meaning = trim($meaning, ' $');
  56. if (! empty($meaning)) {
  57. $m = explode('$', $meaning);
  58. Cache::put("{$prefix}/{$word->word}/{$word->language}", $m[0]);
  59. }
  60. $bar->advance();
  61. $count++;
  62. if ($count % 1000 === 0) {
  63. if (Tools::isStop()) {
  64. return 0;
  65. }
  66. }
  67. }
  68. $bar->finish();
  69. return 0;
  70. }
  71. }