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
27 # Regular expression defining a WikiWord
28 # (but this definition is also assumed in other places)
29 file_re = re.compile(r"^\b([A-Za-z0-9_\.\-]+)\b$")
30 word_re = re.compile(r"^\b([A-Z][a-z]+){2,}\b$")
31 img_re = re.compile(r"^.*\.(png|gif|jpg|jpeg)$", re.IGNORECASE)
32 url_re = re.compile(r"^[a-z]{3,8}://[^\s'\"]+\S$")
37 # CGI stuff ---------------------------------------------------------
40 return environ.get('SCRIPT_NAME', '')
42 def privileged_path():
43 return privileged_url or script_name()
46 user = environ.get('REMOTE_USER', '')
47 if user is None or user == '' or user == 'anonymous':
48 user = 'AnonymousCoward'
52 return environ.get('REMOTE_ADDR', '')
54 def get_hostname(addr):
56 from socket import gethostbyaddr
57 return gethostbyaddr(addr)[0] + ' (' + addr + ')'
61 # Formatting stuff --------------------------------------------------
63 def emit_header(type="text/html"):
64 print "Content-type: " + type + "; charset=utf-8"
67 def send_guru(msg, msg_type):
68 if msg is None or msg == '': return
69 print '<pre id="guru" onclick="this.style.display = \'none\'" class="' + msg_type + '">'
70 if msg_type == 'error':
71 print ' Software Failure. Press left mouse button to continue.\n'
73 if msg_type == 'error':
74 print ' Guru Meditation #DEADBEEF.ABADC0DE'
76 # FIXME: This simple JS snippet is harder to pass than ACID 3.0
78 <script language="JavaScript" type="text/javascript">
79 var guru = document.getElementById('guru');
80 // Firefox 2.0 doesn't take border-color, but returns border-top-color fine
81 var color = document.defaultView.getComputedStyle(guru,null).getPropertyValue('border-top-color');
84 guru.style.setProperty('border-color', color, '');
85 setTimeout('guruOff()', 1000);
88 guru.style.setProperty('border-color', '#000000', '');
89 setTimeout('guruOn()', 1000);
91 // Safari 2.0 returns this rgba crap
92 // Konqueror 3.5.6 doesn't seem to support computed properties
93 if (color && color != 'rgba(0, 0, 0, 0)') {
94 //window.alert("enabled! color='" + color + "'");
99 def send_title(name, text="Limbo", msg=None, msg_type='error'):
101 if title_done: return
105 print """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
106 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
107 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
109 print "<head><title>%s: %s</title>" % (site_name, text)
110 print ' <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />'
112 print ' <meta name="robots" content="noindex,nofollow" />'
114 print ' <link rel="stylesheet" type="text/css" href="%s" />' % css_url
118 if name and privileged_url is not None:
119 print '<body ondblclick="location.href=\'' + privileged_path() + '?edit=' + name + '\'">'
124 send_guru(msg, msg_type)
127 print '<div class="nav">'
128 print ' <b>' + site_name + ': ',
130 print link_tag('?fullsearch=' + name, text) + '</b> '
133 print ' | ' + link_tag('FrontPage', 'Front Page', 'navlink')
134 print ' | ' + link_tag('FindPage', 'Find Page', 'navlink')
135 print ' | <a href="' + history_url + '" class="navlink">Recent Changes</a>'
138 print ' | <a href="' + history_url + '?a=history;f=' + name + '" class="navlink">Page History</a>'
139 print ' | ' + link_tag('?raw=' + name, 'Raw Text', 'navlink')
140 if privileged_url is not None:
141 print ' | ' + link_tag('?edit=' + name, 'Edit Page', 'navlink', authentication=True)
143 print ' | <i>Immutable Page</i>'
146 if user != 'AnonymousCoward':
147 print ' | <span class="login"><i>logged in as <b>' + cgi.escape(user) + '</b></i></span>'
151 def link_tag(params, text=None, ss_class=None, authentication=False):
153 text = params # default
156 classattr += 'class="%s" ' % ss_class
157 # Prevent crawlers from following links potentially added by spammers or to generated pages
158 if ss_class == 'external' or ss_class == 'navlink':
159 classattr += 'rel="nofollow" '
161 path = privileged_path()
164 return '<a %shref="%s/%s">%s</a>' % (classattr, path, params, text)
166 # Search ---------------------------------------------------
168 def do_fullsearch(needle):
169 send_title(None, 'Full text search for "%s"' % (needle))
171 needle_re = re.compile(needle, re.IGNORECASE)
173 all_pages = page_list()
174 for page_name in all_pages:
175 body = Page(page_name).get_raw_body()
176 count = len(needle_re.findall(body))
178 hits.append((count, page_name))
180 # The default comparison for tuples compares elements in order,
181 # so this sorts by number of hits
186 for (count, page_name) in hits:
187 print '<li><p>' + Page(page_name).link_to()
188 print ' . . . . ' + `count`
189 print ['match', 'matches'][count <> 1]
193 print_search_stats(len(hits), len(all_pages))
196 def do_titlesearch(needle):
197 # TODO: check needle is legal -- but probably we can just accept any RE
198 send_title(None, "Title search for \"" + needle + '"')
200 needle_re = re.compile(needle, re.IGNORECASE)
201 all_pages = page_list()
202 hits = filter(needle_re.search, all_pages)
205 for filename in hits:
206 print '<li><p>' + Page(filename).link_to() + "</p></li>"
209 print_search_stats(len(hits), len(all_pages))
212 def print_search_stats(hits, searched):
213 print "<p>%d hits out of %d pages searched.</p>" % (hits, searched)
215 def do_edit(pagename):
216 Page(pagename).send_editor()
218 def do_raw(pagename):
219 Page(pagename).send_raw()
221 def do_savepage(pagename):
224 if 'preview' in form:
225 pg.send_editor(form['savetext'].value)
227 pg.save_text(form['savetext'].value)
229 elif 'cancel' in form:
230 pg.msg = 'Editing canceled'
231 pg.msg_type = 'notice'
234 raise 'What did you press?'
236 def make_index_key():
238 links = map(lambda ch: '<a href="#%s">%s</a>' % (ch, ch),
240 s = s + string.join(links, ' | ')
241 s = s + '</center></p>'
246 return filter(word_re.match, os.listdir(text_dir))
249 def send_footer(name, mod_string=None):
251 cgi.print_arguments()
252 cgi.print_form(cgi.FieldStorage())
254 print '<div id="footer"><hr />'
255 print '<p class="copyright">Powered by <a href="http://www.codewiz.org/wiki/GeekiGeeki">GeekiGeeki</a></p>'
257 print '<p class="modified">last modified %s</p>' % mod_string
258 print '</div></body></html>'
261 # ----------------------------------------------------------
263 def _macro_TitleSearch(*vargs):
264 return _macro_search("titlesearch")
266 def _macro_FullSearch(*vargs):
267 return _macro_search("fullsearch")
269 def _macro_search(type):
270 if form.has_key('value'):
271 default = form["value"].value
274 return """<form method="get"><input name="%s" size="30" value="%s" /><input type="submit" value="Search" /></form>""" % (type, default)
276 def _macro_WordIndex(*vargs):
278 pages = list(page_list())
280 word_re = re.compile('[A-Z][a-z]+')
282 for word in word_re.findall(name):
284 map[word].append(name)
288 all_words = map.keys()
292 for word in all_words:
293 letter = string.lower(word[0])
294 if letter <> last_letter:
295 s = s + '; <a name="%s"><h3>%s</h3></a>' % (letter, letter)
298 s = s + '<b>%s</b><ul>' % word
303 if name == last_page: continue
304 s = s + '<li>' + Page(name).link_to()
309 def _macro_TitleIndex(*vargs):
311 pages = list(page_list())
313 current_letter = None
315 letter = string.lower(name[0])
316 if letter <> current_letter:
317 s = s + '<a name="%s"><h3>%s</h3></a>' % (letter, letter)
318 current_letter = letter
321 s = s + Page(name).link_to()
325 # ----------------------------------------------------------
327 """Object that turns Wiki markup into HTML.
329 All formatting commands can be parsed one line at a time, though
330 some state is carried over between lines.
332 def __init__(self, raw):
334 self.is_em = self.is_b = 0
337 self.list_indents = []
339 self.in_table = False
342 self.in_header = True
344 def _emph_repl(self, word):
346 self.is_b = not self.is_b
347 return ['</strong>', '<strong>'][self.is_b]
349 self.is_em = not self.is_em
350 return ['</em>', '<em>'][self.is_em]
352 def _tit_repl(self, word):
354 result = '</h%d>' % self.h_level
357 self.h_level = len(word) - 1
359 result = '<h%d id="%d"><a class="heading" href="#%d">*</a> ' % (self.h_level, self.h_count, self.h_count)
362 def _rule_repl(self, word):
363 return self._undent() + '\n<hr size="%d" noshade="noshade" />\n' % (len(word) - 2)
365 def _word_repl(self, word):
366 return Page(word).link_to()
368 def _img_repl(self, word):
369 path = script_name() + '/' + word;
370 return '<a href="%s"><img border="0" src="%s" /></a>' % (path, path)
372 def _url_repl(self, word):
373 if img_re.match(word):
374 return '<a href="%s"><img border="0" src="%s" /></a>' % (word, word)
376 return '<a href="%s" rel="nofollow" class="external">%s</a>' % (word, word)
378 def _hurl_repl(self, word):
379 m = re.compile("\[\[(\S+)([^\]]*)\]\]").match(word)
381 descr = m.group(2).strip() or name
383 macro = globals().get('_macro_' + name)
385 return apply(macro, (name, descr))
386 elif img_re.match(name):
387 return '<a href="%s"><img border="0" src="%s" alt="%s" /></a>' % (name, name, descr)
388 elif url_re.match(name):
389 return '<a href="%s" rel="nofollow" class="external">%s</a>' % (name, descr)
390 elif name.startswith('/'):
391 return '<a href="%s">%s</a>' % (name, descr)
393 return link_tag(name, descr)
395 def _email_repl(self, word):
396 return '<a href="mailto:%s">%s</a>' % (word, word)
398 def _html_repl(self, word):
399 return word; # Pass through
401 def _ent_repl(self, s):
402 return {'&': '&',
406 def _li_repl(self, match):
409 def _pre_repl(self, word):
410 if word == '{{{' and not self.in_pre:
418 def _hi_repl(self, word):
425 return '<strong class="highlight ' + cl + '">' + word + '</strong>'
427 def _var_repl(self, word):
428 if word == '{{' and not self.in_var:
436 def _tr_repl(self, word):
438 if not self.in_table:
441 out = '</p><table><tbody>\n'
443 return out + '<tr class="' + ['even', 'odd'][self.tr_cnt % 2] + '"><td>'
445 def _tre_repl(self, word):
450 def _td_repl(self, word):
455 def _indent_level(self):
456 return len(self.list_indents) and self.list_indents[-1]
458 def _indent_to(self, new_level):
459 if self._indent_level() == new_level:
462 while self._indent_level() > new_level:
463 del(self.list_indents[-1])
465 while self._indent_level() < new_level:
466 self.list_indents.append(new_level)
473 res += '</ul>' * len(self.list_indents)
475 self.list_indents = []
478 def replace(self, match):
479 for type, hit in match.groupdict().items():
481 return apply(getattr(self, '_' + type + '_repl'), (hit,))
483 raise "Can't handle match " + `match`
485 def print_html(self):
486 print '<div class="wiki"><p>'
488 # For each line, we scan through looking for magic
489 # strings, outputting verbatim any intervening text
490 # TODO: highlight search words (look at referer)
491 scan_re = re.compile(
494 + r"(?P<emph>'{2,3})"
495 + r"|(?P<tit>\={2,6})"
496 + r"|(?P<rule>^-{3,})"
497 + r"|(?P<html><(/|)(div|span|iframe)[^<>]*>)"
499 + r"|(?P<hi>\b(FIXME|TODO|DONE)\b)"
502 + r"|(?P<img>\b[a-zA-Z0-9_-]+\.(png|gif|jpg|jpeg|bmp))"
503 + r"|(?P<word>\b(?:[A-Z][a-z]+){2,}\b)"
504 + r"|(?P<hurl>\[\[\S+[^\]]*\]\])"
505 + r"|(?P<url>(http|https|ftp|mailto)\:[^\s'\"]+\S)"
506 + r"|(?P<email>[-\w._+]+\@[\w.-]+)"
510 + r"|(?P<pre>(\{\{\{|\s*\}\}\}))"
511 + r"|(?P<var>(\{\{|\}\}))"
514 + r"|(?P<tr>^\s*\|\|\s*)"
515 + r"|(?P<tre>\s*\|\|\s*$)"
516 + r"|(?P<td>\s*\|\|\s*)"
520 + r"(?P<pre>\s*\}\}\})"
522 blank_re = re.compile(r"^\s*$")
523 indent_re = re.compile(r"^\s*")
524 tr_re = re.compile(r"^\s*\|\|")
525 eol_re = re.compile(r"\r?\n")
526 raw = string.expandtabs(self.raw)
527 for line in eol_re.split(raw):
530 if line.startswith('#'):
532 self.in_header = False
535 print re.sub(pre_re, self.replace, line)
537 if self.in_table and not tr_re.match(line):
538 self.in_table = False
539 print '</tbody></table><p>'
541 if blank_re.match(line):
544 indent = indent_re.match(line)
545 print self._indent_to(len(indent.group(0)))
546 print re.sub(scan_re, self.replace, line)
548 if self.in_pre: print '</pre>'
549 if self.in_table: print '</tbody></table><p>'
553 # ----------------------------------------------------------
555 def __init__(self, page_name):
556 self.page_name = page_name
558 self.msg_type = 'error'
561 def split_title(self):
562 # look for the end of words and the start of a new word,
563 # and insert a space there
564 return re.sub('([a-z])([A-Z])', r'\1 \2', self.page_name)
566 def _text_filename(self):
567 return path.join(text_dir, self.page_name)
569 def _tmp_filename(self):
570 return path.join(text_dir, ('#' + self.page_name + '.' + `os.getpid()` + '#'))
574 os.stat(self._text_filename())
577 if er.errno == errno.ENOENT:
583 word = self.page_name
585 return link_tag(word, word, 'wikilink')
588 return link_tag(word, '?', 'nonexistent') + word
590 return link_tag(word, word, 'nonexistent')
592 def get_raw_body(self):
594 return open(self._text_filename(), 'rt').read()
596 if er.errno == errno.ENOENT:
597 # just doesn't exist, use default
598 return 'Describe %s here.' % self.page_name
606 file = open(self._text_filename(), 'rt')
607 attr_re = re.compile(r"^#(\S*)(.*)$")
609 m = attr_re.match(line)
612 self.attrs[m.group(1)] = m.group(2).strip()
613 #print "bernie: attrs[" + m.group(1) + "] = " + m.group(2) + "<br>\n"
615 if er.errno != errno.ENOENT:
619 def can(self, action, default=True):
620 attrs = self.get_attrs()
622 # SomeUser:read,write All:read
624 for rule in acl.split():
625 (user,perms) = rule.split(':')
626 if user == remote_user() or user == "All":
627 if action in perms.split(','):
632 except Exception, er:
637 return self.can("write", True)
640 return self.can("read", True)
645 page_name = self.page_name
646 send_title(page_name, self.split_title(), msg=self.msg, msg_type=self.msg_type)
648 PageFormatter(self.get_raw_body()).print_html()
650 send_guru("Read access denied by ACLs", "notice")
651 send_footer(page_name, self._last_modified())
653 def _last_modified(self):
654 if not self.exists():
656 from time import localtime, strftime
657 modtime = localtime(os.stat(self._text_filename())[stat.ST_MTIME])
658 return strftime(datetime_fmt, modtime)
660 def send_editor(self, preview=None):
661 send_title(None, 'Edit ' + self.split_title(), msg=self.msg, msg_type=self.msg_type)
662 if not self.can_write():
663 send_guru("Write access denied by ACLs", "error")
666 print ('<p><b>Editing ' + self.page_name
667 + ' for ' + cgi.escape(remote_user())
668 + ' from ' + cgi.escape(get_hostname(remote_host()))
670 print '<div class="editor"><form method="post" action="%s/%s">' % (script_name(), self.page_name)
671 print '<input type="hidden" name="savepage" value="%s">' % (self.page_name)
672 print """<textarea wrap="virtual" id="editor" name="savetext" rows="17" cols="80">%s</textarea>""" % (preview or self.get_raw_body())
675 <input type="submit" name="save" value="Save" />
676 <input type="submit" name="preview" value="Preview" />
677 <input type="reset" value="Reset" />
678 <input type="submit" name="cancel" value="Cancel" />
681 print "<p>" + Page('EditingTips').link_to() + "</p>"
683 print "<div class='preview'>"
684 PageFormatter(preview).print_html()
686 send_footer(self.page_name)
689 if not self.can_read():
690 send_title(None, msg='Read access denied by ACLs', msg_type='notice')
692 emit_header("text/plain")
693 print self.get_raw_body()
695 def _write_file(self, text):
696 tmp_filename = self._tmp_filename()
697 open(tmp_filename, 'wt').write(text.replace('\r\n', '\n'))
698 text = self._text_filename()
700 # Bad Bill! POSIX rename ought to replace. :-(
704 if er.errno <> errno.ENOENT: raise er
705 os.rename(tmp_filename, text)
707 def save_text(self, newtext):
708 if not self.can_write():
709 self.msg = 'Write access denied by ACLs'
710 self.msg_type = 'error'
713 self._write_file(newtext)
716 # FIXME: what's the std way to perform shell quoting in python?
717 cmd = ( post_edit_hook
718 + " '" + text_dir + '/' + self.page_name
719 + "' '" + remote_user()
720 + "' '" + remote_host() + "'"
726 self.msg += "Post-editing hook returned %d.\n" % rc
727 self.msg += 'Command was: ' + cmd + '\n'
729 self.msg += 'Output follows:\n' + msg
731 self.msg = 'Thank you for your contribution. Your attention to detail is appreciated.'
732 self.msg_type = 'success'
734 def send_verbatim(filename, mime_type='application/octet-stream'):
735 pathname = path.join(text_dir, filename)
736 data = open(pathname, 'rb').read()
737 emit_header(mime_type)
738 sys.stdout.write(data)
740 # Main ---------------------------------------------------------------
742 # Configuration values
743 site_name = 'Codewiz'
745 # set to None for read-only sites, leave empty ('') to allow anonymous edits
746 # otherwise, set to a URL that requires authentication
747 privileged_url = 'https://www.codewiz.org/~bernie/wiki'
749 data_dir = '/home/bernie/public_html/wiki'
750 text_dir = path.join(data_dir, 'text')
751 css_url = '../wikidata/geekigeeki.css' # optional stylesheet link
752 history_url = '../wikigit/wiki.git'
753 post_edit_hook = './post_edit_hook.sh'
754 datetime_fmt = '%a %d %b %Y %I:%M %p'
755 allow_edit = True # Is it possible to edit pages?
756 show_hosts = True # show hostnames?
757 nonexist_qm = False # show '?' for nonexistent?
758 debug_cgi = False # Set to True for CGI var dump
760 form = cgi.FieldStorage()
762 handlers = { 'fullsearch': do_fullsearch,
763 'titlesearch': do_titlesearch,
766 'savepage': do_savepage }
768 for cmd in handlers.keys():
769 if form.has_key(cmd):
770 apply(handlers[cmd], (form[cmd].value,))
773 path_info = environ.get('PATH_INFO', '')
774 if len(path_info) and path_info[0] == '/':
775 query = path_info[1:] or 'FrontPage'
777 query = environ.get('QUERY_STRING', '') or 'FrontPage'
779 if file_re.match(query):
780 if word_re.match(query):
781 Page(query).send_page()
782 elif img_re.match(query):
783 #FIXME: use correct mime type
784 send_verbatim(query, 'image/jpeg')
789 send_title(None, msg='Can\'t work out query: ' + query)
792 msg=traceback.format_exc()
794 send_guru(msg, "error")
796 send_title(None, msg=msg)