wiki-api 0.1.2

Backend for wiki-tui
Documentation
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
use crate::{
    document::{Document, HeaderKind},
    parser::{Parser, WikipediaParser},
    Endpoint,
};
use anyhow::{anyhow, Context, Result};
use reqwest::{Client, Response};
use scraper::Html;
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use tracing::{debug, warn};
use url::Url;
use uuid::Uuid;

use super::languages::Language;

pub mod link_data {
    use crate::{languages::Language, search::Namespace, Endpoint};
    use serde::{Deserialize, Serialize};
    use url::Url;

    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    pub struct InternalData {
        pub namespace: Namespace,
        pub page: String,
        pub title: String,
        pub endpoint: Endpoint,
        pub language: Language,
        pub anchor: Option<AnchorData>,
    }

    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    pub struct AnchorData {
        pub anchor: String,
        pub title: String,
    }

    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    pub struct RedLinkData {
        pub url: Url,
        pub title: String,
    }

    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    pub struct MediaData {
        pub url: Url,
        pub title: String,
    }

    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    pub struct ExternalData {
        pub url: Url,
    }

    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    pub struct ExternalToInteralData {}
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Link {
    /// Interal link to another page in the same wiki
    Internal(link_data::InternalData),
    /// Anchor to a specific section in the current page
    /// Note: this only corresponds to anchors on the current page. For anchors in another page on
    /// the same wiki, `LinkType::Internal` is used
    Anchor(link_data::AnchorData),
    /// A special type of link that leads to an internal page that doesn't exist yet
    RedLink(link_data::RedLinkData),
    /// Link pointing to a media
    MediaLink(link_data::MediaData),
    /// External link to a page at another website
    External(link_data::ExternalData),
    /// External link to an interal page in the same wiki
    ExternalToInternal(link_data::ExternalToInteralData),
}

impl Link {
    pub fn title(&self) -> Option<&str> {
        match self {
            Link::Anchor(link_data) => Some(&link_data.title),
            Link::RedLink(link_data) => Some(&link_data.title),
            &Link::External(_) => None,
            &Link::ExternalToInternal(_) => None,
            Link::MediaLink(link_data) => Some(&link_data.title),
            Link::Internal(link_data) => Some(&link_data.title),
        }
    }
}

// TODO: replace this with Link::Internal
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LanguageLink {
    #[serde(rename = "langname")]
    pub name: String,
    #[serde(rename = "lang")]
    pub language: Language,
    pub autonym: String,
    pub title: String,
    pub url: Url,
    pub endpoint: Endpoint,
}

#[derive(Debug, Deserialize, Clone, PartialEq, Eq, Serialize)]
pub struct Section {
    #[serde(skip_deserializing)]
    pub index: usize,
    #[serde(rename = "toclevel")]
    pub header_kind: HeaderKind,
    #[serde(rename = "line")]
    pub text: String,
    pub number: String,
    pub anchor: String,
}

#[derive(Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Page {
    pub title: String,
    pub pageid: usize,
    pub content: Document,
    pub language: Language,
    pub language_links: Option<Vec<LanguageLink>>,
    pub sections: Option<Vec<Section>>,
    pub revision_id: Option<usize>,
    pub uuid: Uuid,
}

impl Page {
    #[cfg(debug_assertions)]
    pub fn from_path(path: &std::path::PathBuf) -> Option<Page> {
        if !path.exists() {
            return None;
        }

        let content = std::fs::read_to_string(path).ok()?;
        let nodes = WikipediaParser::parse_document(
            &content,
            url::Url::parse("https://en.wikipedia.org/w/api.php").ok()?,
            Language::default(),
        )
        .nodes();

        Some(Page {
            title: "DEBUG: FILE".to_string(),
            pageid: 0,
            content: Document { nodes },
            language: Language::default(),
            language_links: None,
            sections: None,
            revision_id: None,
            uuid: Uuid::new_v4(),
        })
    }

    pub fn builder() -> PageBuilder<NoPageID, NoPage, NoEndpoint, NoLanguage> {
        PageBuilder::default()
    }

