Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/woip/rb/server.rb
blob: 0b2ea29d66a98771eaf18c614a859cb9613e2f27 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
require 'rubygems'
require 'mongrel'
require 'inline'

READER = "WP, the free offline Wikipedia reader thing"
MAXRES = 40
USAGE = %Q[
<strong>Supported requests:</strong>
<ul>
<li><tt>/wiki/Foo</tt> - parsed Wikipedia article on Foo</li>
<li><tt>/raw/Foo</tt> - raw text of article</li>
<li><tt>/search?s=foo</tt> - article titles containing foo</li>
</ul>
]
SEARCH_BOX=%Q{<div width="100" style="float: right;">
      <form action="/search" method="get">
      <input type="text" name="s" accesskey="s" /></form></div>}

class String
  def titleize
    self[0..0].upcase + self[1..-1]
  end
end

class Parser
  # Parser.php is 5k lines, and has a billion dependencies, transclusion logic,
  # magic-word support, langauge processing, templating code (a literally
  # Turing-complete sublanguage), and stuff... so let's just use regexps
    
  def parse_infobox(str)
    str.gsub(/^\|([^=]+)[:space:]*=[:space:]*(.*)$/) { "<b>#{$1}</b> #{$2}<br>" }
  end

  def parse_template(str)
    str.gsub(/\{\{(([^[:space:]]+) (\w+)$)?([^\}\{]+)\}\}/) { 
    if $2 == "Infobox"
      %Q[ <div style="width: 200px; background-color: #eee; border: 1px solid #bbb; float: right">
      <h2>#{$3}</h2>
      #{parse_infobox($4)} </div> ]
    else
      ""
    end
    }
  end

  def parse(str)
    # Lists; this barely works in even the simplest cases
    str = str.gsub(/^\*(.+)$/) { "<span style=\"display: block\">* #{$1}</span>" }

    # Bold
    str = str.gsub(/'''(.+?)'''/) { "<b>#{$1}</b>" }

    # Italic
    str = str.gsub(/''(.+?)''/) { "<i>#{$1}</i>" }

    # Headings
    str = str.gsub(/(={2,})([^=]+)\1/) {"<h#{$1.size}>#{$2}</h#{$1.size}>"}

    # Interwiki links
    str = str.gsub(/\[\[([^\|\]]+)\|([^\]]+)\]\](\w*)/) { Article.link_to($1, $2 + $3) }
    str = str.gsub(/\[\[(.*?)\]\](\w*)/) { Article.link_to($1, $1 + $2) }

    # Strip refs
    str = str.gsub(/<ref>.*?<\/ref>/, '')

    # Templates, which may be nested, and which we need to parse from innermost first
    while (new = parse_template(str)) != str
      str = new
    end

    # External links
    str = str.gsub(/\[([^\][:space:]]+) ([^\]]+)\]/) {"<a href=\"#{$1}\">#{$2}</a>"}
    str = str.gsub(/\[([^\][:space:]]+)\]/) {"<a href=\"#{$1}\">#{$1}</a>"}
  end
end

class Article
  attr_accessor :text
  attr_accessor :block
  attr_accessor :title

  def parsed_text
    Parser.new.parse(self.text)
  end

  def as_html
    %Q[<html><head><title>#{title} - #{READER}</title></head>
     <body>
      #{SEARCH_BOX}
      <h1>#{title}</h1><h5>#{READER}</h5><p><small>#{text.size} bytes from block #{block}
      (<a href="/raw/#{CGI::escape(title.titleize)}">raw</a>)</small></p>
      #{parsed_text}
     </body></html>]
  end

  def self.link_to(name, text)
    "<a href=\"/wiki/#{CGI::escape(name.titleize)}\">#{text}</a>"
  end
end

class SearchResult
  attr_accessor :results
  attr_accessor :needle

  def as_html
    %Q[<html><head><title>Search: #{needle} - #{READER}</title></head>
    <body>
    #{SEARCH_BOX}
    <h1>Search: #{needle}</h1>
    <ul>#{results.map{|r| "<li>" + Article.link_to(r, r) + "</li>"}.join}</ul>
    <p><small>Searches return up to #{MAXRES} articles containing the search string anywhere in their title.
    Results are case-insensitive. Exact matches appear first, followed by prefix matches, and, lastly, substring matches.
    Press ^S to quickly jump to search.</small></p>
    </body>
    </html>]
  end
