2 # -*- coding: utf-8 -*-
4 # Copyright 1999, 2000 Martin Pool <mbp@humbug.org.au>
5 # Copyright 2002 Gerardo Poggiali
6 # Copyright 2007, 2008 Bernardo Innocenti <bernie@codewiz.org>
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 __version__ = '$Id$'[4:12]
23 from time import clock
26 import cgi, sys, string, os, re, errno, stat
27 from os import path, environ
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,}/)*([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$")
39 # CGI stuff ---------------------------------------------------------
42 return environ.get('SCRIPT_NAME', '')
44 def privileged_path():
45 return privileged_url or script_name()
48 user = environ.get('REMOTE_USER', '')
49 if user is None or user == '' or user == 'anonymous':
50 user = 'AnonymousCoward'
54 return environ.get('REMOTE_ADDR', '')
56 def get_hostname(addr):
58 from socket import gethostbyaddr
59 return gethostbyaddr(addr)[0] + ' (' + addr + ')'
63 def relative_url(path, privileged=False):
64 if not (url_re.match(path) or path.startswith('/')):
66 url = privileged_path()
69 path = url + '/' + path
72 # Formatting stuff --------------------------------------------------
74 def emit_header(type="text/html"):
75 print "Content-type: " + type + "; charset=utf-8"
78 def send_guru(msg, msg_type):
79 if msg is None or msg == '': return
80 print '<pre id="guru" onclick="this.style.display = \'none\'" class="' + msg_type + '">'
81 if msg_type == 'error':
82 print ' Software Failure. Press left mouse button to continue.\n'
84 if msg_type == 'error':
85 print ' Guru Meditation #DEADBEEF.ABADC0DE'
87 # FIXME: This simple JS snippet is harder to pass than ACID 3.0
89 <script language="JavaScript" type="text/javascript">
90 var guru = document.getElementById('guru');
91 // Firefox 2.0 doesn't take border-color, but returns border-top-color fine
92 var color = document.defaultView.getComputedStyle(guru,null).getPropertyValue('border-top-color');
95 guru.style.setProperty('border-color', color, '');
96 setTimeout('guruOff()', 1000);
99 guru.style.setProperty('border-color', '#000000', '');
100 setTimeout('guruOn()', 1000);
102 // Safari 2.0 returns this rgba crap
103 // Konqueror 3.5.6 doesn't seem to support computed properties
104 if (color && color != 'rgba(0, 0, 0, 0)') {
105 //window.alert("enabled! color='" + color + "'");
110 def send_title(name, text="Limbo", msg=None, msg_type='error'):
112 if title_done: return
116 print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
117 print ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
118 print '<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">'
120 site_name = globals().get('site_name', 'Unconfigured Site')
121 print "<head><title>%s: %s</title>" % (site_name, text)
122 print ' <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />'
124 print ' <meta name="robots" content="noindex,nofollow" />'
126 print ' <link rel="stylesheet" type="text/css" href="%s" />' % relative_url(css)
130 if name and privileged_url is not None:
131 print '<body ondblclick="location.href=\'' + privileged_path() + '?edit=' + name + '\'">'
136 send_guru(msg, msg_type)
139 print '<div class="nav">'
140 print ' <b>' + site_name + ': ',
142 print link_tag('?fullsearch=' + name, text, 'navlink') + '</b> '
145 print ' | ' + link_tag('FrontPage', 'Front Page', 'navlink')
146 print ' | ' + link_tag('FindPage', 'Find Page', 'navlink')
147 if globals().has_key('history_url'):
148 print ' | <a href="' + history_url + '" class="navlink">Recent Changes</a>'
150 print ' | <a href="' + history_url + '?a=history;f=' + name + '" class="navlink">Page History</a>'
153 print ' | ' + link_tag('?raw=' + name, 'Raw Text', 'navlink')
154 if privileged_url is not None:
155 print ' | ' + link_tag('?edit=' + name, 'Edit Page', 'navlink', privileged=True)
157 print ' | <i>Immutable Page</i>'
160 if user != 'AnonymousCoward':
161 print ' | <span class="login"><i>logged in as <b>' + cgi.escape(user) + '</b></i></span>'
165 def link_tag(params, text=None, ss_class=None, privileged=False):
167 text = params # default
170 classattr += 'class="%s" ' % ss_class
171 # Prevent crawlers from following links potentially added by spammers or to generated pages
172 if ss_class == 'external' or ss_class == 'navlink':
173 classattr += 'rel="nofollow" '
174 return '<a %shref="%s">%s</a>' % (classattr, relative_url(params, privileged=privileged), text)
176 # Search ---------------------------------------------------
178 def do_fullsearch(needle):
179 send_title(None, 'Full text search for "%s"' % (needle))
181 needle_re = re.compile(needle, re.IGNORECASE)
183 all_pages = page_list()
184 for page_name in all_pages:
185 body = Page(page_name).get_raw_body()
186 count = len(needle_re.findall(body))
188 hits.append((count, page_name))
190 # The default comparison for tuples compares elements in order,
191 # so this sorts by number of hits
196 for (count, page_name) in hits:
197 print '<li><p>' + Page(page_name).link_to()
198 print ' . . . . ' + `count`
199 print ['match', 'matches'][count != 1]
203 print_search_stats(len(hits), len(all_pages))
205 def do_titlesearch(needle):
206 # TODO: check needle is legal -- but probably we can just accept any RE
207 send_title(None, "Title search for \"" + needle + '"')
209 needle_re = re.compile(needle, re.IGNORECASE)
210 all_pages = page_list()
211 hits = filter(needle_re.search, all_pages)
214 for filename in hits:
215 print '<li><p>' + Page(filename).link_to() + "</p></li>"
218 print_search_stats(len(hits), len(all_pages))
220 def print_search_stats(hits, searched):
221 print "<p>%d hits out of %d pages searched.</p>" % (hits, searched)
223 #TODO: merge into do_savepage()
224 def do_edit(pagename):
225 Page(pagename).send_editor()
227 def do_raw(pagename):
228 Page(pagename).send_raw()
230 def do_savepage(pagename):
233 if 'preview' in form:
234 pg.send_editor(form['savetext'].value)
236 pg.save_text(form['savetext'].value)
238 elif 'cancel' in form:
239 pg.msg = 'Editing canceled'
240 pg.msg_type = 'notice'
243 raise 'What did you press?'
245 def make_index_key():
247 links = map(lambda ch: '<a href="#%s">%s</a>' % (ch, ch),
249 s = s + string.join(links, ' | ')
250 s = s + '</center></p>'
254 return filter(word_re.match, os.listdir(data_dir))
256 def send_footer(name, mod_string=None):
257 if globals().get('debug_cgi', False):
258 cgi.print_arguments()
259 cgi.print_form(cgi.FieldStorage())
262 print '<div id="footer"><hr />'
263 print ('<p class="copyright"><span class="benchmark">generated in %0.3fs</span>' +
264 ' by <a href="http://www.codewiz.org/wiki/GeekiGeeki">GeekiGeeki</a>' +
265 ' version %s</p>') % (clock() - start_time, __version__)
267 print '<p class="modified">last modified %s</p>' % mod_string
268 print '</div></body></html>'
270 # ----------------------------------------------------------
272 def _macro_TitleSearch(*vargs):
273 return _macro_search("titlesearch")
275 def _macro_FullSearch(*vargs):
276 return _macro_search("fullsearch")
278 def _macro_search(type):
279 if form.has_key('value'):
280 default = form["value"].value
283 return """<form method="get"><input name="%s" size="30" value="%s" /><input type="submit" value="Search" /></form>""" % (type, default)
285 def _macro_WordIndex(*vargs):
287 pages = list(page_list())
289 word_re = re.compile('[A-Z][a-z]+')
291 for word in word_re.findall(name):
293 map[word].append(name)
297 all_words = map.keys()
301 for word in all_words:
302 letter = string.lower(word[0])
303 if letter != last_letter:
304 s = s + '; <a name="%s"><h3>%s</h3></a>' % (letter, letter)
307 s = s + '<b>%s</b><ul>' % word
312 if name == last_page: continue
313 s = s + '<li>' + Page(name).link_to()
318 def _macro_TitleIndex(*vargs):
320 pages = list(page_list())
322 current_letter = None
324 letter = string.lower(name[0])
325 if letter != current_letter:
326 s = s + '<a name="%s"><h3>%s</h3></a>' % (letter, letter)
327 current_letter = letter
330 s = s + Page(name).link_to()
334 # ----------------------------------------------------------
336 """Object that turns Wiki markup into HTML.
338 All formatting commands can be parsed one line at a time, though
339 some state is carried over between lines.
341 def __init__(self, raw):
344 self.in_pre = self.in_table = False
345 self.in_header = True
346 self.list_indents = []
347 self.tr_cnt = self.h_cnt = 0
357 "^^": ["sup", False],
361 def _b_repl(self, word):
362 style = self.styles[word]
363 style[1] = not style[1]
364 return ['</','<'][style[1]] + style[0] + '>'
366 def _tit_repl(self, word):
368 result = '</h%d>' % self.h_level
371 self.h_level = len(word) - 1
373 #abridged = re.sub('[^a-z_]', '', word.lower().replace(' ', '_'))
374 result = '<h%d id="%d"><a class="heading" href="#%d">¶</a> ' % (self.h_level, self.h_cnt, self.h_cnt)
377 def _br_repl(self, word):
380 def _rule_repl(self, word):
381 return self._undent() + '\n<hr size="%d" noshade="noshade" />\n' % (len(word) - 2)
383 def _word_repl(self, word):
384 return Page(word).link_to()
386 def _img_repl(self, word):
387 path = relative_url(word)
388 return '<a href="%s"><img border="0" src="%s" /></a>' % (path, path)
390 def _url_repl(self, word):
391 if img_re.match(word):
392 return '<a href="%s"><img border="0" src="%s" /></a>' % (word, word)
394 return '<a href="%s" rel="nofollow" class="external">%s</a>' % (word, word)
396 def _hurl_repl(self, word):
397 m = re.compile("\[\[([^ \t\n\r\f\v\|]+)(?:\s*\|\s*([^\]]+)|)\]\]").match(word)
399 descr = m.group(2) or name
401 macro = globals().get('_macro_' + name)
403 return apply(macro, (name, descr))
404 elif img_re.match(name):
405 # The "extthumb" nonsense works around a limitation of the HTML block model
406 return '<div class="extthumb"><div class="thumb"><a href="%s"><img border="0" src="%s" alt="%s" /></a><div class="caption">%s</div></div></div>' % (name, name, descr, descr)
408 if img_re.match(descr):
409 descr = '<img border="0" src="' + descr + '" />'
411 if url_re.match(name):
412 return '<a href="%s" rel="nofollow" class="external">%s</a>' % (name, descr)
413 elif name.startswith('/'):
414 return '<a href="%s">%s</a>' % (name, descr)
416 return link_tag(name, descr, 'wikilink')
418 def _email_repl(self, word):
419 return '<a href="mailto:%s">%s</a>' % (word, word)
421 def _html_repl(self, word):
422 return word; # Pass through
424 def _ent_repl(self, s):
425 return {'&': '&',
429 def _li_repl(self, match):
432 def _pre_repl(self, word):
433 if word == '{{{' and not self.in_pre:
441 def _hi_repl(self, word):
448 return '<strong class="highlight ' + cl + '">' + word + '</strong>'
450 def _tr_repl(self, word):
452 if not self.in_table:
455 out = '</p><table><tbody>\n'
457 out = out + '<tr class="' + ['even', 'odd'][self.tr_cnt % 2] + '">'
458 return out + ['<td>', '<th>'][word.strip() == '||=']
460 def _td_repl(self, word):
462 return ['</td><td>', '</th><th>'][word.strip() == '||=']
465 def _tre_repl(self, word):
467 return ['</td></tr>', '</th></tr>'][word.strip() == '||=']
470 def _indent_level(self):
471 return len(self.list_indents) and self.list_indents[-1]
473 def _indent_to(self, new_level):
474 if self._indent_level() == new_level:
477 while self._indent_level() > new_level:
478 del(self.list_indents[-1])
480 while self._indent_level() < new_level:
481 self.list_indents.append(new_level)
488 res += '</ul>' * len(self.list_indents)
490 self.list_indents = []
493 def replace(self, match):
494 for type, hit in match.groupdict().items():
496 return apply(getattr(self, '_' + type + '_repl'), (hit,))
498 raise "Can't handle match " + `match`
500 def print_html(self):
501 print '<div class="wiki"><p>'
503 # For each line, we scan through looking for magic
504 # strings, outputting verbatim any intervening text
505 # TODO: highlight search words (look at referer)
506 scan_re = re.compile(
509 + r"(?P<b>\*\*|'''|//|''|##|``|__|\^\^|,,)"
510 + r"|(?P<tit>\={2,6})"
512 + r"|(?P<rule>^-{3,})"
513 + r"|(?P<html><(/|)(div|span|iframe)[^<>]*>)"
515 + r"|(?P<hi>\b(FIXME|TODO|DONE)\b)"
518 + r"|(?P<img>\b[a-zA-Z0-9_-]+\.(png|gif|jpg|jpeg|bmp))"
519 + r"|(?P<word>\b(?:[A-Z][a-z]+){2,}\b)"
520 + r"|(?P<hurl>\[\[([^ \t\n\r\f\v\|]+)(?:\s*\|\s*([^\]]+)|)\]\])"
521 + r"|(?P<url>(http|https|ftp|mailto)\:[^\s'\"]+\S)"
522 + r"|(?P<email>[-\w._+]+\@[\w.-]+)"
525 + r"|(?P<li>^\s+[\*#] +)"
526 + r"|(?P<pre>\{\{\{|\s*\}\}\})"
529 + r"|(?P<tr>^\s*\|\|(=|)\s*)"
530 + r"|(?P<tre>\s*\|\|(=|)\s*$)"
531 + r"|(?P<td>\s*\|\|(=|)\s*)"
535 + r"(?P<pre>\s*\}\}\})"
537 blank_re = re.compile(r"^\s*$")
538 indent_re = re.compile(r"^\s*")
539 tr_re = re.compile(r"^\s*\|\|")
540 eol_re = re.compile(r"\r?\n")
541 raw = string.expandtabs(self.raw)
542 for line in eol_re.split(raw):
545 if line.startswith('#'):
547 self.in_header = False
550 print re.sub(pre_re, self.replace, line)
552 if self.in_table and not tr_re.match(line):
553 self.in_table = False
554 print '</tbody></table><p>'
556 if blank_re.match(line):
559 indent = indent_re.match(line)
560 print self._indent_to(len(indent.group(0)))
561 print re.sub(scan_re, self.replace, line)
563 if self.in_pre: print '</pre>'
564 if self.in_table: print '</tbody></table><p>'
568 # ----------------------------------------------------------
570 def __init__(self, page_name):
571 self.page_name = page_name
573 self.msg_type = 'error'
575 def split_title(self):
576 # look for the end of words and the start of a new word,
577 # and insert a space there
578 return re.sub('([a-z])([A-Z])', r'\1 \2', self.page_name)
580 def _text_filename(self):
581 return path.join(data_dir, self.page_name)
583 def _tmp_filename(self):
584 return path.join(data_dir, ('#' + self.page_name + '.' + `os.getpid()` + '#'))
588 os.stat(self._text_filename())
591 if er.errno == errno.ENOENT:
597 word = self.page_name
599 return link_tag(word, word, 'wikilink')
601 return link_tag(word, nonexist_pfx + word, 'nonexistent')
603 def get_raw_body(self):
605 return open(self._text_filename(), 'rt').read()
607 if er.errno == errno.ENOENT:
608 return '' # just doesn't exist, use default
612 if self.__dict__.has_key('attrs'):
616 file = open(self._text_filename(), 'rt')
617 attr_re = re.compile(r"^#(\S*)(.*)$")
619 m = attr_re.match(line)
622 self.attrs[m.group(1)] = m.group(2).strip()
623 #print "bernie: attrs[" + m.group(1) + "] = " + m.group(2) + "<br>\n"
625 if er.errno != errno.ENOENT:
629 def get_attr(self, name, default):
630 if self.get_attrs().has_key(name):
631 return self.get_attrs()[name]
635 def can(self, action, default=True):
637 #acl SomeUser:read,write All:read
638 acl = self.get_attr("acl", None)
639 for rule in acl.split():
640 (user,perms) = rule.split(':')
641 if user == remote_user() or user == "All":
642 if action in perms.split(','):
647 except Exception, er:
652 return self.can("write", True)
655 return self.can("read", True)
660 page_name = self.page_name
662 #FIXME: are there security implications?
665 css_url = css_url + self.get_attr("css", "").split()
667 send_title(page_name, self.split_title(), msg=self.msg, msg_type=self.msg_type)
669 PageFormatter(self.get_raw_body()).print_html()
671 send_guru("Read access denied by ACLs", "notice")
672 send_footer(page_name, self._last_modified())
674 def _last_modified(self):
676 from time import localtime, strftime
677 modtime = localtime(os.stat(self._text_filename())[stat.ST_MTIME])
679 if er.errno != errno.ENOENT:
682 return strftime(datetime_fmt, modtime)
684 def send_editor(self, preview=None):
685 send_title(None, 'Edit ' + self.split_title(), msg=self.msg, msg_type=self.msg_type)
686 if not self.can_write():
687 send_guru("Write access denied by ACLs", "error")
690 print ('<p><b>Editing ' + self.page_name
691 + ' for ' + cgi.escape(remote_user())
692 + ' from ' + cgi.escape(get_hostname(remote_host()))
694 print '<div class="editor"><form method="post" action="%s">' % relative_url(self.page_name)
695 print '<input type="hidden" name="savepage" value="%s">' % (self.page_name)
696 print """<textarea wrap="off" spellcheck="true" id="editor" name="savetext" rows="17" cols="100">%s</textarea>""" % (preview or self.get_raw_body())
699 <input type="submit" name="save" value="Save" />
700 <input type="submit" name="preview" value="Preview" />
701 <input type="reset" value="Reset" />
702 <input type="submit" name="cancel" value="Cancel" />
705 print "<p>" + Page('EditingTips').link_to() + "</p>"
707 print "<div class='preview'>"
708 PageFormatter(preview).print_html()
710 send_footer(self.page_name)
713 if not self.can_read():
714 send_title(None, msg='Read access denied by ACLs', msg_type='notice')
716 emit_header("text/plain")
717 print self.get_raw_body()
719 def _write_file(self, text):
720 tmp_filename = self._tmp_filename()
721 open(tmp_filename, 'wt').write(text.replace('\r\n', '\n'))
722 text = self._text_filename()
724 # Bad Bill! POSIX rename ought to replace. :-(
728 if er.errno != errno.ENOENT: raise er
729 os.rename(tmp_filename, text)
731 def save_text(self, newtext):
732 if not self.can_write():
733 self.msg = 'Write access denied by ACLs'
734 self.msg_type = 'error'
737 self._write_file(newtext)
740 # FIXME: what's the std way to perform shell quoting in python?
741 cmd = ( post_edit_hook
742 + " '" + data_dir + '/' + self.page_name
743 + "' '" + remote_user()
744 + "' '" + remote_host() + "'"
750 self.msg += "Post-editing hook returned %d.\n" % rc
751 self.msg += 'Command was: ' + cmd + '\n'
753 self.msg += 'Output follows:\n' + msg
755 self.msg = 'Thank you for your contribution. Your attention to detail is appreciated.'
756 self.msg_type = 'success'
758 #TODO: merge into send_raw()
759 def send_verbatim(filename, mime_type='application/octet-stream'):
760 pathname = path.join(data_dir, filename)
761 data = open(pathname, 'rb').read()
762 emit_header(mime_type)
763 sys.stdout.write(data)
765 # Main ---------------------------------------------------------------
767 execfile("geekigeeki.conf.py")
769 form = cgi.FieldStorage()
771 handlers = { 'fullsearch': do_fullsearch,
772 'titlesearch': do_titlesearch,
775 'savepage': do_savepage }
777 for cmd in handlers.keys():
778 if form.has_key(cmd):
779 apply(handlers[cmd], (form[cmd].value,))
782 path_info = environ.get('PATH_INFO', '')
783 if len(path_info) and path_info[0] == '/':
784 query = path_info[1:] or 'FrontPage'
786 query = environ.get('QUERY_STRING', '') or 'FrontPage'
788 if file_re.match(query):
789 if word_re.match(query):
790 Page(query).send_page()
791 elif img_re.match(query):
792 #FIXME: use correct mime type
793 send_verbatim(query, 'image/jpeg')
797 print "Status: 404 Not Found"
798 send_title(None, msg='Can\'t work out query: ' + query)
801 msg=traceback.format_exc()
803 send_guru(msg, "error")
805 send_title(None, msg=msg)