2 from HTMLParser import HTMLParser
3 from htmlentitydefs import name2codepoint
11 def save(self, text=u'', summary=u'', minor=False):
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)
20 data['wpEdittime'] = time.strftime('%Y%m%d%H%M%S', time.gmtime())
22 data['wpStarttime'] = time.strftime('%Y%m%d%H%M%S', self.edit_time)
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())
28 data['wpMinoredit'] = '1'
29 data['title'] = self.name
31 page_data = self.site.raw_index('submit', **data)
33 page = EditPage('editform')
39 raise errors.ProtectedPageError(self)
40 self.get_token('edit', True)
41 raise errors.EditError(page.title, data)
44 def move(self, new_title, reason='', move_talk=True):
45 postdata = {'wpNewTitle': new_title,
46 'wpOldTitle': self.name,
49 'wpEditToken': self.get_token('move')}
51 postdata['wpMovetalk'] = '1'
52 postdata['title'] = 'Special:Movepage'
54 page_data = self.site.raw_index('submit', **data)
56 page = EditPage('movepage')
57 page.feed(page_data.decode('utf-8', 'ignore'))
60 if 'wpEditToken' in page.data:
61 raise errors.EditError(page.title, postdata)
64 def delete(self, reason=''):
65 postdata = {'wpReason': reason,
66 'wpConfirmB': 'Delete',
67 'mw-filedelete-submit': 'Delete',
68 'wpEditToken': self.get_token('delete'),
71 page_data = self.site.raw_index('delete', **postdata)
74 class EditPage(HTMLParser):
76 def __init__(self, form):
77 HTMLParser.__init__(self)
91 def handle_starttag(self, tag, attrs):
92 self.in_title = (tag == 'title')
94 if (u'id', self.form) in attrs:
97 self.action = attrs['action']
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:
103 self.data[attrs[u'name']] = attrs.get(u'value', u'')
105 if self.in_form and tag == 'textarea':
107 self.readonly = (u'readonly', u'readonly') in attrs
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':
114 if self.in_text and tag == 'textarea':
117 def handle_data(self, data):
119 self.textdata.append(data)
123 def handle_entityref(self, name):
124 if name in name2codepoint:
125 self.handle_data(unichr(name2codepoint[name]))
127 self.handle_data(u'&%s;' % name)
129 def handle_charref(self, name):
131 self.handle_data(unichr(int(name)))
133 self.handle_data(u'&#$s;' % name)