4 from ...types import Timestamp
5 from ...util import none_or
6 from ..errors import MalformedResponse
7 from .collection import Collection
9 logger = logging.getLogger("mw.api.collections.deletedrevs")
12 class DeletedRevisions(Collection):
13 PROPERTIES = {'ids', 'flags', 'timestamp', 'user', 'userid', 'size',
14 'sha1', 'contentmodel', 'comment', 'parsedcomment', 'content',
18 # This is *not* the right way to do this, but it should work for all queries.
21 def get(self, rev_id, *args, **kwargs):
25 revs = list(self.query(revids={rev_id}, **kwargs))
28 raise KeyError(rev_id)
32 def query(self, *args, limit=sys.maxsize, **kwargs):
34 Queries deleted revisions.
35 See https://www.mediawiki.org/wiki/API:Deletedrevs
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
45 A title from which to start querying (alphabetically)
47 A title from which to stop querying (alphabetically)
49 A title prefix to match on
51 When more results are available, use this to continue (3) Note: may only work if drdir is set to newer.
53 List only one revision for each page
55 Only list revision tagged with this tag
57 Only list revisions saved by this user_text
59 Do not list revision saved by this user_text
61 Only list pages in this namespace (id)
63 Limit the number of results
67 A list of properties to include in the results:
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.
83 # `limit` means something diffent here
84 kwargs['limit'] = min(limit, self.MAX_REVISIONS)
87 while not done and revisions_yielded <= limit:
88 rev_docs, query_continue = self._query(*args, **kwargs)
91 revisions_yielded += 1
92 if revisions_yielded >= limit:
95 if query_continue != "" and len(rev_docs) > 0:
96 kwargs['query_continue'] = query_continue
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):
107 'prop': "deletedrevisions"
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)
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': ""})
124 doc = self.session.get(params)
128 if 'continue' in doc:
129 query_continue = doc['continue']
133 pages = doc['query']['pages'].values()
136 for page_doc in pages:
137 page_rev_docs = page_doc.get('deletedrevisions', [])
139 try: del page_doc['deletedrevisions']
140 except KeyError: pass
142 for rev_doc in page_rev_docs:
143 rev_doc['page'] = page_doc
145 rev_docs.extend(page_rev_docs)
147 return rev_docs, query_continue
149 except KeyError as e:
150 raise MalformedResponse(str(e), doc)