Skip to content

Instantly share code, notes, and snippets.

@janpipek
Created May 3, 2015 06:17
Show Gist options
  • Save janpipek/4529baa89ee58cf674b4 to your computer and use it in GitHub Desktop.
Save janpipek/4529baa89ee58cf674b4 to your computer and use it in GitHub Desktop.
Export documents from old instiki database
def export_from_instiki(file_name, output_dir, extension="md"):
"""Export data from instiki database to external files."""
import sqlite3
import os
import codecs
db = sqlite3.connect(file_name)
cur = db.execute("SELECT * from pages")
if not os.path.exists(output_dir):
os.makedirs(output_dir)
while True:
row = cur.fetchone()
if not row:
break
pid = row[0]
name = row[5]
cur2 = db.execute("SELECT * from revisions WHERE page_id=%d order by id DESC" % pid)
row2 = cur2.fetchone()
page_id = name.lower().encode('ascii', 'ignore').replace(" ", "_").replace("#", "sharp").replace("+", "plus")
page_file = os.path.join(output_dir, "%s.%s" % (page_id, extension))
content = "# " + name + u"\n\n" + row2[5]
print page_file, name, len(content)
with codecs.open(page_file, "w", encoding="utf-8") as f:
f.write(content)
db.close()
@janpipek
Copy link
Author

janpipek commented May 3, 2015

I had one old instiki database in a version probably not supported now. This script enabled me to parse the database file and export all pages to separate documents.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment