aboutsummaryrefslogtreecommitdiffstats
path: root/cms-backd/src/sitemap.rs
blob: 46a6e989a10d92a00c24c9e6c3eb72e1b22500ed (plain)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// -*- coding: utf-8 -*-
//
// Simple CMS
//
// Copyright (C) 2011-2024 Michael Büsch <m@bues.ch>
//
// Licensed under the Apache License version 2.0
// or the MIT license, at your option.
// SPDX-License-Identifier: Apache-2.0 OR MIT

use crate::{
    comm::{CmsComm, CommGetPage, CommPage, CommSubPages},
    config::CmsConfig,
};
use anyhow as ah;
use chrono::prelude::*;
use cms_ident::{CheckedIdent, UrlComp};
use std::{fmt::Write as _, sync::Arc, write as wr, writeln as ln};

const MAX_DEPTH: usize = 64;
const DEFAULT_ELEMS_ALLOC: usize = 256;
const DEFAULT_HTML_ALLOC: usize = 1024 * 16;

fn xml_escape(mut s: String) -> String {
    if !s.is_empty() {
        if s.contains('&') {
            s = s.replace('&', "&amp;");
        }
        if s.contains('\'') {
            s = s.replace('\'', "&apos;");
        }
        if s.contains('"') {
            s = s.replace('"', "&quot;");
        }
        if s.contains('>') {
            s = s.replace('>', "&gt;");
        }
        if s.contains('<') {
            s = s.replace('<', "&lt;");
        }
    }
    s
}

pub struct SiteMapContext<'a> {
    pub comm: &'a mut CmsComm,
    pub config: Arc<CmsConfig>,
    pub root: &'a CheckedIdent,
    pub protocol: &'a str,
}

struct SiteMapElem {
    loc: String,
    lastmod: String,
    changefreq: String,
    priority: String,
}

async fn do_build_elems(
    ctx: &mut SiteMapContext<'_>,
    elems: &mut Vec<SiteMapElem>,
    ident: &CheckedIdent,
    stamp: DateTime<Utc>,
    nav_stop: bool,
    depth: usize,
) -> ah::Result<()> {
    if depth >= MAX_DEPTH {
        return Ok(());
    }

    let loc = ident.url(UrlComp {
        protocol: Some(ctx.protocol),
        domain: Some(ctx.config.domain()),
        base: Some(ctx.config.url_base()),
    });
    let lastmod;
    let changefreq;
    let priority;
    if depth == 1 {
        // Main groups
        lastmod = String::new();
        changefreq = "monthly".to_string();
        priority = "0.3".to_string();
    } else {
        // Pages, main page and sub groups
        lastmod = stamp.format("%Y-%m-%dT%H:%M:%SZ").to_string();
        changefreq = String::new();
        priority = "0.7".to_string();
    }

    elems.push(SiteMapElem {
        loc,
        lastmod,
        changefreq,
        priority,
    });

    if !nav_stop {
        let Ok(CommSubPages {
            mut names,
            nav_stops,
            stamps,
            ..
        }) = ctx.comm.get_db_sub_pages(ident).await
        else {
            return Ok(());
        };

        names.sort_unstable();
        for i in 0..names.len() {
            let sub_ident = ident.clone_append(&names[i]).into_checked()?;
            Box::pin(do_build_elems(
                ctx,
                elems,
                &sub_ident,
                stamps[i],
                nav_stops[i],
                depth + 1,
            ))
            .await?;
        }
    }

    Ok(())
}

async fn build_elems(
    ctx: &mut SiteMapContext<'_>,
    elems: &mut Vec<SiteMapElem>,
    ident: &CheckedIdent,
) -> ah::Result<()> {
    let Ok(CommPage { stamp, .. }) = ctx
        .comm
        .get_db_page(CommGetPage {
            path: ident.clone(),
            get_stamp: true,
            ..Default::default()
        })
        .await
    else {
        return Ok(());
    };
    do_build_elems(ctx, elems, ident, stamp.unwrap_or_default(), false, 0).await
}

async fn build_user_elems(
    ctx: &mut SiteMapContext<'_>,
    elems: &mut Vec<SiteMapElem>,
) -> ah::Result<()> {
    let user_site_map = ctx.comm.get_db_string("site-map").await?;
    for line in user_site_map.lines() {
        let line = line.trim();
        if line.is_empty() || line.starts_with('#') {
            continue;
        }
        let mut line = line.split_whitespace();
        let Some(loc) = line.next() else {
            continue;
        };
        let loc = format!("{}://{}/{}", ctx.protocol, ctx.config.domain(), loc);
        let priority = line.next().unwrap_or("0.7");
        let changefreq = line.next().unwrap_or("always");
        elems.push(SiteMapElem {
            loc,
            lastmod: String::new(),
            changefreq: changefreq.to_string(),
            priority: priority.to_string(),
        });
    }
    Ok(())
}

/// Site map generator.
/// Specification: https://www.sitemaps.org/protocol.html
pub struct SiteMap {
    elems: Vec<SiteMapElem>,
}

impl SiteMap {
    pub async fn build(mut ctx: SiteMapContext<'_>) -> ah::Result<Self> {
        let mut elems = Vec::with_capacity(DEFAULT_ELEMS_ALLOC);
        let root = ctx.root.clone();
        build_elems(&mut ctx, &mut elems, &root).await?;
        build_user_elems(&mut ctx, &mut elems).await?;
        Ok(Self { elems })
    }

    #[rustfmt::skip]
    pub fn get_xml(&self) -> ah::Result<String> {
        let mut b = String::with_capacity(DEFAULT_HTML_ALLOC);
        ln!(b, r#"<?xml version="1.0" encoding="UTF-8"?>"#)?;
        wr!(b, r#"<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9" "#)?;
        wr!(b, r#"xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" "#)?;
        wr!(b, r#"xsi:schemaLocation="https://www.sitemaps.org/schemas/sitemap/0.9 "#)?;
        ln!(b, r#"https://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">"#)?;
        for elem in &self.elems {
            let loc = xml_escape(elem.loc.clone());
            let lastmod = xml_escape(elem.lastmod.clone());
            let changefreq = xml_escape(elem.changefreq.clone());
            let priority = xml_escape(elem.priority.clone());

            ln!(b, r#"<url>"#)?;
            if !loc.is_empty() {
                ln!(b, r#"<loc>{loc}</loc>"#)?;
            }
            if !lastmod.is_empty() {
                ln!(b, r#"<lastmod>{lastmod}</lastmod>"#)?;
            }
            if !changefreq.is_empty() {
                ln!(b, r#"<changefreq>{changefreq}</changefreq>"#)?;
            }
            if !priority.is_empty() {
                ln!(b, r#"<priority>{priority}</priority>"#)?;
            }
            ln!(b, r#"</url>"#)?;
        }
        wr!(b, r#"</urlset>"#)?;
        Ok(b)
    }
}

// vim: ts=4 sw=4 expandtab
bues.ch cgit interface