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
// Copyright 2018 Fredrik Portström <https://portstrom.com>
// This is free software distributed under the terms specified in
// the file LICENSE at the top-level directory of this distribution.

//! Parse XML dumps exported from Mediawiki.
//!
//! This module parses [XML dumps](https://www.mediawiki.org/wiki/Help:Export) exported from Mediawiki, providing each page from the dump through an iterator. This is useful for parsing the [dumps from Wikipedia and other Wikimedia projects](https://dumps.wikimedia.org).
//!
//! # Caution
//!
//! If you need to parse any wiki text extracted from a dump, please use the crate Parse Wiki Text ([crates.io](https://crates.io/crates/parse_wiki_text), [Github](https://github.com/portstrom/parse_wiki_text)). Correctly parsing wiki text requires dealing with an astonishing amount of difficult and counterintuitive cases. Parse Wiki Text automatically deals with all these cases, giving you an unambiguous tree of parsed elements that is easy to work with.
//!
//! # Limitations
//!
//! This module only parses dumps containing only one revision of each page. This is what you get from the page `Special:Export` when enabling the option “Include only the current revision, not the full history”, as well as what you get from the Wikimedia dumps with file names ending with `-pages-articles.xml.bz2`.
//!
//! This module ignores the `siteinfo` element, every child element of the `page` element except `ns`, `revision` and `title`, and every element inside the `revision` element except `format`, `model` and `text`.
//!
//! Until there is a real use case that justifies going beyond these limitations, they will remain in order to avoid premature design driven by imagined requirements.
//!
//! # Examples
//!
//! Parse a bzip2 compressed file and distinguish ordinary articles from other pages. A running example with complete error handling is available in the `examples` folder.
//!
//! ```rust,no_run
//! extern crate bzip2;
//! extern crate parse_mediawiki_dump;
//!
//! fn main() {
//!     let file = std::fs::File::open("example.xml.bz2").unwrap();
//!     let file = std::io::BufReader::new(file);
//!     let file = bzip2::bufread::BzDecoder::new(file);
//!     let file = std::io::BufReader::new(file);
//!     for result in parse_mediawiki_dump::parse(file) {
//!         match result {
//!             Err(error) => {
//!                 eprintln!("Error: {}", error);
//!                 break;
//!             }
//!             Ok(page) => if page.namespace == 0 && match &page.format {
//!                 None => false,
//!                 Some(format) => format == "text/x-wiki"
//!             } && match &page.model {
//!                 None => false,
//!                 Some(model) => model == "wikitext"
//!             } {
//!                 println!(
//!                     "The page {title:?} is an ordinary article with byte length {length}.",
//!                     title = page.title,
//!                     length = page.text.len()
//!                 );
//!             } else {
//!                 println!("The page {:?} has something special to it.", page.title);
//!             }
//!         }
//!     }
//! }
//! ```

#![forbid(unsafe_code)]
#![warn(missing_docs)]

extern crate quick_xml;

use quick_xml::{events::Event, Reader};
use std::io::BufRead;

enum PageChildElement {
    Ns,
    Revision,
    Title,
    Unknown,
}

enum RevisionChildElement {
    Format,
    Model,
    Text,
    Unknown,
}

#[derive(Debug)]
/// The error type for `Parser`.
pub enum Error {
    /// Format not matching expectations.
    ///
    /// Indicates the position in the stream.
    Format(usize),

    /// The source contains a feature not supported by the parser.
    ///
    /// In particular, this means a `page` element contains more than one `revision` element.
    NotSupported(usize),

    /// Error from the XML reader.
    XmlReader(quick_xml::Error),
}

/// Parsed page.
///
/// Parsed from the `page` element.
///
/// Although the `format` and `model` elements are defined as mandatory in the [schema](https://www.mediawiki.org/xml/export-0.10.xsd), previous versions of the schema don't contain them. Therefore the corresponding fields can be `None`.
#[derive(Debug)]
pub struct Page {
    /// The format of the revision if any.
    ///
    /// Parsed from the text content of the `format` element in the `revision` element. `None` if the element is not present.
    ///
    /// For ordinary articles the format is `text/x-wiki`.
    pub format: Option<String>,

    /// The model of the revision if any.
    ///
    /// Parsed from the text content of the `model` element in the `revision` element. `None` if the element is not present.
    ///
    /// For ordinary articles the model is `wikitext`.
    pub model: Option<String>,

    /// The namespace of the page.
    ///
    /// Parsed from the text content of the `ns` element in the `page` element.
    ///
    /// For ordinary articles the namespace is 0.
    pub namespace: u32,

    /// The text of the revision.
    ///
    /// Parsed from the text content of the `text` element in the `revision` element.
    pub text: String,

    /// The title of the page.
    ///
    /// Parsed from the text content of the `title` element in the `page` element.
    pub title: String,
}

/// Parser working as an iterator over pages.
pub struct Parser<R: BufRead> {
    buffer: Vec<u8>,
    namespace_buffer: Vec<u8>,
    reader: Reader<R>,
    started: bool,
}

impl std::fmt::Display for Error {
    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Error::Format(position) => write!(formatter, "Invalid format at position {}", position),
            Error::NotSupported(position) => write!(
                formatter,
                "The element at position {} is not supported",
                position
            ),
            Error::XmlReader(error) => error.fmt(formatter),
        }
    }
}

impl From<quick_xml::Error> for Error {
    fn from(value: quick_xml::Error) -> Self {
        Error::XmlReader(value)
    }
}

impl<R: BufRead> Iterator for Parser<R> {
    type Item = Result<Page, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        Some(match next(self) {
            Err(error) => Err(error),
            Ok(item) => Ok(item?),
        })
    }
}

