]> git.openstreetmap.org Git - osqa.git/blob - forum_modules/sximporter/importer.py
Fixes the sx importer to use the latest db schema. Also, the fake orm is created...
[osqa.git] / forum_modules / sximporter / importer.py
1 # -*- coding: utf-8 -*-
2
3 from datetime import datetime
4 import time
5 import re
6 import os
7 import gc
8 from django.utils.translation import ugettext as _
9
10 from django.utils.encoding import force_unicode
11
12 try:
13     from cPickle import loads, dumps
14 except ImportError:
15     from pickle import loads, dumps
16
17 from copy import deepcopy
18 from base64 import b64encode, b64decode
19 from zlib import compress, decompress
20
21 from xml.sax import make_parser
22 from xml.sax.handler import ContentHandler
23
24 def create_orm():
25     from django.conf import settings
26     from south.orm import FakeORM
27
28     get_migration_number_re = re.compile(r'^((\d+)_.*)\.py$')
29
30     migrations_folder = os.path.join(settings.SITE_SRC_ROOT, 'forum/migrations')
31
32     highest_number = 0
33     highest_file = None
34
35     for f in os.listdir(migrations_folder):
36         if os.path.isfile(os.path.join(migrations_folder, f)):
37             m = get_migration_number_re.match(f)
38
39             if m:
40                 found = int(m.group(2))
41
42                 if found > highest_number:
43                     highest_number = found
44                     highest_file = m.group(1)
45
46     mod = __import__('forum.migrations.%s' % highest_file, globals(), locals(), ['forum.migrations'])
47     return FakeORM(getattr(mod, 'Migration'), "forum")
48
49 orm = create_orm()
50
51 class SXTableHandler(ContentHandler):
52     def __init__(self, fname, callback):
53         self.in_row = False
54         self.el_data = {}
55         self.ch_data = ''
56
57         self.fname = fname.lower()
58         self.callback = callback
59
60     def startElement(self, name, attrs):
61         if name.lower() == self.fname:
62             pass
63         elif name.lower() == "row":
64             self.in_row = True
65
66     def characters(self, ch):
67         self.ch_data += ch
68
69     def endElement(self, name):
70         if name.lower() == self.fname:
71             pass
72         elif name.lower() == "row":
73             self.callback(self.el_data)
74
75             self.in_row = False
76             del self.el_data
77             self.el_data = {}
78         elif self.in_row:
79             self.el_data[name.lower()] = self.ch_data.strip()
80             del self.ch_data
81             self.ch_data = ''
82
83
84 def readTable(path, name, callback):
85     parser = make_parser()
86     handler = SXTableHandler(name, callback)
87     parser.setContentHandler(handler)
88
89     f = os.path.join(path, "%s.xml" % name)
90     parser.parse(f)
91
92
93 def dbsafe_encode(value):
94     return force_unicode(b64encode(compress(dumps(deepcopy(value)))))
95
96 def getText(el):
97     rc = ""
98     for node in el.childNodes:
99         if node.nodeType == node.TEXT_NODE:
100             rc = rc + node.data
101     return rc.strip()
102
103 msstrip = re.compile(r'^(.*)\.\d+')
104 def readTime(ts):
105     noms = msstrip.match(ts)
106     if noms:
107         ts = noms.group(1)
108
109     return datetime(*time.strptime(ts, '%Y-%m-%dT%H:%M:%S')[0:6])
110
111 #def readEl(el):
112 #    return dict([(n.tagName.lower(), getText(n)) for n in el.childNodes if n.nodeType == el.ELEMENT_NODE])
113
114 #def readTable(dump, name):
115 #    for e in minidom.parseString(dump.read("%s.xml" % name)).getElementsByTagName('row'):
116 #        yield readEl(e)
117 #return [readEl(e) for e in minidom.parseString(dump.read("%s.xml" % name)).getElementsByTagName('row')]
118
119 google_accounts_lookup = re.compile(r'^https?://www.google.com/accounts/')
120 yahoo_accounts_lookup = re.compile(r'^https?://me.yahoo.com/a/')
121
122 openid_lookups = [
123         re.compile(r'^https?://www.google.com/profiles/(?P<uname>\w+(\.\w+)*)/?$'),
124         re.compile(r'^https?://me.yahoo.com/(?P<uname>\w+(\.\w+)*)/?$'),
125         re.compile(r'^https?://openid.aol.com/(?P<uname>\w+(\.\w+)*)/?$'),
126         re.compile(r'^https?://(?P<uname>\w+(\.\w+)*).myopenid.com/?$'),
127         re.compile(r'^https?://flickr.com/(\w+/)*(?P<uname>\w+(\.\w+)*)/?$'),
128         re.compile(r'^https?://technorati.com/people/technorati/(?P<uname>\w+(\.\w+)*)/?$'),
129         re.compile(r'^https?://(?P<uname>\w+(\.\w+)*).wordpress.com/?$'),
130         re.compile(r'^https?://(?P<uname>\w+(\.\w+)*).blogspot.com/?$'),
131         re.compile(r'^https?://(?P<uname>\w+(\.\w+)*).livejournal.com/?$'),
132         re.compile(r'^https?://claimid.com/(?P<uname>\w+(\.\w+)*)/?$'),
133         re.compile(r'^https?://(?P<uname>\w+(\.\w+)*).pip.verisignlabs.com/?$'),
134         re.compile(r'^https?://getopenid.com/(?P<uname>\w+(\.\w+)*)/?$'),
135         re.compile(r'^https?://[\w\.]+/(\w+/)*(?P<uname>\w+(\.\w+)*)/?$'),
136         re.compile(r'^https?://(?P<uname>[\w\.]+)/?$'),
137         ]
138
139 def final_username_attempt(sxu):
140     openid = sxu.get('openid', None)
141
142     if openid:
143         if google_accounts_lookup.search(openid):
144             return UnknownGoogleUser(sxu.get('id'))
145         if yahoo_accounts_lookup.search(openid):
146             return UnknownYahooUser(sxu.get('id'))
147
148         for lookup in openid_lookups:
149             if lookup.search(openid):
150                 return lookup.search(openid).group('uname')
151
152     return UnknownUser(sxu.get('id'))
153
154 class UnknownUser(object):
155     def __init__(self, id):
156         self._id = id
157
158     def __str__(self):
159         return _("user-%(id)s") % {'id': self._id}
160
161     def __unicode__(self):
162         return self.__str__()
163
164     def encode(self, *args):
165         return self.__str__()
166
167 class UnknownGoogleUser(UnknownUser):
168     def __str__(self):
169         return _("user-%(id)s (google)") % {'id': self._id}
170
171 class UnknownYahooUser(UnknownUser):
172     def __str__(self):
173         return _("user-%(id)s (yahoo)") % {'id': self._id}
174
175
176 class IdMapper(dict):
177     def __getitem__(self, key):
178         key = int(key)
179         return super(IdMapper, self).get(key, 1)
180
181     def __setitem__(self, key, value):
182         super(IdMapper, self).__setitem__(int(key), int(value))
183
184 class IdIncrementer():
185     def __init__(self, initial):
186         self.value = initial
187
188     def inc(self):
189         self.value += 1
190
191 openidre = re.compile('^https?\:\/\/')
192 def userimport(path, options):
193
194     usernames = []
195     openids = set()
196     uidmapper = IdMapper()
197
198     authenticated_user = options.get('authenticated_user', None)
199     owneruid = options.get('owneruid', None)
200     #check for empty values
201     if not owneruid:
202         owneruid = None
203
204     def callback(sxu):
205         create = True
206
207         if sxu.get('id') == '-1':
208             return
209         #print "\n".join(["%s : %s" % i for i in sxu.items()])
210
211         if int(sxu.get('id')) == int(owneruid):
212             if authenticated_user:
213                 osqau = orm.User.objects.get(id=authenticated_user.id)
214
215                 for assoc in orm.AuthKeyUserAssociation.objects.filter(user=osqau):
216                     openids.add(assoc.key)
217
218                 uidmapper[owneruid] = osqau.id
219                 uidmapper[-1] = osqau.id
220                 create = False
221             else:
222                 uidmapper[owneruid] = int(owneruid)
223                 uidmapper[-1] = int(owneruid)
224
225
226         sxbadges = sxu.get('badgesummary', None)
227         badges = {'1':'0', '2':'0', '3':'0'}
228
229         if sxbadges:
230             badges.update(dict([b.split('=') for b in sxbadges.split()]))
231
232         if create:
233             username = unicode(sxu.get('displayname',
234                                sxu.get('displaynamecleaned', sxu.get('realname', final_username_attempt(sxu)))))[:30]
235
236             if username in usernames:
237             #if options.get('mergesimilar', False) and sxu.get('email', 'INVALID') == user_by_name[username].email:
238             #    osqau = user_by_name[username]
239             #    create = False
240             #    uidmapper[sxu.get('id')] = osqau.id
241             #else:
242                 inc = 0
243
244                 while True:
245                     inc += 1
246                     totest = "%s %d" % (username[:29 - len(str(inc))], inc)
247
248                     if not totest in usernames:
249                         username = totest
250                         break
251
252             osqau = orm.User(
253                     id           = sxu.get('id'),
254                     username     = username,
255                     password     = '!',
256                     email        = sxu.get('email', ''),
257                     is_superuser = sxu.get('usertypeid') == '5',
258                     is_staff     = sxu.get('usertypeid') == '4',
259                     is_active    = True,
260                     date_joined  = readTime(sxu.get('creationdate')),
261                     last_seen    = readTime(sxu.get('lastaccessdate')),
262                     about         = sxu.get('aboutme', ''),
263                     date_of_birth = sxu.get('birthday', None) and readTime(sxu['birthday']) or None,
264                     email_isvalid = int(sxu.get('usertypeid')) > 2,
265                     website       = sxu.get('websiteurl', ''),
266                     reputation    = int(sxu.get('reputation')),
267                     gold          = int(badges['1']),
268                     silver        = int(badges['2']),
269                     bronze        = int(badges['3']),
270                     real_name     = sxu.get('realname', '')[:30],
271                     location      = sxu.get('location', ''),
272                     )
273
274             osqau.save()
275
276             user_joins = orm.Action(
277                     action_type = "userjoins",
278                     action_date = osqau.date_joined,
279                     user = osqau
280                     )
281             user_joins.save()
282
283             rep = orm.ActionRepute(
284                     value = 1,
285                     user = osqau,
286                     date = osqau.date_joined,
287                     action = user_joins
288                     )
289             rep.save()
290
291             try:
292                 orm.SubscriptionSettings.objects.get(user=osqau)
293             except:
294                 s = orm.SubscriptionSettings(user=osqau)
295                 s.save()
296
297             uidmapper[osqau.id] = osqau.id
298         else:
299             new_about = sxu.get('aboutme', None)
300             if new_about and osqau.about != new_about:
301                 if osqau.about:
302                     osqau.about = "%s\n|\n%s" % (osqau.about, new_about)
303                 else:
304                     osqau.about = new_about
305
306             osqau.username = sxu.get('displayname',
307                                      sxu.get('displaynamecleaned', sxu.get('realname', final_username_attempt(sxu))))
308             osqau.email = sxu.get('email', '')
309             osqau.reputation += int(sxu.get('reputation'))
310             osqau.gold += int(badges['1'])
311             osqau.silver += int(badges['2'])
312             osqau.bronze += int(badges['3'])
313
314             osqau.date_joined = readTime(sxu.get('creationdate'))
315             osqau.website = sxu.get('websiteurl', '')
316             osqau.date_of_birth = sxu.get('birthday', None) and readTime(sxu['birthday']) or None
317             osqau.location = sxu.get('location', '')
318             osqau.real_name = sxu.get('realname', '')
319
320             #merged_users.append(osqau.id)
321             osqau.save()
322
323         usernames.append(osqau.username)
324
325         openid = sxu.get('openid', None)
326         if openid and openidre.match(openid) and (not openid in openids):
327             assoc = orm.AuthKeyUserAssociation(user=osqau, key=openid, provider="openidurl")
328             assoc.save()
329             openids.add(openid)
330
331         openidalt = sxu.get('openidalt', None)
332         if openidalt and openidre.match(openidalt) and (not openidalt in openids):
333             assoc = orm.AuthKeyUserAssociation(user=osqau, key=openidalt, provider="openidurl")
334             assoc.save()
335             openids.add(openidalt)
336
337     readTable(path, "Users", callback)
338
339     if uidmapper[-1] == -1:
340         uidmapper[-1] = 1
341
342     return uidmapper
343
344 def tagsimport(dump, uidmap):
345
346     tagmap = {}
347
348     def callback(sxtag):
349         otag = orm.Tag(
350                 id = int(sxtag['id']),
351                 name = sxtag['name'],
352                 used_count = int(sxtag['count']),
353                 created_by_id = uidmap[sxtag.get('userid', 1)],
354                 )
355         otag.save()
356
357         tagmap[otag.name] = otag
358
359     readTable(dump, "Tags", callback)
360
361     return tagmap
362
363 def add_post_state(name, post, action):
364     if not "(%s)" % name in post.state_string:
365         post.state_string = "%s(%s)" % (post.state_string, name)
366         post.save()
367
368     try:
369         state = orm.NodeState.objects.get(node=post, state_type=name)
370         state.action = action
371         state.save()
372     except:
373         state = orm.NodeState(node=post, state_type=name, action=action)
374         state.save()
375
376 def remove_post_state(name, post):
377     if "(%s)" % name in post.state_string:
378         try:
379             state = orm.NodeState.objects.get(state_type=name, post=post)
380             state.delete()
381         except:
382             pass
383     post.state_string = "".join("(%s)" % s for s in re.findall('\w+', post.state_string) if s != name)
384
385 def postimport(dump, uidmap, tagmap):
386     all = []
387
388     def callback(sxpost):
389         nodetype = (sxpost.get('posttypeid') == '1') and "nodetype" or "answer"
390
391         post = orm.Node(
392                 node_type = nodetype,
393                 id = sxpost['id'],
394                 added_at = readTime(sxpost['creationdate']),
395                 body = sxpost['body'],
396                 score = sxpost.get('score', 0),
397                 author_id = sxpost.get('deletiondate', None) and 1 or uidmap[sxpost.get('owneruserid', 1)]
398                 )
399
400         post.save()
401
402         create_action = orm.Action(
403                 action_type = (nodetype == "nodetype") and "ask" or "answer",
404                 user_id = post.author_id,
405                 node = post,
406                 action_date = post.added_at
407                 )
408
409         create_action.save()
410
411         if sxpost.get('lasteditoruserid', None):
412             revise_action = orm.Action(
413                     action_type = "revise",
414                     user_id = uidmap[sxpost.get('lasteditoruserid')],
415                     node = post,
416                     action_date = readTime(sxpost['lasteditdate']),
417                     )
418
419             revise_action.save()
420             post.last_edited = revise_action
421
422         if sxpost.get('communityowneddate', None):
423             wikify_action = orm.Action(
424                     action_type = "wikify",
425                     user_id = 1,
426                     node = post,
427                     action_date = readTime(sxpost['communityowneddate'])
428                     )
429
430             wikify_action.save()
431             add_post_state("wiki", post, wikify_action)
432
433         if sxpost.get('lastactivityuserid', None):
434             post.last_activity_by_id = uidmap[sxpost['lastactivityuserid']]
435             post.last_activity_at = readTime(sxpost['lastactivitydate'])
436
437         if sxpost.get('posttypeid') == '1': #question
438             post.node_type = "question"
439             post.title = sxpost['title']
440
441             tagnames = sxpost['tags'].replace(u'ö', '-').replace(u'é', '').replace(u'à', '')
442             post.tagnames = tagnames
443
444             post.extra_count = sxpost.get('viewcount', 0)
445
446             add_tags_to_post(post, tagmap)
447
448         else:
449             post.parent_id = sxpost['parentid']
450
451         post.save()
452
453         all.append(int(post.id))
454         create_and_activate_revision(post)
455
456         del post
457
458     readTable(dump, "Posts", callback)
459
460     return all
461
462 def comment_import(dump, uidmap, posts):
463     currid = IdIncrementer(max(posts))
464     mapping = {}
465
466     def callback(sxc):
467         currid.inc()
468         oc = orm.Node(
469                 id = currid.value,
470                 node_type = "comment",
471                 added_at = readTime(sxc['creationdate']),
472                 author_id = uidmap[sxc.get('userid', 1)],
473                 body = sxc['text'],
474                 parent_id = sxc.get('postid'),
475                 )
476
477         if sxc.get('deletiondate', None):
478             delete_action = orm.Action(
479                     action_type = "delete",
480                     user_id = uidmap[sxc['deletionuserid']],
481                     action_date = readTime(sxc['deletiondate'])
482                     )
483
484             oc.author_id = uidmap[sxc['deletionuserid']]
485             oc.save()
486
487             delete_action.node = oc
488             delete_action.save()
489
490             add_post_state("deleted", oc, delete_action)
491         else:
492             oc.author_id = uidmap[sxc.get('userid', 1)]
493             oc.save()
494
495         create_action = orm.Action(
496                 action_type = "comment",
497                 user_id = oc.author_id,
498                 node = oc,
499                 action_date = oc.added_at
500                 )
501
502         create_and_activate_revision(oc)
503
504         create_action.save()
505         oc.save()
506
507         posts.append(int(oc.id))
508         mapping[int(sxc['id'])] = int(oc.id)
509
510     readTable(dump, "PostComments", callback)
511     return posts, mapping
512
513
514 def add_tags_to_post(post, tagmap):
515     tags = [tag for tag in [tagmap.get(name.strip()) for name in post.tagnames.split(u' ') if name] if tag]
516     post.tagnames = " ".join([t.name for t in tags]).strip()
517     post.tags = tags
518
519
520 def create_and_activate_revision(post):
521     rev = orm.NodeRevision(
522             author_id = post.author_id,
523             body = post.body,
524             node_id = post.id,
525             revised_at = post.added_at,
526             revision = 1,
527             summary = 'Initial revision',
528             tagnames = post.tagnames,
529             title = post.title,
530             )
531
532     rev.save()
533     post.active_revision_id = rev.id
534     post.save()
535
536 def post_vote_import(dump, uidmap, posts):
537     close_reasons = {}
538
539     def close_callback(r):
540         close_reasons[r['id']] = r['name']
541
542     readTable(dump, "CloseReasons", close_callback)
543
544     user2vote = []
545
546     def callback(sxv):
547         action = orm.Action(
548                 user_id=uidmap[sxv['userid']],
549                 action_date = readTime(sxv['creationdate']),
550                 )
551
552         if not int(sxv['postid']) in posts: return
553         node = orm.Node.objects.get(id=sxv['postid'])
554         action.node = node
555
556         if sxv['votetypeid'] == '1':
557             answer = node
558             question = orm.Node.objects.get(id=answer.parent_id)
559
560             action.action_type = "acceptanswer"
561             action.save()
562
563             answer.marked = True
564
565             question.extra_ref_id = answer.id
566
567             answer.save()
568             question.save()
569
570         elif sxv['votetypeid'] in ('2', '3'):
571             if not (action.node.id, action.user_id) in user2vote:
572                 user2vote.append((action.node.id, action.user_id))
573
574                 action.action_type = (sxv['votetypeid'] == '2') and "voteup" or "votedown"
575                 action.save()
576
577                 ov = orm.Vote(
578                         node_id = action.node.id,
579                         user_id = action.user_id,
580                         voted_at = action.action_date,
581                         value = sxv['votetypeid'] == '2' and 1 or -1,
582                         action = action
583                         )
584                 ov.save()
585             else:
586                 action.action_type = "unknown"
587                 action.save()
588
589         elif sxv['votetypeid'] in ('4', '12', '13'):
590             action.action_type = "flag"
591             action.save()
592
593             of = orm.Flag(
594                     node = action.node,
595                     user_id = action.user_id,
596                     flagged_at = action.action_date,
597                     reason = '',
598                     action = action
599                     )
600
601             of.save()
602
603         elif sxv['votetypeid'] == '5':
604             action.action_type = "favorite"
605             action.save()
606
607         elif sxv['votetypeid'] == '6':
608             action.action_type = "close"
609             action.extra = dbsafe_encode(close_reasons[sxv['comment']])
610             action.save()
611
612             node.marked = True
613             node.save()
614
615         elif sxv['votetypeid'] == '7':
616             action.action_type = "unknown"
617             action.save()
618
619             node.marked = False
620             node.save()
621
622             remove_post_state("closed", node)
623
624         elif sxv['votetypeid'] == '10':
625             action.action_type = "delete"
626             action.save()
627
628         elif sxv['votetypeid'] == '11':
629             action.action_type = "unknown"
630             action.save()
631
632             remove_post_state("deleted", node)
633
634         else:
635             action.action_type = "unknown"
636             action.save()
637
638         if sxv.get('targetrepchange', None):
639             rep = orm.ActionRepute(
640                     action = action,
641                     date = action.action_date,
642                     user_id = uidmap[sxv['targetuserid']],
643                     value = int(sxv['targetrepchange'])
644                     )
645
646             rep.save()
647
648         if sxv.get('voterrepchange', None):
649             rep = orm.ActionRepute(
650                     action = action,
651                     date = action.action_date,
652                     user_id = uidmap[sxv['userid']],
653                     value = int(sxv['voterrepchange'])
654                     )
655
656             rep.save()
657
658         if action.action_type in ("acceptanswer", "delete", "close"):
659             state = {"acceptanswer": "accepted", "delete": "deleted", "close": "closed"}[action.action_type]
660             add_post_state(state, node, action)
661
662     readTable(dump, "Posts2Votes", callback)
663
664
665 def comment_vote_import(dump, uidmap, comments):
666     user2vote = []
667     comments2score = {}
668
669     def callback(sxv):
670         if sxv['votetypeid'] == "2":
671             comment_id = comments[int(sxv['postcommentid'])]
672             user_id = uidmap[sxv['userid']]
673
674             if not (comment_id, user_id) in user2vote:
675                 user2vote.append((comment_id, user_id))
676
677                 action = orm.Action(
678                         action_type = "voteupcomment",
679                         user_id = user_id,
680                         action_date = readTime(sxv['creationdate']),
681                         node_id = comment_id
682                         )
683                 action.save()
684
685                 ov = orm.Vote(
686                         node_id = comment_id,
687                         user_id = user_id,
688                         voted_at = action.action_date,
689                         value = 1,
690                         action = action
691                         )
692
693                 ov.save()
694
695                 if not comment_id in comments2score:
696                     comments2score[comment_id] = 1
697                 else:
698                     comments2score[comment_id] += 1
699
700     readTable(dump, "Comments2Votes", callback)
701
702     for cid, score in comments2score.items():
703         orm.Node.objects.filter(id=cid).update(score=score)
704
705
706 def badges_import(dump, uidmap, post_list):
707
708     sxbadges = {}
709
710     def sxcallback(b):
711         sxbadges[int(b['id'])] = b
712
713     readTable(dump, "Badges", sxcallback)
714
715     obadges = dict([(b.cls, b) for b in orm.Badge.objects.all()])
716     user_badge_count = {}
717
718     sx_to_osqa = {}
719
720     for id, sxb in sxbadges.items():
721         cls = "".join(sxb['name'].replace('&', 'And').split(' '))
722
723         if cls in obadges:
724             sx_to_osqa[id] = obadges[cls]
725         else:
726             osqab = orm.Badge(
727                     cls = cls,
728                     awarded_count = 0,
729                     type = sxb['class']
730                     )
731             osqab.save()
732             sx_to_osqa[id] = osqab
733
734     osqaawards = []
735
736     def callback(sxa):
737         badge = sx_to_osqa[int(sxa['badgeid'])]
738
739         user_id = uidmap[sxa['userid']]
740         if not user_badge_count.get(user_id, None):
741             user_badge_count[user_id] = 0
742
743         action = orm.Action(
744                 action_type = "award",
745                 user_id = user_id,
746                 action_date = readTime(sxa['date'])
747                 )
748
749         action.save()
750
751         osqaa = orm.Award(
752                 user_id = uidmap[sxa['userid']],
753                 badge = badge,
754                 node_id = post_list[user_badge_count[user_id]],
755                 awarded_at = action.action_date,
756                 action = action
757                 )
758
759         osqaa.save()
760         badge.awarded_count += 1
761         user_badge_count[user_id] += 1
762
763     readTable(dump, "Users2Badges", callback)
764
765     for badge in obadges.values():
766         badge.save()
767
768 def save_setting(k, v):
769     try:
770         kv = orm.KeyValue.objects.get(key=k)
771         kv.value = v
772     except:
773         kv = orm.KeyValue(key = k, value = v)
774
775     kv.save()
776
777
778 def pages_import(dump, currid):
779     currid = IdIncrementer(currid)
780     registry = {}
781
782     def callback(sxp):
783         currid.inc()
784         page = orm.Node(
785                 id = currid.value,
786                 node_type = "page",
787                 title = sxp['name'],
788                 body = b64decode(sxp['value']),
789                 extra = dbsafe_encode({
790                 'path': sxp['url'][1:],
791                 'mimetype': sxp['contenttype'],
792                 'template': (sxp['usemaster'] == "true") and "default" or "none",
793                 'render': "html",
794                 'sidebar': "",
795                 'sidebar_wrap': True,
796                 'sidebar_render': "html",
797                 'comments': False
798                 }),
799                 author_id = 1
800                 )
801
802         create_and_activate_revision(page)
803
804         page.save()
805         registry[sxp['url'][1:]] = page.id
806
807         create_action = orm.Action(
808                 action_type = "newpage",
809                 user_id = page.author_id,
810                 node = page
811                 )
812
813         create_action.save()
814
815         if sxp['active'] == "true" and sxp['contenttype'] == "text/html":
816             pub_action = orm.Action(
817                     action_type = "publish",
818                     user_id = page.author_id,
819                     node = page
820                     )
821
822             pub_action.save()
823             add_post_state("published", page, pub_action)
824
825     readTable(dump, "FlatPages", callback)
826
827     save_setting('STATIC_PAGE_REGISTRY', dbsafe_encode(registry))
828
829 sx2osqa_set_map = {
830 u'theme.html.name': 'APP_TITLE',
831 u'theme.html.footer': 'CUSTOM_FOOTER',
832 u'theme.html.sidebar': 'SIDEBAR_UPPER_TEXT',
833 u'theme.html.sidebar-low': 'SIDEBAR_LOWER_TEXT',
834 u'theme.html.welcome': 'APP_INTRO',
835 u'theme.html.head': 'CUSTOM_HEAD',
836 u'theme.html.header': 'CUSTOM_HEADER',
837 u'theme.css': 'CUSTOM_CSS',
838 }
839
840 html_codes = (
841 ('&amp;', '&'),
842 ('&lt;', '<'),
843 ('&gt;', '>'),
844 ('&quot;', '"'),
845 ('&#39;', "'"),
846 )
847
848 def html_decode(html):
849     html = force_unicode(html)
850
851     for args in html_codes:
852         html = html.replace(*args)
853
854     return html
855
856
857 def static_import(dump):
858     sx_unknown = {}
859
860     def callback(set):
861         if unicode(set['name']) in sx2osqa_set_map:
862             save_setting(sx2osqa_set_map[set['name']], dbsafe_encode(html_decode(set['value'])))
863         else:
864             sx_unknown[set['name']] = html_decode(set['value'])
865
866     readTable(dump, "ThemeTextResources", callback)
867
868     save_setting('SXIMPORT_UNKNOWN_SETS', dbsafe_encode(sx_unknown))
869
870 def disable_triggers():
871     from south.db import db
872     if db.backend_name == "postgres":
873         db.execute_many(PG_DISABLE_TRIGGERS)
874         db.commit_transaction()
875         db.start_transaction()
876
877 def enable_triggers():
878     from south.db import db
879     if db.backend_name == "postgres":
880         db.start_transaction()
881         db.execute_many(PG_ENABLE_TRIGGERS)
882         db.commit_transaction()
883
884 def reset_sequences():
885     from south.db import db
886     if db.backend_name == "postgres":
887         db.start_transaction()
888         db.execute_many(PG_SEQUENCE_RESETS)
889         db.commit_transaction()
890
891 def reindex_fts():
892     from south.db import db
893     if db.backend_name == "postgres":
894         db.start_transaction()
895         db.execute_many("UPDATE forum_noderevision set id = id WHERE TRUE;")
896         db.commit_transaction()
897
898
899 def sximport(dump, options):
900     try:
901         disable_triggers()
902         triggers_disabled = True
903     except:
904         triggers_disabled = False
905
906     uidmap = userimport(dump, options)
907     tagmap = tagsimport(dump, uidmap)
908     gc.collect()
909
910     posts = postimport(dump, uidmap, tagmap)
911     gc.collect()
912
913     posts, comments = comment_import(dump, uidmap, posts)
914     gc.collect()
915
916     post_vote_import(dump, uidmap, posts)
917     gc.collect()
918
919     comment_vote_import(dump, uidmap, comments)
920     gc.collect()
921
922     badges_import(dump, uidmap, posts)
923
924     pages_import(dump, max(posts))
925     static_import(dump)
926     gc.collect()
927
928     from south.db import db
929     db.commit_transaction()
930
931     reset_sequences()
932
933     if triggers_disabled:
934         enable_triggers()
935         reindex_fts()
936
937
938 PG_DISABLE_TRIGGERS = """
939 ALTER table auth_user DISABLE TRIGGER ALL;
940 ALTER table auth_user_groups DISABLE TRIGGER ALL;
941 ALTER table auth_user_user_permissions DISABLE TRIGGER ALL;
942 ALTER table forum_keyvalue DISABLE TRIGGER ALL;
943 ALTER table forum_action DISABLE TRIGGER ALL;
944 ALTER table forum_actionrepute DISABLE TRIGGER ALL;
945 ALTER table forum_subscriptionsettings DISABLE TRIGGER ALL;
946 ALTER table forum_validationhash DISABLE TRIGGER ALL;
947 ALTER table forum_authkeyuserassociation DISABLE TRIGGER ALL;
948 ALTER table forum_tag DISABLE TRIGGER ALL;
949 ALTER table forum_markedtag DISABLE TRIGGER ALL;
950 ALTER table forum_node DISABLE TRIGGER ALL;
951 ALTER table forum_nodestate DISABLE TRIGGER ALL;
952 ALTER table forum_node_tags DISABLE TRIGGER ALL;
953 ALTER table forum_noderevision DISABLE TRIGGER ALL;
954 ALTER table forum_node_tags DISABLE TRIGGER ALL;
955 ALTER table forum_questionsubscription DISABLE TRIGGER ALL;
956 ALTER table forum_vote DISABLE TRIGGER ALL;
957 ALTER table forum_flag DISABLE TRIGGER ALL;
958 ALTER table forum_badge DISABLE TRIGGER ALL;
959 ALTER table forum_award DISABLE TRIGGER ALL;
960 ALTER table forum_openidnonce DISABLE TRIGGER ALL;
961 ALTER table forum_openidassociation DISABLE TRIGGER ALL;
962 """
963
964 PG_ENABLE_TRIGGERS = """
965 ALTER table auth_user ENABLE TRIGGER ALL;
966 ALTER table auth_user_groups ENABLE TRIGGER ALL;
967 ALTER table auth_user_user_permissions ENABLE TRIGGER ALL;
968 ALTER table forum_keyvalue ENABLE TRIGGER ALL;
969 ALTER table forum_action ENABLE TRIGGER ALL;
970 ALTER table forum_actionrepute ENABLE TRIGGER ALL;
971 ALTER table forum_subscriptionsettings ENABLE TRIGGER ALL;
972 ALTER table forum_validationhash ENABLE TRIGGER ALL;
973 ALTER table forum_authkeyuserassociation ENABLE TRIGGER ALL;
974 ALTER table forum_tag ENABLE TRIGGER ALL;
975 ALTER table forum_markedtag ENABLE TRIGGER ALL;
976 ALTER table forum_node ENABLE TRIGGER ALL;
977 ALTER table forum_nodestate ENABLE TRIGGER ALL;
978 ALTER table forum_node_tags ENABLE TRIGGER ALL;
979 ALTER table forum_noderevision ENABLE TRIGGER ALL;
980 ALTER table forum_node_tags ENABLE TRIGGER ALL;
981 ALTER table forum_questionsubscription ENABLE TRIGGER ALL;
982 ALTER table forum_vote ENABLE TRIGGER ALL;
983 ALTER table forum_flag ENABLE TRIGGER ALL;
984 ALTER table forum_badge ENABLE TRIGGER ALL;
985 ALTER table forum_award ENABLE TRIGGER ALL;
986 ALTER table forum_openidnonce ENABLE TRIGGER ALL;
987 ALTER table forum_openidassociation ENABLE TRIGGER ALL;
988 """
989
990 PG_SEQUENCE_RESETS = """
991 SELECT setval('"auth_user_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "auth_user";
992 SELECT setval('"auth_user_groups_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "auth_user_groups";
993 SELECT setval('"auth_user_user_permissions_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "auth_user_user_permissions";
994 SELECT setval('"forum_keyvalue_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_keyvalue";
995 SELECT setval('"forum_action_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_action";
996 SELECT setval('"forum_actionrepute_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_actionrepute";
997 SELECT setval('"forum_subscriptionsettings_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_subscriptionsettings";
998 SELECT setval('"forum_validationhash_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_validationhash";
999 SELECT setval('"forum_authkeyuserassociation_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_authkeyuserassociation";
1000 SELECT setval('"forum_tag_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_tag";
1001 SELECT setval('"forum_markedtag_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_markedtag";
1002 SELECT setval('"forum_node_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_node";
1003 SELECT setval('"forum_nodestate_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_nodestate";
1004 SELECT setval('"forum_node_tags_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_node_tags";
1005 SELECT setval('"forum_noderevision_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_noderevision";
1006 SELECT setval('"forum_node_tags_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_node_tags";
1007 SELECT setval('"forum_questionsubscription_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_questionsubscription";
1008 SELECT setval('"forum_vote_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_vote";
1009 SELECT setval('"forum_flag_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_flag";
1010 SELECT setval('"forum_badge_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_badge";
1011 SELECT setval('"forum_award_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_award";
1012 SELECT setval('"forum_openidnonce_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_openidnonce";
1013 SELECT setval('"forum_openidassociation_id_seq"', coalesce(max("id"), 1) + 2, max("id") IS NOT null) FROM "forum_openidassociation";
1014 """
1015
1016
1017     
1018