wiki_loader/
page.rs

1// Standard Lib
2use std::fmt::{Debug, Display};
3use std::path::Path;
4
5// Third Party
6use serde::{Deserialize, Serialize};
7
8use crate::bzip::{use_bzip_block_n_detailed, BZipTable};
9
10#[derive(Deserialize, Debug, Serialize)]
11pub struct NameSpace {
12    #[serde(rename = "@key")]
13    key: String,
14    #[serde(rename = "@case")]
15    case: String,
16    #[serde(rename = "$value")]
17    value: Option<String>,
18}
19
20#[derive(Deserialize, Debug, Serialize)]
21struct NameSpaces {
22    namespace: Vec<NameSpace>,
23}
24
25#[derive(Deserialize, Debug, Serialize)]
26#[serde(rename(serialize = "siteinfo"))]
27pub struct SiteInfo {
28    sitename: String,
29    dbname: String,
30    base: String,
31    generator: String,
32    case: String,
33    namespaces: Vec<NameSpaces>,
34}
35
36#[derive(Deserialize, Debug, Serialize, PartialEq, Eq, PartialOrd, Ord)]
37pub struct Redirect {
38    #[serde(rename = "@title")]
39    pub title: String,
40}
41
42#[derive(Deserialize, Debug, Serialize, PartialEq, Eq, PartialOrd, Ord)]
43pub struct Text {
44    #[serde(rename = "@bytes")]
45    pub bytes: u32,
46    #[serde(rename = "@xml:space")]
47    pub xml_space: Option<String>,
48    #[serde(rename = "$value")]
49    pub value: Option<String>,
50}
51
52#[derive(Deserialize, Debug, Serialize, PartialEq, Eq, PartialOrd, Ord)]
53pub struct RevisionDetailedPage {
54    pub id: u32,
55    pub parentid: Option<u32>,
56    pub timestamp: String,
57    pub format: Option<String>,
58    pub model: String,
59    pub text: Option<Text>,
60}
61
62#[derive(Deserialize, Debug, Serialize, PartialEq, Eq, PartialOrd, Ord)]
63pub struct RevisionPage {
64    pub id: u32,
65    pub parentid: Option<u32>,
66    pub timestamp: String,
67    pub format: Option<String>,
68    pub model: String,
69}
70
71#[derive(Deserialize, Debug, Serialize, PartialEq, Eq, PartialOrd, Ord)]
72pub struct Page {
73    pub title: String,
74    pub id: u32,
75    pub block_id: Option<usize>,
76    // Shouldn't be needed for indexing so saving memory
77    // pub ns: u32,
78    // pub redirect: Option<Redirect>,
79    // pub revision: Option<RevisionPage>,
80}
81
82#[derive(Deserialize, Debug, Serialize, PartialEq, Eq, PartialOrd, Ord)]
83pub struct DetailedPage {
84    pub title: String,
85    pub ns: u32,
86    pub id: u32,
87    pub block_id: Option<usize>,
88    pub redirect: Option<Redirect>,
89    pub revision: Option<RevisionDetailedPage>,
90}
91
92impl Display for DetailedPage {
93    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
94        let mut out = format!("Title:{},ns:{},id:{}", self.title, self.ns, self.id);
95        out = match &self.redirect {
96            Some(redirect) => format!("{},redirect:{}", out, redirect.title),
97            None => out,
98        };
99        return write!(f, "{}", out);
100    }
101}
102
103trait PageItem {}
104impl PageItem for Page {}
105impl PageItem for DetailedPage {}
106
107pub fn get_detailed_page(
108    table: &BZipTable,
109    page_id: u64,
110    block_id: u64,
111    path: &Path,
112) -> Option<DetailedPage> {
113    let pages_block = use_bzip_block_n_detailed(&table, path, block_id as usize);
114
115    let mut pages = pages_block.unwrap();
116
117    let mut selected_id: Option<usize> = None;
118    for (index, page) in pages.iter().enumerate() {
119        if page.id == page_id as u32 {
120            selected_id = Some(index);
121        }
122    }
123    return match selected_id {
124        Some(id) => Some(pages.remove(id)),
125        None => None,
126    };
127}