]> code.communitydata.science - rises_declines_wikia_code.git/blob - mediawiki_dump_tools/Mediawiki-Utilities/mw/api/collections/deleted_revisions.py
Initial commit
[rises_declines_wikia_code.git] / mediawiki_dump_tools / Mediawiki-Utilities / mw / api / collections / deleted_revisions.py
1 import logging
2 import sys
3
4 from ...types import Timestamp
5 from ...util import none_or
6 from ..errors import MalformedResponse
7 from .collection import Collection
8
9 logger = logging.getLogger("mw.api.collections.deletedrevs")
10
11
12 class DeletedRevisions(Collection):
13     PROPERTIES = {'ids', 'flags', 'timestamp', 'user', 'userid', 'size',
14                   'sha1', 'contentmodel', 'comment', 'parsedcomment', 'content',
15                   'tags'}
16
17     # TODO:
18     # This is *not* the right way to do this, but it should work for all queries.
19     MAX_REVISIONS = 500
20
21     def get(self, rev_id, *args, **kwargs):
22
23         rev_id = int(rev_id)
24
25         revs = list(self.query(revids={rev_id}, **kwargs))
26
27         if len(revs) < 1:
28             raise KeyError(rev_id)
29         else:
30             return revs[0]
31
32     def query(self, *args, limit=sys.maxsize, **kwargs):
33         """
34         Queries deleted revisions.
35         See https://www.mediawiki.org/wiki/API:Deletedrevs
36
37         :Parameters:
38             titles : set(str)
39                 A set of page names to query (note that namespace prefix is expected)
40             start : :class:`mw.Timestamp`
41                 A timestamp to start querying from
42             end : :class:`mw.Timestamp`
43                 A timestamp to end querying
44             from_title : str
45                 A title from which to start querying (alphabetically)
46             to_title : str
47                 A title from which to stop querying (alphabetically)
48             prefix : str
49                 A title prefix to match on
50             drcontinue : str
51                 When more results are available, use this to continue (3) Note: may only work if drdir is set to newer.
52             unique : bool
53                 List only one revision for each page
54             tag : str
55                 Only list revision tagged with this tag
56             user : str
57                 Only list revisions saved by this user_text
58             excludeuser : str
59                 Do not list revision saved by this user_text
60             namespace : int
61                 Only list pages in this namespace (id)
62             limit : int
63                 Limit the number of results
64             direction : str
65                 "newer" or "older"
66             properties : set(str)
67                 A list of properties to include in the results:
68
69
70                 * ids            - The ID of the revision.
71                 * flags          - Revision flags (minor).
72                 * timestamp      - The timestamp of the revision.
73                 * user           - User that made the revision.
74                 * userid         - User ID of the revision creator.
75                 * size           - Length (bytes) of the revision.
76                 * sha1           - SHA-1 (base 16) of the revision.
77                 * contentmodel   - Content model ID of the revision.
78                 * comment        - Comment by the user for the revision.
79                 * parsedcomment  - Parsed comment by the user for the revision.
80                 * content        - Text of the revision.
81                 * tags           - Tags for the revision.
82         """
83         # `limit` means something diffent here
84         kwargs['limit'] = min(limit, self.MAX_REVISIONS)
85         revisions_yielded = 0
86         done = False
87         while not done and revisions_yielded <= limit:
88             rev_docs, query_continue = self._query(*args, **kwargs)
89             for doc in rev_docs:
90                 yield doc
91                 revisions_yielded += 1
92                 if revisions_yielded >= limit:
93                     break
94
95             if query_continue != "" and len(rev_docs) > 0:
96                 kwargs['query_continue'] = query_continue
97             else:
98                 done = True
99
100     def _query(self, titles=None, pageids=None, revids=None,
101                start=None, end=None, query_continue=None, unique=None, tag=None,
102                user=None, excludeuser=None, namespace=None, limit=None,
103                properties=None, direction=None):
104
105         params = {
106             'action': "query",
107             'prop': "deletedrevisions"
108         }
109
110         params['titles'] = self._items(titles)
111         params['pageids'] = self._items(pageids)
112         params['revids'] = self._items(revids)
113         params['drvprop'] = self._items(properties, levels=self.PROPERTIES)
114         params['drvlimit'] = none_or(limit, int)
115         params['drvstart'] = self._check_timestamp(start)
116         params['drvend'] = self._check_timestamp(end)
117
118         params['drvdir'] = self._check_direction(direction)
119         params['drvuser'] = none_or(user, str)
120         params['drvexcludeuser'] = none_or(excludeuser, int)
121         params['drvtag'] = none_or(tag, str)
122         params.update(query_continue or {'continue': ""})
123
124         doc = self.session.get(params)
125         doc_copy = dict(doc)
126
127         try:
128             if 'continue' in doc:
129                 query_continue = doc['continue']
130             else:
131                 query_continue = ''
132
133             pages = doc['query']['pages'].values()
134             rev_docs = []
135
136             for page_doc in pages:
137                 page_rev_docs = page_doc.get('deletedrevisions', [])
138
139                 try: del page_doc['deletedrevisions']
140                 except KeyError: pass
141
142                 for rev_doc in page_rev_docs:
143                     rev_doc['page'] = page_doc
144
145                 rev_docs.extend(page_rev_docs)
146
147             return rev_docs, query_continue
148
149         except KeyError as e:
150             raise MalformedResponse(str(e), doc)

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