end

class WPArticleReader
  inline(:C) do |builder|
    builder.add_compile_flags "-I../c -I. -lbz2 -DDEBUG"
    builder.add_compile_flags "../c/bzipreader.c"
    builder.add_compile_flags "../c/wp.c"
    builder.add_compile_flags "../c/lsearcher.c"
    builder.add_compile_flags "../c/safe.c"
    builder.add_compile_flags "../c/blocks.c"

    builder.prefix %Q$
      #include "wp.h"
      #define MAXRES #{MAXRES}
      #define MAXSTR 1024
      
      wp_dump d = {0};
      wp_article a = {0};

      char results[MAXRES][MAXSTR];
      int nresults;

      bool __handle_result(char *s) {
        strncpy(results[nresults], s, MAXSTR);
        results[nresults][MAXSTR - 1] = \'\\0\';
        char *end = strrchr(results[nresults], \' \');

        if(end) {
          *(end - 1) = \'\\0\';
          nresults++;
        }

        return nresults < MAXRES;
      }
    $

    builder.c 'void __load_dump(char *dump, char *loc, char *ploc, char *blocks) {
      load_dump(&d, dump, loc, ploc, blocks);
      init_article(&a);
    }'

    builder.c 'char *__load_article(char *name) {
      a.block = 0;
      a.text[0] = \'\0\';
      load_article(&d, name, &a);
      return a.text;
    }'

    builder.c 'int __article_block() {
      return a.block;
    }'

    builder.c 'int __article_size() {
      return strlen(a.text);
    }'


    builder.c 'int __search(char *needle) {
      nresults = 0;
      search(&d.index, needle, __handle_result, NULL, true, true);
      return nresults;
    }'

    builder.c 'char *__result(int n) {
      return results[n];
    }'
  end
      
  def initialize(opts)
    @locatedb = opts[:locatedb]
    @prefixdb = opts[:prefixdb]
    @blockdb = opts[:blockdb]
    @dump = opts[:dump]
    
    __load_dump(@dump, @locatedb, @prefixdb, @blockdb)
  end

  def fetch(name)
    text = __load_article(name)
    a = Article.new
    a.text = text
    a.block = __article_block
    a.title = name
    a
  end

  def find(name)
    n = __search(name)
    r = SearchResult.new
    r.needle = name
    r.results = (0..n - 1).map {|n| __result(n)}
    r
  end
end

class WPHandler < Mongrel::HttpHandler
  def initialize(base)
    $stderr.puts "Using base #{base}"
    @reader = WPArticleReader.new(:locatedb => "#{base}.locate.db",
                                  :prefixdb => "#{base}.locate.prefixdb",
                                  :blockdb => "#{base}.blocks.db",
                                  :dump => "#{base}.processed")
    @parser = Parser.new 
  end

  def path(req)
    CGI::unescape(req.params["REQUEST_URI"])
  end

  def notfound(resp, str)
    respond(resp, true, 404) { "Couldn't find #{str}. <p>#{USAGE}</p>" }
  end

  def respond(resp, html=true, status=200)
    resp.start(200) do |h, o|
      h["Content-type"] = (html ? 'text/html' : 'text/plain') + '; charset=utf-8'
      o.write yield
    end
  end

  def process(req, resp)
    if path(req) =~ /^\/(wiki|raw)\/(.+)$/
      article = @reader.fetch($2)

      if article.text.empty?
        notfound(resp, $2)
      else
        if $1 == "wiki"
          respond(resp) { article.as_html }
        elsif $1 == "raw"
          respond(resp, false) { article.text }
        end
      end
    elsif path(req) =~ /^\/search\?s=(.+)$/
      respond(resp) { @reader.find($1).as_html }
    else 
      notfound(resp, path(req))
    end
  end
end

class WPServer
  def self.start_on(port)
    self.new(:port => port).run
  end

  def initialize(opts={})
    @port = opts[:port] || 9000
    @host = opts[:host] || '0.0.0.0'
  end

  def run
    $stderr.puts "Binding to #{@host}:#{@port}"

    conf = Mongrel::Configurator.new(:port => @port, 
                                     :host => @host) {
      listener do
        uri '/', :handler => WPHandler.new(ARGV.first), :in_front => true
      end

      trap("INT") { stop }

      run
    }

    conf.join
  end
end