Rework action dispatcher
authorBernie Innocenti <bernie@codewiz.org>
Sat, 25 Apr 2009 06:41:30 +0000 (15:41 +0900)
committerBernie Innocenti <bernie@codewiz.org>
Sat, 25 Apr 2009 06:41:30 +0000 (15:41 +0900)
geekigeeki.py

index 8bd3e0b9be13ec871c6fe3aea0ed84966a2682ff..6170d7394ca6bb7b91d3fdb195d0d2ba6a5b4503 100755 (executable)
@@ -143,7 +143,7 @@ def send_title(name, text="Limbo", msg_text=None, msg_type='error', writable=Fal
 
     if name and writable and privileged_url is not None:
         print(' <link rel="alternate" type="application/x-wiki" title="Edit this page" href="%s" />' \
-            % (privileged_path() + '?edit=' + name))
+            % (privileged_path() + '?a=edit&q=' + name))
 
     if history_url is not None:
         print(' <link rel="alternate" type="application/rss+xml" title="RSS" href="%s" />' \
@@ -153,7 +153,7 @@ def send_title(name, text="Limbo", msg_text=None, msg_type='error', writable=Fal
 
     # Body
     if name and writable and privileged_url is not None:
-        print('<body ondblclick="location.href=\'' + privileged_path() + '?edit=' + name + '\'">')
+        print('<body ondblclick="location.href=\'' + privileged_path() + '?a=edit&q=' + name + '\'">')
     else:
         print('<body>')
 
@@ -177,7 +177,7 @@ def send_title(name, text="Limbo", msg_text=None, msg_type='error', writable=Fal
         print(' | ' + link_tag('?raw=' + name, 'Raw Text', 'navlink'))
         if privileged_url is not None:
             if writable:
-                print(' | ' + link_tag('?edit=' + name, 'Edit', 'navlink', privileged=True))
+                print(' | ' + link_tag('?a=edit&q=' + name, 'Edit', 'navlink', privileged=True))
             else:
                 print(' | ' + link_tag(name, 'Login', 'navlink', privileged=True))
 
@@ -232,8 +232,12 @@ def link_inline(name, descr=None, kvargs={}):
 
 # Search ---------------------------------------------------
 
-def handle_fullsearch(needle):
-    send_title(None, 'Full text search for "%s"' % (needle))
+def print_search_stats(hits, searched):
+    print("<p>%d hits out of %d pages searched.</p>" % (hits, searched))
+
+def handle_fullsearch(query, form):
+    needle = form['q'].value
+    send_title(None, 'Full text search for "' + needle + '"')
 
     needle_re = re.compile(needle, re.IGNORECASE)
     hits = []
@@ -259,9 +263,10 @@ def handle_fullsearch(needle):
 
     print_search_stats(len(hits), len(all_pages))
 
-def handle_titlesearch(needle):
+def handle_titlesearch(query, form):
     # TODO: check needle is legal -- but probably we can just accept any RE
-    send_title(None, "Title search for \"" + needle + '"')
+    needle = form['q'].value
+    send_title(None, 'Title search for "' + needle + '"')
 
     needle_re = re.compile(needle, re.IGNORECASE)
     all_pages = page_list()
@@ -274,22 +279,19 @@ def handle_titlesearch(needle):
 
     print_search_stats(len(hits), len(all_pages))
 
-def print_search_stats(hits, searched):
-    print("<p>%d hits out of %d pages searched.</p>" % (hits, searched))
-
-def handle_raw(pagename):
+def handle_raw(pagename, form):
     if not file_re.match(pagename):
         send_httperror("403 Forbidden", pagename)
         return
 
     Page(pagename).send_raw()
 
-def handle_edit(pagename):
+def handle_edit(pagename, form):
     if not file_re.match(pagename):
         send_httperror("403 Forbidden", pagename)
         return
 
-    pg = Page(pagename)
+    pg = Page(form['q'].value)
     if 'save' in form:
         if form['file'].value:
             pg.save(form['file'].file.read(), form['changelog'].value)
@@ -306,6 +308,18 @@ def handle_edit(pagename):
             text = form['savetext'].value
         pg.send_editor(text)
 
+def handle_get(pagename, form):
+        if file_re.match(pagename):
+            # FIMXE: this is all bullshit, MimeTypes bases its guess on the extension!
+            from mimetypes import MimeTypes
+            mimetype, encoding = MimeTypes().guess_type(pagename)
+            if mimetype:
+                Page(pagename).send_raw(mimetype=mimetype, args=form)
+            else:
+                Page(pagename).format()
+        else:
+            send_httperror("403 Forbidden", pagename)
+
 # Used by macros/WordIndex and macros/TitleIndex
 def make_index_key():
     links = ['<a href="#%s">%s</a>' % (ch, ch) for ch in 'abcdefghijklmnopqrstuvwxyz']
@@ -800,29 +814,16 @@ class Page:
             self.msg_text = 'Thank you for your contribution.  Your attention to detail is appreciated.'
             self.msg_type = 'success'
 
-def main():
-    for cmd in form:
-        handler = globals().get('handle_' + cmd)
-        if handler:
-            handler(form[cmd].value)
-            break
-    else:
-        query = query_string()
-        if file_re.match(query):
-            # FIMXE: this is all bullshit, MimeTypes bases its guess on the extension!
-            from mimetypes import MimeTypes
-            mimetype, encoding = MimeTypes().guess_type(query)
-            if mimetype:
-                Page(query).send_raw(mimetype=mimetype, args=form)
-            else:
-                Page(query).format()
-        else:
-            send_httperror("403 Forbidden", query)
-
 try:
     exec(open("geekigeeki.conf.py").read())
     form = cgi.FieldStorage()
-    main()
+    action = form.getvalue('a', 'get')
+    handler = globals().get('handle_' + action)
+    if handler:
+        handler(query_string(), form)
+    else:
+        send_httperror("403 Forbidden", query_string())
+
 except Exception:
     import traceback
     msg_text = traceback.format_exc()