Commit 03f1ef19 authored by Pablo Rauzy's avatar Pablo Rauzy
Browse files

add sitemap.xml feature

parent d8628b42
......@@ -97,7 +97,7 @@ try {
$ok = $ok && $db->exec("insert into groups values (1, '$title')");
$ok = $ok && $db->exec("insert into pages values ('Home', 'index.html', '<h2>Welcome</h2><p>Hello, world!</p>', 0, 1, 'new')");
$ok = $ok && $db->exec("insert into pages values ('Not Found', '404.html', '<h2>404 Not Found</h2><p>☹</p>', 0, 0, 'new')");
$ok = $ok && $db->exec("insert into settings values ('url', '{$config['url']}', 't'), ('title', '$title', 't'), ('title_hformat', '%group% : %page% — %site%', 't'), ('title_bformat', '%group%', 't'), ('css', '$css', 't'), ('logo', '$logo', 't'), ('html', '', 't'), ('diskquota', '5242880', 'i'), ('rss', 'mod', 't'), ('pro', '0', 'b')");
$ok = $ok && $db->exec("insert into settings values ('url', '{$config['url']}', 't'), ('title', '$title', 't'), ('title_hformat', '%group% : %page% — %site%', 't'), ('title_bformat', '%group%', 't'), ('css', '$css', 't'), ('logo', '$logo', 't'), ('html', '', 't'), ('diskquota', '5242880', 'i'), ('rss', 'mod', 't'), ('sitemap', 'mod', 't'), ('pro', '0', 'b')");
if (!$ok || !$db->exec("commit")) {
$db->exec("rollback");
echo 'An error occurred while creating your database.';
......@@ -134,6 +134,7 @@ else {
// Fetch pages
$pages = array();
$nonpubmod = ($settings['rss'] != 'pub') ? 1 : 0;
$nonpubmod += ($settings['sitemap'] != 'pub') ? 1 : 0;
if (($res = $db->query("select * from pages order by grp asc, ord asc")) === false) {
echo 'An error occurred while retrieving your page list.'; exit;
}
......@@ -566,6 +567,21 @@ function rss ($num) {
return true;
}
// Build sitemap
function sitemap () {
global $settings, $pages;
$files = array_keys(array_filter($pages, function ($p) { return !in_array($p['status'], array('lnk', 'del', 'newdel')) && $p['grp'] > 0; }));
ob_start();
echo '<?xml version="1.0" encoding="utf-8"?>'; ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php foreach ($files as $f): ?>
<url><loc><?php echo $settings['url'],preg_replace('/index\.html$/', '', $f); ?></loc></url>
<?php endforeach; ?>
</urlset><?php
if (!file_put_contents($settings['dir'].'sitemap.xml', ob_get_clean())) { return false; }
return true;
}
// Publish website
if (isset($_GET['publish'])) {
if (!isset($_GET['ok'])) {
......@@ -584,6 +600,9 @@ if (isset($_GET['publish'])) {
if ($settings['rss'] == 'mod') {
$info[] = 'Rebuild <strong>RSS feed</strong>.';
}
if ($settings['sitemap'] == 'mod') {
$info[] = 'Rebuild <strong>Sitemap</strong>.';
}
confirm('publish website', 'publish', 'actions', $info);
}
else {
......@@ -625,6 +644,18 @@ if (isset($_GET['publish'])) {
$messages[] = $msg;
}
}
if ($settings['sitemap'] == 'mod') {
if (!sitemap()) {
$messages[] = 'Rebuilding <strong>Sitemap</strong>… FAILURE ☹.';
}
else {
$msg = 'Rebuilding <strong>Sitemap</strong>… SUCCESS ☺.';
if (!$db->exec("update settings set value='pub' where key='sitemap'")) {
$msg .= ' But failed to update databse.';
}
$messages[] = $msg;
}
}
inform($messages, 'actions');
}
}
......@@ -782,6 +813,7 @@ elseif (isset($_GET['newpage'])) {
if ($group > 0) {
$ok = $ok && $db->exec("update pages set status='mod' where status='pub' and grp='$group'");
}
if ($type != 'link') { $ok = $ok && $db->exec("update settings set value='mod' where key='sitemap'"); }
if (!$ok || !$db->exec("commit")) {
$db->exec("rollback");
inform(array('An error occurred while creating your new page.'), 'pages');
......@@ -800,6 +832,7 @@ elseif (isset($_GET['deletepage'])) {
$ok = $ok && $db->exec("update pages set status='mod' where status='pub' and grp='{$pages[$file]['grp']}'");
}
$ok = $ok && $db->exec("update pages set status='$del' where file='$file'");
if ($del == 'del') { $ok = $ok && $db->exec("update settings set value='mod' where key='sitemap'"); }
if (!$ok || !$db->exec("commit")) {
$db->exec("rollback");
inform(array('An error occurred while deleting the page.'), 'pages');
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment