1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
4
|
|
|
|
5
|
|
|
use App\Src\UseCases\Domain\Ports\UserRepository; |
6
|
|
|
use App\Src\UseCases\Domain\Users\EditUserStats; |
7
|
|
|
use Illuminate\Console\Command; |
8
|
|
|
use Illuminate\Support\Facades\DB; |
9
|
|
|
|
10
|
|
|
class ImportUsersStatsFromWiki extends Command |
11
|
|
|
{ |
12
|
|
|
protected $signature = 'users:import-wiki-stats'; |
13
|
|
|
|
14
|
|
|
protected $description = 'Import the wiki stats for all users'; |
15
|
|
|
|
16
|
|
|
private $dbWiki; |
17
|
|
|
private $userRepository; |
18
|
|
|
|
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
parent::__construct(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function handle(UserRepository $userRepository) |
25
|
|
|
{ |
26
|
|
|
$this->dbWiki = DB::connection('mysql_wiki'); |
27
|
|
|
$this->userRepository = $userRepository; |
28
|
|
|
|
29
|
|
|
$this->dbWiki->table('user') |
30
|
|
|
->orderBy('user.user_id', 'desc') |
31
|
|
|
->chunk(200, function ($records){ |
32
|
|
|
foreach ($records as $record) { |
33
|
|
|
|
34
|
|
|
$user = $this->userRepository->getByEmail($record->user_email); |
35
|
|
|
if(!isset($user)){ |
36
|
|
|
continue; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$questions = $this->getQuestionsNumber($record->user_id); |
40
|
|
|
$answers = $this->getAnswersNumber($record->user_id); |
41
|
|
|
$votes = $this->getVotesNumber($record->user_id); |
42
|
|
|
$editsOnWiki = $this->getEditsOnWikiNumber($record->user_id); |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
$numberContributions = $questions + $answers + $votes + $editsOnWiki; |
46
|
|
|
$fields = [ |
47
|
|
|
'number_contributions' => $numberContributions, |
48
|
|
|
'number_questions' => $questions, |
49
|
|
|
'number_answers' => $answers, |
50
|
|
|
'number_votes' => $votes, |
51
|
|
|
'number_validations' => 0, |
52
|
|
|
'number_wiki_edit' => $editsOnWiki, |
53
|
|
|
'number_contributions_last_30_days' => 0 |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
app(EditUserStats::class)->edit($user->id(), $fields); |
57
|
|
|
} |
58
|
|
|
}); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function getQuestionsNumber(int $userId):int |
62
|
|
|
{ |
63
|
|
|
$questions = $this->dbWiki->table("cs_watchlist", 'cw') |
64
|
|
|
->join('cs_comment_data', 'cs_comment_data.cst_page_id', 'cw.cst_wl_page_id') |
65
|
|
|
->whereNull('cs_comment_data.cst_parent_page_id') |
66
|
|
|
->where('cw.cst_wl_user_id', $userId) |
67
|
|
|
->selectRaw('count(*) as number_questions') |
68
|
|
|
->first(); |
69
|
|
|
return $questions->number_questions; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function getAnswersNumber(int $userId) |
73
|
|
|
{ |
74
|
|
|
$answers = $this->dbWiki->table("user", 'u') |
75
|
|
|
->join('actor', 'actor.actor_user', 'u.user_id') |
76
|
|
|
->join('revision_actor_temp', 'revision_actor_temp.revactor_actor', 'actor.actor_id') |
77
|
|
|
->join('cs_comment_data', 'cs_comment_data.cst_page_id', 'revision_actor_temp.revactor_page') |
78
|
|
|
->whereNotNull('cs_comment_data.cst_parent_page_id') |
79
|
|
|
->where('u.user_id', $userId) |
80
|
|
|
->selectRaw('count(*) as number_answers') |
81
|
|
|
->first(); |
82
|
|
|
return $answers->number_answers; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function getVotesNumber(int $userId) |
86
|
|
|
{ |
87
|
|
|
$votes = $this->dbWiki->table("cs_votes", 'cv') |
88
|
|
|
->where('cv.cst_v_user_id', $userId) |
89
|
|
|
->selectRaw('count(*) as number_votes') |
90
|
|
|
->first(); |
91
|
|
|
return $votes->number_votes; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function getEditsOnWikiNumber(int $userId) |
95
|
|
|
{ |
96
|
|
|
$editsOnWiki = $this->dbWiki->table("recentchanges", 'rc') |
97
|
|
|
->join('actor', 'actor.actor_id', 'rc.rc_actor') |
98
|
|
|
->join('user', 'user.user_id', 'actor.actor_user') |
99
|
|
|
->where('user.user_id', $userId) |
100
|
|
|
->where('rc.rc_source', 'mw.edit') |
101
|
|
|
->selectRaw('count(*) as number_edit') |
102
|
|
|
->first(); |
103
|
|
|
return $editsOnWiki->number_edit; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|