2 # -*- coding: utf-8 -*-
4 # Copyright 1999, 2000 Martin Pool <mbp@humbug.org.au>
5 # Copyright 2002 Gerardo Poggiali
6 # Copyright 2007, 2008, 2009 Bernie 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, os, re, errno, stat
28 # Regular expression defining a WikiWord
29 # (but this definition is also assumed in other places)
30 word_re = re.compile(r"^\b((([A-Z][a-z0-9]+){2,}/)*([A-Z][a-z0-9]+){2,})\b$")
31 # FIXME: we accept stuff like foo/../bar and we shouldn't
32 file_re = re.compile(r"^\b([A-Za-z0-9_\-][A-Za-z0-9_\.\-/]*)\b$")
33 img_re = re.compile(r"^.*\.(png|gif|jpg|jpeg|bmp|ico|ogm|ogg|mkv|mpg|mpeg|mp4|avi|asf|flv|wmv|qt)$", re.IGNORECASE)
34 video_re = re.compile(r"^.*\.(ogm|ogg|mkv|mpg|mpeg|mp4|avi|asf|flv|wmv|qt)$", re.IGNORECASE)
35 url_re = re.compile(r"^[a-z]{3,8}://[^\s'\"]+\S$")
36 link_re = re.compile(r"(?:\[\[|{{)([^\s\|]+)(?:\s*\|\s*([^\]]+)|)(?:\]\]|}})")
37 ext_re = re.compile(r"\.([^\./]+)$")
41 # CGI stuff ---------------------------------------------------------
43 return os.environ.get('SCRIPT_NAME', '')
46 path_info = os.environ.get('PATH_INFO', '')
47 if len(path_info) and path_info[0] == '/':
48 return path_info[1:] or 'FrontPage'
50 return os.environ.get('QUERY_STRING', '') or 'FrontPage'
52 def privileged_path():
53 return privileged_url or script_name()
56 user = os.environ.get('REMOTE_USER', '')
57 if user is None or user == '' or user == 'anonymous':
58 user = 'AnonymousCoward'
62 return os.environ.get('REMOTE_ADDR', '')
64 def get_hostname(addr):
66 from socket import gethostbyaddr
67 return gethostbyaddr(addr)[0] + ' (' + addr + ')'
71 def is_external_url(pathname):
72 return (url_re.match(pathname) or pathname.startswith('/'))
74 def relative_url(pathname, privileged=False):
75 if not is_external_url(pathname):
77 url = privileged_path()
80 pathname = url + '/' + pathname
81 return cgi.escape(pathname, quote=True)
84 return re.sub(' ', '-', re.sub('[^a-z0-9_ ]', '', s.lower()).strip())
86 # Formatting stuff --------------------------------------------------
87 def emit_header(mime_type="text/html"):
88 print "Content-type: " + mime_type + "; charset=utf-8\n"
90 def send_guru(msg_text, msg_type):
91 if not msg_text: return
92 print '<pre id="guru" onclick="this.style.display = \'none\'" class="' + msg_type + '">'
93 if msg_type == 'error':
94 print ' Software Failure. Press left mouse button to continue.\n'
96 if msg_type == 'error':
97 print '\n Guru Meditation #DEADBEEF.ABADC0DE'
99 print '<script language="JavaScript" type="text/javascript" src="%s" defer="defer"></script>' \
100 % relative_url('sys/GuruMeditation.js')
102 def send_title(name, text="Limbo", msg_text=None, msg_type='error', writable=False):
104 if title_done: return
108 print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
109 print ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
110 print '<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">'
112 print "<head><title>%s: %s</title>" % (site_name, text)
113 print ' <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />'
115 print ' <meta name="robots" content="noindex,nofollow" />'
117 for meta in meta_urls:
118 http_equiv, content = meta
119 print ' <meta http-equiv="%s" content="%s" />' % (http_equiv, relative_url(content))
121 for link in link_urls:
123 print ' <link rel="%s" href="%s" />' % (rel, relative_url(href))
125 if name and writable and privileged_url is not None:
126 print ' <link rel="alternate" type="application/x-wiki" title="Edit this page" href="%s" />' \
127 % (privileged_path() + '?edit=' + name)
129 if history_url is not None:
130 print ' <link rel="alternate" type="application/rss+xml" title="RSS" href="%s" />' \
131 % relative_url(history_url + '?a=rss')
136 if name and writable and privileged_url is not None:
137 print '<body ondblclick="location.href=\'' + privileged_path() + '?edit=' + name + '\'">'
142 send_guru(msg_text, msg_type)
145 print '<div class="nav">'
146 print link_tag('FrontPage', site_icon or 'Home', 'navlink')
148 print ' <b>' + link_tag('?fullsearch=' + name, text, 'navlink') + '</b> '
150 print ' <b>' + text + '</b> '
151 print ' | ' + link_tag('FindPage', 'Find Page', 'navlink')
152 if 'history_url' in globals():
153 print ' | <a href="' + relative_url(history_url) + '" class="navlink">Recent Changes</a>'
155 print ' | <a href="' + relative_url(history_url + '?a=history;f=' + name) + '" class="navlink">Page History</a>'
158 print ' | ' + link_tag('?raw=' + name, 'Raw Text', 'navlink')
159 if privileged_url is not None:
161 print ' | ' + link_tag('?edit=' + name, 'Edit', 'navlink', privileged=True)
163 print ' | ' + link_tag(name, 'Login', 'navlink', privileged=True)
166 print ' | <i>Immutable Page</i>'
169 if user != 'AnonymousCoward':
170 print ' | <span class="login"><i><b>' + link_tag('User/' + user, user) + '</b></i></span>'
174 def send_httperror(status="403 Not Found", query=""):
175 print "Status: %s" % status
176 send_title(None, msg_text=("%s: on query '%s'" % (status, query)))
179 def link_tag(params, text=None, link_class=None, privileged=False):
181 text = params # default
182 elif img_re.match(text):
183 text = '<img border="0" src="' + relative_url(text) + '" alt="' + text + '" />'
186 if is_external_url(params):
187 link_class = 'external'
188 elif file_re.match(params) and Page(params).exists():
189 link_class = 'wikilink'
191 params = nonexist_pfx + params
192 link_class = 'nonexistent'
194 classattr = 'class="%s" ' % link_class
195 # Prevent crawlers from following links potentially added by spammers or to generated pages
196 if link_class == 'external' or link_class == 'navlink':
197 classattr += 'rel="nofollow"'
199 return '<a %shref="%s">%s</a>' % (classattr, relative_url(params, privileged=privileged), text)
201 def link_inline(name, descr=None, args=''):
202 if not descr: descr = name
203 url = relative_url(name)
204 if video_re.match(name):
205 return '<video src="%s">Your browser does not support the HTML5 video tag</video>' % url
206 elif img_re.match(name):
207 return '<a href="%s"><img border="0" src="%s" alt="%s" /></a>' % (url, url + args, descr)
208 elif file_re.match(name) and not ext_re.search(name): # FIXME: this guesses a wiki page
209 return Page(name).send_naked()
211 return '<iframe width="100%%" scrolling="auto" frameborder="0" src="%s"><a href="%s">%s</a></iframe>' \
214 # Search ---------------------------------------------------
216 def handle_fullsearch(needle):
217 send_title(None, 'Full text search for "%s"' % (needle))
219 needle_re = re.compile(needle, re.IGNORECASE)
221 all_pages = page_list()
222 for page_name in all_pages:
223 body = Page(page_name).get_raw_body()
224 count = len(needle_re.findall(body))
226 hits.append((count, page_name))
228 # The default comparison for tuples compares elements in order,
229 # so this sorts by number of hits
234 for (count, page_name) in hits:
235 print '<li><p>' + link_tag(page_name)
236 print ' . . . . ' + `count`
237 print ['match', 'matches'][count != 1]
241 print_search_stats(len(hits), len(all_pages))
243 def handle_titlesearch(needle):
244 # TODO: check needle is legal -- but probably we can just accept any RE
245 send_title(None, "Title search for \"" + needle + '"')
247 needle_re = re.compile(needle, re.IGNORECASE)
248 all_pages = page_list()
249 hits = filter(needle_re.search, all_pages)
252 for filename in hits:
253 print '<li><p>' + link_tag(filename) + "</p></li>"
256 print_search_stats(len(hits), len(all_pages))
258 def print_search_stats(hits, searched):
259 print "<p>%d hits out of %d pages searched.</p>" % (hits, searched)
261 def handle_raw(pagename):
262 if not file_re.match(pagename):
263 send_httperror("403 Forbidden", pagename)
266 Page(pagename).send_raw()
268 def handle_edit(pagename):
269 if not file_re.match(pagename):
270 send_httperror("403 Forbidden", pagename)
275 if form['file'].value:
276 pg.save(form['file'].file.read(), form['changelog'].value)
278 pg.save(form['savetext'].value.replace('\r\n', '\n'), form['changelog'].value)
280 elif 'cancel' in form:
281 pg.msg_text = 'Editing canceled'
282 pg.msg_type = 'notice'
284 else: # preview or edit
286 if 'preview' in form:
287 text = form['savetext'].value
290 # Used by macros/WordIndex and macros/TitleIndex
291 def make_index_key():
292 links = map(lambda ch: '<a href="#%s">%s</a>' % (ch, ch), 'abcdefghijklmnopqrstuvwxyz')
293 return '<p style="text-align: center">'+ ' | '.join(links) + '</p>'
295 def page_list(dirname = None, re = word_re):
296 return sorted(filter(re.match, os.listdir(dirname or data_dir)))
298 def send_footer(mod_string=None):
299 if globals().get('debug_cgi', False):
300 cgi.print_arguments()
304 <div id="footer"><hr />
305 <p class="copyright">
306 <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/"><img class="license" alt="Creative Commons License" src="%s" /></a>
307 <span class="benchmark">generated in %0.3fs</span> by <a href="http://www.codewiz.org/wiki/GeekiGeeki">GeekiGeeki</a> version %s
309 ''' % (relative_url('cc-by-sa.png'), clock() - start_time, __version__)
311 print '<p class="modified">last modified %s</p>' % mod_string
312 print '</div></body></html>'
315 """Object that turns Wiki markup into HTML.
317 All formatting commands can be parsed one line at a time, though
318 some state is carried over between lines.
320 def __init__(self, raw):
323 self.in_pre = self.in_html = self.in_table = self.in_li = False
324 self.in_header = True
325 self.list_indents = []
333 "^^": ["sup", False],
334 ",,": ["sub", False],
335 "''": ["em", False], # LEGACY
336 "'''": ["b", False], # LEGACY
337 "``": ["tt", False], # LEGACY
340 def _b_repl(self, word):
341 style = self.styles[word]
342 style[1] = not style[1]
343 return ['</', '<'][style[1]] + style[0] + '>'
345 def _tit_repl(self, word):
347 result = '</h%d><p>\n' % self.h_level
350 self.h_level = len(word) - 1
351 link = permalink(self.line)
352 result = '\n</p><h%d id="%s"><a class="heading" href="#%s">¶</a> ' % (self.h_level, link, link)
355 def _br_repl(self, word):
358 def _rule_repl(self, word):
359 return self._undent() + '\n<hr size="%d" noshade="noshade" />\n' % (len(word) - 2)
361 def _macro_repl(self, word):
362 m = re.compile("\<\<([^\s\|\>]+)(?:\s*\|\s*([^\>]+)|)\>\>").match(word)
366 argv.extend(m.group(2).split('|'))
367 argv = map(str.strip, argv)
369 macro = globals().get('_macro_' + name)
372 execfile("macros/" + name + ".py", globals())
374 if err.errno == errno.ENOENT: pass
375 macro = globals().get('_macro_' + name)
379 msg = '<<' + '|'.join(argv) + '>>'
381 msg = '<strong class="error">' + msg + '</strong>'
384 def _hurl_repl(self, word):
385 m = link_re.match(word)
386 return link_tag(m.group(1), m.group(2))
388 def _inl_repl(self, word):
389 (name, descr) = link_re.match(word).groups()
392 argv = descr.split('|')
396 args = '?' + '&'.join(argv)
398 # The "extthumb" nonsense works around a limitation of the HTML block model
399 return '<div class="extthumb"><div class="thumb">' \
400 + link_inline(name, descr, args) \
401 + '<div class="caption">' + descr + '</div></div></div>'
403 return link_inline(name, name)
405 def _html_repl(self, word):
406 if not self.in_html and word.startswith('<div'): word = '</p>' + word
408 return word; # Pass through
410 def _htmle_repl(self, word):
412 if not self.in_html and word.startswith('</div'): word += '<p>'
413 return word; # Pass through
415 def _ent_repl(self, s):
417 return s; # Pass through
418 return {'&': '&',
422 def _img_repl(self, word): # LEGACY
423 return self._inl_repl('{{' + word + '}}')
425 def _word_repl(self, word): # LEGACY
426 if self.in_html: return word # pass through
427 return link_tag(word)
429 def _url_repl(self, word): # LEGACY
430 if self.in_html: return word # pass through
431 return link_tag(word)
433 def _email_repl(self, word): # LEGACY
434 if self.in_html: return word # pass through
435 return '<a href="mailto:%s">%s</a>' % (word, word)
437 def _li_repl(self, match):
444 def _pre_repl(self, word):
445 if word == '{{{' and not self.in_pre:
453 def _hi_repl(self, word):
454 return '<strong class="highlight ' + word + '">' + word + '</strong>'
456 def _tr_repl(self, word):
458 if not self.in_table:
461 out = '</p><table><tbody>\n'
463 out = out + '<tr class="' + ['even', 'odd'][self.tr_cnt % 2] + '">'
464 return out + ['<td>', '<th>'][word.strip() == '||=']
466 def _td_repl(self, word):
468 return ['</td><td>', '</th><th>'][word.strip() == '||=']
471 def _tre_repl(self, word):
473 return ['</td></tr>', '</th></tr>'][word.strip() == '||=']
476 def _indent_level(self):
477 return len(self.list_indents) and self.list_indents[-1]
479 def _indent_to(self, new_level):
480 if self._indent_level() == new_level:
483 while self._indent_level() > new_level:
484 del(self.list_indents[-1])
487 self.in_li = False # FIXME
489 while self._indent_level() < new_level:
490 self.list_indents.append(new_level)
497 res += '</ul>' * len(self.list_indents)
499 self.list_indents = []
502 def replace(self, match):
503 for rule, hit in match.groupdict().items():
505 return getattr(self, '_' + rule + '_repl')(hit)
507 raise "Can't handle match " + repr(match)
509 def print_html(self):
510 print '<div class="wiki"><p>'
512 scan_re = re.compile(r"""(?:
513 # Styles and formatting
514 (?P<b> \*\*|'''|//|''|\#\#|``|__|\^\^|,,)
518 | (?P<hi> \b( FIXME | TODO | DONE )\b )
521 | (?P<macro> \<\<([^\s\|\>]+)(?:\s*\|\s*([^\>]+)|)\>\>)
522 | (?P<hurl> \[\[([^\s\|]+)(?:\s*\|\s*([^\]]+)|)\]\])
525 | (?P<html> <(br|hr|div|span|form|iframe|input|textarea|a|img|h[1-5])\b )
526 | (?P<htmle> ( /\s*> | </(br|hr|div|span|form|iframe|input|textarea|a|img|h[1-5])> ) )
529 # Auto links (LEGACY)
530 | (?P<img> \b[a-zA-Z0-9_/-]+\.(png|gif|jpg|jpeg|bmp|ico|ogm|ogg|mkv|mpg|mpeg|mp4|avi|asf|flv|wmv|qt))
531 | (?P<word> \b(?:[A-Z][a-z]+){2,}\b)
532 | (?P<url> (http|https|ftp|mailto)\:[^\s'\"]+\S)
533 | (?P<email> [-\w._+]+\@[\w.-]+)
536 | (?P<li> ^\s+[\*\#]\s+)
537 | (?P<pre> \{\{\{|\s*\}\}\})
538 | (?P<inl> \{\{([^\s\|]+)(?:\s*\|\s*([^\]]+)|)\}\})
541 | (?P<tr> ^\s*\|\|(=|)\s*)
542 | (?P<tre> \s*\|\|(=|)\s*$)
543 | (?P<td> \s*\|\|(=|)\s*)
545 # TODO: highlight search words (look at referrer)
547 pre_re = re.compile("""(?:
551 blank_re = re.compile(r"^\s*$")
552 indent_re = re.compile(r"^\s*")
553 tr_re = re.compile(r"^\s*\|\|")
554 eol_re = re.compile(r"\r?\n")
556 # For each line, we scan through looking for magic strings, outputting verbatim any intervening text
557 for self.line in eol_re.split(self.raw.expandtabs()):
560 if self.line.startswith('#'):
562 self.in_header = False
565 print re.sub(pre_re, self.replace, self.line)
567 if self.in_table and not tr_re.match(self.line):
568 self.in_table = False
569 print '</tbody></table><p>'
571 if blank_re.match(self.line):
574 indent = indent_re.match(self.line)
575 print self._indent_to(len(indent.group(0))) ,
576 print re.sub(scan_re, self.replace, self.line)
578 if self.in_pre: print '</pre>'
579 if self.in_table: print '</tbody></table><p>'
584 def __init__(self, page_name):
585 self.page_name = page_name
587 self.msg_type = 'error'
589 def split_title(self):
590 # look for the end of words and the start of a new word and insert a space there
591 return re.sub('([a-z])([A-Z])', r'\1 \2', self.page_name)
594 return os.path.join(data_dir, self.page_name)
596 def _tmp_filename(self):
597 return os.path.join(data_dir, ('#' + self.page_name.replace('/','_') + '.' + `os.getpid()` + '#'))
601 os.stat(self._filename())
604 if err.errno == errno.ENOENT:
608 def get_raw_body(self, default=None):
610 return open(self._filename(), 'rb').read()
612 if err.errno == errno.ENOENT:
614 default = '//[[?edit=%s|Describe %s]]//' % (self.page_name, self.page_name)
616 if err.errno == errno.EISDIR:
617 return self.format_dir()
620 def format_dir(self):
623 for dirname in self.page_name.split('/'):
624 pathname = (pathname + '/' + dirname) if pathname else dirname
625 out += '[[' + pathname + '|' + dirname + ']]/'
628 for filename in page_list(self._filename(), file_re):
629 if img_re.match(filename):
631 maxwidth_arg = '|maxwidth=' + str(image_maxwidth)
632 out += '{{' + self.page_name + '/' + filename + '|' + filename + maxwidth_arg + '}}\n'
634 out += ' * [[' + self.page_name + '/' + filename + ']]\n'
638 if not '_pragmas' in self.__dict__:
641 f = open(self._filename(), 'rt')
642 attr_re = re.compile(r"^#(\S*)(.*)$")
644 m = attr_re.match(line)
647 self._pragmas[m.group(1)] = m.group(2).strip()
648 #print "bernie: _pragmas[" + m.group(1) + "] = " + m.group(2) + "<br>\n"
650 if err.errno != errno.ENOENT and err.errno != errno.EISDIR:
654 def pragma(self, name, default):
655 return self.pragmas().get(name, default)
657 def can(self, action, default=True):
660 #acl SomeUser:read,write All:read
661 acl = self.pragma("acl", None)
662 for rule in acl.split():
663 (user, perms) = rule.split(':')
664 if user == remote_user() or user == "All":
665 return action in perms.split(',')
669 self.msg_text = 'Illegal acl line: ' + acl
673 return self.can("write", True)
676 return self.can("read", True)
678 def send_naked(self):
680 WikiFormatter(self.get_raw_body()).print_html()
682 send_guru("Read access denied by ACLs", "notice")
686 value = self.pragma("css", None)
689 link_urls += [ [ "stylesheet", value ] ]
691 send_title(self.page_name, self.split_title(),
692 msg_text=self.msg_text, msg_type=self.msg_type, writable=self.can_write())
694 send_footer(self._last_modified())
696 def _last_modified(self):
698 from time import localtime, strftime
699 modtime = localtime(os.stat(self._filename())[stat.ST_MTIME])
701 if err.errno != errno.ENOENT:
704 return strftime(datetime_fmt, modtime)
706 def send_editor(self, preview=None):
707 send_title(None, 'Edit ' + self.split_title(), msg_text=self.msg_text, msg_type=self.msg_type)
708 if not self.can_write():
709 send_guru("Write access denied by ACLs", "error")
714 filename = form['file'].value
716 print ('<p><b>Editing ' + self.page_name
717 + ' for ' + cgi.escape(remote_user())
718 + ' from ' + cgi.escape(get_hostname(remote_host()))
720 print '<div class="editor"><form name="editform" method="post" enctype="multipart/form-data" action="%s">' % relative_url(self.page_name)
721 print '<input type="hidden" name="edit" value="%s">' % (self.page_name)
722 print '<input type="input" id="editor" name="changelog" value="Edit page %s" accesskey="c" /><br />' % (self.page_name)
723 print '<textarea wrap="off" spellcheck="true" id="editor" name="savetext" rows="17" cols="100" accesskey="e">%s</textarea>' \
724 % cgi.escape(preview or self.get_raw_body(default=''))
725 print '<label for="file" accesskey="u">Or Upload a file:</label> <input type="file" name="file" value="%s" />' % filename
728 <input type="submit" name="save" value="Save" accesskey="s">
729 <input type="submit" name="preview" value="Preview" accesskey="p" />
730 <input type="reset" value="Reset" />
731 <input type="submit" name="cancel" value="Cancel" />
734 <script language="javascript">
736 document.editform.savetext.focus()
740 print "<p>" + link_tag('EditingTips') + "</p>"
742 print "<div class='preview'>"
743 WikiFormatter(preview).print_html()
747 def send_raw(self, mimetype='text/plain', args=[]):
748 if not self.can_read():
749 send_title(None, msg_text='Read access denied by ACLs', msg_type='notice')
752 if 'maxwidth' in args:
754 emit_header(mimetype)
756 subprocess.check_call(['gm', 'convert', self._filename(),
757 '-scale', args['maxwidth'].value + ' >', '-'])
759 body = self.get_raw_body()
760 emit_header(mimetype)
763 def _write_file(self, data):
764 tmp_filename = self._tmp_filename()
765 open(tmp_filename, 'wb').write(data)
766 name = self._filename()
768 # Bad Bill! POSIX rename ought to replace. :-(
772 if err.errno != errno.ENOENT: raise err
773 os.rename(tmp_filename, name)
775 def save(self, newdata, changelog):
776 if not self.can_write():
777 self.msg_text = 'Write access denied by ACLs'
778 self.msg_type = 'error'
781 self._write_file(newdata)
785 cmd = [ post_edit_hook, data_dir + '/' + self.page_name, remote_user(), remote_host(), changelog]
786 child = subprocess.Popen(cmd, stdout=subprocess.PIPE, close_fds=True)
787 output = child.stdout.read()
790 self.msg_text += "Post-editing hook returned %d. Command was:\n'%s'\n" % (rc, "' '".join(cmd))
792 self.msg_text += 'Output follows:\n' + output
794 self.msg_text = 'Thank you for your contribution. Your attention to detail is appreciated.'
795 self.msg_type = 'success'
799 handler = globals().get('handle_' + cmd)
801 handler(form[cmd].value)
804 query = query_string()
805 if file_re.match(query):
806 # FIMXE: this is all bullshit, MimeTypes bases its guess on the extension!
807 from mimetypes import MimeTypes
808 mimetype, encoding = MimeTypes().guess_type(query)
810 Page(query).send_raw(mimetype=mimetype, args=form)
814 send_httperror("403 Forbidden", query)
817 execfile("geekigeeki.conf.py")
818 form = cgi.FieldStorage()
822 msg_text = traceback.format_exc()
824 send_guru(msg_text, "error")
826 send_title(None, msg_text=msg_text)