-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate_sitemap.php
More file actions
75 lines (64 loc) · 2.54 KB
/
generate_sitemap.php
File metadata and controls
75 lines (64 loc) · 2.54 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
<?php
// run
// php generate_sitemap.php
require_once 'vendor/autoload.php'; // Pastikan Anda telah menginstal symfony/yaml melalui Composer
require_once 'functions.php'; // Pastikan file ini ada dan berisi fungsi getPost() dan getPosts()
use Symfony\Component\Yaml\Yaml;
function getConfig() {
$configFile = 'config.yml';
if (!file_exists($configFile)) {
die("Error: config.yml file not found.\n");
}
return Yaml::parseFile($configFile);
}
function generateSitemap() {
$config = getConfig();
$baseUrl = $config['url'] ?? 'https://example.com';
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"/>');
// Tambahkan halaman utama
addUrlToSitemap($xml, $baseUrl, 'daily', '1.0');
// Tambahkan halaman statis
$staticPages = getPost(null, 'page');
if (is_array($staticPages)) {
foreach ($staticPages as $slug => $page) {
$pageUrl = $baseUrl . '/page/' . $slug;
$lastMod = file_exists('pages/' . $slug . '.md') ? date('Y-m-d', filemtime('pages/' . $slug . '.md')) : date('Y-m-d');
addUrlToSitemap($xml, $pageUrl, 'weekly', '0.9', $lastMod);
}
} else {
echo "Warning: No static pages found or error in getPost() function.\n";
}
// Ambil semua post
$page = 1;
do {
$result = getPosts($page);
if (isset($result['posts']) && is_array($result['posts'])) {
foreach ($result['posts'] as $post) {
$postUrl = $baseUrl . '/post/' . $post['slug'];
$lastMod = file_exists('posts/' . $post['slug'] . '.md') ? date('Y-m-d', filemtime('posts/' . $post['slug'] . '.md')) : date('Y-m-d');
addUrlToSitemap($xml, $postUrl, 'weekly', '0.8', $lastMod);
}
$page++;
} else {
echo "Warning: No posts found or error in getPosts() function.\n";
break;
}
} while ($page <= ($result['totalPages'] ?? 0));
// Simpan sitemap
if ($xml->asXML('sitemap.xml')) {
echo "Sitemap generated successfully!\n";
} else {
echo "Error: Unable to save sitemap.xml\n";
}
}
function addUrlToSitemap($xml, $loc, $changefreq, $priority, $lastmod = null) {
$url = $xml->addChild('url');
$url->addChild('loc', $loc);
if ($lastmod) {
$url->addChild('lastmod', $lastmod);
}
$url->addChild('changefreq', $changefreq);
$url->addChild('priority', $priority);
}
// Panggil fungsi untuk menghasilkan sitemap
generateSitemap();