#!/usr/bin/env python2 # -*- Encoding: utf-8 -*- ### # Copyright (c) 2018 Krytarik Raido # # This program is free software; you can redistribute it and/or modify # it under the terms of version 2 of the GNU General Public License as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # ### import sqlite, sqlite3 class Factoid: def __init__(self, name, value, author, added, popularity, editor=None, edited=None): try: self.name = name.decode('ascii') except UnicodeDecodeError: try: self.name = name.decode('utf-8') except UnicodeDecodeError: try: self.name = name.decode('cp1252') except UnicodeDecodeError as e: print '%s: %s' % (e, name) return try: self.value = value.decode('ascii') except UnicodeDecodeError: try: self.value = value.decode('utf-8') except UnicodeDecodeError: try: self.value = value.decode('cp1252') except UnicodeDecodeError as e: print '%s: %s' % (e, value) return self.author = author; self.added = added self.editor = editor; self.edited = edited self.popularity = popularity class Log: def __init__(self, author, added): self.author = author; self.added = added # Get old data con2 = sqlite.connect('ubuntu.db') cur2 = con2.cursor() cur2.execute("SELECT name, value, author, added, popularity FROM facts ORDER BY id") factoids = [Factoid(*x) for x in cur2.fetchall()] # Create new database con3 = sqlite3.connect('ubuntu-new.db') cur3 = con3.cursor() cur3.execute("""CREATE TABLE facts ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, value TEXT NOT NULL, author TEXT NOT NULL, added TEXT NOT NULL, editor TEXT, edited TEXT, popularity INTEGER NOT NULL DEFAULT 0 )""") cur3.execute("""CREATE TABLE log ( id INTEGER PRIMARY KEY, type TEXT NOT NULL, name TEXT NOT NULL, value TEXT NOT NULL, author TEXT NOT NULL, added TEXT NOT NULL )""") cur3.execute("""CREATE TABLE requests ( id INTEGER PRIMARY KEY, type TEXT NOT NULL, name TEXT NOT NULL, value TEXT NOT NULL, requester TEXT NOT NULL, requested TEXT NOT NULL )""") # Write new data for fact in factoids: if not hasattr(fact, 'author'): continue # Get old last edit data cur2.execute("SELECT author, added FROM log WHERE name = '%s' ORDER BY id DESC LIMIT 1" % fact.name.encode('utf-8').replace("'","''")) edit = [Log(*x) for x in cur2.fetchall()] if edit: fact.editor, fact.edited = edit[0].author, edit[0].added cur3.execute("INSERT INTO facts (name, value, author, added, editor, edited, popularity) VALUES (?, ?, ?, ?, ?, ?, ?)", (fact.name, fact.value, fact.author, fact.added, fact.editor, fact.edited, fact.popularity)) con3.commit() con3.close() con2.close()