paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | from os import mkdir, makedirs, symlink |
paul@0 | 4 | from os.path import extsep, join, splitext |
paul@0 | 5 | from zipfile import ZipFile |
paul@0 | 6 | from cStringIO import StringIO |
paul@0 | 7 | import codecs |
paul@0 | 8 | import xmlread |
paul@0 | 9 | |
paul@0 | 10 | class ConfluenceHandler: |
paul@0 | 11 | |
paul@0 | 12 | "Handle content from a Confluence Wiki dump." |
paul@0 | 13 | |
paul@0 | 14 | def __init__(self, directory): |
paul@0 | 15 | self.content = {} |
paul@0 | 16 | self.elements = [] |
paul@0 | 17 | self.directory = directory |
paul@0 | 18 | |
paul@0 | 19 | def handle_object(self, name, elements, attributes, all_text, text): |
paul@0 | 20 | |
paul@0 | 21 | "Handle objects according to type." |
paul@0 | 22 | |
paul@0 | 23 | objecttype = attributes[-1]["class"] |
paul@0 | 24 | identifier = text.strip() |
paul@0 | 25 | content = self.content |
paul@0 | 26 | |
paul@0 | 27 | pages_dir = join(self.directory, "pages") |
paul@0 | 28 | versions_dir = join(self.directory, "versions") |
paul@0 | 29 | |
paul@0 | 30 | # Handle particular types. |
paul@0 | 31 | |
paul@0 | 32 | if objecttype == "Page": |
paul@0 | 33 | |
paul@0 | 34 | # Handle pages. |
paul@0 | 35 | |
paul@0 | 36 | if content.has_key("historicalVersions"): |
paul@0 | 37 | versions = content["historicalVersions"] |
paul@0 | 38 | |
paul@0 | 39 | # Make a page directory and links from the versions to separate |
paul@0 | 40 | # files. |
paul@0 | 41 | |
paul@0 | 42 | page_dir = join(pages_dir, identifier) |
paul@0 | 43 | revisions_dir = join(page_dir, "revisions") |
paul@0 | 44 | |
paul@0 | 45 | mkdir(page_dir) |
paul@0 | 46 | mkdir(revisions_dir) |
paul@0 | 47 | |
paul@0 | 48 | # Historical versions are stored separately. |
paul@0 | 49 | |
paul@0 | 50 | for n, (cls, version) in enumerate(versions): |
paul@0 | 51 | if cls == "Page": |
paul@0 | 52 | symlink(join("..", "..", "..", "..", versions_dir, version), join(revisions_dir, str(n+1))) |
paul@0 | 53 | |
paul@0 | 54 | # The page always seems to hold the current version itself. |
paul@0 | 55 | |
paul@0 | 56 | symlink(join("..", "..", "..", "..", versions_dir, identifier), join(revisions_dir, str(len(versions)+1))) |
paul@0 | 57 | |
paul@0 | 58 | # Handle pages and revisions, adding revisions to the page manifest. |
paul@0 | 59 | |
paul@0 | 60 | if content.has_key("originalVersion"): |
paul@0 | 61 | pageid = content["originalVersion"] |
paul@0 | 62 | else: |
paul@0 | 63 | pageid = identifier |
paul@0 | 64 | |
paul@0 | 65 | versionfile = join(versions_dir, identifier) |
paul@0 | 66 | |
paul@0 | 67 | # Note page metadata, not necessarily in the correct order. |
paul@0 | 68 | |
paul@0 | 69 | mkdirs(join(pages_dir, pageid)) |
paul@0 | 70 | |
paul@0 | 71 | append(join(pages_dir, pageid, "import"), "%s|AddRevision|%s|%s|%s|%s\n" % ( |
paul@0 | 72 | content["version"], |
paul@0 | 73 | versionfile, |
paul@0 | 74 | content["title"], |
paul@0 | 75 | content["lastModifierName"], |
paul@0 | 76 | content["versionComment"])) |
paul@0 | 77 | |
paul@0 | 78 | # Some metadata is not particularly relevant. For example, |
paul@0 | 79 | # ancestors, children, parent are navigation-related. |
paul@0 | 80 | |
paul@0 | 81 | # Other metadata could be added to the page content itself. |
paul@0 | 82 | # For example, labelling could be converted to categories. |
paul@0 | 83 | |
paul@0 | 84 | # Handle revisions. |
paul@0 | 85 | |
paul@0 | 86 | elif objecttype == "BodyContent": |
paul@0 | 87 | write(join(versions_dir, content["content"]), content["body"]) |
paul@0 | 88 | |
paul@0 | 89 | self.content = {} |
paul@0 | 90 | |
paul@0 | 91 | def handle_property(self, name, elements, attributes, all_text, text): |
paul@0 | 92 | |
paul@0 | 93 | "Record properties in the current content dictionary." |
paul@0 | 94 | |
paul@0 | 95 | self.content[attributes[-1]["name"]] = text.strip() |
paul@0 | 96 | |
paul@0 | 97 | def handle_id(self, name, elements, attributes, all_text, text): |
paul@0 | 98 | |
paul@0 | 99 | "Promote identifiers to the parent element's text." |
paul@0 | 100 | |
paul@0 | 101 | all_text[-2].append(text) |
paul@0 | 102 | |
paul@0 | 103 | def handle_collection(self, name, elements, attributes, all_text, text): |
paul@0 | 104 | |
paul@0 | 105 | "Record collections in the current content dictionary." |
paul@0 | 106 | |
paul@0 | 107 | self.content[attributes[-1]["name"]] = self.elements |
paul@0 | 108 | self.elements = [] |
paul@0 | 109 | |
paul@0 | 110 | def handle_element(self, name, elements, attributes, all_text, text): |
paul@0 | 111 | |
paul@0 | 112 | "Add elements to the current collection." |
paul@0 | 113 | |
paul@0 | 114 | self.elements.append((attributes[-1]["class"], text.strip())) |
paul@0 | 115 | |
paul@0 | 116 | def mkdirs(name): |
paul@0 | 117 | try: |
paul@0 | 118 | makedirs(name) |
paul@0 | 119 | except OSError: |
paul@0 | 120 | pass |
paul@0 | 121 | |
paul@0 | 122 | def append(filename, s): |
paul@0 | 123 | write(filename, s, True) |
paul@0 | 124 | |
paul@0 | 125 | def write(filename, s, append=False): |
paul@0 | 126 | f = codecs.open(filename, append and "ab" or "wb", encoding="utf-8") |
paul@0 | 127 | try: |
paul@0 | 128 | f.write(s) |
paul@0 | 129 | finally: |
paul@0 | 130 | f.close() |
paul@0 | 131 | |
paul@0 | 132 | if __name__ == "__main__": |
paul@0 | 133 | import sys |
paul@0 | 134 | |
paul@0 | 135 | filename = sys.argv[1] |
paul@0 | 136 | is_zipfile = splitext(filename)[-1] == extsep + "zip" |
paul@0 | 137 | |
paul@0 | 138 | directory = sys.argv[2] |
paul@0 | 139 | |
paul@0 | 140 | if exists(directory): |
paul@0 | 141 | print >>sys.stderr, "Directory exists. Please choose another or remove its contents." |
paul@0 | 142 | sys.exit(1) |
paul@0 | 143 | |
paul@0 | 144 | mkdir(directory) |
paul@0 | 145 | mkdirs(join(directory, "pages")) |
paul@0 | 146 | mkdirs(join(directory, "versions")) |
paul@0 | 147 | |
paul@0 | 148 | p = xmlread.ConfigurableParser() |
paul@0 | 149 | handler = ConfluenceHandler(directory) |
paul@0 | 150 | |
paul@0 | 151 | p["object"] = handler.handle_object |
paul@0 | 152 | p["property"] = handler.handle_property |
paul@0 | 153 | p["id"] = handler.handle_id |
paul@0 | 154 | p["collection"] = handler.handle_collection |
paul@0 | 155 | p["element"] = handler.handle_element |
paul@0 | 156 | |
paul@0 | 157 | f = open(filename) |
paul@0 | 158 | |
paul@0 | 159 | if is_zipfile: |
paul@0 | 160 | zf = ZipFile(f) |
paul@0 | 161 | ff = StringIO(zf.read("entities.xml")) |
paul@0 | 162 | else: |
paul@0 | 163 | ff = f |
paul@0 | 164 | |
paul@0 | 165 | try: |
paul@0 | 166 | p.parse(ff) |
paul@0 | 167 | finally: |
paul@0 | 168 | f.close() |
paul@0 | 169 | |
paul@0 | 170 | # vim: tabstop=4 expandtab shiftwidth=4 |