2 """Quick-quick implementation of WikiWikiWeb in Python
5 # Copyright (C) 1999, 2000 Martin Pool <mbp@humbug.org.au>
6 # This version includes additional changes by Gerardo Poggiali (2002)
7 # This version includes additional changes by Bernardo Innocenti (2007)
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 __version__ = '$Revision: 1.63+gerry+bernie $'[11:-2];
24 import cgi, sys, string, os, re, errno, time, stat
25 from os import path, environ
26 from socket import gethostbyaddr
27 from time import localtime, strftime
29 # Regular expression defining a WikiWord
30 # (but this definition is also assumed in other places)
31 file_re = re.compile(r"^\b([A-Za-z0-9_\.\-]+)\b$")
32 word_re = re.compile(r"^\b([A-Z][a-z]+){2,}\b$")
33 img_re = re.compile(r"^.*\.(png|gif|jpg|jpeg)$", re.IGNORECASE)
34 url_re = re.compile(r"^[a-z]{3,8}://[^\s'\"]+\S$")
38 def emit_header(type="text/html"):
39 print "Content-type: " + type + "; charset=utf-8"
43 # Formatting stuff --------------------------------------------------
46 return environ.get('SCRIPT_NAME', '')
48 def get_hostname(addr):
50 return gethostbyaddr(addr)[0] + ' (' + addr + ')'
54 def send_guru(msg, msg_type):
55 if msg is None or len(msg) == 0: return
56 print '<pre id="guru" onclick="this.style.display = \'none\'" class="' + msg_type + '">'
57 if msg_type == 'error':
58 print ' Software Failure. Press left mouse button to continue.\n'
60 if msg_type == 'error':
61 print ' Guru Meditation #DEADBEEF.ABADC0DE'
63 # FIXME: This simple JS code is harder to pass than ACID 3.0
65 <script language="JavaScript" type="text/javascript">
66 var guru = document.getElementById('guru');
67 // Firefox 2.0 doesn't take border-color, but returns border-top-color fine
68 var color = document.defaultView.getComputedStyle(guru,null).getPropertyValue('border-top-color');
71 guru.style.setProperty('border-color', color, '');
72 setTimeout('guruOff()', 1000);
75 guru.style.setProperty('border-color', '#000000', '');
76 setTimeout('guruOn()', 1000);
78 // Safari 2.0 returns this rgba crap
79 // Konqueror 3.5.6 doesn't seem to support computed properties
80 if (color && color != 'rgba(0, 0, 0, 0)') {
81 //window.alert("enabled! color='" + color + "'");
86 def send_title(name, text="Limbo", msg=None, msg_type = 'error'):
92 print """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
93 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
94 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
96 print "<head><title>%s: %s</title>" % (site_name, text)
97 print ' <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'
99 print ' <meta name="robots" content="noindex,nofollow">'
101 print ' <link rel="stylesheet" type="text/css" href="%s" />' % css_url
105 if name and allow_edit:
106 print '<body ondblclick="location.href=\'?edit=' + name + '\'">'
110 send_guru(msg, msg_type)
113 print '<div class="navigator">'
114 print '<b>' + site_name + ': '
116 print link_tag('?fullsearch=' + name, text) + '</b> '
119 print ' | ' + link_tag('FrontPage', 'Front Page', 'navlink')
120 print ' | ' + link_tag('FindPage', 'Find Page', 'navlink')
121 print ' | <a href="/wikigit/wiki.git" class="navlink">Recent Changes</a>'
123 print ' | <a href="/wikigit/wiki.git?a=history;f=' + name + '" class="navlink">Page History</a>'
124 print ' | ' + link_tag('?raw=' + name, 'Raw Text', 'navlink')
126 print ' | ' + link_tag('?edit=' + name, 'Edit Page', 'navlink')
128 print ' | <i>Immutable Page</i>'
134 def link_tag(params, text=None, ss_class=None):
136 text = params # default
138 # Prevent crawlers from following links to generated pages
139 # and links added by potential spammers
140 if ss_class == 'external' or ss_class == 'navlink':
141 classattr += 'rel="nofollow" '
143 classattr += 'class="%s" ' % ss_class
144 return '<a %shref="%s/%s">%s</a>' % (classattr, get_scriptname(),
147 # Search ---------------------------------------------------
149 def do_fullsearch(needle):
150 send_title(None, 'Full text search for "%s"' % (needle))
152 needle_re = re.compile(needle, re.IGNORECASE)
154 all_pages = page_list()
155 for page_name in all_pages:
156 body = Page(page_name).get_raw_body()
157 count = len(needle_re.findall(body))
159 hits.append((count, page_name))
161 # The default comparison for tuples compares elements in order,
162 # so this sorts by number of hits
167 for (count, page_name) in hits:
168 print '<li><p>' + Page(page_name).link_to()
169 print ' . . . . ' + `count`
170 print ['match', 'matches'][count <> 1]
174 print_search_stats(len(hits), len(all_pages))
177 def do_titlesearch(needle):
178 # TODO: check needle is legal -- but probably we can just accept any RE
179 send_title(None, "Title search for \"" + needle + '"')
181 needle_re = re.compile(needle, re.IGNORECASE)
182 all_pages = page_list()
183 hits = filter(needle_re.search, all_pages)
186 for filename in hits:
187 print '<li><p>' + Page(filename).link_to() + "</p></li>"
190 print_search_stats(len(hits), len(all_pages))
193 def print_search_stats(hits, searched):
194 print "<p>%d hits out of %d pages searched.</p>" % (hits, searched)
196 def do_edit(pagename):
197 Page(pagename).send_editor()
199 def do_raw(pagename):
200 Page(pagename).send_raw()
202 def do_savepage(pagename):
204 raise 'editing disallowed for ' + pagename
208 if 'preview' in form:
209 pg.send_editor(form['savetext'].value)
211 pg.save_text(form['savetext'].value)
213 elif 'cancel' in form:
214 pg.msg = 'Editing cancelled'
215 pg.msg_type = 'notice'
218 raise 'What did you press?'
220 def make_index_key():
222 links = map(lambda ch: '<a href="#%s">%s</a>' % (ch, ch),
224 s = s + string.join(links, ' | ')
225 s = s + '</center></p>'
230 return filter(word_re.match, os.listdir(text_dir))
233 def send_footer(name, mod_string=None):
234 print '<div class="footer">'
236 print "last modified %s" % mod_string
237 print '</div></body></html>'
240 # ----------------------------------------------------------
242 def _macro_TitleSearch():
243 return _macro_search("titlesearch")
245 def _macro_FullSearch():
246 return _macro_search("fullsearch")
248 def _macro_search(type):
249 if form.has_key('value'):
250 default = form["value"].value
253 return """<form method="get"><input name="%s" size="30" value="%s"><input type="submit" value="Go"></form>""" % (type, default)
255 def _macro_WordIndex():
257 pages = list(page_list())
259 word_re = re.compile('[A-Z][a-z]+')
261 for word in word_re.findall(name):
263 map[word].append(name)
267 all_words = map.keys()
271 for word in all_words:
272 letter = string.lower(word[0])
273 if letter <> last_letter:
274 s = s + '; <a name="%s"><h3>%s</h3></a>' % (letter, letter)
277 s = s + '<b>%s</b><ul>' % word
282 if name == last_page: continue
283 s = s + '<li>' + Page(name).link_to()
288 def _macro_TitleIndex():
290 pages = list(page_list())
292 current_letter = None
294 letter = string.lower(name[0])
295 if letter <> current_letter:
296 s = s + '<a name="%s"><h3>%s</h3></a>' % (letter, letter)
297 current_letter = letter
300 s = s + Page(name).link_to()
304 # ----------------------------------------------------------
306 """Object that turns Wiki markup into HTML.
308 All formatting commands can be parsed one line at a time, though
309 some state is carried over between lines.
311 def __init__(self, raw):
313 self.is_em = self.is_b = 0
315 self.list_indents = []
318 self.in_header = True
320 def _emph_repl(self, word):
322 self.is_b = not self.is_b
323 return ['</b>', '<b>'][self.is_b]
325 self.is_em = not self.is_em
326 return ['</em>', '<em>'][self.is_em]
328 def _tit_repl(self, word):
330 result = "</h%d>" % self.h_level
333 self.h_level = len(word) - 1
334 result = "<h%d>" % self.h_level
337 def _rule_repl(self, word):
340 s = s + "\n<hr size='1' noshade=\"noshade\" />\n"
342 s = s + "\n<hr size='%d' noshade=\"noshade\" />\n" % (len(word) - 2 )
345 def _word_repl(self, word):
346 return Page(word).link_to()
348 def _img_repl(self, word):
349 return '<img border="0" src="%s/%s" />' % (get_scriptname(), word)
351 def _url_repl(self, word):
352 if img_re.match(word):
353 return '<img border="0" src="%s" />' % word
355 return '<a href="%s" rel="nofollow" class="external">%s</a>' % (word, word)
357 def _hurl_repl(self, word):
358 m = re.compile("\[\[(\S+)\ (.+)\]\]").match(word)
361 if img_re.match(anchor):
362 return '<img border="0" src="%s" alt="%s" />' % (anchor, descr)
363 elif url_re.match(anchor):
364 return '<a href="%s" rel="nofollow" class="external">%s</a>' % (anchor, descr)
365 elif anchor.startswith('/'):
366 return '<a href="%s">%s</a>' % (anchor, descr)
368 return link_tag(anchor, descr)
370 def _email_repl(self, word):
371 return '<a href="mailto:%s">%s</a>' % (word, word)
374 def _ent_repl(self, s):
375 return {'&': '&',
380 def _li_repl(self, match):
384 def _pre_repl(self, word):
385 if word == '{{{' and not self.in_pre:
394 def _var_repl(self, word):
395 if word == '{{' and not self.in_var:
403 def _macro_repl(self, word):
404 macro_name = word[2:-2]
405 # TODO: Somehow get the default value into the search field
406 return apply(globals()['_macro_' + macro_name], ())
408 def _indent_level(self):
409 return len(self.list_indents) and self.list_indents[-1]
411 def _indent_to(self, new_level):
412 if self._indent_level() == new_level:
415 while self._indent_level() > new_level:
416 del(self.list_indents[-1])
418 while self._indent_level() < new_level:
419 self.list_indents.append(new_level)
426 res += '</ul>' * len(self.list_indents)
428 self.list_indents = []
431 def replace(self, match):
432 for type, hit in match.groupdict().items():
434 return apply(getattr(self, '_' + type + '_repl'), (hit,))
436 raise "Can't handle match " + `match`
438 def print_html(self):
439 print "<div class='wiki'><p>"
441 # For each line, we scan through looking for magic
442 # strings, outputting verbatim any intervening text
443 scan_re = re.compile(
445 + r"(?P<emph>'{2,3})"
446 + r"|(?P<tit>\={2,6})"
448 + r"|(?P<img>\b[a-zA-Z0-9_-]+\.(png|gif|jpg|jpeg|bmp))"
449 + r"|(?P<word>\b(?:[A-Z][a-z]+){2,}\b)"
450 + r"|(?P<rule>^-{3,})"
451 + r"|(?P<hurl>\[\[\S+\s+.+\]\])"
452 + r"|(?P<url>(http|ftp|nntp|news|mailto)\:[^\s'\"]+\S)"
453 + r"|(?P<email>[-\w._+]+\@[\w.-]+)"
455 + r"|(?P<pre>(\{\{\{|\s*\}\}\}))"
456 + r"|(?P<var>(\{\{|\}\}))"
457 + r"|(?P<macro>\[\[(TitleSearch|FullSearch|WordIndex|TitleIndex)\]\])"
461 + r"(?P<pre>\s*\}\}\})"
463 blank_re = re.compile("^\s*$")
464 indent_re = re.compile("^\s*")
465 eol_re = re.compile(r'\r?\n')
466 raw = string.expandtabs(self.raw)
467 for line in eol_re.split(raw):
470 if line.startswith('#'):
472 self.in_header = False
474 print re.sub(pre_re, self.replace, line)
476 # XXX: Should we check these conditions in this order?
477 if blank_re.match(line):
480 indent = indent_re.match(line)
481 print self._indent_to(len(indent.group(0)))
482 print re.sub(scan_re, self.replace, line)
483 if self.in_pre: print '</pre>'
487 # ----------------------------------------------------------
489 def __init__(self, page_name):
490 self.page_name = page_name
491 self.remote_user = 'AnonymousCoward'
492 self.remote_host = environ.get('REMOTE_ADDR', '')
494 self.msg_type = 'error'
497 def split_title(self):
498 # look for the end of words and the start of a new word,
499 # and insert a space there
500 return re.sub('([a-z])([A-Z])', r'\1 \2', self.page_name)
503 def _text_filename(self):
504 return path.join(text_dir, self.page_name)
507 def _tmp_filename(self):
508 return path.join(text_dir, ('#' + self.page_name + '.' + `os.getpid()` + '#'))
513 os.stat(self._text_filename())
516 if er.errno == errno.ENOENT:
522 word = self.page_name
524 return link_tag(word, word, 'wikilink')
527 return link_tag(word, '?', 'nonexistent') + word
529 return link_tag(word, word, 'nonexistent')
532 def get_raw_body(self):
534 return open(self._text_filename(), 'rt').read()
536 if er.errno == errno.ENOENT:
537 # just doesn't exist, use default
538 return 'Describe %s here.' % self.page_name
546 file = open(self._text_filename(), 'rt')
547 attr_re = re.compile(r"^#(\S*)(.*)$")
549 m = attr_re.match(line)
552 self.attrs[m.group(1)] = m.group(2).strip()
553 #print "bernie: attrs[" + m.group(1) + "] = " + m.group(2) + "<br>\n"
555 if er.errno != errno.ENOENT:
560 attrs = self.get_attrs()
562 # SomeUser:read,write All:read
564 for rule in acl.split():
565 (user,perms) = acl.split(':')
566 if user == self.remote_user or user == "All":
567 if 'write' in perms.split(','):
577 page_name = self.page_name
578 send_title(page_name, self.split_title(), msg=self.msg, msg_type=self.msg_type)
579 PageFormatter(self.get_raw_body()).print_html()
580 send_footer(page_name, self._last_modified())
582 def _last_modified(self):
583 if not self.exists():
585 modtime = localtime(os.stat(self._text_filename())[stat.ST_MTIME])
586 return strftime(datetime_fmt, modtime)
588 def send_editor(self, preview=None):
589 send_title(None, 'Edit ' + self.split_title(), msg=self.msg, msg_type=self.msg_type)
591 print ('<p><b>Editing ' + self.page_name
592 + ' for ' + self.remote_user
593 + ' from ' + get_hostname(self.remote_host)
595 print '<div class="editor"><form method="post" action="%s/%s">' % (get_scriptname(), self.page_name)
596 print '<input type="hidden" name="savepage" value="%s">' % (self.page_name)
597 print """<textarea wrap="virtual" id="editor" name="savetext" rows="17" cols="80">%s</textarea>""" % (preview or self.get_raw_body())
600 <input type="submit" name="save" value="Save" />
601 <input type="submit" name="preview" value="Preview" />
602 <input type="reset" value="Reset" />
603 <input type="submit" name="cancel" value="Cancel" />
606 print "<p>" + Page('EditingTips').link_to() + "</p>"
608 print "<div class='preview'>"
609 PageFormatter(preview).print_html()
613 emit_header("text/plain")
614 print self.get_raw_body()
616 def _write_file(self, text):
617 tmp_filename = self._tmp_filename()
618 open(tmp_filename, 'wt').write(text.replace('\r\n', '\n'))
619 text = self._text_filename()
621 # Bad Bill! POSIX rename ought to replace. :-(
625 if er.errno <> errno.ENOENT: raise er
626 os.rename(tmp_filename, text)
628 def save_text(self, newtext):
629 self._write_file(newtext)
632 cmd = ( post_edit_hook
633 + ' ' + text_dir + '/' + self.page_name
634 + ' ' + self.remote_user
635 + ' ' + self.remote_host
641 self.msg += "Post-editing hook returned %d.\n" % rc
642 self.msg += 'Command was: ' + cmd + '\n'
644 self.msg += 'Output follows:\n' + msg
646 self.msg = 'Thankyou for your contribution. Your attention to detail is appreciated.'
647 self.msg_type = 'success'
649 def send_verbatim(filename, mime_type='application/octet-stream'):
650 pathname = path.join(text_dir, filename)
651 data = open(pathname, 'rb').read()
652 emit_header(mime_type)
653 sys.stdout.write(data)
655 # ---------------------------------------------------------------
657 # Configuration values
658 data_dir = '/home/bernie/public_html/wiki'
659 text_dir = path.join(data_dir, 'text')
660 allow_edit = True # Is it possible to edit pages?
661 site_name = 'codewiz.org'
662 changed_time_fmt = '[%I:%M %p] '
663 date_fmt = '%a %d %b %Y'
664 datetime_fmt = '%a %d %b %Y %I:%M %p'
665 show_hosts = True # show hostnames?
666 css_url = '../wikidata/geekigeeki.css' # optional stylesheet link
667 nonexist_qm = False # show '?' for nonexistent?
668 post_edit_hook = './post_edit_hook.sh'
670 form = cgi.FieldStorage()
672 handlers = { 'fullsearch': do_fullsearch,
673 'titlesearch': do_titlesearch,
676 'savepage': do_savepage }
678 for cmd in handlers.keys():
679 if form.has_key(cmd):
680 apply(handlers[cmd], (form[cmd].value,))
683 path_info = environ.get('PATH_INFO', '')
684 if len(path_info) and path_info[0] == '/':
685 query = path_info[1:] or 'FrontPage'
687 query = environ.get('QUERY_STRING', '') or 'FrontPage'
689 if file_re.match(query):
690 if word_re.match(query):
691 Page(query).send_page()
692 elif img_re.match(query):
693 send_verbatim(query, 'image/jpeg')
698 send_title(None, msg='Can\'t work out query: ' + query)
701 msg=traceback.format_exc()
703 send_guru(msg, "error")
705 send_title(None, msg=msg)