-
Notifications
You must be signed in to change notification settings - Fork 417
Expand file tree
/
Copy pathOsuWiki.php
More file actions
190 lines (157 loc) · 5.33 KB
/
OsuWiki.php
File metadata and controls
190 lines (157 loc) · 5.33 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.
namespace App\Libraries;
use App\Exceptions\GitHubInvalidDataException;
use App\Exceptions\GitHubNotFoundException;
use App\Exceptions\GitHubTooLargeException;
use App\Jobs\UpdateWiki;
use GitHub;
use Github\Exception\RuntimeException as GithubException;
use Illuminate\Support\Collection;
class OsuWiki
{
const CACHE_DURATION = 60;
public array $data;
public function __construct(public string $path)
{
$this->data = $this->fetch();
}
public static function cleanPath($path)
{
return preg_replace('|//+|', '/', trim($path, '/'));
}
public static function getPageList(): Collection
{
return collect(static::getTree()['tree'])
->filter(function ($item) {
return $item['type'] === 'blob'
&& starts_with($item['path'], 'wiki/')
&& ends_with($item['path'], '.md');
})
->pluck('path');
}
public static function getTree(?string $sha = null, bool $recursive = true): array
{
return GitHub::gitData()->trees()->show(static::user(), static::repository(), $sha ?? static::branch(), $recursive);
}
public static function fetchContent($path)
{
return (new static($path))->content();
}
/**
* Parses a github repository path of a wiki file,
* and returns informations such as type, wiki path and locale.
*/
public static function parseGithubPath(string $path): ?array
{
$matches = [];
if (starts_with($path, 'wiki/')) {
if ($path === 'wiki/redirect.yaml') {
return ['type' => 'redirect'];
}
$found = preg_match('/^(?:wiki\/)(.*)\/(.*)\.(.{2,})$/', $path, $matches);
if ($found > 0) {
if (static::isImage($path)) {
return [
'type' => 'image',
'path' => $matches[1].'/'.$matches[2].'.'.$matches[3],
];
} else {
return [
'type' => 'page',
'locale' => $matches[2],
'path' => $matches[1],
];
}
}
} elseif (starts_with($path, 'news/')) {
$found = preg_match('/^(?:news\/)(?:\d{4}\/)?(.*)\.md$/', $path, $matches);
if ($found > 0) {
return [
'type' => 'news_post',
'slug' => $matches[1],
];
}
}
return null;
}
public static function updateFromGithub($data)
{
dispatch(new UpdateWiki($data['before'], $data['after']));
}
public static function getUpdatedFiles($old, $new)
{
$diff = GitHub::repo()
->commits()
->compare(static::user(), static::repository(), $old, $new);
return $diff['files'];
}
public static function fetchLastCommitDate(string $path): ?string
{
try {
$commits = GitHub::repo()
->commits()
->all(static::user(), static::repository(), [
'path' => $path,
'sha' => static::branch(),
'per_page' => 1,
]);
return $commits[0]['commit']['committer']['date'] ?? null;
} catch (GithubException $e) {
return null;
}
}
public static function fetchCommitDate(string $hash): ?string
{
try {
$commit = GitHub::repo()
->commits()
->show(static::user(), static::repository(), $hash);
return $commit['commit']['committer']['date'] ?? null;
} catch (GithubException $e) {
return null;
}
}
public static function isImage($path)
{
return preg_match('/\.(?:jpe?g|gif|png)$/i', $path) === 1;
}
public static function branch()
{
return $GLOBALS['cfg']['osu']['wiki']['branch'];
}
public static function repository()
{
return $GLOBALS['cfg']['osu']['wiki']['repository'];
}
public static function user()
{
return $GLOBALS['cfg']['osu']['wiki']['user'];
}
public function content()
{
return base64_decode($this->data['content'], true);
}
private function fetch(): array
{
try {
$ret = GitHub::repo()
->contents()
->show(static::user(), static::repository(), $this->path, static::branch());
if (!is_array($ret) || !isset($ret['content'])) {
$json = json_encode($ret);
throw new GitHubInvalidDataException("Received object for '{$this->path}' is missing content field: {$json}");
}
return $ret;
} catch (GithubException $e) {
$message = $e->getMessage();
if ($e->getCode() === 404) {
throw new GitHubNotFoundException($message);
} elseif (starts_with($message, 'This API returns blobs up to 1 MB in size.')) {
throw new GitHubTooLargeException($message);
}
throw $e;
}
}
}