]> projects.mako.cc - wikipedia-api-cdsw/blob - mwclient/page_nowriteapi.py
d6f68cdb7a0cc7c896bd399e3ac526b1fb9a75ef
[wikipedia-api-cdsw] / mwclient / page_nowriteapi.py
1 import time
2 from HTMLParser import HTMLParser
3 from htmlentitydefs import name2codepoint
4
5 import errors
6
7
8 class OldPage(object):
9
10     @staticmethod
11     def save(self, text=u'', summary=u'', minor=False):
12         data = {}
13         data['wpTextbox1'] = text
14         data['wpSummary'] = summary
15         data['wpSave'] = 'Save page'
16         data['wpEditToken'] = self.get_token('edit')
17         if self.last_rev_time:
18             data['wpEdittime'] = time.strftime('%Y%m%d%H%M%S', self.last_rev_time)
19         else:
20             data['wpEdittime'] = time.strftime('%Y%m%d%H%M%S', time.gmtime())
21         if self.edit_time:
22             data['wpStarttime'] = time.strftime('%Y%m%d%H%M%S', self.edit_time)
23         else:
24             data['wpStarttime'] = time.strftime('%Y%m%d%H%M%S', time.gmtime())
25         data['wpStarttime'] = time.strftime('%Y%m%d%H%M%S', time.gmtime())
26
27         if minor:
28             data['wpMinoredit'] = '1'
29         data['title'] = self.name
30
31         page_data = self.site.raw_index('submit', **data)
32
33         page = EditPage('editform')
34         page.feed(page_data)
35         page.close()
36
37         if page.data:
38             if page.readonly:
39                 raise errors.ProtectedPageError(self)
40             self.get_token('edit', True)
41             raise errors.EditError(page.title, data)
42
43     @staticmethod
44     def move(self, new_title, reason='', move_talk=True):
45         postdata = {'wpNewTitle': new_title,
46                     'wpOldTitle': self.name,
47                     'wpReason': reason,
48                     'wpMove': '1',
49                     'wpEditToken': self.get_token('move')}
50         if move_talk:
51             postdata['wpMovetalk'] = '1'
52         postdata['title'] = 'Special:Movepage'
53
54         page_data = self.site.raw_index('submit', **data)
55
56         page = EditPage('movepage')
57         page.feed(page_data.decode('utf-8', 'ignore'))
58         page.close()
59
60         if 'wpEditToken' in page.data:
61             raise errors.EditError(page.title, postdata)
62
63     @staticmethod
64     def delete(self, reason=''):
65         postdata = {'wpReason': reason,
66                     'wpConfirmB': 'Delete',
67                     'mw-filedelete-submit': 'Delete',
68                     'wpEditToken': self.get_token('delete'),
69                     'title': self.name}
70
71         page_data = self.site.raw_index('delete', **postdata)
72
73
74 class EditPage(HTMLParser):
75
76     def __init__(self, form):
77         HTMLParser.__init__(self)
78
79         self.form = form
80
81         self.in_form = False
82         self.in_text = False
83         self.in_title = False
84
85         self.data = {}
86         self.textdata = []
87         self.title = u''
88
89         self.readonly = True
90
91     def handle_starttag(self, tag, attrs):
92         self.in_title = (tag == 'title')
93
94         if (u'id', self.form) in attrs:
95             attrs = dict(attrs)
96             self.in_form = True
97             self.action = attrs['action']
98
99         if tag == 'input' and self.in_form and (u'type', u'submit') \
100                 not in attrs and (u'type', u'checkbox') not in attrs:
101             attrs = dict(attrs)
102             if u'name' in attrs:
103                 self.data[attrs[u'name']] = attrs.get(u'value', u'')
104
105         if self.in_form and tag == 'textarea':
106             self.in_text = True
107             self.readonly = (u'readonly', u'readonly') in attrs
108
109     def handle_endtag(self, tag):
110         if self.in_title and tag == 'title':
111             self.in_title = False
112         if self.in_form and tag == 'form':
113             self.in_form = False
114         if self.in_text and tag == 'textarea':
115             self.in_text = False
116
117     def handle_data(self, data):
118         if self.in_text:
119             self.textdata.append(data)
120         if self.in_title:
121             self.title += data
122
123     def handle_entityref(self, name):
124         if name in name2codepoint:
125             self.handle_data(unichr(name2codepoint[name]))
126         else:
127             self.handle_data(u'&%s;' % name)
128
129     def handle_charref(self, name):
130         try:
131             self.handle_data(unichr(int(name)))
132         except ValueError:
133             self.handle_data(u'&#$s;' % name)

Benjamin Mako Hill || Want to submit a patch?