    pub fn available_languages(&self) -> Option<usize> {
        if let Some(ref links) = self.language_links {
            return Some(links.len());
        }
        None
    }

    pub fn sections(&self) -> Option<&Vec<Section>> {
        if let Some(ref sections) = self.sections {
            return Some(sections);
        }
        None
    }
}

impl std::fmt::Debug for Page {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Page")
            .field("title", &self.title)
            .field("pageid", &self.pageid)
            .field("content", &self.content)
            .field("language", &self.language)
            .field("language_links", &self.language_links.is_some())
            .field("sections", &self.sections.is_some())
            .field("revision_id", &self.revision_id)
            .finish()
    }
}

#[derive(Clone)]
/// Which pieces of information to get about the article
pub enum Property {
    /// Gives the parsed text of the wikitext
    Text,
    /// Gives the language links in the parsed wikitext
    LangLinks,
    /// Gives the categories in the parsed wikitext
    Categories,
    /// Gives the HTML version of the categories
    CategoriesHTML,
    /// Gives the templates in the parsed wikitext
    Templates,
    /// Gives the images in the parsed wikitext
    Images,
    /// Gives the external links in the parsed wikitext
    ExternalLinks,
    /// Gives the sections in the parsed wikitext
    Sections,
    /// Adds the revision ID of the parsed page
    RevID,
    /// Adds the title of the parsed wikitext
    DisplayTitle,
    /// Adds the page subtitle for the parsed page
    Subtitle,
    /// Gives parsed doctype, opening `<html>`, `<head>` and opening `<body>` of the page
    HeadHTML,
    /// Gives the HTML of page status indicators used on the page
    Indicators,
    /// Gives interwiki links in the parsed wikitext
    InterwikiLinks,
    /// Gives the original wikitext that was parsed
    Wikitext,
    /// Gives various properties defined in the parsed wikitext
    Properties,
    /// Gives the limit report in a structured way
    LimitReportData,
    /// Gives the HTML version of the limit report
    LimitReportHTML,
    /// The XML parse tree of revision content (requires content model `wikitext`)
    ParseTree,
    /// Gives the warnings that occurred while parsing content (as wikitext)
    ParseWarnings,
    /// Gives the warnings that occurred while parsing content (as HTML)
    ParseWarningsHTML,
}

impl Display for Property {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Property::Text => write!(f, "text"),
            Property::LangLinks => write!(f, "langlinks"),
            Property::Categories => write!(f, "categories"),
            Property::CategoriesHTML => write!(f, "categorieshtml"),
            Property::Templates => write!(f, "templates"),
            Property::Images => write!(f, "images"),
            Property::ExternalLinks => write!(f, "externallinks"),
            Property::Sections => write!(f, "sections"),
            Property::RevID => write!(f, "revid"),
            Property::DisplayTitle => write!(f, "displaytitle"),
            Property::Subtitle => write!(f, "subtitle"),
            Property::HeadHTML => write!(f, "headhtml"),
            Property::Indicators => write!(f, "indicators"),
            Property::InterwikiLinks => write!(f, "iwlinks"),
            Property::Wikitext => write!(f, "wikitext"),
            Property::Properties => write!(f, "properties"),
            Property::LimitReportData => write!(f, "limitreportdata"),
            Property::LimitReportHTML => write!(f, "limitreporthtml"),
            Property::ParseTree => write!(f, "parsetree"),
            Property::ParseWarnings => write!(f, "parsewarnings"),
            Property::ParseWarningsHTML => write!(f, "parsewarningshtml"),
        }
    }
}

pub struct WithPageID(usize);
#[derive(Default)]
pub struct NoPageID;

pub struct WithPage(String);
#[derive(Default)]
pub struct NoPage;

pub struct WithEndpoint(Url);
#[derive(Default)]
pub struct NoEndpoint;

pub struct WithLanguage(Language);
#[derive(Default)]
pub struct NoLanguage;

#[derive(Default)]
pub struct PageBuilder<I, P, E, L> {
    pageid: I,
    page: P,
    endpoint: E,
    language: L,
    revision: Option<usize>,
    redirects: Option<bool>,
    properties: Option<Vec<Property>>,
}

