1.1 --- a/convert.py Sun Apr 01 23:26:59 2012 +0200
1.2 +++ b/convert.py Sat Apr 21 02:13:37 2012 +0200
1.3 @@ -50,9 +50,14 @@
1.4
1.5 # Handle particular types.
1.6
1.7 - if objecttype == "Page":
1.8 + if objecttype in ("Page", "Comment"):
1.9
1.10 # Handle pages and revisions, adding revisions to the page manifest.
1.11 + # The original version is used as a unifying identifier for all the
1.12 + # different revisions (each of which being defined by a Page
1.13 + # element). Although "original" implies the first identifier used,
1.14 + # it actually appears to be the latest and will have the highest
1.15 + # version number.
1.16
1.17 if content.has_key("originalVersion"):
1.18 pageid = content["originalVersion"]
1.19 @@ -62,16 +67,29 @@
1.20 versionfile = join(versions_dir, identifier)
1.21
1.22 # Note page metadata, not necessarily in the correct order.
1.23 + # For comments, the title will need to be rewritten, since they
1.24 + # should be defined in terms of their owner page.
1.25
1.26 mkdirs(join(pages_dir, pageid))
1.27
1.28 append(join(pages_dir, pageid, "manifest"), "%s|AddRevision|%s|%s|%s|%s\n" % (
1.29 content["version"],
1.30 versionfile,
1.31 - content["title"],
1.32 + content["title"] or content["version"], # comment titles will incorporate the version
1.33 content["lastModifierName"],
1.34 content["versionComment"]))
1.35
1.36 + # Write comments as subpages.
1.37 +
1.38 + if content.has_key("comments"):
1.39 +
1.40 + # Define a page directory for each comment, and write the page
1.41 + # title in a special file for later processing.
1.42 +
1.43 + for _comment, commentid in content["comments"]:
1.44 + mkdirs(join(pages_dir, commentid))
1.45 + append(join(pages_dir, commentid, "pagetitle"), content["title"])
1.46 +
1.47 # Some metadata is not particularly relevant. For example,
1.48 # ancestors, children, parent are navigation-related.
1.49
1.50 @@ -138,19 +156,48 @@
1.51 finally:
1.52 f.close()
1.53
1.54 -def sort_manifest(filename):
1.55 +def read(filename):
1.56 +
1.57 + """
1.58 + Read from the file with the given 'filename', returning a string containing
1.59 + its contents.
1.60 + """
1.61 +
1.62 + f = codecs.open(filename, encoding="utf-8")
1.63 + try:
1.64 + return f.read()
1.65 + finally:
1.66 + f.close()
1.67
1.68 - "Sort the manifest given in 'filename' according to revision."
1.69 +def sort_manifest(filename, pagetitle):
1.70 +
1.71 + """
1.72 + Sort the manifest given in 'filename' according to revision. If a
1.73 + 'pagetitle' file exists, the title column in the manifest will be augmented
1.74 + with the contents of that file.
1.75 + """
1.76 +
1.77 + if exists(pagetitle):
1.78 + title = read(pagetitle)
1.79 + else:
1.80 + title = None
1.81
1.82 f = codecs.open(filename, "r", encoding="utf-8")
1.83 try:
1.84 lines = [x.split("|") for x in f.readlines()]
1.85 lines.sort(cmp=lambda x, y: cmp(int(x[0]), int(y[0])))
1.86 - lines = ["|".join(x[1:]) for x in lines]
1.87 +
1.88 + # Reconstruct the lines, optionally changing the titles.
1.89 +
1.90 + result = []
1.91 + for x in lines:
1.92 + if title is not None:
1.93 + x[3] = "%s/%s" % (title, x[3])
1.94 + result.append("|".join(x[1:]))
1.95 finally:
1.96 f.close()
1.97
1.98 - write(filename, "".join(lines))
1.99 + write(filename, "".join(result))
1.100
1.101 if __name__ == "__main__":
1.102 import sys
1.103 @@ -201,6 +248,7 @@
1.104
1.105 for pageid in listdir(pages_dir):
1.106 manifest = join(pages_dir, pageid, "manifest")
1.107 - sort_manifest(manifest)
1.108 + pagetitle = join(pages_dir, pageid, "pagetitle")
1.109 + sort_manifest(manifest, pagetitle)
1.110
1.111 # vim: tabstop=4 expandtab shiftwidth=4