fn match_namespace(namespace: Option<&[u8]>) -> bool {
    match namespace {
        None => false,
        Some(namespace) => namespace == b"http://www.mediawiki.org/xml/export-0.10/" as &[u8],
    }
}

fn next(parser: &mut Parser<impl BufRead>) -> Result<Option<Page>, Error> {
    if !parser.started {
        loop {
            parser.buffer.clear();
            if let (namespace, Event::Start(event)) = parser
                .reader
                .read_namespaced_event(&mut parser.buffer, &mut parser.namespace_buffer)?
            {
                if match_namespace(namespace) && event.local_name() == b"mediawiki" {
                    break;
                }
                return Err(Error::Format(parser.reader.buffer_position()));
            }
        }
        parser.started = true;
    }
    loop {
        parser.buffer.clear();
        if !match parser
            .reader
            .read_namespaced_event(&mut parser.buffer, &mut parser.namespace_buffer)?
        {
            (_, Event::End(_)) => return Ok(None),
            (namespace, Event::Start(event)) => {
                match_namespace(namespace) && event.local_name() == b"page"
            }
            _ => continue,
        } {
            skip_element(parser)?;
            continue;
        }
        let mut format = None;
        let mut model = None;
        let mut namespace = None;
        let mut text = None;
        let mut title = None;
        loop {
            parser.buffer.clear();
            match match parser
                .reader
                .read_namespaced_event(&mut parser.buffer, &mut parser.namespace_buffer)?
            {
                (_, Event::End(_)) => {
                    return match (namespace, text, title) {
                        (Some(namespace), Some(text), Some(title)) => Ok(Some(Page {
                            format,
                            model,
                            namespace,
                            text,
                            title,
                        })),
                        _ => Err(Error::Format(parser.reader.buffer_position())),
                    }
                }
                (namespace, Event::Start(event)) => {
                    if match_namespace(namespace) {
                        match event.local_name() {
                            b"ns" => PageChildElement::Ns,
                            b"revision" => PageChildElement::Revision,
                            b"title" => PageChildElement::Title,
                            _ => PageChildElement::Unknown,
                        }
                    } else {
                        PageChildElement::Unknown
                    }
                }
                _ => continue,
            } {
                PageChildElement::Ns => match parse_text(parser, &namespace)?.parse() {
                    Err(_) => return Err(Error::Format(parser.reader.buffer_position())),
                    Ok(value) => {
                        namespace = Some(value);
                        continue;
                    }
                },
                PageChildElement::Revision => {
                    if text.is_some() {
                        return Err(Error::NotSupported(parser.reader.buffer_position()));
                    }
                    loop {
                        parser.buffer.clear();
                        match match parser.reader.read_namespaced_event(
                            &mut parser.buffer,
                            &mut parser.namespace_buffer,
                        )? {
                            (_, Event::End(_)) => match text {
                                None => return Err(Error::Format(parser.reader.buffer_position())),
                                Some(_) => break,
                            },
                            (namespace, Event::Start(event)) => if match_namespace(namespace) {
                                match event.local_name() {
                                    b"format" => RevisionChildElement::Format,
                                    b"model" => RevisionChildElement::Model,
                                    b"text" => RevisionChildElement::Text,
                                    _ => RevisionChildElement::Unknown,
                                }
                            } else {
                                RevisionChildElement::Unknown
                            },
                            _ => continue,
                        } {
                            RevisionChildElement::Format => {
                                format = Some(parse_text(parser, &format)?)
                            }
                            RevisionChildElement::Model => {
                                model = Some(parse_text(parser, &model)?)
                            }
                            RevisionChildElement::Text => text = Some(parse_text(parser, &text)?),
                            RevisionChildElement::Unknown => skip_element(parser)?,
                        }
                    }
                    continue;
                }
                PageChildElement::Title => {
                    title = Some(parse_text(parser, &title)?);
                    continue;
                }
                PageChildElement::Unknown => skip_element(parser)?,
            }
        }
    }
}

/// Creates a parser for a stream.
///
/// The stream is parsed as an XML dump exported from Mediawiki. The parser is an iterator over the pages in the dump.
pub fn parse<R: BufRead>(source: R) -> Parser<R> {
    let mut reader = Reader::from_reader(source);
    reader.expand_empty_elements(true);
    Parser {
        buffer: vec![],
        namespace_buffer: vec![],
        reader,
        started: false,
    }
}

fn parse_text(
    parser: &mut Parser<impl BufRead>,
    output: &Option<impl Sized>,
) -> Result<String, Error> {
    if output.is_some() {
        return Err(Error::Format(parser.reader.buffer_position()));
    }
    parser.buffer.clear();
    let text = match parser
        .reader
        .read_namespaced_event(&mut parser.buffer, &mut parser.namespace_buffer)?
        .1
    {
        Event::Text(text) => text.unescape_and_decode(&parser.reader)?,
        Event::End { .. } => return Ok(String::new()),
        _ => return Err(Error::Format(parser.reader.buffer_position())),
    };
    parser.buffer.clear();
    if let Event::End(_) = parser
        .reader
        .read_namespaced_event(&mut parser.buffer, &mut parser.namespace_buffer)?
        .1
    {
        Ok(text)
    } else {
        Err(Error::Format(parser.reader.buffer_position()))
    }
}

fn skip_element(parser: &mut Parser<impl BufRead>) -> Result<(), quick_xml::Error> {
    let mut level = 0;
    loop {
        parser.buffer.clear();
        match parser
            .reader
            .read_namespaced_event(&mut parser.buffer, &mut parser.namespace_buffer)?
            .1
        {
            Event::End(_) => {
                if level == 0 {
                    return Ok(());
                }
                level -= 1;
            }
            Event::Start(_) => level += 1,
            _ => {}
        }
    }
}