pub type PageRequest = PageBuilder<NoPageID, WithPage, WithEndpoint, WithLanguage>;
pub type PageRequestID = PageBuilder<WithPageID, NoPage, WithEndpoint, WithLanguage>;

impl<E, L> PageBuilder<NoPageID, NoPage, E, L> {
    /// Parse content of this page
    pub fn pageid(self, pageid: usize) -> PageBuilder<WithPageID, NoPage, E, L> {
        PageBuilder {
            pageid: WithPageID(pageid),
            page: self.page,
            endpoint: self.endpoint,
            revision: self.revision,
            redirects: self.redirects,
            properties: self.properties,
            language: self.language,
        }
    }

    /// Parse content of this page
    pub fn page(self, page: impl Into<String>) -> PageBuilder<NoPageID, WithPage, E, L> {
        PageBuilder {
            pageid: self.pageid,
            page: WithPage(page.into()),
            endpoint: self.endpoint,
            revision: self.revision,
            redirects: self.redirects,
            properties: self.properties,
            language: self.language,
        }
    }
}

impl<I, P, L> PageBuilder<I, P, NoEndpoint, L> {
    pub fn url(self, url: impl Into<Url>) -> PageBuilder<I, P, WithEndpoint, L> {
        PageBuilder {
            pageid: self.pageid,
            page: self.page,
            endpoint: WithEndpoint(url.into()),
            revision: self.revision,
            redirects: self.redirects,
            properties: self.properties,
            language: self.language,
        }
    }

    pub fn endpoint(self, endpoint: Url) -> PageBuilder<I, P, WithEndpoint, L> {
        PageBuilder {
            pageid: self.pageid,
            page: self.page,
            endpoint: WithEndpoint(endpoint),
            revision: self.revision,
            redirects: self.redirects,
            properties: self.properties,
            language: self.language,
        }
    }
}

impl<I, P, E> PageBuilder<I, P, E, NoLanguage> {
    pub fn language(self, language: Language) -> PageBuilder<I, P, E, WithLanguage> {
        PageBuilder {
            pageid: self.pageid,
            page: self.page,
            endpoint: self.endpoint,
            language: WithLanguage(language),
            revision: self.revision,
            redirects: self.redirects,
            properties: self.properties,
        }
    }
}

impl<I, P, U, L> PageBuilder<I, P, U, L> {
    /// Revision ID, for `{{REVISIONID}}` and similar variables
    pub fn revision(mut self, revision: usize) -> Self {
        self.revision = Some(revision);
        self
    }

    /// If page or pageid is set to a redirect, resolve it
    pub fn redirects(mut self, redirects: bool) -> Self {
        self.redirects = Some(redirects);
        self
    }

    /// Which pieces of information to get
    pub fn properties(mut self, properties: Vec<Property>) -> Self {
        self.properties = Some(properties);
        self
    }
}

impl<I, P> PageBuilder<I, P, WithEndpoint, WithLanguage> {
    async fn fetch_with_params(self, mut params: Vec<(&str, String)>) -> Result<Page> {
        async fn action_parse(params: Vec<(&str, String)>, endpoint: Url) -> Result<Response> {
            Client::new()
                .get(endpoint)
                .header(
                    "User-Agent",
                    format!(
                        "wiki-tui/{} (https://github.com/Builditluc/wiki-tui)",
                        env!("CARGO_PKG_VERSION")
                    ),
                )
                .query(&[
                    ("action", "parse"),
                    ("format", "json"),
                    ("formatversion", "2"),
                    ("parsoid", "true"),
                ])
                .query(&params)
                .send()
                .await
                .inspect(|response| {
                    debug!("response url: '{}'", response.url().as_str());
                })
                .context("failed sending the request")
        }

        if let Some(revision) = self.revision {
            params.push(("revid", revision.to_string()));
        }

        if let Some(redirects) = self.redirects {
            params.push(("redirects", redirects.to_string()));
        }

        if let Some(ref prop) = self.properties {
            let mut prop_str = String::new();
            for prop in prop {
                prop_str.push('|');
                prop_str.push_str(&prop.to_string())
            }
            params.push(("prop", prop_str));
        }

        let response = action_parse(params, self.endpoint.0.clone())
            .await?
            .error_for_status()
            .context("the server returned an error")?;

        let res_json: serde_json::Value = serde_json::from_str(
            &response
                .text()
                .await
                .context("failed reading the response")?,
        )
        .context("failed interpreting the response as json")?;

        self.serialize_result(res_json)
            .context("failed serializing the returned response")
    }

