Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import MySQLdb
import os
import shutil
# KnowledgeTree default place to store documents
ktdocument = '/var/www/ktdms/Documents/'
conn = MySQLdb.connect(user='', passwd='',db='', charset="utf8", use_unicode=True)
cursor = conn.cursor()
# global variables FTW
cursor.execute('''select id, parent_id, name from folders;''')
allfolders = cursor.fetchall()
cursor.execute('''select id, folder_id from documents where status_id=1;''')
alldocuments = cursor.fetchall()
cursor.execute('''select document_id, filename, storage_path from document_content_version v1 where not exists(select * from document_content_version v2 where v1.document_id = v2.document_id and<> and v2.major_version*100+v2.minor_version > v1.major_version*100+v1.minor_version);''')
document_locations = cursor.fetchall()
# create folder tree which matches whatever the database suggests exists
def create_folder_tree(parent_id, path):
directories = [x for x in allfolders if x[1] == parent_id]
for directory in directories:
d = '%s/%s/' % (path, directory[2])
print d
# get all the files that belong in this directory
for document in [x for x in alldocuments if x[1] == directory[0]]:
location = [x for x in document_locations if document[0] == x[0]][0]
print 'copy %s%s %s%s' % (ktdocument, location[2], d, location[1])
shutil.copy2('%s%s' % (ktdocument, location[2]), '%s%s' % (d, location[1]))
print 'ERROR exporting - Usually due to a linked document.'
create_folder_tree(parent_id=directory[0], path='%s/%s' % (path, directory[2]))
create_folder_tree(parent_id=1, path='export')
Copy link

tpokorra commented Sep 21, 2018

original version from

I have updated it to:

  • ignore deleted and archived and published documents, only export live documents
  • always exporting the last version of a document

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