|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpEarth\Stats\Command; |
|
4
|
|
|
|
|
5
|
|
|
use PhpEarth\Stats\Fetcher; |
|
6
|
|
|
use Symfony\Component\Console\Command\Command; |
|
7
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Question\Question; |
|
12
|
|
|
use PhpEarth\Stats\Auth; |
|
13
|
|
|
use PhpEarth\Stats\Config; |
|
14
|
|
|
use PhpEarth\Stats\Mapper; |
|
15
|
|
|
use Twig_Environment; |
|
16
|
|
|
use Twig_Template; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class GenerateCommand |
|
20
|
|
|
* @package PhpEarth\Stats\Command |
|
21
|
|
|
*/ |
|
22
|
|
|
class GenerateCommand extends Command |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $reportsDir; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var Twig_Template |
|
31
|
|
|
*/ |
|
32
|
|
|
private $template; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var Config |
|
36
|
|
|
*/ |
|
37
|
|
|
private $config; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var Auth |
|
41
|
|
|
*/ |
|
42
|
|
|
private $auth; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var Translator |
|
|
|
|
|
|
46
|
|
|
*/ |
|
47
|
|
|
private $translator; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Set configuration. |
|
51
|
|
|
* |
|
52
|
|
|
* @param Config $config |
|
53
|
|
|
*/ |
|
54
|
|
|
public function setConfig(Config $config) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->config = $config; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Set template. |
|
61
|
|
|
* |
|
62
|
|
|
* @param Twig_Environment $twig |
|
63
|
|
|
*/ |
|
64
|
|
|
public function setTemplate(Twig_Environment $twig) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->template = $twig->loadTemplate('report.txt.twig'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Set authentication. |
|
71
|
|
|
* |
|
72
|
|
|
* @param Auth $auth |
|
73
|
|
|
*/ |
|
74
|
|
|
public function setAuth(Auth $auth) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->auth = $auth; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Set Reports directory |
|
81
|
|
|
*/ |
|
82
|
|
|
public function setReportsDir($reportsDir) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->reportsDir = $reportsDir; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Set Translator |
|
89
|
|
|
*/ |
|
90
|
|
|
public function setTranslator($translator) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->translator = $translator; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Configures generate command for Console component. |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function configure() |
|
99
|
|
|
{ |
|
100
|
|
|
try { |
|
101
|
|
|
$this |
|
102
|
|
|
->setName('generate') |
|
103
|
|
|
->setDescription('Generates Facebook group stats report.') |
|
104
|
|
|
->setHelp('This command generates Facebook group stats report.') |
|
105
|
|
|
->addOption( |
|
106
|
|
|
'from', |
|
107
|
|
|
'f', |
|
108
|
|
|
InputOption::VALUE_REQUIRED, |
|
109
|
|
|
'Start date of the generated stats report ('.date('Y-m-d', strtotime('last monday')).')', |
|
110
|
|
|
date('Y-m-d', strtotime('last monday')) |
|
111
|
|
|
) |
|
112
|
|
|
->addOption( |
|
113
|
|
|
'to', |
|
114
|
|
|
't', |
|
115
|
|
|
InputOption::VALUE_REQUIRED, |
|
116
|
|
|
'End date of the generated stats report ('.date('Y-m-d', strtotime('last monday +7 days')).')', |
|
117
|
|
|
date('Y-m-d', strtotime('last monday +7 days')) |
|
118
|
|
|
) |
|
119
|
|
|
->addOption( |
|
120
|
|
|
'animation', |
|
121
|
|
|
'a', |
|
122
|
|
|
InputOption::VALUE_NONE, |
|
123
|
|
|
'Animation of group activity visualized with Gource' |
|
124
|
|
|
) |
|
125
|
|
|
; |
|
126
|
|
|
} catch (\Exception $e) { |
|
127
|
|
|
echo $e->getMessage(); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @inheritdoc |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
135
|
|
|
{ |
|
136
|
|
|
$fromDate = \DateTime::createFromFormat('Y-m-d H:i:s', $input->getOption('from').' 00:00:00'); |
|
137
|
|
|
$fromDate->setTimezone(new \DateTimeZone('UTC')); |
|
138
|
|
|
$this->config->setParameter('start_datetime', $fromDate); |
|
139
|
|
|
|
|
140
|
|
|
$toDate = \DateTime::createFromFormat('Y-m-d H:i:s', $input->getOption('to').' 00:00:00'); |
|
141
|
|
|
$toDate->setTimezone(new \DateTimeZone('UTC')); |
|
142
|
|
|
$this->config->setParameter('end_datetime', $toDate); |
|
143
|
|
|
|
|
144
|
|
|
$progress = new ProgressBar($output, 40); |
|
145
|
|
|
$progress->setFormat(" %message%\n %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%\n\n"); |
|
146
|
|
|
$progress->setMessage('Starting...'); |
|
147
|
|
|
$progress->setProgressCharacter("\xF0\x9F\x8D\xBA"); |
|
148
|
|
|
|
|
149
|
|
|
if (!$this->auth->isValid()) { |
|
150
|
|
|
$helper = $this->getHelper('question'); |
|
151
|
|
|
|
|
152
|
|
|
$question = new Question($this->auth->getError()."\n\n". |
|
153
|
|
|
"Enter user access token from the Graph API Explorer\n". |
|
154
|
|
|
"https://developers.facebook.com/tools/explorer\n"); |
|
155
|
|
|
$auth = $this->auth; |
|
156
|
|
|
$question->setValidator(function ($token) use ($auth) { |
|
157
|
|
|
if (trim($token) == '') { |
|
158
|
|
|
throw new \Exception('The token can not be empty'); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
$auth->setToken($token); |
|
162
|
|
|
|
|
163
|
|
|
if (!$auth->isValid()) { |
|
164
|
|
|
throw new \Exception($auth->getError()); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
return $token; |
|
168
|
|
|
}); |
|
169
|
|
|
|
|
170
|
|
|
$question->setHidden(true); |
|
171
|
|
|
$question->setMaxAttempts(20); |
|
172
|
|
|
|
|
173
|
|
|
$helper->ask($input, $output, $question); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
$output->writeln('Generating report from '.$this->config->getParameter('start_datetime')->format('c').' to '.$this->config->getParameter('end_datetime')->format('c')."\n"); |
|
177
|
|
|
$progress->start(); |
|
178
|
|
|
$progress->setMessage('Setting up Facebook service...'); |
|
179
|
|
|
$progress->advance(); |
|
180
|
|
|
|
|
181
|
|
|
try { |
|
182
|
|
|
$fetcher = new Fetcher($this->config, $progress, $this->auth->fb); |
|
183
|
|
|
$mapper = new Mapper($this->config, $fetcher->getFeed()); |
|
184
|
|
|
$topics = $mapper->getTopics(); |
|
185
|
|
|
$comments = $mapper->getComments(); |
|
186
|
|
|
$replies = $mapper->getReplies(); |
|
187
|
|
|
$users = $mapper->getUsers(); |
|
188
|
|
|
$newMembers = $fetcher->getNewMembers(); |
|
189
|
|
|
|
|
190
|
|
|
$progress->advance(); |
|
191
|
|
|
|
|
192
|
|
|
$progress->finish(); |
|
193
|
|
|
$output->writeln("\n"); |
|
194
|
|
|
|
|
195
|
|
|
$start = $this->config->getParameter('start_datetime'); |
|
196
|
|
|
$start->setTimezone(new \DateTimeZone(date_default_timezone_get())); |
|
197
|
|
|
|
|
198
|
|
|
$end = $this->config->getParameter('end_datetime'); |
|
199
|
|
|
$end->setTimezone(new \DateTimeZone(date_default_timezone_get())); |
|
200
|
|
|
|
|
201
|
|
|
$output->writeln($this->template->render([ |
|
202
|
|
|
'translator' => $this->translator, |
|
203
|
|
|
'start_date' => $start->format('Y-m-d'), |
|
204
|
|
|
'end_date' => $end->format('Y-m-d'), |
|
205
|
|
|
'new_members' => count($newMembers), |
|
206
|
|
|
'top_members' => $this->config->getParameter('top_users_count'), |
|
207
|
|
|
'banned_count' => $this->config->getParameter('new_blocked_count') - $this->config->getParameter('last_blocked_count'), |
|
208
|
|
|
'topics' => $topics, |
|
209
|
|
|
'active_members' => $users->count(), |
|
210
|
|
|
'new_comments' => $comments->count(), |
|
211
|
|
|
'new_replies' => $replies->count(), |
|
212
|
|
|
'top_users' => $users->getTopUsers($this->config->getParameter('top_users_count'), $this->config->getParameter('ignored_users')), |
|
213
|
|
|
'commits_count' => 3, |
|
214
|
|
|
'top_topics' => $this->config->getParameter('top_topics'), |
|
215
|
|
|
'group_id' => $this->config->getParameter('group_id') |
|
216
|
|
|
])); |
|
217
|
|
|
|
|
218
|
|
|
$this->generateReports($users, $topics, $newMembers); |
|
219
|
|
|
|
|
220
|
|
|
if ($input->getOption('animation')) { |
|
221
|
|
|
$this->generateGourceLog($topics, $comments, $replies); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
} catch (\Exception $e) { |
|
225
|
|
|
$output->writeln($e->getMessage()); |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
private function generateReports($users, $topics, $newMembers) |
|
230
|
|
|
{ |
|
231
|
|
|
// Create reports folder |
|
232
|
|
|
if (!file_exists($this->reportsDir)) { |
|
233
|
|
|
mkdir($this->reportsDir); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
// Contributors |
|
237
|
|
|
foreach ($users->getTopUsers() as $id => $user) { |
|
238
|
|
|
$msg = $id."\t"; |
|
239
|
|
|
$msg .= $user->getName()."\t"; |
|
240
|
|
|
$msg .= 'Points: '.$user->getPointsCount()."\t"; |
|
241
|
|
|
$msg .= 'Topics: '.$user->getTopicsCount()."\t"; |
|
242
|
|
|
$msg .= 'Comments: '.$user->getCommentsCount()."\n"; |
|
243
|
|
|
file_put_contents($this->reportsDir.'/contributors.txt', $msg, FILE_APPEND | LOCK_EX); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
// Topics |
|
247
|
|
|
foreach ($topics as $topic) { |
|
248
|
|
|
$msg = $topic->getId()."\t"; |
|
249
|
|
|
$msg .= ' Reactions: '.$topic->getReactionsCount()."\t"; |
|
250
|
|
|
$msg .= ' Comments: '.$topic->getCommentsCount()."\n"; |
|
251
|
|
|
file_put_contents($this->reportsDir.'/topics.txt', $msg, FILE_APPEND | LOCK_EX); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
// New members |
|
255
|
|
|
foreach ($newMembers as $member) { |
|
256
|
|
|
$msg = $member[0]."\t".$member[1]."\n"; |
|
257
|
|
|
file_put_contents($this->reportsDir.'/members.txt', $msg, FILE_APPEND | LOCK_EX); |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
private function generateGourceLog($topics, $comments, $replies) |
|
262
|
|
|
{ |
|
263
|
|
|
$count = ceil($topics->count()/10); |
|
264
|
|
|
$msg = ''; |
|
265
|
|
|
foreach ($topics as $topic) { |
|
266
|
|
|
$topicTitle = ($topic->getMessage()) ? substr($topic->getMessage(), 0, 20) : $topic->getId(); |
|
267
|
|
|
//$topicTitle = trim(preg_replace('/\s\s+/', ' ', $topicTitle)); |
|
268
|
|
|
$topicTitle = preg_replace('/\s+/', ' ', trim($topicTitle)); |
|
269
|
|
|
$topicTitle = sha1($topicTitle); |
|
270
|
|
|
|
|
271
|
|
|
$name = ($topic->getUser()) ? $topic->getUser()->getName() : ''; |
|
272
|
|
|
|
|
273
|
|
|
$msg .= $topic->getCreatedTime()->getTimeStamp().'|'.($name).'|A|'.'/'.$count.'/'.$topic->getId().'/'.$topicTitle.".Topics\n"; |
|
274
|
|
|
} |
|
275
|
|
View Code Duplication |
foreach ($comments as $comment) { |
|
|
|
|
|
|
276
|
|
|
$commentMessage = substr($comment->getMessage(), 0, 30); |
|
277
|
|
|
$commentMessage = trim(preg_replace('/\s\s+/', ' ', $commentMessage)); |
|
278
|
|
|
$commentMessage = sha1($commentMessage); |
|
279
|
|
|
|
|
280
|
|
|
$msg .= $comment->getCreatedTime()->getTimeStamp().'|'.$comment->getUser()->getName().'|A|'.'/'.$count.'/'.$comment->getTopicId().'/'.$comment->getId().'/'.$commentMessage.".Comments\n"; |
|
281
|
|
|
} |
|
282
|
|
View Code Duplication |
foreach($replies as $reply) { |
|
|
|
|
|
|
283
|
|
|
$replyMessage = substr($reply->getMessage(), 0, 30); |
|
284
|
|
|
$replyMessage = trim(preg_replace('/\s\s+/', ' ', $replyMessage)); |
|
285
|
|
|
$replyMessage = sha1($replyMessage); |
|
286
|
|
|
|
|
287
|
|
|
$msg .= $reply->getCreatedTime()->getTimeStamp().'|'.$reply->getUser()->getName().'|A|'.'/'.$count.'/'.$reply->getTopicId().'/'.$reply->getCommentId().'/'.$reply->getId().'/'.$replyMessage.".Replies\n"; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
file_put_contents($this->reportsDir.'/../gource.log', $msg, FILE_APPEND | LOCK_EX); |
|
291
|
|
|
|
|
292
|
|
|
$data = file($this->reportsDir.'/../gource.log'); |
|
293
|
|
|
natsort($data); |
|
294
|
|
|
$refactored = []; |
|
295
|
|
|
$count = 0; |
|
|
|
|
|
|
296
|
|
|
foreach ($data as $line) { |
|
297
|
|
|
$items = explode('|', $line); |
|
298
|
|
|
$items[3] = str_replace("\n", "", $items[3]); |
|
299
|
|
|
$items[] = $this->randomColor()."\n"; |
|
300
|
|
|
$items = implode('|', $items); |
|
301
|
|
|
$refactored[] = $items; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
file_put_contents($this->reportsDir.'/../gource_sorted.log', $refactored); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
private function randomColorPart() |
|
308
|
|
|
{ |
|
309
|
|
|
return str_pad(dechex(mt_rand(0, 255)), 2, '0', STR_PAD_LEFT); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
private function randomColor() |
|
313
|
|
|
{ |
|
314
|
|
|
return $this->randomColorPart().$this->randomColorPart().$this->randomColorPart(); |
|
315
|
|
|
} |
|
316
|
|
|
} |
|
317
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths