paul@113 | 1 | #!/usr/bin/env python |
paul@113 | 2 | |
paul@113 | 3 | """ |
paul@113 | 4 | Handle Confluence wiki requests performing searches. |
paul@113 | 5 | """ |
paul@113 | 6 | |
paul@113 | 7 | from urllib import urlencode |
paul@113 | 8 | import cgi, os, sys |
paul@113 | 9 | |
paul@113 | 10 | # An empty string means that the wiki is anchored at the site root. |
paul@113 | 11 | |
paul@113 | 12 | URL_PREFIX = "" |
paul@113 | 13 | |
paul@140 | 14 | def redirect(page=None, args=None): |
paul@140 | 15 | location = "%s/%s%s" % (URL_PREFIX, page or "", args and "?" + urlencode(args) or "") |
paul@113 | 16 | |
paul@113 | 17 | print """\ |
paul@113 | 18 | Status: 302 Redirect to page |
paul@113 | 19 | Location: %s |
paul@113 | 20 | Content-Type: text/html |
paul@113 | 21 | |
paul@113 | 22 | <html> |
paul@113 | 23 | <head><title>Redirecting to Page</title></head> |
paul@113 | 24 | <body> |
paul@113 | 25 | <h1>Redirecting to Page</h1> |
paul@113 | 26 | <p>If you see this message, try following <a href="%s">this link</a>.</p> |
paul@113 | 27 | </body> |
paul@113 | 28 | </html> |
paul@113 | 29 | """ % (location, cgi.escape(location, True)) |
paul@113 | 30 | sys.exit(0) |
paul@113 | 31 | |
paul@113 | 32 | def main(): |
paul@113 | 33 | args = cgi.parse_qs(os.environ.get("QUERY_STRING", "")) |
paul@113 | 34 | path = os.environ.get("PATH_INFO", "").strip("/") |
paul@113 | 35 | space = (args.get("searchQuery.spaceKey") or args.get("where") or [None])[0] |
paul@113 | 36 | query = (args.get("searchQuery.queryString") or args.get("queryString") or [None])[0] |
paul@140 | 37 | reset = args.get("reset") |
paul@140 | 38 | |
paul@140 | 39 | if reset: |
paul@140 | 40 | redirect(page="FindPage") |
paul@140 | 41 | return |
paul@113 | 42 | |
paul@113 | 43 | search = [] |
paul@113 | 44 | if space: |
paul@113 | 45 | search.append("title:%s/" % space) |
paul@113 | 46 | if query: |
paul@113 | 47 | search.append(query) |
paul@113 | 48 | search = " ".join(search) |
paul@113 | 49 | |
paul@140 | 50 | redirect(args={ |
paul@113 | 51 | "action" : "fullsearch", # Moin action performing searches |
paul@113 | 52 | "context" : "180", # Textual context in characters |
paul@113 | 53 | "value" : search, |
paul@113 | 54 | "fullsearch" : "Text" # Indicates a page text (not title) search |
paul@113 | 55 | }) |
paul@113 | 56 | |
paul@113 | 57 | if __name__ == "__main__": |
paul@113 | 58 | main() |
paul@113 | 59 | |
paul@113 | 60 | # vim: tabstop=4 expandtab shiftwidth=4 |