1 | 2 | shake | #!/usr/bin/python2.4 |
2 | | | # -*- coding: utf8 -*- |
3 | | | |
4 | | | import re |
5 | | | |
6 | | | import os |
7 | | | import sys |
8 | | | import MySQLdb |
9 | | | |
10 | | | import md5 |
11 | | | |
12 | | | USER_ID_OFFSET=100 |
13 | | | USER_GROUP_ID=2 |
14 | | | USER_PERMISSIONS='00000000006xv1ssxs' |
15 | | | |
16 | | | DB_HOST="localhost" |
17 | 3 | shake | DB_NAME="cexp" |
18 | 2 | shake | DB_USER="root" |
19 | | | DB_PASS="" |
20 | | | |
21 | 3 | shake | POSTS_BITFIELD="/8A=" |
22 | | | |
23 | 2 | shake | # escape strings for mysql |
24 | | | def mysql_escape_string(str): |
25 | | | newstr = str.replace('\\', '\\\\') |
26 | | | newstr = newstr.replace('\'', '\\\'') |
27 | | | return newstr |
28 | | | |
29 | | | # generate bbcode_uid |
30 | | | def make_uid(timestamp): |
31 | | | return "a"+(str(timestamp))[0:7] |
32 | | | #return (timestamp, 16, 36)[0, 8] |
33 | | | |
34 | 3 | shake | def get_bbcode_bitfield(post): |
35 | | | global POSTS_BITFIELD |
36 | | | return POSTS_BITFIELD |
37 | | | |
38 | 2 | shake | # count new user id |
39 | | | def tuid(origid): |
40 | | | global USER_ID_OFFSET |
41 | | | return (USER_ID_OFFSET + origid) |
42 | | | |
43 | | | def tpost(orig_post, bbcode_uid): |
44 | 3 | shake | zz = ['quote', 'code', 'color', 'url', 'b', 'i', 'u', 'size', 'list', 'list=', 'img'] |
45 | | | new_post = orig_post |
46 | | | for a in zz: |
47 | | | new_post = new_post.replace('['+a.upper()+']', '['+a+':'+bbcode_uid+']') |
48 | | | new_post = new_post.replace('['+a+']', '['+a+':'+bbcode_uid+']') |
49 | | | new_post = new_post.replace('[/'+a.upper()+']', '[/'+a+':'+bbcode_uid+']') |
50 | | | new_post = new_post.replace('[/'+a+']', '[/'+a+':'+bbcode_uid+']') |
51 | 2 | shake | return new_post |
52 | | | |
53 | | | # counts correct left_id and right_id for all forum items from forum_index |
54 | | | def updateBTree(parent_id): |
55 | | | global forum_index |
56 | | | global current_left_id |
57 | | | |
58 | | | for forum_id in forum_index: |
59 | | | if forum_index[forum_id]['parent_id'] == parent_id: |
60 | | | forum_index[forum_id]['left_id'] = current_left_id |
61 | | | current_left_id += 1 |
62 | | | updateBTree(forum_id) |
63 | | | forum_index[forum_id]['right_id'] = current_left_id |
64 | | | current_left_id += 1 |
65 | | | return |
66 | | | |
67 | | | def generateBTreeUpdates(): |
68 | | | global forum_index |
69 | | | global current_left_id |
70 | | | |
71 | | | current_left_id = 1 |
72 | | | updateBTree(0) |
73 | | | for forum_id in forum_index: |
74 | | | print "UPDATE phpbb_forums SET left_id=%d, right_id=%d WHERE forum_id=%d;" % (forum_index[forum_id]['left_id'], forum_index[forum_id]['right_id'], forum_id) |
75 | | | return |
76 | | | |
77 | | | def generateForumInserts(): |
78 | | | global forum_index |
79 | | | global db |
80 | | | |
81 | | | try: |
82 | | | c = db.cursor() |
83 | | | msg = "SELECT * FROM forum ORDER BY forumid;" |
84 | | | c.execute(msg) |
85 | | | while (1): |
86 | | | row = c.fetchone() |
87 | | | if row == None: |
88 | | | break |
89 | | | forumid = row[0] |
90 | | | styleid = row[1] |
91 | | | title = row[2] |
92 | | | description = row[3] |
93 | | | active = row[4] |
94 | | | displayorder = row[5] |
95 | | | replycount = row[6] |
96 | | | lastpost = row[7] |
97 | | | lastposter = row[8] |
98 | | | threadcount = row[9] |
99 | | | allowposting = row[10] |
100 | | | cancontainthreads = row[11] |
101 | | | daysprune = row[12] |
102 | | | newpostemail = row[13] |
103 | | | newthreademail = row[14] |
104 | | | moderatenew = row[15] |
105 | | | moderateattach = row[16] |
106 | | | allowbbcode = row[17] |
107 | | | allowimages = row[18] |
108 | | | allowhtml = row[19] |
109 | | | allowsmilies = row[20] |
110 | | | allowicons = row[21] |
111 | | | parentid = row[22] |
112 | | | parentlist = row[23] |
113 | | | allowratings = row[24] |
114 | | | countposts = row[25] |
115 | | | styleoverride = row[26] |
116 | | | attachlimit = row[27] |
117 | | | |
118 | | | phpbb_forum_flags = 32 |
119 | | | phpbb_display_on_index = 0 |
120 | | | phpbb_left_id = 0 |
121 | | | phpbb_right_id = 0 |
122 | | | if parentid==-1: |
123 | | | parentid = 0 |
124 | | | phpbb_forum_flags = 48 |
125 | | | |
126 | | | forum_index[forumid] = { 'left_id': phpbb_left_id, 'right_id': phpbb_right_id, 'parent_id': parentid } |
127 | | | |
128 | | | print ("INSERT INTO phpbb_forums SET forum_id=%d, parent_id=%d, left_id=%d, right_id=%d, forum_parents='', forum_name='%s', forum_desc='%s', forum_rules='', forum_type=%d, forum_flags=%d, display_on_index=%d;" % (forumid, parentid, phpbb_left_id, phpbb_right_id, mysql_escape_string(title), mysql_escape_string(description), cancontainthreads, phpbb_forum_flags, phpbb_display_on_index)) |
129 | | | print ("INSERT INTO phpbb_acl_groups VALUES (5, %d, 0, 14, 0);" % (forumid)) |
130 | | | except: |
131 | | | print "Error while accessing data: ", sys.exc_info()[0] |
132 | | | raise |
133 | | | |
134 | | | def generateTopicInserts(): |
135 | | | global forum_index |
136 | | | global db |
137 | | | |
138 | | | try: |
139 | | | c = db.cursor() |
140 | | | msg = "SELECT * FROM thread ORDER BY threadid;" |
141 | | | c.execute(msg) |
142 | | | while (1): |
143 | | | row = c.fetchone() |
144 | | | if row == None: |
145 | | | break |
146 | | | |
147 | | | threadid = row[0] |
148 | | | title = row[1] |
149 | | | lastpost = row[2] |
150 | | | forumid = row[3] |
151 | | | pollid = row[4] |
152 | | | open = row[5] |
153 | | | replycount = row[6] |
154 | | | postusername = row[7] |
155 | | | postuserid = row[8] |
156 | | | lastposter = row[9] |
157 | | | dateline = row[10] |
158 | | | views = row[11] |
159 | | | iconid = row[12] |
160 | | | notes = row[13] |
161 | | | visible = row[14] |
162 | | | sticky = row[15] |
163 | | | votenum = row[16] |
164 | | | votetotal = row[17] |
165 | | | attach = row[18] |
166 | | | karma = row[19] |
167 | | | firstpostid = row[20] |
168 | | | |
169 | | | print ("INSERT INTO phpbb_topics SET topic_id=%d, forum_id=%d, topic_title='%s', topic_poster=%d, topic_time=%d, topic_first_post_id=%d, topic_first_poster_name='%s', topic_last_poster_name='%s', topic_last_post_time=%d;" % (threadid, forumid, mysql_escape_string(title), tuid(postuserid), dateline, firstpostid, mysql_escape_string(postusername), mysql_escape_string(lastposter), lastpost)) |
170 | | | print ("UPDATE phpbb_forums SET forum_topics = forum_topics+1, forum_topics_real = forum_topics_real+1 WHERE forum_id=%d;" % (forumid)) |
171 | | | except: |
172 | | | print "Error while accessing data: ", sys.exc_info()[0] |
173 | | | raise |
174 | | | |
175 | | | def generatePostInserts(): |
176 | | | global forum_index |
177 | | | global db |
178 | | | |
179 | | | |
180 | | | try: |
181 | | | c = db.cursor() |
182 | | | msg = "SELECT * FROM post ORDER BY postid;" |
183 | | | c.execute(msg) |
184 | | | while (1): |
185 | | | row = c.fetchone() |
186 | | | if row == None: |
187 | | | break |
188 | | | |
189 | | | postid = row[0] |
190 | | | threadid = row[1] |
191 | | | username = row[2] |
192 | | | userid = row[3] |
193 | | | title = row[4] |
194 | | | dateline = row[5] |
195 | | | attachmentid = row[6] |
196 | | | pagetext = row[7] |
197 | | | allowsmilie = row[8] |
198 | | | showsignature = row[9] |
199 | | | ipaddress = row[10] |
200 | | | iconid = row[11] |
201 | | | visible = row[12] |
202 | | | edituserid = row[13] |
203 | | | editdate = row[14] |
204 | | | modtype = row[15] |
205 | | | moduser = row[16] |
206 | | | |
207 | | | phpbb_bbcode_uid = make_uid(dateline) |
208 | | | phpbb_text = tpost(pagetext, phpbb_bbcode_uid) |
209 | | | |
210 | 3 | shake | print ("INSERT INTO phpbb_posts SET post_id=%d, topic_id=%d, forum_id=(SELECT forum_id FROM phpbb_topics WHERE topic_id=%d), poster_id=%d, post_time=%d, post_username='%s', post_subject='%s', post_text='%s', post_checksum='%s', bbcode_bitfield='%s', bbcode_uid='%s', post_edit_time=%d;" % (postid, threadid, threadid, tuid(userid), dateline, mysql_escape_string(username), mysql_escape_string(title), mysql_escape_string(phpbb_text), mysql_escape_string(md5.new(phpbb_text).hexdigest()), get_bbcode_bitfield(phpbb_text), phpbb_bbcode_uid, editdate)) |
211 | 2 | shake | print ("UPDATE phpbb_topics SET topic_replies = topic_replies+1, topic_replies_real = topic_replies_real+1 WHERE topic_id=%d;" % (threadid)) |
212 | | | print ("UPDATE phpbb_forums SET forum_posts = forum_posts+1, forum_last_post_id=%d, forum_last_post_subject='%s', forum_last_post_time=%d, forum_last_poster_name='%s' WHERE forum_id=(SELECT forum_id FROM phpbb_topics WHERE topic_id=%d);" % (postid, mysql_escape_string(title), dateline, mysql_escape_string(username), threadid)) |
213 | | | except: |
214 | | | print "Error while accessing data: ", sys.exc_info()[0] |
215 | | | raise |
216 | | | |
217 | | | def generateUserInserts(): |
218 | | | global forum_index |
219 | | | global db |
220 | | | global USER_GROUP_ID |
221 | | | global USER_PERMISSIONS |
222 | | | |
223 | | | try: |
224 | | | c = db.cursor() |
225 | 3 | shake | msg = "SELECT *, (SELECT count(*) FROM user WHERE username=t1.username) AS numusers FROM user AS t1 ORDER BY userid;" |
226 | | | opsusercount = dict() |
227 | 2 | shake | c.execute(msg) |
228 | | | while (1): |
229 | | | row = c.fetchone() |
230 | | | if row == None: |
231 | | | break |
232 | | | |
233 | | | userid = row[0] |
234 | | | usergroupid = row[1] |
235 | | | username = row[2] |
236 | | | password = row[3] |
237 | | | email = row[4] |
238 | | | styleid = row[5] |
239 | | | parentemail = row[6] |
240 | | | coppauser = row[7] |
241 | | | homepage = row[8] |
242 | | | icq = row[9] |
243 | | | aim = row[10] |
244 | | | yahoo = row[11] |
245 | | | signature = row[12] |
246 | | | adminemail = row[13] |
247 | | | showemail = row[14] |
248 | | | invisible = row[15] |
249 | | | usertitle = row[16] |
250 | | | customtitle = row[17] |
251 | | | joindate = row[18] |
252 | | | cookieuser = row[19] |
253 | | | daysprune = row[20] |
254 | | | lastvisit = row[21] |
255 | | | lastactivity = row[22] |
256 | | | lastpost = row[23] |
257 | | | posts = row[24] |
258 | | | timezoneoffset = row[25] |
259 | | | emailnotification = row[26] |
260 | | | ignorelist = row[27] |
261 | | | pmfolders = row[28] |
262 | | | receivepm = row[29] |
263 | | | emailonpm = row[30] |
264 | | | karma = row[31] |
265 | | | cangivekarma = row[32] |
266 | | | showkarma = row[33] |
267 | | | latin1 = row[34] |
268 | | | perms = row[35] |
269 | | | rooms = row[36] |
270 | | | reg_time = row[37] |
271 | | | ip = row[38] |
272 | | | mapperms = row[39] |
273 | | | buddylist = row[40] |
274 | | | avatarid = row[41] |
275 | | | inforum = row[42] |
276 | | | options = row[43] |
277 | | | birthday = row[44] |
278 | | | maxposts = row[45] |
279 | | | startofweek = row[46] |
280 | | | ipaddress = row[47] |
281 | | | referrerid = row[48] |
282 | | | pmpopup = row[49] |
283 | | | nosessionhash = row[50] |
284 | 3 | shake | numusers = row[51] |
285 | 2 | shake | |
286 | 3 | shake | if (numusers > 1): |
287 | | | if (username in opsusercount): |
288 | | | opsusercount[username] += 1 |
289 | | | username=username+"_OPS"+str(opsusercount[username]) |
290 | | | else: |
291 | | | opsusercount[username] = 0 |
292 | | | |
293 | 2 | shake | phpbb_user_type = 2 |
294 | | | print ("INSERT INTO phpbb_users SET user_id=%d, user_type=%d, group_id=%d, user_permissions='%s', username='%s', username_clean='%s';" % (tuid(userid), phpbb_user_type, USER_GROUP_ID, USER_PERMISSIONS, mysql_escape_string(username), mysql_escape_string(username))) |
295 | | | except: |
296 | | | print "Error while accessing data: ", sys.exc_info()[0] |
297 | | | raise |
298 | | | |
299 | | | db = MySQLdb.connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) |
300 | | | #c.execute = "SET NAMES utf-8;" |
301 | | | |
302 | | | current_left_id = 1 |
303 | | | forum_index = dict() |
304 | | | |
305 | | | generateForumInserts() |
306 | | | generateBTreeUpdates() |
307 | | | generateTopicInserts() |
308 | | | generatePostInserts() |
309 | | | generateUserInserts() |
310 | | | |
311 | | | db.commit() |
312 | | | db.close() |
313 | | | |