-
Notifications
You must be signed in to change notification settings - Fork 0
/
genstats.php
152 lines (129 loc) · 3.17 KB
/
genstats.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
// SPDX-License-Identifier: GPL-2.0
/*
* Stats generator for CP_ID repo.
*
* Copyright (C) 2021 Ammar Faizi <ammarfaizi2@gmail.com>
*/
function dir_has_readme_txt(string $dir)
{
return file_exists($dir."/README.txt");
}
function fetch_solutions(string $dir): array
{
$ret = [];
$scan = scandir($dir);
foreach ($scan as $f) {
if ($f === "." || $f === "..")
continue;
$ret[] = $f;
}
return $ret;
}
function fetch_users(array &$ret, string $basedir, string $dir)
{
$scan = scandir($dir);
$rldir = explode($basedir."/", $dir, 2)[1];
foreach ($scan as $f) {
if ($f === "." || $f === "..")
continue;
$user = $f;
$f = $dir."/".$f;
if (is_dir($f))
$ret[$user][$rldir] = fetch_solutions($f);
}
}
function _scan_user_p(array &$ret, string $basedir, string $dir): void
{
$scan = scandir($dir);
foreach ($scan as $f) {
if ($f === "." || $f === "..")
continue;
$f = $dir."/".$f;
if (is_dir($f)) {
if (dir_has_readme_txt($f))
fetch_users($ret, $basedir, $f);
else
_scan_user_p($ret, $basedir, $f);
}
}
}
function scan_user_p(array &$ret, string $basedir): void
{
_scan_user_p($ret, $basedir, $basedir);
}
function genstats(string &$readmeStub, string $basedir, string $hashFile,
string $stubString): int
{
$oldHash = file_exists($hashFile) ? trim(file_get_contents($hashFile)) : "";
$ret = [];
scan_user_p($ret, $basedir);
ob_start();
printf("-------------------------------------------------------\n");
foreach ($ret as $k => $v) {
$buffer = "";
$count = 0;
foreach ($v as $kk => $vv) {
foreach ($vv as $kkk => $vvv) {
$buffer .= "\t{$kk} ({$vvv})\n";
$count++;
}
}
printf("%s (%d):\n%s\n", $k, $count, $buffer);
}
printf("# Generated at {{generated_at}}\n");
printf("-------------------------------------------------------");
$out = ob_get_clean();
$newHash = sha1($out);
$ret = ($oldHash !== $newHash);
if ($ret)
file_put_contents($hashFile, $newHash."\n");
$out = str_replace("{{generated_at}}", date("c"), $out);
$readmeStub = str_replace($stubString, $out, $readmeStub);
return $ret;
}
function genstats_codewars(string &$readmeStub, string $baseDir,
string $hashFile): int
{
return genstats($readmeStub, $baseDir, $hashFile, "{{codewars_stats}}");
}
function genstats_toki(string &$readmeStub, string $baseDir,
string $hashFile): int
{
return genstats($readmeStub, $baseDir, $hashFile, "{{toki_stats}}");
}
function main(?int $argc, ?array $argv): int
{
$ret = 0;
if (PHP_SAPI !== "cli") {
printf("This script can only be run in CLI environment!\n");
exit(1);
}
if (!$argv || !$argv) {
printf("Invalid argv/argc!\n");
exit(1);
}
if ($argc > 1)
goto out_usage;
$readmeStub = file_get_contents(__DIR__."/README.stub.md");
$ret |= genstats_codewars(
$readmeStub,
__DIR__."/Codewars",
__DIR__."/.stats_hash/Codewars.lock"
);
$ret |= genstats_toki(
$readmeStub,
__DIR__."/Toki",
__DIR__."/.stats_hash/Toki.lock"
);
var_dump($ret);
if ($ret)
file_put_contents(__DIR__."/README.md", $readmeStub);
return 0;
out_usage:
printf("Usage: php %s\n", $argv[0]);
return $ret;
}
$argv = $_SERVER["argv"] ?? NULL;
$argc = $_SERVER["argc"] ?? NULL;
exit(main($argc, $argv));