#! /usr/bin/env python
-"""Quick-quick implementation of WikiWikiWeb in Python
-"""
#
-# Copyright (C) 1999, 2000 Martin Pool <mbp@humbug.org.au>
-# This version includes additional changes by Gerardo Poggiali (2002)
-# This version includes additional changes by Bernardo Innocenti (2007)
+# Copyright 1999, 2000 Martin Pool <mbp@humbug.org.au>
+# Copyright 2002 Gerardo Poggiali
+# Copyright 2007 Bernardo Innocenti <bernie@codewiz.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-__version__ = '$Revision: 1.63+gerry+bernie $'[11:-2]
+__version__ = '$Id$'[4:12]
-import cgi, sys, string, os, re, errno, time, stat
+from time import clock
+start_time = clock()
+
+import cgi, sys, string, os, re, errno, stat
from os import path, environ
# Regular expression defining a WikiWord
# Head
emit_header()
- print """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-"""
+ print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
+ print ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
+ print '<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">'
+
+ site_name = globals().get('site_name', 'Unconfigured Site')
print "<head><title>%s: %s</title>" % (site_name, text)
print ' <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />'
if not name:
print ' <meta name="robots" content="noindex,nofollow" />'
- if css_url:
+ if globals().has_key('css_url'):
print ' <link rel="stylesheet" type="text/css" href="%s" />' % css_url
print '</head>'
print text + '</b> '
print ' | ' + link_tag('FrontPage', 'Front Page', 'navlink')
print ' | ' + link_tag('FindPage', 'Find Page', 'navlink')
- print ' | <a href="' + history_url + '" class="navlink">Recent Changes</a>'
+ if globals().has_key('history_url'):
+ print ' | <a href="' + history_url + '" class="navlink">Recent Changes</a>'
+ if name:
+ print ' | <a href="' + history_url + '?a=history;f=' + name + '" class="navlink">Page History</a>'
if name:
- print ' | <a href="' + history_url + '?a=history;f=' + name + '" class="navlink">Page History</a>'
print ' | ' + link_tag('?raw=' + name, 'Raw Text', 'navlink')
if privileged_url is not None:
print ' | ' + link_tag('?edit=' + name, 'Edit Page', 'navlink', authentication=True)
for (count, page_name) in hits:
print '<li><p>' + Page(page_name).link_to()
print ' . . . . ' + `count`
- print ['match', 'matches'][count <> 1]
+ print ['match', 'matches'][count != 1]
print '</p></li>'
print "</ul>"
print_search_stats(len(hits), len(all_pages))
-
def do_titlesearch(needle):
# TODO: check needle is legal -- but probably we can just accept any RE
send_title(None, "Title search for \"" + needle + '"')
print_search_stats(len(hits), len(all_pages))
-
def print_search_stats(hits, searched):
print "<p>%d hits out of %d pages searched.</p>" % (hits, searched)
+#TODO: merge into do_savepage()
def do_edit(pagename):
Page(pagename).send_editor()
Page(pagename).send_raw()
def do_savepage(pagename):
- if privileged_url is None:
- raise 'editing disallowed for ' + pagename
-
global form
pg = Page(pagename)
if 'preview' in form:
pg.save_text(form['savetext'].value)
pg.send_page()
elif 'cancel' in form:
- pg.msg = 'Editing cancelled'
+ pg.msg = 'Editing canceled'
pg.msg_type = 'notice'
pg.send_page()
else:
s = s + '</center></p>'
return s
-
def page_list():
return filter(word_re.match, os.listdir(text_dir))
-
def send_footer(name, mod_string=None):
- if debug_cgi:
+ if globals().get('debug_cgi', False):
cgi.print_arguments()
cgi.print_form(cgi.FieldStorage())
cgi.print_environ()
+ global __version__
print '<div id="footer"><hr />'
- print '<p class="copyright">Powered by <a href="http://www.codewiz.org/wiki/GeekiGeeki">GeekiGeeki</a></p>'
+ print ('<p class="copyright"><span class="benchmark">generated in %0.3fs</span>' +
+ ' by <a href="http://www.codewiz.org/wiki/GeekiGeeki">GeekiGeeki</a>' +
+ ' version %s</p>') % (clock() - start_time, __version__)
if mod_string:
print '<p class="modified">last modified %s</p>' % mod_string
print '</div></body></html>'
-
# ----------------------------------------------------------
# Macros
def _macro_TitleSearch(*vargs):
# set title
for word in all_words:
letter = string.lower(word[0])
- if letter <> last_letter:
+ if letter != last_letter:
s = s + '; <a name="%s"><h3>%s</h3></a>' % (letter, letter)
last_letter = letter
current_letter = None
for name in pages:
letter = string.lower(name[0])
- if letter <> current_letter:
+ if letter != current_letter:
s = s + '<a name="%s"><h3>%s</h3></a>' % (letter, letter)
current_letter = letter
else:
def exists(self):
try:
os.stat(self._text_filename())
- return 1
+ return True
except OSError, er:
if er.errno == errno.ENOENT:
- return 0
+ return False
else:
raise er
if self.exists():
return link_tag(word, word, 'wikilink')
else:
- if nonexist_qm:
- return link_tag(word, '?', 'nonexistent') + word
- else:
- return link_tag(word, word, 'nonexistent')
-
+ return link_tag(word, nonexist_pfx + word, 'nonexistent')
def get_raw_body(self):
try:
return open(self._text_filename(), 'rt').read()
except IOError, er:
if er.errno == errno.ENOENT:
- # just doesn't exist, use default
- return 'Describe %s here.' % self.page_name
- else:
- raise er
+ return '' # just doesn't exist, use default
+ raise er
def get_attrs(self):
if self.attrs:
if self.can_read():
PageFormatter(self.get_raw_body()).print_html()
else:
- print "<p>This page is not visible to you</p>"
+ send_guru("Read access denied by ACLs", "notice")
send_footer(page_name, self._last_modified())
def _last_modified(self):
- if not self.exists():
+ try:
+ from time import localtime, strftime
+ modtime = localtime(os.stat(self._text_filename())[stat.ST_MTIME])
+ except OSError, er:
+ if er.errno != errno.ENOENT:
+ raise er
return None
- from time import localtime, strftime
- modtime = localtime(os.stat(self._text_filename())[stat.ST_MTIME])
return strftime(datetime_fmt, modtime)
def send_editor(self, preview=None):
send_title(None, 'Edit ' + self.split_title(), msg=self.msg, msg_type=self.msg_type)
+ if not self.can_write():
+ send_guru("Write access denied by ACLs", "error")
+ return
print ('<p><b>Editing ' + self.page_name
+ ' for ' + cgi.escape(remote_user())
send_footer(self.page_name)
def send_raw(self):
+ if not self.can_read():
+ send_title(None, msg='Read access denied by ACLs', msg_type='notice')
+ return
emit_header("text/plain")
print self.get_raw_body()
try:
os.remove(text)
except OSError, er:
- if er.errno <> errno.ENOENT: raise er
+ if er.errno != errno.ENOENT: raise er
os.rename(tmp_filename, text)
def save_text(self, newtext):
+ if not self.can_write():
+ self.msg = 'Write access denied by ACLs'
+ self.msg_type = 'error'
+ return
+
self._write_file(newtext)
rc = 0
if post_edit_hook:
if msg:
self.msg += 'Output follows:\n' + msg
else:
- self.msg = 'Thankyou for your contribution. Your attention to detail is appreciated.'
+ self.msg = 'Thank you for your contribution. Your attention to detail is appreciated.'
self.msg_type = 'success'
+#TODO: merge into send_raw()
def send_verbatim(filename, mime_type='application/octet-stream'):
pathname = path.join(text_dir, filename)
data = open(pathname, 'rb').read()
# Main ---------------------------------------------------------------
try:
- # Configuration values
- site_name = 'Codewiz'
-
- # set to None for read-only sites, leave empty ('') to allow anonymous edits
- # otherwise, set to a URL that requires authentication
- privileged_url = 'https://www.codewiz.org/~bernie/wiki'
-
- data_dir = '/home/bernie/public_html/wiki'
- text_dir = path.join(data_dir, 'text')
- css_url = '../wikidata/geekigeeki.css' # optional stylesheet link
- history_url = '../wikigit/wiki.git'
- post_edit_hook = './post_edit_hook.sh'
- datetime_fmt = '%a %d %b %Y %I:%M %p'
- allow_edit = True # Is it possible to edit pages?
- show_hosts = True # show hostnames?
- nonexist_qm = False # show '?' for nonexistent?
- debug_cgi = False # Set to True for CGI var dump
+ execfile("geekigeeki.conf.py")
form = cgi.FieldStorage()
else:
send_verbatim(query)
else:
- # TODO: return 404?
+ print "Status: 404 Not Found"
send_title(None, msg='Can\'t work out query: ' + query)
except:
import traceback