]> projects.mako.cc - wikipedia-api-cdsw/blob - mwclient/compatibility.py
273a16fb8a50633fff508f4e37ab0841788176ff
[wikipedia-api-cdsw] / mwclient / compatibility.py
1 import upload
2 import errors
3
4
5 def title(prefix, new_format):
6     if new_format:
7         return prefix + 'title'
8     else:
9         return 'titles'
10
11
12 def userinfo(data, new_format=None):
13     if new_format is None:
14         # Unknown version; trying to guess
15         if 'userinfo' in data:
16             return data['userinfo']
17         elif 'userinfo' in data.get('query', ()):
18             return data['query']['userinfo']
19         else:
20             return {}
21     elif new_format:
22         return data['query']['userinfo']
23     else:
24         return data['userinfo']
25
26
27 def iiprop(version):
28     if version[:2] >= (1, 13):
29         return 'timestamp|user|comment|url|size|sha1|metadata|archivename'
30     if version[:2] >= (1, 12):
31         return 'timestamp|user|comment|url|size|sha1|metadata'
32     else:
33         return 'timestamp|user|comment|url|size|sha1'
34
35
36 def cmtitle(page, new_format, prefix=''):
37     if new_format:
38         return prefix + 'title', page.name
39     else:
40         return prefix + 'category', page.strip_namespace(page.name)
41
42
43 def protectright(version):
44     if version[:2] >= (1, 13):
45         return 'editprotected'
46     else:
47         return 'protect'
48
49 from cStringIO import StringIO
50
51
52 def old_upload(self, file, filename, description, license='', ignore=False, file_size=None):
53     image = self.Images[filename]
54     if not image.can('upload'):
55         raise errors.InsufficientPermission(filename)
56     if image.exists and not ignore:
57         raise errors.FileExists(filename)
58
59     if type(file) is str:
60         file_size = len(file)
61         file = StringIO(file)
62     if file_size is None:
63         file.seek(0, 2)
64         file_size = file.tell()
65         file.seek(0, 0)
66
67     predata = {}
68     # Do this thing later so that an incomplete upload won't work
69     # predata['wpDestFile'] = filename
70     predata['wpUploadDescription'] = description
71     predata['wpLicense'] = license
72     if ignore:
73         predata['wpIgnoreWarning'] = 'true'
74     predata['wpUpload'] = 'Upload file'
75     predata['wpSourceType'] = 'file'
76     predata['wpDestFile'] = filename
77     predata['wpEditToken'] = image.get_token('edit')
78
79     postdata = upload.UploadFile('wpUploadFile', filename, file_size, file, predata)
80
81     wait_token = self.wait_token()
82     while True:
83         try:
84             self.connection.post(self.host,
85                                  self.path + 'index.php?title=Special:Upload&maxlag='
86                                  + self.max_lag, data=postdata).read()
87         except errors.HTTPStatusError, e:
88             if e[0] == 503 and e[1].getheader('X-Database-Lag'):
89                 self.wait(wait_token, int(e[1].getheader('Retry-After')))
90             elif e[0] < 500 or e[0] > 599:
91                 raise
92             else:
93                 self.wait(wait_token)
94         except errors.HTTPError:
95             self.wait(wait_token)
96         else:
97             return
98         file.seek(0, 0)

Benjamin Mako Hill || Want to submit a patch?