2 from ...util import none_or
3 from .state import State
6 def track(session, rev_id, page_id=None, revert_radius=reverts.defaults.RADIUS,
7 future_revisions=reverts.defaults.RADIUS, properties=None):
9 Computes a persistence score for a revision by processing the revisions
10 that took place around it.
13 session : :class:`mw.api.Session`
14 An API session to make use of
16 the ID of the revision to check
18 the ID of the page the revision occupies (slower if not provided)
20 a positive integer indicating the maximum number of revisions that can be reverted
23 if not hasattr(session, "revisions"):
24 raise TypeError("session is wrong type. Expected a mw.api.Session.")
27 page_id = none_or(page_id, int)
28 revert_radius = int(revert_radius)
30 raise TypeError("invalid radius. Expected a positive integer.")
31 properties = set(properties) if properties is not None else set()
34 # If we don't have the page_id, we're going to need to look them up
36 rev = session.revisions.get(rev_id, properties={'ids'})
37 page_id = rev['page']['pageid']
39 # Load history and current rev
40 current_and_past_revs = list(session.revisions.query(
42 limit=revert_radius + 1,
45 properties={'ids', 'timestamp', 'content', 'sha1'} | properties
49 # Extract current rev and reorder history
50 current_rev, past_revs = (
51 current_and_past_revs[0], # Current rev is the first one returned
52 reversed(current_and_past_revs[1:]) # The rest are past revs, but they are in the wrong order
55 # Only way to get here is if there isn't enough history. Couldn't be
56 # reverted. Just return None.
59 # Load future revisions
60 future_revs = session.revisions.query(
62 limit=future_revisions,
63 start_id=rev_id + 1, # Ensures that we skip the current revision
65 properties={'ids', 'timestamp', 'content', 'sha1'} | properties
68 state = State(revert_radius=revert_radius)
70 # Process old revisions
72 state.process(rev.get('*', ""), rev, rev.get('sha1'))
74 # Process current revision
75 _, tokens_added, _ = state.process(current_rev.get('*'), current_rev,
76 current_rev.get('sha1'))
78 # Process new revisions
79 future_revs = list(future_revs)
80 for rev in future_revs:
81 state.process(rev.get('*', ""), rev, rev.get('sha1'))
83 return current_rev, tokens_added, future_revs