]> code.communitydata.science - rises_declines_wikia_code.git/blob - mediawiki_dump_tools/Mediawiki-Utilities/mw/xml_dump/iteration/revision.py
Initial commit
[rises_declines_wikia_code.git] / mediawiki_dump_tools / Mediawiki-Utilities / mw / xml_dump / iteration / revision.py
1 from ...types import serializable, Timestamp
2 from ...util import none_or
3 from .comment import Comment
4 from .contributor import Contributor
5 from .text import Text
6 from .util import consume_tags
7
8
9 class Revision(serializable.Type):
10     """
11     Revision meta data.
12     """
13     __slots__ = ('id', 'timestamp', 'contributor', 'minor', 'comment', 'text',
14                  'bytes', 'sha1', 'parent_id', 'model', 'format',
15                  'beginningofpage')
16
17     TAG_MAP = {
18         'id': lambda e: int(e.text),
19         'timestamp': lambda e: Timestamp(e.text),
20         'contributor': lambda e: Contributor.from_element(e),
21         'minor': lambda e: True,
22         'comment': lambda e: Comment.from_element(e),
23         'text': lambda e: Text.from_element(e),
24         'sha1': lambda e: str(e.text),
25         'parentid': lambda e: int(e.text),
26         'model': lambda e: str(e.text),
27         'format': lambda e: str(e.text)
28     }
29
30     def __init__(self, id, timestamp, contributor=None, minor=None,
31                  comment=None, text=None, bytes=None, sha1=None,
32                  parent_id=None, model=None, format=None,
33                  beginningofpage=False):
34         self.id = none_or(id, int)
35         """
36         Revision ID : `int`
37         """
38
39         self.timestamp = none_or(timestamp, Timestamp)
40         """
41         Revision timestamp : :class:`mw.Timestamp`
42         """
43
44         self.contributor = none_or(contributor, Contributor.deserialize)
45         """
46         Contributor meta data : :class:`~mw.xml_dump.Contributor` | `None`
47         """
48
49         self.minor = False or none_or(minor, bool)
50         """
51         Is revision a minor change? : `bool`
52         """
53
54         self.comment = none_or(comment, Comment)
55         """
56         Comment left with revision : :class:`~mw.xml_dump.Comment` (behaves like `str`, with additional members)
57         """
58
59         self.text = none_or(text, Text)
60         """
61         Content of text : :class:`~mw.xml_dump.Text` (behaves like `str`, with additional members)
62         """
63
64         self.bytes = none_or(bytes, int)
65         """
66         Number of bytes of content : `str`
67         """
68
69         self.sha1 = none_or(sha1, str)
70         """
71         sha1 hash of the content : `str`
72         """
73
74         self.parent_id = none_or(parent_id, int)
75         """
76         Revision ID of preceding revision : `int` | `None`
77         """
78
79         self.model = none_or(model, str)
80         """
81         TODO: ??? : `str`
82         """
83
84         self.format = none_or(format, str)
85         """
86         TODO: ??? : `str`
87         """
88
89         self.beginningofpage = bool(beginningofpage)
90         """
91         Is the first revision of a page : `bool`
92         Used to identify the first revision of a page when using Wikihadoop
93         revision pairs.  Otherwise is always set to False.  Do not expect to use
94         this when processing an XML dump directly.
95         """
96
97     @classmethod
98     def from_element(cls, element):
99         values = consume_tags(cls.TAG_MAP, element)
100
101         return cls(
102             values.get('id'),
103             values.get('timestamp'),
104             values.get('contributor'),
105             values.get('minor') is not None,
106             values.get('comment'),
107             values.get('text'),
108             values.get('bytes'),
109             values.get('sha1'),
110             values.get('parentid'),
111             values.get('model'),
112             values.get('format'),
113             element.attr('beginningofpage') is not None
114                     # For Wikihadoop.
115                     # Probably never used by anything, ever.
116         )

Community Data Science Collective || Want to submit a patch?