# HG changeset patch # User Paul Boddie # Date 1374075266 -7200 # Node ID 914efdc23592f35d99912b5e33903ac502bede0b # Parent 7c7e460c54d7381300eb17fa75bb3a542d7c51be Added a script to perform redirects for legacy Confluence URLs. diff -r 7c7e460c54d7 -r 914efdc23592 README.txt --- a/README.txt Wed Jul 17 15:45:49 2013 +0200 +++ b/README.txt Wed Jul 17 17:34:26 2013 +0200 @@ -111,6 +111,9 @@ structure. The third mapping is provided as a convenience, combining the "tiny URL" conversion and the arbitrary mapping to page names. +Translating Requests Using the Mappings +--------------------------------------- + Where Web server facilities such as RewriteMap are available for use, the first and third mapping files can be used directly. See the Apache documentation for details of RewriteMap: @@ -120,7 +123,18 @@ Otherwise, it is more likely that the first file is used by a program that can perform a redirect to the appropriate wiki page, and the "tiny URL" decoding is also done by this program when deployed in a suitable location to receive -such requests. +such requests. To support this, the following resources are provided: + + * scripts/redirect.py + * config/mailmanwiki-redirect + +The latter configuration file should be combined with the Web server +configuration file such that the appropriate aliases are able to capture +requests and invoke the redirect.py script before the main wiki aliases are +consulted. The script itself should be placed in a suitable filesystem +location, and the mapping-id-to-page.txt file should be placed alongside it, +or it should be placed in a different location and the MAPPING_ID_TO_PAGE +variable changed in the script to refer to this different location. Output Structure ---------------- diff -r 7c7e460c54d7 -r 914efdc23592 config/mailmanwiki-redirect --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/config/mailmanwiki-redirect Wed Jul 17 17:34:26 2013 +0200 @@ -0,0 +1,2 @@ +ScriptAlias /x "/var/www/mmwiki-scripts/redirect.py" +ScriptAlias /pages/viewpage.action "/var/www/mmwiki-scripts/redirect.py" diff -r 7c7e460c54d7 -r 914efdc23592 scripts/redirect.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/redirect.py Wed Jul 17 17:34:26 2013 +0200 @@ -0,0 +1,94 @@ +#!/usr/bin/env python + +""" +Handle Confluence wiki requests employing special identifiers that should map to +wiki pages. +""" + +import cgi, os, sys + +# Location of the mapping. + +MAPPING_ID_TO_PAGE = "mapping-id-to-page.txt" + +# An empty string means that the wiki is anchored at the site root. + +URL_PREFIX = "" + +# See the scripts/tiny.py program for similar code in a stand-alone program. + +from base64 import b64decode +from struct import unpack + +def identifier(s): + if len(s) > 6: + return None + bytes = b64decode(s.replace("-", "/").replace("_", "+") + "=" * (6 - len(s))) + return str(unpack(" +Bad Page Identifier + +

Bad Page Identifier

+

The identifier given in the URL%s does not seem to refer to a page in this wiki.

+ + +""" % (pageid and " (%s)" % pageid or "") + sys.exit(0) + +def redirect(pagename): + location = "%s/%s" % (URL_PREFIX, pagename) + + print """\ +Status: 302 Redirect to page +Location: %s +Content-Type: text/html + + +Redirecting to Page + +

Redirecting to Page

+

If you see this message, try following this link.

+ + +""" % (location, cgi.escape(location, True)) + sys.exit(0) + +def find(f, pageid): + for line in f.xreadlines(): + columns = line.strip().split("\t") + if columns[0] == pageid: + redirect(columns[1]) + +def main(): + args = cgi.parse_qs(os.environ.get("QUERY_STRING", "")) + path = os.environ.get("PATH_INFO", "").strip("/") + + pageid = args.get("pageId", [None])[0] or identifier(path) + if pageid is None: + fail(pageid) + + f = open(MAPPING_ID_TO_PAGE) + try: + # With an identifier, find the corresponding page name. + + find(f, pageid) + + # Didn't find the page. + + fail(pageid) + + finally: + f.close() + +if __name__ == "__main__": + main() + +# vim: tabstop=4 expandtab shiftwidth=4