1 #!/usr/bin/env python 2 3 import sys, os 4 5 wiki_dir, archive_dir = sys.argv[1:3] 6 dry_run = ("-n" in sys.argv) 7 8 pages = os.path.join(wiki_dir, "wiki", "data", "pages") 9 users = os.path.join(wiki_dir, "wiki", "data", "user") 10 11 # Make the archive's page directory. 12 13 archive_pages = os.path.join(archive_dir, "wiki", "data", "pages") 14 if not dry_run and not os.path.exists(archive_pages): 15 os.makedirs(archive_pages) 16 17 archive_users = os.path.join(archive_dir, "wiki", "data", "user") 18 if not dry_run and not os.path.exists(archive_users): 19 os.makedirs(archive_users) 20 21 # Copy user details. 22 23 print "Copying users..." 24 if not dry_run: 25 cmd = "cp %s %s" % (os.path.join(users, "*"), archive_users) 26 print cmd 27 os.system(cmd) 28 29 # Find active Wiki pages. 30 31 for page in os.listdir(pages): 32 page_dir = os.path.join(pages, page) 33 34 if os.path.isdir(page_dir) and "current" in os.listdir(page_dir): 35 36 f = open(os.path.join(page_dir, "current")) 37 try: 38 current = f.read().strip() 39 finally: 40 f.close() 41 42 archive_page_dir = os.path.join(archive_pages, page) 43 archive_page_revisions = os.path.join(archive_page_dir, "revisions") 44 page_revisions = os.path.join(page_dir, "revisions") 45 46 if current not in os.listdir(page_revisions): 47 continue 48 49 print "Archiving", page 50 print "Revision", current 51 52 if not dry_run: 53 if not os.path.exists(archive_page_revisions): 54 os.makedirs(archive_page_revisions) 55 56 f = open(os.path.join(archive_page_dir, "current"), "w") 57 try: 58 f.write("00000001") 59 finally: 60 f.close() 61 62 cmd = "cp %s %s" % (os.path.join(page_revisions, current), os.path.join(archive_page_revisions, "00000001")) 63 print cmd 64 if not dry_run: 65 os.system(cmd) 66 67 attachments_dir = os.path.join(page_dir, "attachments") 68 if os.path.exists(attachments_dir): 69 print "Archiving attachments..." 70 71 archive_attachments_dir = os.path.join(archive_page_dir, "attachments") 72 if not dry_run and not os.path.exists(archive_attachments_dir): 73 os.mkdir(archive_attachments_dir) 74 75 cmd = "cp %s %s" % (os.path.join(attachments_dir, "*"), archive_attachments_dir) 76 print cmd 77 if not dry_run: 78 os.system(cmd) 79 80 print 81 82 print "Now run the following, if adding to a live Wiki:" 83 print "chown -R www-data: %s/www %s/wiki" % (archive_dir, archive_dir) 84 85 # vim: tabstop=4 expandtab shiftwidth=4