Permalinks changed from id to title slug...
This commit is contained in:
parent
07aa53689e
commit
db4a48d08d
7 changed files with 267 additions and 35 deletions
55
sitemap.php
55
sitemap.php
|
|
@ -21,6 +21,34 @@ function sitemap_article_id_is_valid(string $id): bool {
|
|||
return (bool)preg_match('/^[0-9]{14}[a-f0-9]{16}$/', $id);
|
||||
}
|
||||
|
||||
function sitemap_slug_from_title(string $title, string $fallback = 'post'): string {
|
||||
$slug = strtr($title, [
|
||||
'æ'=>'ae','Æ'=>'ae','ø'=>'o','Ø'=>'o','å'=>'aa','Å'=>'aa',
|
||||
'ä'=>'ae','Ä'=>'ae','ö'=>'oe','Ö'=>'oe','ü'=>'ue','Ü'=>'ue','ß'=>'ss','ẞ'=>'ss',
|
||||
'č'=>'c','Č'=>'c','ć'=>'c','Ć'=>'c','ž'=>'z','Ž'=>'z','š'=>'s','Š'=>'s','đ'=>'d','Đ'=>'d',
|
||||
]);
|
||||
if (function_exists('iconv')) {
|
||||
$ascii = @iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $slug);
|
||||
if ($ascii !== false) $slug = $ascii;
|
||||
}
|
||||
$slug = strtolower($slug);
|
||||
$slug = preg_replace('/[^a-z0-9]+/', '-', $slug) ?? '';
|
||||
$slug = trim($slug, '-');
|
||||
return $slug !== '' ? $slug : $fallback;
|
||||
}
|
||||
|
||||
function sitemap_title_from_data_file(string $dir): string {
|
||||
$file = rtrim($dir, '/') . '/data.txt';
|
||||
if (is_file($file)) {
|
||||
foreach (file($file, FILE_IGNORE_NEW_LINES) ?: [] as $line) {
|
||||
if (!str_contains($line, ':')) continue;
|
||||
[$key, $value] = array_map('trim', explode(':', $line, 2));
|
||||
if (in_array(strtolower($key), ['title','name'], true) && $value !== '') return $value;
|
||||
}
|
||||
}
|
||||
return basename($dir);
|
||||
}
|
||||
|
||||
function sitemap_add_url(array &$urls, string $loc, string $lastmod, string $changefreq, string $priority): void {
|
||||
$urls[$loc] = [
|
||||
'loc' => $loc,
|
||||
|
|
@ -43,7 +71,10 @@ foreach ($staticPages as [$loc, $path, $changefreq, $priority]) {
|
|||
}
|
||||
}
|
||||
|
||||
foreach (glob(rtrim($uploadsDir, '/') . '/*', GLOB_ONLYDIR) ?: [] as $dir) {
|
||||
$articleEntries = [];
|
||||
$articleDirs = glob(rtrim($uploadsDir, '/') . '/*', GLOB_ONLYDIR) ?: [];
|
||||
sort($articleDirs, SORT_STRING);
|
||||
foreach ($articleDirs as $dir) {
|
||||
if (!is_file($dir . '/.mvlog-permalink')) continue;
|
||||
|
||||
$idFile = $dir . '/.mvlog-id';
|
||||
|
|
@ -68,11 +99,27 @@ foreach (glob(rtrim($uploadsDir, '/') . '/*', GLOB_ONLYDIR) ?: [] as $dir) {
|
|||
if (is_file($videoPath)) $lastmodCandidates[] = (int)filemtime($videoPath);
|
||||
}
|
||||
|
||||
$lastmodTs = $lastmodCandidates ? max($lastmodCandidates) : (int)filemtime($dir);
|
||||
$articleEntries[] = [
|
||||
'id' => $articleId,
|
||||
'base_slug' => sitemap_slug_from_title(sitemap_title_from_data_file($dir), $articleId),
|
||||
'lastmod' => sitemap_lastmod_from_ts($lastmodCandidates ? max($lastmodCandidates) : (int)filemtime($dir)),
|
||||
];
|
||||
}
|
||||
|
||||
$usedSlugs = [];
|
||||
foreach ($articleEntries as $entry) {
|
||||
$slug = $entry['base_slug'];
|
||||
if (isset($usedSlugs[$slug])) $slug = $entry['base_slug'] . '-' . substr($entry['id'], -6);
|
||||
$n = 2;
|
||||
while (isset($usedSlugs[$slug])) {
|
||||
$slug = $entry['base_slug'] . '-' . substr($entry['id'], -6) . '-' . $n;
|
||||
$n++;
|
||||
}
|
||||
$usedSlugs[$slug] = true;
|
||||
sitemap_add_url(
|
||||
$urls,
|
||||
'https://bubulescu.org/?id=' . rawurlencode($articleId),
|
||||
sitemap_lastmod_from_ts($lastmodTs),
|
||||
'https://bubulescu.org/post/' . rawurlencode($slug),
|
||||
$entry['lastmod'],
|
||||
'monthly',
|
||||
'0.8'
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue