paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@8 | 3 | """ |
paul@8 | 4 | Confluence XML dump conversion to a MoinMoin-compatible representation. |
paul@8 | 5 | |
paul@8 | 6 | Copyright (C) 2012 Paul Boddie <paul@boddie.org.uk> |
paul@8 | 7 | |
paul@8 | 8 | This software is free software; you can redistribute it and/or |
paul@8 | 9 | modify it under the terms of the GNU General Public License as |
paul@8 | 10 | published by the Free Software Foundation; either version 2 of |
paul@8 | 11 | the License, or (at your option) any later version. |
paul@8 | 12 | |
paul@8 | 13 | This software is distributed in the hope that it will be useful, |
paul@8 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@8 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@8 | 16 | GNU General Public License for more details. |
paul@8 | 17 | |
paul@8 | 18 | You should have received a copy of the GNU General Public |
paul@8 | 19 | License along with this library; see the file LICENCE.txt |
paul@8 | 20 | If not, write to the Free Software Foundation, Inc., |
paul@8 | 21 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paul@8 | 22 | """ |
paul@8 | 23 | |
paul@3 | 24 | from os import listdir, mkdir, makedirs |
paul@1 | 25 | from os.path import exists, extsep, join, splitext |
paul@0 | 26 | from zipfile import ZipFile |
paul@0 | 27 | from cStringIO import StringIO |
paul@0 | 28 | import codecs |
paul@0 | 29 | import xmlread |
paul@0 | 30 | |
paul@0 | 31 | class ConfluenceHandler: |
paul@0 | 32 | |
paul@0 | 33 | "Handle content from a Confluence Wiki dump." |
paul@0 | 34 | |
paul@0 | 35 | def __init__(self, directory): |
paul@0 | 36 | self.content = {} |
paul@0 | 37 | self.elements = [] |
paul@0 | 38 | self.directory = directory |
paul@0 | 39 | |
paul@0 | 40 | def handle_object(self, name, elements, attributes, all_text, text): |
paul@0 | 41 | |
paul@0 | 42 | "Handle objects according to type." |
paul@0 | 43 | |
paul@0 | 44 | objecttype = attributes[-1]["class"] |
paul@0 | 45 | identifier = text.strip() |
paul@0 | 46 | content = self.content |
paul@0 | 47 | |
paul@0 | 48 | pages_dir = join(self.directory, "pages") |
paul@0 | 49 | versions_dir = join(self.directory, "versions") |
paul@0 | 50 | |
paul@0 | 51 | # Handle particular types. |
paul@0 | 52 | |
paul@0 | 53 | if objecttype == "Page": |
paul@0 | 54 | |
paul@0 | 55 | # Handle pages and revisions, adding revisions to the page manifest. |
paul@0 | 56 | |
paul@0 | 57 | if content.has_key("originalVersion"): |
paul@0 | 58 | pageid = content["originalVersion"] |
paul@0 | 59 | else: |
paul@0 | 60 | pageid = identifier |
paul@0 | 61 | |
paul@0 | 62 | versionfile = join(versions_dir, identifier) |
paul@0 | 63 | |
paul@0 | 64 | # Note page metadata, not necessarily in the correct order. |
paul@0 | 65 | |
paul@0 | 66 | mkdirs(join(pages_dir, pageid)) |
paul@0 | 67 | |
paul@3 | 68 | append(join(pages_dir, pageid, "manifest"), "%s|AddRevision|%s|%s|%s|%s\n" % ( |
paul@0 | 69 | content["version"], |
paul@0 | 70 | versionfile, |
paul@0 | 71 | content["title"], |
paul@0 | 72 | content["lastModifierName"], |
paul@0 | 73 | content["versionComment"])) |
paul@0 | 74 | |
paul@0 | 75 | # Some metadata is not particularly relevant. For example, |
paul@0 | 76 | # ancestors, children, parent are navigation-related. |
paul@0 | 77 | |
paul@0 | 78 | # Other metadata could be added to the page content itself. |
paul@0 | 79 | # For example, labelling could be converted to categories. |
paul@0 | 80 | |
paul@0 | 81 | # Handle revisions. |
paul@0 | 82 | |
paul@0 | 83 | elif objecttype == "BodyContent": |
paul@0 | 84 | write(join(versions_dir, content["content"]), content["body"]) |
paul@0 | 85 | |
paul@0 | 86 | self.content = {} |
paul@0 | 87 | |
paul@0 | 88 | def handle_property(self, name, elements, attributes, all_text, text): |
paul@0 | 89 | |
paul@0 | 90 | "Record properties in the current content dictionary." |
paul@0 | 91 | |
paul@0 | 92 | self.content[attributes[-1]["name"]] = text.strip() |
paul@0 | 93 | |
paul@0 | 94 | def handle_id(self, name, elements, attributes, all_text, text): |
paul@0 | 95 | |
paul@0 | 96 | "Promote identifiers to the parent element's text." |
paul@0 | 97 | |
paul@0 | 98 | all_text[-2].append(text) |
paul@0 | 99 | |
paul@0 | 100 | def handle_collection(self, name, elements, attributes, all_text, text): |
paul@0 | 101 | |
paul@0 | 102 | "Record collections in the current content dictionary." |
paul@0 | 103 | |
paul@0 | 104 | self.content[attributes[-1]["name"]] = self.elements |
paul@0 | 105 | self.elements = [] |
paul@0 | 106 | |
paul@0 | 107 | def handle_element(self, name, elements, attributes, all_text, text): |
paul@0 | 108 | |
paul@0 | 109 | "Add elements to the current collection." |
paul@0 | 110 | |
paul@0 | 111 | self.elements.append((attributes[-1]["class"], text.strip())) |
paul@0 | 112 | |
paul@0 | 113 | def mkdirs(name): |
paul@2 | 114 | |
paul@2 | 115 | "Make the directory with the given 'name' at any depth." |
paul@2 | 116 | |
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@2 | 123 | |
paul@2 | 124 | "Append to the file with the given 'filename' the string 's'." |
paul@2 | 125 | |
paul@0 | 126 | write(filename, s, True) |
paul@0 | 127 | |
paul@0 | 128 | def write(filename, s, append=False): |
paul@2 | 129 | |
paul@2 | 130 | """ |
paul@2 | 131 | Write to the file with the given 'filename' the string 's'. If the optional |
paul@2 | 132 | 'append' parameter is set to a true value, 's' will be appended to the file. |
paul@2 | 133 | """ |
paul@2 | 134 | |
paul@2 | 135 | f = codecs.open(filename, append and "a" or "w", encoding="utf-8") |
paul@0 | 136 | try: |
paul@0 | 137 | f.write(s) |
paul@0 | 138 | finally: |
paul@0 | 139 | f.close() |
paul@0 | 140 | |
paul@3 | 141 | def sort_manifest(filename): |
paul@3 | 142 | |
paul@3 | 143 | "Sort the manifest given in 'filename' according to revision." |
paul@3 | 144 | |
paul@5 | 145 | f = codecs.open(filename, "r", encoding="utf-8") |
paul@3 | 146 | try: |
paul@3 | 147 | lines = [x.split("|") for x in f.readlines()] |
paul@3 | 148 | lines.sort(cmp=lambda x, y: cmp(int(x[0]), int(y[0]))) |
paul@3 | 149 | lines = ["|".join(x[1:]) for x in lines] |
paul@3 | 150 | finally: |
paul@3 | 151 | f.close() |
paul@3 | 152 | |
paul@3 | 153 | write(filename, "".join(lines)) |
paul@3 | 154 | |
paul@0 | 155 | if __name__ == "__main__": |
paul@0 | 156 | import sys |
paul@0 | 157 | |
paul@0 | 158 | filename = sys.argv[1] |
paul@0 | 159 | is_zipfile = splitext(filename)[-1] == extsep + "zip" |
paul@0 | 160 | |
paul@0 | 161 | directory = sys.argv[2] |
paul@0 | 162 | |
paul@0 | 163 | if exists(directory): |
paul@0 | 164 | print >>sys.stderr, "Directory exists. Please choose another or remove its contents." |
paul@0 | 165 | sys.exit(1) |
paul@0 | 166 | |
paul@0 | 167 | mkdir(directory) |
paul@0 | 168 | mkdirs(join(directory, "pages")) |
paul@0 | 169 | mkdirs(join(directory, "versions")) |
paul@0 | 170 | |
paul@0 | 171 | p = xmlread.ConfigurableParser() |
paul@0 | 172 | handler = ConfluenceHandler(directory) |
paul@0 | 173 | |
paul@0 | 174 | p["object"] = handler.handle_object |
paul@0 | 175 | p["property"] = handler.handle_property |
paul@0 | 176 | p["id"] = handler.handle_id |
paul@0 | 177 | p["collection"] = handler.handle_collection |
paul@0 | 178 | p["element"] = handler.handle_element |
paul@0 | 179 | |
paul@2 | 180 | # Open the XML dump. |
paul@2 | 181 | |
paul@0 | 182 | f = open(filename) |
paul@0 | 183 | |
paul@0 | 184 | if is_zipfile: |
paul@0 | 185 | zf = ZipFile(f) |
paul@0 | 186 | ff = StringIO(zf.read("entities.xml")) |
paul@0 | 187 | else: |
paul@0 | 188 | ff = f |
paul@0 | 189 | |
paul@2 | 190 | # Parse the data. |
paul@2 | 191 | |
paul@0 | 192 | try: |
paul@0 | 193 | p.parse(ff) |
paul@0 | 194 | finally: |
paul@0 | 195 | f.close() |
paul@0 | 196 | |
paul@2 | 197 | # Tidy up the import manifests, sorting each of them by revision and |
paul@2 | 198 | # finalising them. |
paul@2 | 199 | |
paul@3 | 200 | pages_dir = join(directory, "pages") |
paul@3 | 201 | |
paul@3 | 202 | for pageid in listdir(pages_dir): |
paul@3 | 203 | manifest = join(pages_dir, pageid, "manifest") |
paul@3 | 204 | sort_manifest(manifest) |
paul@3 | 205 | |
paul@0 | 206 | # vim: tabstop=4 expandtab shiftwidth=4 |