Last active
September 21, 2018 04:38
-
-
Save tpokorra/71526bc60ced2649dba2c528d5cd2259 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 v1.id<>v2.id 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 | |
os.makedirs(d) | |
# get all the files that belong in this directory | |
for document in [x for x in alldocuments if x[1] == directory[0]]: | |
try: | |
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])) | |
except: | |
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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
original version from https://boyter.org/2015/09/exporting-documents-knowledgetree-3-7-0-2/
I have updated it to: