]> ToastFreeware Gitweb - philipp/winterrodeln/wradmin.git/blob - wradmin/wradmin/lib/mediawiki.py
32e43037bd2455ba7289cf07fc58b0ecde2821f2
[philipp/winterrodeln/wradmin.git] / wradmin / wradmin / lib / mediawiki.py
1 #!/usr/bin/python2.5
2 # -*- coding: iso-8859-15 -*-
3 # $Id$
4 "MediaWiki communication functions"
5 import datetime
6 import re
7
8 from authkit.users import UsersReadOnly, md5
9 import formencode, formencode.national
10 import logging
11 log = logging.getLogger(__name__)
12
13 import wradmin.model as model
14 import wradmin.model.validators
15
16
17 # Converter functions
18 # -------------------
19
20 def to_title(value):
21     """Line 2237 of includes/Title.php says: $this->mTextform = str_replace( '_', ' ', $dbkey );
22     No not check for None because a missing title is an error"""
23     return value.replace(u'_', u' ')
24
25
26 # deprecated
27 def to_phone_info(value):
28     return model.validators.PhoneInfo(messages={'phoneInfo': u"Bitte verwenden Sie ein Format wie '0512/123456 (Schnee Alm)'."}).to_python(value)
29
30
31 def conv(fnct, value, fieldname):
32     "Like one of the to_xxx functions (e.g. to_bool), but adds the field name to the error message"
33     try: return fnct(value)
34     except formencode.Invalid, e: raise formencode.Invalid(u"Conversion error in field '%s': %s" % (fieldname, unicode_e(e)), e.value, e.state)
35
36
37 def unicode_e(exception):
38     """Does "unicode(exception)" as it should be. This is a workaround for bug http://bugs.python.org/issue2517
39     that is not fixed in python 2.5.2.
40     Details of bug: "unicode(Exception(u'\xe4'))" raises an UnicodeEncodeError exception."""
41     if exception.message: return unicode(exception.message)
42     return unicode(exception)
43
44
45 def wikipage_to_wrsleddingcache1_2(page_id, page_title, page_text):
46     """Converts a wiki page about a sledding route to a wradmin.model.WrSleddingCache1_2 class
47     that can be inserted to the wradmin.model.wrsleddingcache1_2_table.
48     It needs the wiki page id, the wiki page title and the page text ("old_text") as they come from the database."""
49     sl = model.WrSleddingCache1_2()
50     sl.page_id = page_id
51     sl.page_title = to_title(page_title)
52     
53     # Match Rodelbahnbox
54     wikitext = page_text
55     regexp = re.compile(u"\{\{(Rodelbahnbox[^\}]*)\}\}", re.DOTALL)
56     match = regexp.search(wikitext)
57     if not match:
58         raise Exception(u"No 'Rodelbahnbox' found")
59     box = match.group(1)
60     
61     # Process Rodelbahnbox
62     for property in box.split('|'):
63         property = property.strip()
64         if property == u'Rodelbahnbox': continue
65         key_value = property.split('=')
66         if len(key_value) != 2:
67             raise Exception(u"Property '%s' has unexpected format" % key_value)
68         key = key_value[0].strip()
69         value = key_value[1].strip()
70         if key == u'Rodelbahnnummer': pass
71         elif key == u'Länge': sl.length = conv(model.validators.Unsigned().to_python, value, u'Länge')
72         elif key == u'Gehzeit': sl.walktime = conv(model.validators.Unsigned().to_python, value, u'Gehzeit')
73         elif key == u'Höhe oben': sl.height_top = conv(model.validators.Unsigned().to_python, value, u'Höhe oben')
74         elif key == u'Höhe unten': sl.height_bottom = conv(model.validators.Unsigned().to_python, value, u'Höhe unten')
75         elif key == u'Aufstieg getrennt': sl.walkup_separate = conv(model.validators.GermanBoolNone().to_python, value, u'Aufstieg getrennt')
76         elif key == u'Lift': sl.lift = conv(model.validators.GermanBoolNone().to_python, value, u'Lift')
77         elif key == u'Beleuchtung': sl.night_light = conv(model.validators.GermanBoolNone().to_python, value, u'Beleuchtung')
78         elif key == u'Rodelverleih': sl.sledge_rental = conv(model.validators.GermanBoolNone().to_python, value, u'Rodelverleih')
79         elif key == u'Öffentliche Anreise': sl.public_transport = conv(model.validators.GermanBoolNone().to_python, value, u'Öffentliche Anreise')
80         elif key == u'Bild': sl.image = conv(model.validators.UnicodeNone().to_python, value, key)
81         elif key == u'Position': (sl.position_latitude, sl.position_longitude) = conv(model.validators.GeoNone().to_python, value, u'Position') # '47.583333 N 15.75 E'
82         elif key == u'Auskunft': sl.information = conv(model.validators.AustrianPhoneNumberCommentLoop().to_python, value, u'Auskunft')
83         elif key == u'In Übersichtskarte': sl.show_in_overview = conv(model.validators.GermanBoolNone().to_python, value, u'In Übersichtskarte')
84         elif key == u'Aufnahmedatum': sl.creation_date = conv(model.validators.DateNone().to_python, value, u'Aufnahmedatum') # '2006-03-15'
85         elif key == u'Lawinengefahr':
86             # sl.avalanches is not part of the 1.2 sleddingcache table. We store it in the WrSleddingCache1_2 anyway.
87             sl.avalanches = conv(model.validators.GermanAvalanches().to_python, value, key)
88         else: raise formencode.Invalid(u"Unbekannte Eigenschaft der Rodelbahnbox: '%s' (mit Wert '%s')" % (key, value), value, None)
89     sl.under_construction = None
90     
91     # Match Forumlink (e.g. {{Forumlink|68}})
92     match = re.search(u"\{\{Forumlink\|(\d+)\}\}", wikitext)
93     if match: sl.forum_id = match.group(1)
94     
95     return sl
96
97
98 def wikipage_to_wrsleddingcache(page_id, page_title, page_text):
99     """Converts a sled-route wiki page (wradmin.model.page_table) 
100     to a sledding route wrsleddingcache database record (wradmin.model.wrsleddingcache_table).
101     Raises a RuntimeError if the format is not OK
102     sledding_wiki is a column of tabe "page".
103     Returns the WrSleddingCache class"""
104     sl = model.WrSleddingCache()
105     sl.page_id = page_id
106     sl.page_title = to_title(page_title)
107     errors = [] # List of errors with localized messages
108     
109     # Match Rodelbahnbox
110     wikitext = page_text
111     regexp = re.compile(u"\{\{(Rodelbahnbox[^\}]*)\}\}", re.DOTALL)
112     match = regexp.search(wikitext)
113     if not match:
114         raise RuntimeError(_(u"No 'Rodelbahnbox' found"))
115     box = match.group(1)
116     
117     # Process Rodelbahnbox
118     for property in box.split('|'):
119         property = property.strip()
120         if property == u'Rodelbahnbox': continue
121         key_value = property.split('=')
122         if len(key_value) != 2:
123             raise RuntimeError(_(u"Property '%s' has unexpected format") % key_value)
124         key = key_value[0].strip()
125         value = key_value[1].strip()
126         if key in [u'Rodelbahnnummer', u'Lift']:
127             errors.append(_("Property '%s' is not supported anymore, see %s.") % (key, 'http://www.winterrodeln.org/wiki/Vorlage:Rodelbahnbox'))
128         elif key == u'Position': sl.position_latitude, sl.position_longitude = conv(model.validators.GeoNone().to_python, value, key) # '47.583333 N 15.75 E'
129         elif key == u'Position oben': sl.top_latitude, sl.top_longitude = conv(model.validators.GeoNone().to_python, value, key) # '47.583333 N 15.75 E'
130         elif key == u'Höhe oben': sl.top_elevation = conv(model.validators.UnsignedNone().to_python, value, key) # '2000'
131         elif key == u'Position unten': sl.bottom_latitude, sl.bottom_longitude = conv(model.validators.GeoNone().to_python, value, key) # '47.583333 N 15.75 E'
132         elif key == u'Höhe unten': sl.bottom_elevation = conv(model.validators.UnsignedNone().to_python, value, key) # '1200'
133         elif key == u'Länge': sl.length = conv(model.validators.UnsignedNone().to_python, value, key) # 3500
134         elif key == u'Schwierigkeit': sl.difficulty = conv(model.validators.GermanDifficulty().to_python, value, key) # 'mittel'
135         elif key == u'Lawinen': sl.avalanches = conv(model.validators.GermanAvalanches().to_python, value, key) # 'kaum'
136         elif key == u'Betreiber': sl.operator = conv(model.validators.UnicodeNone().to_python, value, key) # 'Max Mustermann'
137         elif key == u'Öffentliche Anreise': sl.public_transport = conv(model.validators.GermanPublicTransport().to_python, value, key) # 'Mittelmäßig'
138         elif key == u'Gehzeit': sl.walkup_time = conv(model.validators.UnsignedNone().to_python, value, key) # 90
139         elif key == u'Aufstieg getrennt': sl.walkup_separate, sl.walkup_separate_comment = conv(model.validators.GermanTristateFloatComment().to_python, value, key) # 'Ja'
140         elif key == u'Aufstiegshilfe': sl.lift, sl.lift_details = conv(model.validators.GermanLift().to_python, value, key) # 'Gondel (unterer Teil)'
141         elif key == u'Beleuchtungsanlage': sl.night_light, sl.night_light_comment = conv(model.validators.GermanTristateFloatComment().to_python, value, key)
142         elif key == u'Beleuchtungstage': sl.night_light_days, sl.night_light_days_comment = conv(model.validators.UnsignedCommentNone(7).to_python, value, key) # '3 (Montag, Mittwoch, Freitag)'
143         elif key == u'Rodelverleih': sl.sled_rental, sl.sled_rental_comment = conv(model.validators.SledRental().to_python, value, key) # 'Talstation Serlesbahnan'
144         elif key == u'Gütesiegel': sl.cachet = conv(model.validators.GermanCachet().to_python, value, key) # 'Tiroler Naturrodelbahn-Gütesiegel 2009 mittel'
145         elif key == u'Webauskunft': sl.information_web = conv(model.validators.UrlNeinNone().to_python, value, key) # 'http://www.nösslachhütte.at/page9.php'
146         elif key == u'Telefonauskunft': sl.information_phone = conv(model.validators.PhoneCommentListNeinLoopNone(comments_are_optional=False).to_python, value, key) # '+43-664-5487520 (Mitterer Alm)'
147         elif key == u'Bild': sl.image = conv(model.validators.UnicodeNone().to_python, value, key)
148         elif key == u'In Übersichtskarte': sl.show_in_overview = conv(model.validators.GermanBoolNone().to_python, value, key)
149         elif key == u'Forumid': sl.forum_id = conv(model.validators.UnsignedNeinNone().to_python, value, key)
150         else: raise formencode.Invalid(u"Unbekannte Eigenschaft der Rodelbahnbox: '%s' (mit Wert '%s')" % (key, value), value, None)
151     sl.under_construction = None
152     return sl
153
154
155 def wrSleddingCache1_2_to_WrSleddingCache(wrSleddingCache1_2):
156     """Converts the old WrSleddingCache format (1.2) WrSleddingCache1_2
157     to the new format (1.3) WrSleddingCache."""
158     wrSleddingCache = model.WrSleddingCache() # Create an object in the new format
159     wrSleddingCache.page_id = wrSleddingCache1_2.page_id
160     wrSleddingCache.page_title = wrSleddingCache1_2.page_id
161     wrSleddingCache.position_latitude = wrSleddingCache1_2.position_latitude
162     wrSleddingCache.position_longitude = wrSleddingCache1_2.position_longitude
163     wrSleddingCache.top_latitude = None
164     wrSleddingCache.top_longitude = None
165     wrSleddingCache.top_elevation = wrSleddingCache1_2.height_top
166     wrSleddingCache.bottom_latitude = None
167     wrSleddingCache.bottom_longitude = None
168     wrSleddingCache.bottom_elevation = wrSleddingCache1_2.height_bottom
169     wrSleddingCache.length = wrSleddingCache1_2.length
170     wrSleddingCache.difficulty = None
171     if 'avalanches' in dir(wrSleddingCache1_2): wrSleddingCache.avalanches = wrSleddingCache1_2.avalanches
172     else: wrSleddingCache.avalanches = None
173     wrSleddingCache.operator = None
174     if wrSleddingCache1_2.public_transport is None: wrSleddingCache.public_transport = None
175     else: wrSleddingCache.public_transport = 6 if wrSleddingCache1_2.public_transport else 5
176     wrSleddingCache.walkup_time = wrSleddingCache1_2.walktime
177     if wrSleddingCache1_2.walkup_separate is None: wrSleddingCache.walkup_separate = None
178     wrSleddingCache.walkup_separate = 1.0 if wrSleddingCache1_2.walkup_separate else 0.0
179     wrSleddingCache.walkup_separate_comment = None
180     wrSleddingCache.lift = wrSleddingCache1_2.lift
181     if wrSleddingCache1_2.lift is None: wrSleddingCache.lift_details = None
182     elif wrSleddingCache1_2.lift: wrSleddingCache.lift_details = "Sonstige"
183     else: wrSleddingCache.lift_details = None
184     if wrSleddingCache1_2.night_light is None: wrSleddingCache.night_light = None
185     else: wrSleddingCache.night_light = 1.0 if wrSleddingCache1_2.night_light else 0.0
186     wrSleddingCache.night_light_comment = None
187     wrSleddingCache.night_light_days = None
188     wrSleddingCache.night_light_days_comment = None
189     wrSleddingCache.sled_rental = wrSleddingCache1_2.sledge_rental
190     if wrSleddingCache.sled_rental: wrSleddingCache.sled_rental_comment = u'Ja'
191     else: wrSleddingCache.sled_rental_comment = None
192     wrSleddingCache.cachet = None
193     wrSleddingCache.information_web = None
194     if wrSleddingCache1_2.information is None: wrSleddingCache.information_phone = None
195     else:
196         m = re.match('^([-\d/\+]{5,}) \((.+)\)', wrSleddingCache1_2.information)
197         if m is None: raise formencode.Invalid('PhoneInfo is invalid', value, None)
198         (phone, info) = m.groups()
199         # check phone
200         phone = wradmin.model.validators.AustrianPhoneNumber().to_python(phone)
201         # convert phone
202         c = formencode.national.InternationalPhoneNumber(default_cc=lambda: 43)
203         phone = c.to_python(phone)
204         wrSleddingCache.information_phone = '%s (%s)' % (phone, info)
205     wrSleddingCache.image = wrSleddingCache1_2.image
206     wrSleddingCache.show_in_overview = wrSleddingCache1_2.show_in_overview
207     wrSleddingCache.forum_id = wrSleddingCache1_2.forum_id
208     wrSleddingCache.under_construction = wrSleddingCache1_2.under_construction
209     return wrSleddingCache
210
211
212 def wrSleddingCache_to_Rodelbahnbox(wrSleddingCache):
213     """Converts the WrSleddingCache class to the {{Rodelbahnbox}} representation."""
214     keys = []
215     values = []
216     keys.append(u'Position')
217     values.append(model.validators.GeoNone().from_python((wrSleddingCache.position_latitude, wrSleddingCache.position_longitude)))
218     keys.append(u'Position oben')
219     values.append(model.validators.GeoNone().from_python((wrSleddingCache.top_latitude, wrSleddingCache.top_longitude)))
220     keys.append(u'Höhe oben')
221     values.append(model.validators.UnsignedNone().from_python(wrSleddingCache.top_elevation))
222     keys.append(u'Position unten')
223     values.append(model.validators.GeoNone().from_python((wrSleddingCache.bottom_latitude, wrSleddingCache.bottom_longitude)))
224     keys.append(u'Höhe unten')
225     values.append(model.validators.UnsignedNone().from_python(wrSleddingCache.bottom_elevation))
226     keys.append(u'Länge')
227     values.append(model.validators.UnsignedNone().from_python(wrSleddingCache.length))
228     keys.append(u'Schwierigkeit')
229     values.append(model.validators.GermanDifficulty().from_python(wrSleddingCache.difficulty))
230     keys.append(u'Lawinen')
231     values.append(model.validators.GermanAvalanches().from_python(wrSleddingCache.avalanches))
232     keys.append(u'Betreiber')
233     values.append(model.validators.UnicodeNone().from_python(wrSleddingCache.operator))
234     keys.append(u'Öffentliche Anreise')
235     values.append(model.validators.GermanPublicTransport().from_python(wrSleddingCache.public_transport))
236     keys.append(u'Gehzeit')
237     values.append(model.validators.UnsignedNone().from_python(wrSleddingCache.walkup_time))
238     keys.append(u'Aufstieg getrennt')
239     values.append(model.validators.GermanTristateFloatComment().from_python((wrSleddingCache.walkup_separate, wrSleddingCache.walkup_separate_comment)))
240     keys.append(u'Aufstiegshilfe')
241     values.append(model.validators.GermanLift().from_python((wrSleddingCache.lift, wrSleddingCache.lift_details)))
242     keys.append(u'Beleuchtungsanlage')
243     values.append(model.validators.GermanTristateFloatComment().from_python((wrSleddingCache.night_light, wrSleddingCache.night_light_comment)))
244     keys.append(u'Beleuchtungstage')
245     values.append(model.validators.UnsignedCommentNone(max=7).from_python((wrSleddingCache.night_light_days, wrSleddingCache.night_light_days_comment)))
246     keys.append(u'Rodelverleih')
247     values.append(model.validators.SledRental().from_python((wrSleddingCache.sled_rental, wrSleddingCache.sled_rental_comment)))
248     keys.append(u'Gütesiegel')
249     values.append(model.validators.GermanCachet().from_python(wrSleddingCache.cachet))
250     keys.append(u'Webauskunft')
251     values.append(model.validators.UrlNeinNone().from_python(wrSleddingCache.information_web))
252     keys.append(u'Telefonauskunft')
253     values.append(model.validators.PhoneCommentListNeinLoopNone(comments_are_optional=False).from_python(wrSleddingCache.information_phone))
254     keys.append(u'Bild')
255     values.append(model.validators.UnicodeNone().from_python(wrSleddingCache.image))
256     keys.append(u'In Übersichtskarte')
257     values.append(model.validators.GermanBoolNone().from_python(wrSleddingCache.show_in_overview))
258     keys.append(u'Forumid')
259     values.append(model.validators.UnsignedNeinNone().from_python(wrSleddingCache.forum_id))
260     result = [u'{{Rodelbahnbox']
261     for i in xrange(len(keys)): result.append(u'| %-20s = %s' % (keys[i], values[i]))
262     result.append('}}\n')
263     return '\n'.join(result)
264
265
266 def wikipage_to_wrinncache1_2(page_id, page_title, page_text):
267     """Converts a wiki page about an inn to an wradmin.model.WrInnCache1_2 class
268     that can be inserted to the wradmin.model.wrinncache1_2_table.
269     It uses only text operations and does not query or update the database.
270     It needs the wiki page id, the wiki page title and the page text ("old_text") as they come from the database."""
271     inn = model.WrInnCache1_2()
272     inn.page_id = page_id
273     inn.page_title = to_title(page_title)
274     
275     # Match Gasthausbox
276     wikitext = page_text
277     regexp = re.compile(u"\{\{(Gasthausbox[^\}]*)\}\}", re.DOTALL)
278     match = regexp.search(wikitext)
279     if not match:
280         raise Exception(u"No 'Gasthausbox' found")
281     box = match.group(1)
282     
283     # Process Gashausbox
284     for property in box.split('|'):
285         property = property.strip()
286         if property == u'Gasthausbox': continue
287         key_value = property.split('=')
288         if len(key_value) != 2:
289             raise Exception(u"Property '%s' has unexpected format" % key_value)
290         key = key_value[0].strip()
291         value = key_value[1].strip()
292         if key == u'Gasthausnummer': pass
293         elif key == u'E-Mail': inn.email = conv(formencode.validators.Email().to_python, value, u'E-Mail')
294         elif key == u'Homepage': inn.homepage = conv(model.validators.UrlNeinNone().to_python, value, u'Homepage')
295         elif key == u'Höhe': inn.height = conv(model.validators.Unsigned().to_python, value, u'Höhe')
296         elif key == u'Bild': inn.image = conv(model.validators.UnicodeNone().to_python, value, key)
297         elif key == u'Position': inn.position_latitude, inn.position_longitude = conv(model.validators.GeoNone().to_python, value, u'Position') # '47.583333 N 15.75 E'
298         elif key == u'Telefon (Festnetz)': inn.phone = conv(model.validators.AustrianPhoneNumberNone().to_python, value, u'Telefon (Festnetz)')
299         elif key == u'Telefon (Mobil)': inn.mobile_phone = conv(model.validators.AustrianPhoneNumberNone().to_python, value, u'Telefon (Mobil)')
300         elif key == u'Rauchfrei': inn.nonsmoker_area, inn.smoker_area = conv(model.validators.GermanTristateTuple().to_python, value, u'Rauchfrei')
301         elif key == u'Aufnahmedatum': inn.creation_date = conv(model.validators.DateNone().to_python, value, u'Aufnahmedatum') # '2006-03-15'
302         else: raise formencode.Invalid(u"Unbekannte Eigenschaft der Gasthausbox: '%s' (mit Wert '%s')" % (key, value), value, None)
303     inn.under_construction = None
304     return inn
305
306
307 def wikipage_to_wrinncache(page_id, page_title, page_text):
308     """Converts a inn wiki page (wradmin.model.page_table) to a wrinncache database record
309     (wradmin.model.wrinncache_table)."""
310     inn = model.WrInnCache()
311     inn.page_id = page_id
312     inn.page_title = to_title(page_title)
313     
314     # Match Gasthausbox
315     wikitext = page_text
316     regexp = re.compile(u"\{\{(Gasthausbox[^\}]*)\}\}", re.DOTALL)
317     match = regexp.search(wikitext)
318     if not match:
319         raise Exception(u"No 'Gasthausbox' found")
320     box = match.group(1)
321     
322     # Process Gashausbox
323     for property in box.split('|'):
324         property = property.strip()
325         if property == u'Gasthausbox': continue
326         key_value = property.split('=')
327         if len(key_value) != 2:
328             raise Exception(u"Property '%s' has unexpected format" % key_value)
329         key = key_value[0].strip()
330         value = key_value[1].strip()
331         if key == u'Position': inn.position_latitude, inn.position_longitude = conv(model.validators.GeoNone().to_python, value, key) # '47.583333 N 15.75 E'
332         elif key == u'Höhe': inn.position_elevation = conv(model.validators.UnsignedNone().to_python, value, key)
333         elif key == u'Betreiber': inn.operator = conv(model.validators.UnicodeNone().to_python, value, key)
334         elif key == u'Sitzplätze': inn.seats = conv(model.validators.UnsignedNone().to_python, value, key)
335         elif key == u'Übernachtung': inn.overnight, inn.overnight_comment = conv(model.validators.BoolUnicodeTupleValidator().to_python, value, key)
336         elif key == u'Rauchfrei': inn.nonsmoker_area, inn.smoker_area = conv(model.validators.GermanTristateTuple().to_python, value, key)
337         elif key == u'Rodelverleih': inn.sled_rental, inn.sled_rental_comment = conv(model.validators.BoolUnicodeTupleValidator().to_python, value, key)
338         elif key == u'Handyempfang': inn.mobile_provider = conv(model.validators.ValueCommentListNeinLoopNone().to_python, value, key)
339         elif key == u'Homepage': inn.homepage = conv(model.validators.UrlNeinNone().to_python, value, key)
340         elif key == u'E-Mail': inn.email_list = conv(model.validators.EmailCommentListNeinLoopNone().to_python, value, key)
341         elif key == u'Telefon': inn.phone_list = conv(model.validators.PhoneCommentListNeinLoopNone(comments_are_optional=True).to_python, value, key)
342         elif key == u'Bild': inn.image = conv(model.validators.UnicodeNone().to_python, value, key)
343         elif key == u'Rodelbahnen': inn.sledding_list = conv(model.validators.WikiPageListLoopNone().to_python, value, key)
344         else: raise formencode.Invalid(u"Unbekannte Eigenschaft der Gasthausbox: '%s' (mit Wert '%s')" % (key, value), value, None)
345     inn.under_construction = None
346     return inn
347
348
349 def wrInnCache1_2_to_WrInnCache(wrInnCache1_2):
350     """Converts the old WrInnCache format to the new format."""
351     wrInnCache = model.WrInnCache() # Create an object in the new format
352     wrInnCache.page_id = wrInnCache1_2.page_id
353     wrInnCache.page_title = wrInnCache1_2.page_id
354     wrInnCache.position_latitude = wrInnCache1_2.position_latitude
355     wrInnCache.position_longitude = wrInnCache1_2.position_longitude
356     wrInnCache.position_elevation = wrInnCache1_2.height
357     wrInnCache.operator = None
358     wrInnCache.seats = None
359     wrInnCache.overnight = None
360     wrInnCache.overnight_comment = None
361     wrInnCache.smoker_area = wrInnCache1_2.smoker_area
362     wrInnCache.nonsmoker_area = wrInnCache1_2.nonsmoker_area
363     wrInnCache.sled_rental = None
364     wrInnCache.sled_rental_comment = None
365     wrInnCache.mobile_provider = None
366     wrInnCache.homepage = wrInnCache1_2.homepage
367     wrInnCache.email_list = wrInnCache1_2.email
368     phone_list = []
369     c = formencode.national.InternationalPhoneNumber(default_cc=lambda: 43)
370     if not wrInnCache1_2.phone is None: phone_list.append(c.to_python(wrInnCache1_2.phone))
371     if not wrInnCache1_2.mobile_phone is None: phone_list.append(c.to_python(wrInnCache1_2.mobile_phone))
372     if len(phone_list) >= 1: wrInnCache.phone_list = "; ".join(phone_list)
373     else: phone_list = None
374     wrInnCache.image = wrInnCache1_2.image
375     wrInnCache.sledding_list = None
376     wrInnCache.under_construction = wrInnCache1_2.under_construction
377     return wrInnCache
378
379
380 def wrInnCache_to_Gasthausbox(wrInnCache):
381     """Converts the WrInnCache class to the {{Gasthausbox}} representation."""
382     keys = []
383     values = []
384     keys.append(u'Position')
385     values.append(model.validators.GeoNone().from_python((wrInnCache.position_latitude, wrInnCache.position_longitude)))
386     keys.append(u'Höhe')
387     values.append(model.validators.UnsignedNone().from_python(wrInnCache.position_elevation))
388     keys.append(u'Betreiber')
389     values.append(model.validators.UnicodeNone().from_python(wrInnCache.operator))
390     keys.append(u'Sitzplätze')
391     values.append(model.validators.UnsignedNone().from_python(wrInnCache.seats))
392     keys.append(u'Übernachtung')
393     values.append(model.validators.BoolUnicodeTupleValidator().from_python((wrInnCache.overnight, wrInnCache.overnight_comment)))
394     keys.append(u'Rauchfrei')
395     values.append(model.validators.GermanTristateTuple().from_python((wrInnCache.nonsmoker_area, wrInnCache.smoker_area)))
396     keys.append(u'Rodelverleih')
397     values.append(model.validators.BoolUnicodeTupleValidator().from_python((wrInnCache.sled_rental, wrInnCache.sled_rental_comment)))
398     keys.append(u'Handyempfang')
399     values.append(model.validators.ValueCommentListNeinLoopNone().from_python(wrInnCache.mobile_provider))
400     keys.append(u'Homepage')
401     values.append(model.validators.UrlNeinNone().from_python(wrInnCache.homepage))
402     keys.append(u'E-Mail')
403     values.append(model.validators.EmailCommentListNeinLoopNone().from_python(wrInnCache.email_list))
404     keys.append(u'Telefon')
405     values.append(model.validators.PhoneCommentListNeinLoopNone(comments_are_optional=True).from_python(wrInnCache.phone_list))
406     keys.append(u'Bild')
407     values.append(model.validators.UnicodeNone().from_python(wrInnCache.image))
408     keys.append(u'Rodelbahnen')
409     values.append(model.validators.WikiPageListLoopNone().from_python(wrInnCache.sledding_list))
410     result = [u'{{Gasthausbox']
411     for i in xrange(len(keys)): result.append(u'| %-17s = %s' % (keys[i], values[i]))
412     result.append('}}\n')
413     return '\n'.join(result)
414
415
416
417 # User management
418 # ---------------
419
420 class MediaWikiUsers(UsersReadOnly):
421     def __init__(self, data=None, encrypt=None):
422         UsersReadOnly.__init__(self, data, encrypt)
423
424         # Initialize class fields
425         self.usernames = []
426         self.passwords = {}
427         self.roles = {}
428         self.groups = {}
429         self.user_ids = {} # MediaWiki user_id field of the database
430         self.real_names = {} # Real names of the users
431         self.emails = {} # E-Mail addresses of the users
432         
433         # Query database
434         con = model.meta.engine.connect()
435         sql = "SELECT user_id, user_name, user_real_name, user_password, user_email FROM user, user_groups WHERE ug_user=user_id AND ug_group='beauftragte'"
436         result = con.execute(sql)
437         for row in result:
438             user_id, username, real_name, password, email = row
439             username = username.lower()
440             role = []
441             group = None
442             
443             self.usernames.append(username)
444             self.passwords[username] = password
445             self.roles[username] = role
446             self.groups[username] = group
447             self.user_ids[username] = user_id
448             self.real_names[username] = real_name
449             self.emails[username] = email
450         con.close()
451         log.info("%d users loaded from the MediaWiki database" % len(self.usernames))
452     
453     
454     def user_has_password(self, username, password):
455         """
456         Passwords are case sensitive.
457         Returns ``True`` if the user has the password specified, ``False`` otherwise. 
458         Raises an exception if the user doesn't exist.
459         
460         See http://www.winterrodeln.org/trac/wiki/MediaWikiAuthorization
461         """
462         pwd = self.user_password(username)
463         # Example: pwd = ':B:d25b2886:41e46c952790b1b442aac4f24f7ea7a8'
464         pwd_parts = pwd.split(':') # password_parts = ['', 'B', 'd25b2886', '41e46c952790b1b442aac4f24f7ea7a8']
465         if len(pwd_parts) == 4 and pwd_parts[1] == 'B':
466             salt, pwd_md5 = tuple(pwd_parts[2:4]) # salt = 'd25b2886'; pwd_md5 = '41e46c952790b1b442aac4f24f7ea7a8'
467         else:
468             raise AuthKitError("Password in the MediaWiki database format has an unexpected format ('%s' instead of e.g. ':B:d25b2886:41e46c952790b1b442aac4f24f7ea7a8')" % pwd)
469         # log.info("user: '%s'; md5 of salt+' '+entered_pwd: '%s'; md5-part of DB-pwd: %s" % (username, md5(salt + '-' + md5(password)), pwd_md5))
470         return md5(salt + '-' + md5(password)) == pwd_md5