Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/py/server.py
blob: ade7d723dd385635605c538068c75a53ec8d036d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#
# Web server script for Wikiserver project.
#
# TODO
#
# + Send content in the right charset.
# + Find a better way to locate instaview.js.
# + Make a nice looking page template, like the library.
# + Add a home page, like the library.
# + Use a style sheet.
# + Add a search box.
# + Return actual search results.
# + Instead of 404, send to home page.
# + Route non-cached and image links to schoolserver or wikipedia when available.
#
import sys
import BaseHTTPServer
import urllib
import re
import wp

class WikiRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    
    @staticmethod
    def send_article(s, title):
        s.send_response(200)
        s.send_header("Content-type", "text/html; charset=utf-8")
        s.end_headers()

        s.wfile.write("<html><head><title>%s</title></head>" % title)
        s.wfile.write("<body>")
        
        instaview_src = open('../js/instaview.js').read()
        s.wfile.write("<script type='text/javascript'>%s</script>" % instaview_src)

        article_text = unicode(wp.wp_load_article(title), 'utf8')

        jstext = ''
        for l in article_text.split('\n'):
            jstext += re.escape(l) + '\\n\\\n'

        s.wfile.write("<script type='text/javascript'>");
        s.wfile.write("var wikitext = \"%s\";" % jstext.encode('utf8'));
        s.wfile.write("document.write(InstaView.convert(unescape(wikitext)));");
        s.wfile.write("</script>")
        
        s.wfile.write("</body></html>")

    @staticmethod
    def send_searchresult(s, title):
        s.send_response(200)
        s.send_header("Content-type", "text/html; charset=utf-8")
        s.end_headers()

        s.wfile.write("<html><head><title>Search Results for '%s'</title></head>" % title)
        s.wfile.write("<body>")
        
        s.wfile.write("<p>You asked for search term %s.</p>" % title)
        
        s.wfile.write("</body></html>")
    
    def do_GET(s):
        real_path = s.path
        real_path = urllib.url2pathname(real_path)

        m = re.match(r'^/(wiki|raw)/(.+)$', real_path)
        if m:
            WikiRequestHandler.send_article(s, m.group(2))
            return
        
        m = re.match(r'^/search/(.+)$', real_path)
        if m:
            WikiRequestHandler.send_searchresult(s, m.group(1))
            return
        
        s.send_response(404)

dbname = sys.argv[1]

wp.wp_load_dump(
    dbname + '.processed',
    dbname + '.locate.db',
    dbname + '.locate.prefixdb',
    dbname + '.blocks.db')

httpd = BaseHTTPServer.HTTPServer(('', 8000), WikiRequestHandler)
httpd.serve_forever()