    fn serialize_result(self, res_json: serde_json::Value) -> Result<Page> {
        let title = res_json
            .get("parse")
            .and_then(|x| x.get("title"))
            .and_then(|x| x.as_str())
            .map(|x| x.to_string())
            .ok_or_else(|| anyhow!("missing the title"))?;

        let pageid = res_json
            .get("parse")
            .and_then(|x| x.get("pageid"))
            .and_then(|x| x.as_u64())
            .map(|x| x as usize)
            .ok_or_else(|| anyhow!("missing the pageid"))?;

        let endpoint = self.endpoint.0;
        let language = self.language.0;
        let content = res_json
            .get("parse")
            .and_then(|x| x.get("text"))
            .and_then(|x| x.as_str())
            .map(|x| {
                let parser = WikipediaParser::parse_document(x, endpoint.clone(), language);
                Document {
                    nodes: parser.nodes(),
                }
            })
            // HACK: implement correct errors
            .ok_or(anyhow!("missing the content or failed parsing the content"))?;

        let language_links = res_json
            .get("parse")
            .and_then(|x| x.get("langlinks"))
            .and_then(|x| x.as_array())
            .map(|x| x.to_owned())
            .map(|x| {
                x.into_iter()
                    .filter_map(|x| {
                        let mut language_link: LanguageLink = serde_json::from_value(x)
                            .map_err(|err| warn!("language_link parsing error: {:?}", err))
                            .ok()?;
                        let mut endpoint = endpoint.clone();
                        let _ = endpoint.set_host(Some(language_link.url.host_str().unwrap()));
                        language_link.endpoint = endpoint;
                        Some(language_link)
                    })
                    .collect::<Vec<LanguageLink>>()
            })
            .inspect(|x| {
                debug!("language_links: '{}'", x.len());
            });

        let sections = res_json
            .get("parse")
            .and_then(|x| x.get("sections"))
            .and_then(|x| x.as_array())
            .map(|x| x.to_owned())
            .map(|x| {
                x.into_iter()
                    .enumerate()
                    .filter_map(|(i, x)| {
                        serde_json::from_value(x).ok().map(|mut x: Section| {
                            x.index = i + 1;
                            // TODO: render html tags in the toc
                            let fragment = Html::parse_document(&x.text);
                            x.text = fragment.root_element().text().collect();
                            x
                        })
                    })
                    .collect::<Vec<Section>>()
            })
            .map(|mut x| {
                x.insert(
                    0,
                    Section {
                        index: 0,
                        header_kind: HeaderKind::Main,
                        text: "(Top)".to_string(),
                        number: "".to_string(),
                        anchor: "Content_Top".to_string(),
                    },
                );
                x
            });

        let revision_id = res_json
            .get("parse")
            .and_then(|x| x.get("revid"))
            .and_then(|x| x.as_u64())
            .map(|x| x as usize);

        Ok(Page {
            title,
            pageid,
            content,
            language,
            language_links,
            sections,
            revision_id,
            uuid: Uuid::new_v4(),
        })
    }
}

impl PageBuilder<WithPageID, NoPage, WithEndpoint, WithLanguage> {
    pub async fn fetch(self) -> Result<Page> {
        let param = vec![("pageid", self.pageid.0.to_string())];
        self.fetch_with_params(param).await
    }
}

impl PageBuilder<NoPageID, WithPage, WithEndpoint, WithLanguage> {
    pub async fn fetch(self) -> Result<Page> {
        let param = vec![("page", self.page.0.to_string())];
        self.fetch_with_params(param).await
    }
}