-
Notifications
You must be signed in to change notification settings - Fork 4
/
wiki.py
143 lines (115 loc) · 4.72 KB
/
wiki.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/python
# Wiki helpers
import json
from simplemediawiki import MediaWiki
DEBUG = False
def assert_present(value, d):
if not value in d:
print 'Error, %s not present in response' % value
print d
raise Exception('Invalid response')
class Wiki(object):
def __init__(self, url, username, password):
self.wiki = MediaWiki(url)
self.username = username
self.password = password
self.login = self._make_wiki_login_call({'action': 'login'})
self.token = self._make_wiki_login_call(
{'action': 'login', 'lgtoken': self.login['login']['token']})
def _make_wiki_login_call(self, packet):
packet.update({'lgname': self.username,
'lgpassword': self.password})
response = self.wiki.call(packet)
if DEBUG:
print response
return response
def all_pages(self):
response = self.wiki.call({'action': 'query',
'list': 'allpages'})
if DEBUG:
print response
assert_present('query', response)
marker = 'foo'
marker_name = 'foo'
while marker:
if 'query-continue' in response:
for possible in ['apfrom', 'apcontinue']:
if possible in response['query-continue']['allpages']:
marker = \
response['query-continue']['allpages'][possible]
marker_name = possible
break
else:
marker = None
for page in response['query']['allpages']:
yield page['title']
response = self.wiki.call({'action': 'query',
'list': 'allpages',
marker_name: marker})
if DEBUG:
print response
def get_page(self, title):
response = self.wiki.call({'action': 'query',
'titles': title,
'prop': 'revisions',
'rvprop': 'content'})
if DEBUG:
print response
assert_present('query', response)
pages = response['query']['pages']
page_id = pages.keys()[0]
if not 'revisions' in pages[page_id]:
# This is a new page
return ''
return pages[page_id]['revisions'][0]['*']
def check_for_page(self, title):
response = self.wiki.call({'action': 'query',
'titles': title,
'prop': 'revisions',
'rvprop': 'content'})
if DEBUG:
print response
assert_present('query', response)
pages = response['query']['pages']
page_id = pages.keys()[0]
if not 'revisions' in pages[page_id]:
return False
return True
def post_page(self, title, text, minor=True, bot=True):
response = self.wiki.call({'action': 'query',
'prop': 'info',
'titles': title,
'intoken': 'edit'})
if DEBUG:
print response
assert_present('query', response)
pages = response['query']['pages']
page_id = pages.keys()[0]
response = self.wiki.call({'action': 'edit',
'minor': minor,
'bot': bot,
'title': title,
'text': json.dumps(text).replace(
'\\n', '\n')[1:-1],
'token': pages[page_id]['edittoken']})
if DEBUG:
print response
if not 'nochange' in response['edit']:
print 'Modified %s' % title
def create_account(self, username, password, email, realname):
response = self.wiki.call({'action': 'createaccount',
'name': username,
'password': password,
'email': email,
'realname': realname})
if DEBUG:
print response
response = self.wiki.call({'action': 'createaccount',
'name': username,
'password': password,
'email': email,
'realname': realname,
'token': response['createaccount']['token']})
if DEBUG:
print response
return 'error' not in response