1.1 --- a/README.txt Sun Aug 10 18:44:07 2014 +0200
1.2 +++ b/README.txt Sun Aug 10 19:34:36 2014 +0200
1.3 @@ -264,11 +264,12 @@
1.4 information.
1.5
1.6 The redirect.py script, apart from supporting identifier-to-page redirects,
1.7 -also supports PDF page exports since the "doexportpage" action uses
1.8 -identifiers to indicate which page is to be exported. In an environment that
1.9 -uses .htaccess and mod_rewrite, the redirect.py script should also be deployed
1.10 -under separate names (such as export.py and exportpdf.py) so that it can
1.11 -discover whether it should be exporting a page instead of just showing it.
1.12 +also supports attachment downloads and PDF page exports, since both kinds of
1.13 +resource employ identifiers to indicate which page is involved. In an
1.14 +environment that uses .htaccess and mod_rewrite, the redirect.py script should
1.15 +also be deployed under separate names (such as export.py and exportpdf.py) so
1.16 +that it can discover whether it should be exporting a page instead of just
1.17 +showing it.
1.18
1.19 The search.py script redirects search requests in a suitable form to the
1.20 MoinMoin "fullsearch" action.
2.1 --- a/config/mailmanwiki-redirect Sun Aug 10 18:44:07 2014 +0200
2.2 +++ b/config/mailmanwiki-redirect Sun Aug 10 19:34:36 2014 +0200
2.3 @@ -4,3 +4,4 @@
2.4 ScriptAlias /spaces/flyingpdf/pdfpageexport.action "/var/www/mmwiki-scripts/redirect.py"
2.5 ScriptAlias /dosearchsite.action "/var/www/mmwiki-scripts/search.py"
2.6 ScriptAlias /dashboard.action "/var/www/mmwiki-scripts/dashboard.py"
2.7 +ScriptAlias /download/attachment "/var/www/mmwiki-scripts/redirect.py"
3.1 --- a/config/mailmanwiki-redirect-htaccess Sun Aug 10 18:44:07 2014 +0200
3.2 +++ b/config/mailmanwiki-redirect-htaccess Sun Aug 10 19:34:36 2014 +0200
3.3 @@ -21,3 +21,7 @@
3.4 RewriteCond %{REQUEST_FILENAME} !-f
3.5 RewriteCond %{REQUEST_FILENAME} !-d
3.6 RewriteRule ^dashboard.action /dashboard.py [PT,L,QSA]
3.7 +
3.8 +RewriteCond %{REQUEST_FILENAME} !-f
3.9 +RewriteCond %{REQUEST_FILENAME} !-d
3.10 +RewriteRule ^/download/attachment/(.*) /redirect.py/$1 [PT,L,QSA]
4.1 --- a/scripts/redirect.py Sun Aug 10 18:44:07 2014 +0200
4.2 +++ b/scripts/redirect.py Sun Aug 10 19:34:36 2014 +0200
4.3 @@ -16,17 +16,32 @@
4.4 # An empty string means that the wiki is anchored at the site root.
4.5
4.6 URL_PREFIX = ""
4.7 +DOWNLOAD_SCRIPT = "/download.py"
4.8 EXPORT_SCRIPT = "/export.py"
4.9 EXPORT_PDF_SCRIPT = "/exportpdf.py"
4.10
4.11 -# See the scripts/tiny.py program for similar code in a stand-alone program.
4.12 +# Conversion of tiny identifiers to verbose identifiers.
4.13
4.14 from base64 import b64decode
4.15 from struct import unpack
4.16
4.17 def identifier(s):
4.18 +
4.19 + "Extract an identifier from the given string 's'."
4.20 +
4.21 + # Isolate numeric identifiers for things like downloads:
4.22 + # /download/attachment/<pageid>/<filename>?<args>
4.23 +
4.24 + if s.split("/")[0].isdigit():
4.25 + return s.split("/")[0]
4.26 +
4.27 + # Reject strings that are too long for tiny identifiers.
4.28 +
4.29 if len(s) > 6:
4.30 return None
4.31 +
4.32 + # Attempt to unpack tiny identifiers.
4.33 +
4.34 bytes = b64decode(s.replace("-", "/").replace("_", "+") + "=" * (6 - len(s)))
4.35 return str(unpack("<I", bytes + "\x00" * (4 - len(bytes)))[0])
4.36
4.37 @@ -47,8 +62,13 @@
4.38 """ % (pageid and " (%s)" % pageid or "")
4.39 sys.exit(0)
4.40
4.41 -def redirect(pagename, export=False):
4.42 - location = "%s/%s%s" % (URL_PREFIX, quote(pagename), export and "?action=ExportPDF" or "")
4.43 +def redirect(pagename, download=None, export=False):
4.44 + location = "%s/%s%s" % (
4.45 + URL_PREFIX, quote(pagename),
4.46 + download and "?action=AttachFile&do=get&target=%s" % download
4.47 + or export and "?action=ExportPDF"
4.48 + or ""
4.49 + )
4.50
4.51 print """\
4.52 Status: 302 Redirect to page
4.53 @@ -81,6 +101,10 @@
4.54 if pageid is None:
4.55 fail(pageid)
4.56
4.57 + download = (script.endswith(DOWNLOAD_SCRIPT) or
4.58 + script.endswith("/download/attachment")
4.59 + ) and path.rsplit("/", 1)[1] or None
4.60 +
4.61 export = (script.endswith(EXPORT_SCRIPT) or
4.62 script.endswith("/pages/doexportpage.action")
4.63 ) and args.get("type", [""])[0] == "TYPE_PDF" or \
4.64 @@ -101,7 +125,7 @@
4.65
4.66 # Redirect to the page.
4.67
4.68 - redirect(pagename, export)
4.69 + redirect(pagename, download, export)
4.70
4.71 finally:
4.72 f.close()