]> code.communitydata.science - rises_declines_wikia_code.git/blob - mediawiki_dump_tools/Mediawiki-Utilities/mw/lib/persistence/api.py
Initial commit
[rises_declines_wikia_code.git] / mediawiki_dump_tools / Mediawiki-Utilities / mw / lib / persistence / api.py
1 from .. import reverts
2 from ...util import none_or
3 from .state import State
4
5
6 def track(session, rev_id, page_id=None, revert_radius=reverts.defaults.RADIUS,
7           future_revisions=reverts.defaults.RADIUS, properties=None):
8     """
9     Computes a persistence score for a revision by processing the revisions
10     that took place around it.
11
12     :Parameters:
13         session : :class:`mw.api.Session`
14             An API session to make use of
15         rev_id : int
16             the ID of the revision to check
17         page_id : int
18             the ID of the page the revision occupies (slower if not provided)
19         revert_radius : int
20             a positive integer indicating the maximum number of revisions that can be reverted
21     """
22
23     if not hasattr(session, "revisions"):
24         raise TypeError("session is wrong type.  Expected a mw.api.Session.")
25
26     rev_id = int(rev_id)
27     page_id = none_or(page_id, int)
28     revert_radius = int(revert_radius)
29     if revert_radius < 1:
30         raise TypeError("invalid radius.  Expected a positive integer.")
31     properties = set(properties) if properties is not None else set()
32
33
34     # If we don't have the page_id, we're going to need to look them up
35     if page_id is None:
36         rev = session.revisions.get(rev_id, properties={'ids'})
37         page_id = rev['page']['pageid']
38
39     # Load history and current rev
40     current_and_past_revs = list(session.revisions.query(
41         pageids={page_id},
42         limit=revert_radius + 1,
43         start_id=rev_id,
44         direction="older",
45         properties={'ids', 'timestamp', 'content', 'sha1'} | properties
46     ))
47
48     try:
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
53         )
54     except IndexError:
55         # Only way to get here is if there isn't enough history.  Couldn't be
56         # reverted.  Just return None.
57         return None
58
59     # Load future revisions
60     future_revs = session.revisions.query(
61         pageids={page_id},
62         limit=future_revisions,
63         start_id=rev_id + 1, # Ensures that we skip the current revision
64         direction="newer",
65         properties={'ids', 'timestamp', 'content', 'sha1'} | properties
66     )
67
68     state = State(revert_radius=revert_radius)
69
70     # Process old revisions
71     for rev in past_revs:
72         state.process(rev.get('*', ""), rev, rev.get('sha1'))
73
74     # Process current revision
75     _, tokens_added, _ = state.process(current_rev.get('*'), current_rev,
76                                          current_rev.get('sha1'))
77
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'))
82
83     return current_rev, tokens_added, future_revs
84
85 score = track

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