SensitiveChapter.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Console\Commands\Temp;
  3. use Log;
  4. use Illuminate\Console\Command;
  5. use DB;
  6. class SensitiveChapter extends Command
  7. {
  8. /**
  9. * 执行命令 php artisan temp:sensitive_chapter
  10. *
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'temp:sensitive_chapter';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '敏感章节查询';
  22. /**
  23. * Execute the console command.
  24. *
  25. * @return mixed
  26. */
  27. public function handle()
  28. {
  29. print_r("======敏感章节查询 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
  30. Log::info("======敏感章节查询 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
  31. $bad_words = DB::table('bad_book_info')->pluck('badInfo')->all();
  32. dump($bad_words);
  33. $bid_stas = [];
  34. $start = 0;
  35. $n = time();
  36. for($i=1;$i<5000;$i++)
  37. {
  38. dump($i);
  39. $end = 1000 * $i;
  40. $chapters = DB::select("select * from chapters where id >={$start} and id <{$end}");
  41. dump("select * from chapters where id >={$start} and id <{$end}");
  42. $start = $end;
  43. dump('start:' . date("Y-m-d H:i:s"));
  44. if(!$chapters) break;//章节获取即退出
  45. foreach ($chapters as $chapter) {
  46. $one_chapter_has_words = [];
  47. foreach ($bad_words as $bad_word)
  48. {
  49. $bad_word = str_replace('/','\\',$bad_word);
  50. preg_match_all("/{$bad_word}/", $chapter->content, $matches);
  51. $current_count = count($matches[0]);
  52. if ($current_count) {
  53. $one_chapter_has_words[] = $bad_word;
  54. }
  55. }
  56. if($one_chapter_has_words)
  57. {
  58. //图书敏感章节数+1
  59. $bid_stas[$chapter->bid] = @$bid_stas[$chapter->bid] + 1;
  60. //插入章节敏感信息
  61. $_chapter_data = [
  62. 'bid'=>$chapter->bid,
  63. 'cid'=>$chapter->id,
  64. 'sequence'=>$chapter->sequence,
  65. 'name'=>$chapter->name,
  66. 'words'=>implode(',',$one_chapter_has_words),
  67. 'time'=>$n,
  68. 'created_at'=>date("Y-m-d H:i:s"),
  69. 'updated_at'=>date("Y-m-d H:i:s")
  70. ];
  71. DB::table('sensitive_chapters')->insert($_chapter_data);
  72. }
  73. }
  74. dump($bid_stas);
  75. dump('end:' . date("Y-m-d H:i:s"));
  76. }
  77. foreach($bid_stas as $bid=>$num)
  78. {
  79. $book_config = DB::table('book_configs')->select('book_name')->where('bid',$bid)->first();
  80. $_data = [
  81. 'bid'=>$bid,
  82. 'num'=>$num,
  83. 'time'=>$n,
  84. 'book_name'=>$book_config ? $book_config->book_name : '',
  85. 'created_at'=>date("Y-m-d H:i:s"),
  86. 'updated_at'=>date("Y-m-d H:i:s")
  87. ];
  88. DB::table('book_sensitive_stats')->insert($_data);
  89. }
  90. Log::info("======敏感章节查询 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
  91. print_r("======敏感章节查询 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
  92. }
  93. }