|
| 1 | +# -*- coding:utf-8 -*- |
| 2 | +import MySQLdb |
| 3 | +import config |
| 4 | +import uuid |
| 5 | +import cStringIO |
| 6 | + |
| 7 | +db = MySQLdb.connect(host=config.DB_SERVER, passwd=config.DB_PASSWORD, db=config.DB_NAME, user=config.DB_USERNAME) |
| 8 | + |
| 9 | +c = db.cursor() |
| 10 | + |
| 11 | +c.execute("""insert into users(username, password, gender, avatar, intro, sig1, sig2, sig3, hobby, qq, mail, registration_date, last_login_time, num_post, num_reply, num_water, num_sign, current_board, user_agent) select username, password, sex = "男", icon, intro, sig1, sig2, sig3, hobby, qq, mail, regdate, lastdate, post, reply, water, sign, nowboard, logininfo from capubbs.userinfo""") |
| 12 | + |
| 13 | +c.execute("""insert into boards(bid, name, invisible) select bid, bbstitle, hide from capubbs.boardinfo""") |
| 14 | + |
| 15 | +c.execute("""alter table capubbs.threads add column new_tid integer""") |
| 16 | +c.execute("""update capubbs.threads, (select bid, tid, @cur_tid := if(@cur_tid is null, 1, @cur_tid + 1) as new_tid from capubbs.threads order by bid, tid) as ord set threads.new_tid = ord.new_tid where threads.bid = ord.bid and threads.tid = ord.tid""") |
| 17 | +c.execute("""insert into Threads(tid, author_uid, bid, title, replyer_uid, num_click, num_reply, good, sticky, created_at, replied_at) select new_tid, author.uid, bid, title, replier.uid, click, threads.reply, threads.extr, top, postdate, from_unixtime(timestamp) from (capubbs.threads as threads join users as author on threads.author = author.username) left join users as replier on replyer = replier.username""") |
| 18 | + |
| 19 | +c.execute("""alter table capubbs.posts add column new_pid integer""") |
| 20 | +c.execute("""update capubbs.posts, (select bid, tid, pid, @cur_pid := if(@cur_pid is null, 1, @cur_pid + 1) as new_pid from capubbs.posts order by bid, tid, pid) as ord set posts.new_pid = ord.new_pid where posts.bid = ord.bid and posts.tid = ord.tid and posts.pid = ord.pid""") |
| 21 | +c.execute("""insert into posts(pid, uid, bid, tid, title, content, created_at, updated_at, signature, ip, parse_type) select new_pid, author.uid, posts.bid, threads.new_tid, posts.title, text, from_unixtime(replytime), from_unixtime(updatetime), case posts.sig when 1 then author.sig1 when 2 then author.sig2 when 3 then author.sig3 end, ip, case ishtml when 'YES' then 'html' else 'plain' end from capubbs.posts join users as author on posts.author = author.username join capubbs.threads on threads.bid = posts.bid and threads.tid = posts.tid""") |
| 22 | + |
| 23 | +c.execute("""insert into comments(cid, pid, uid, content, time, deleted) select lzl.id, posts.new_pid, users.uid, lzl.text, from_unixtime(time), !lzl.visible from capubbs.lzl join capubbs.posts on lzl.fid = posts.fid join users on lzl.author = users.username""") |
| 24 | + |
| 25 | +c.execute("""insert into messages(mid, sender_uid, receiver_uid, content, time, is_read, sender_deleted, receiver_deleted) select id, sender.uid, receiver.uid, text, from_unixtime(time), hasread, 0, 0 from capubbs.messages join users as sender on messages.sender = sender.username join users as receiver on messages.receiver = receiver.username where sender != 'system'""") |
| 26 | + |
| 27 | +#c.execute("""insert into notifications(nid, uid, time, type, pid, is_read) select id, receiver.uid, from_unixtime(time), case messages.text when 'reply' then 1 when 'at' then 2 when 'replylzl' then 3 when 'replylzlreply' then 4 when 'quote' then 5 end, new_pid, hasread from capubbs.messages join users as receiver on messages.receiver = receiver.username join capubbs.posts on messages.rbid = posts.bid and messages.rtid = posts.tid where sender = 'system'""") |
| 28 | + |
| 29 | +c.close() |
| 30 | + |
| 31 | + |
| 32 | +c2 = db.cursor() |
| 33 | +c3 = db.cursor() |
| 34 | + |
| 35 | +def parse_args(s, pos, begin): |
| 36 | + stat = 'k' |
| 37 | + d = {} |
| 38 | + k = '' |
| 39 | + v = '' |
| 40 | + while True: |
| 41 | + if pos >= len(s): |
| 42 | + d[k] = v |
| 43 | + break |
| 44 | + nxt = s[pos] |
| 45 | + pos += 1 |
| 46 | + if stat == 'k': |
| 47 | + if nxt.isalnum() or nxt == '_': |
| 48 | + k += nxt |
| 49 | + elif nxt == '=': |
| 50 | + stat = 'v' |
| 51 | + else: |
| 52 | + break |
| 53 | + raise KeyError |
| 54 | + elif stat == 'v': |
| 55 | + if nxt.isalnum() or nxt == '_': |
| 56 | + v += nxt |
| 57 | + elif nxt == '&': |
| 58 | + if s[pos:pos + 4] == 'amp;': |
| 59 | + pos += 4 |
| 60 | + d[k] = v |
| 61 | + stat = 'k' |
| 62 | + k = '' |
| 63 | + v = '' |
| 64 | + else: |
| 65 | + d[k] = v |
| 66 | + break |
| 67 | + return (begin, pos, d) |
| 68 | + |
| 69 | +def parse_url(s, pattern): |
| 70 | + res = [] |
| 71 | + pos = s.find(pattern) |
| 72 | + if pos == -1: |
| 73 | + return None |
| 74 | + while pos != -1: |
| 75 | + res.append(parse_args(s, pos + len(pattern), pos)) |
| 76 | + pos = s.find(pattern, pos + 1) |
| 77 | + return res |
| 78 | + |
| 79 | +tbl_name = "tbl_" + uuid.uuid4().hex |
| 80 | +c3.execute("""create table %s (bid integer, tid integer, n integer primary key auto_increment)""" % tbl_name) |
| 81 | + |
| 82 | +def tihuan(pattern, proc): |
| 83 | + c2.execute("""select pid, content from posts where locate('%s', content) > 0 order by pid desc limit 2000""" % pattern) |
| 84 | + res = c2.fetchall() |
| 85 | + actions = [] |
| 86 | + lookups = [] |
| 87 | + for x in res: |
| 88 | + ca = (x, parse_url(x[1], pattern)) |
| 89 | + if ca[1] == None: |
| 90 | + actions.append(None) |
| 91 | + else: |
| 92 | + ca1 = [] |
| 93 | + for y in ca[1]: |
| 94 | + p = proc(y[2]) |
| 95 | + if p: |
| 96 | + lookups.append(p) |
| 97 | + ca1.append(y) |
| 98 | + actions.append((x, ca1)) |
| 99 | + |
| 100 | + c3.executemany("""insert into """ + tbl_name + """(bid, tid) values (%s, %s)""", lookups) |
| 101 | + c3.execute("""select * from """ + tbl_name) |
| 102 | + c3.execute("""select capubbs.threads.new_tid, capubbs.threads.tid, capubbs.threads.bid, n from %s left join capubbs.threads on %s.bid = capubbs.threads.bid and %s.tid = capubbs.threads.tid order by n""" % (tbl_name, tbl_name, tbl_name)) |
| 103 | + res = c3.fetchall() |
| 104 | + updates = [] |
| 105 | + rc = 0 |
| 106 | + for x in actions: |
| 107 | + last = 0 |
| 108 | + o = cStringIO.StringIO() |
| 109 | + s = x[0][1] |
| 110 | + for a in x[1]: |
| 111 | + o.write(s[last:a[0]]) |
| 112 | + o.write('/thread/') |
| 113 | + o.write(str(res[rc][0])) |
| 114 | + o.write('/') |
| 115 | + if 'p' in x[1][0][2]: |
| 116 | + o.write('page/') |
| 117 | + o.write(x[1][0][2]['p']) |
| 118 | + o.write('/') |
| 119 | + last = a[1] - 1 |
| 120 | + o.write(s[last:]) |
| 121 | + updates.append((o.getvalue(), x[0][0])) |
| 122 | + o.close() |
| 123 | + |
| 124 | + c3.executemany("""update posts set content = %s where pid = %s""", updates) |
| 125 | + |
| 126 | +def decode26(s): |
| 127 | + return reduce(lambda tot, x: tot * 26 + ord(x) - ord('a'), s, 0) + 1 |
| 128 | + |
| 129 | +tihuan("""http://www.chexie.net/bbs/content/?""", lambda x: (x['bid'], x['tid']) if ('bid' in x) and ('tid' in x) else None) |
| 130 | +tihuan("""http://chexie.net/bbs/content/?""", lambda x: (x['bid'], x['tid']) if ('bid' in x) and ('tid' in x) else None) |
| 131 | +tihuan("""http://www.chexie.net/cgi-bin/bbs.pl?""", lambda x: (x['b'], decode26(x['see'])) if ('b' in x) and (x['b'] != '') and ('see' in x) else None) |
| 132 | +tihuan("""http://chexie.net/cgi-bin/bbs.pl?""", lambda x: (x['b'], decode26(x['see'])) if ('b' in x) and (x['b'] != '') and ('see' in x) else None) |
| 133 | +c3.execute("""drop table %s""" % tbl_name) |
0 commit comments