| 1 |
# vim: expandtab |
|---|
| 2 |
from trac.Wiki import WikiPage |
|---|
| 3 |
import trac.perm |
|---|
| 4 |
import time |
|---|
| 5 |
from StringIO import StringIO |
|---|
| 6 |
from trac.WikiFormatter import wiki_to_html |
|---|
| 7 |
from trac.util import TracError |
|---|
| 8 |
import re |
|---|
| 9 |
|
|---|
| 10 |
def execute(hdf, args, env): |
|---|
| 11 |
authname = hdf.getValue("trac.authname", "anonymous") |
|---|
| 12 |
db = env.get_db_cnx() |
|---|
| 13 |
perm = trac.perm.PermissionCache(db, authname) |
|---|
| 14 |
pagename = hdf.getValue("args.page", "WikiStart") |
|---|
| 15 |
page = WikiPage(pagename, None, perm, db) |
|---|
| 16 |
wikipreview = hdf.getValue("args.preview", "") |
|---|
| 17 |
appendonly = (args == 'appendonly') |
|---|
| 18 |
readonlypage = int(hdf.getValue("wiki.readonly", "0")) |
|---|
| 19 |
# Can this user add a comment to this page? |
|---|
| 20 |
cancomment = not readonlypage |
|---|
| 21 |
# Is this an "append-only" comment or are we an administrator? |
|---|
| 22 |
if perm.has_permission(trac.perm.WIKI_ADMIN) or appendonly: |
|---|
| 23 |
cancomment = True |
|---|
| 24 |
|
|---|
| 25 |
if not cancomment: |
|---|
| 26 |
raise TracError('Error: Insufficient privileges to AddComment') |
|---|
| 27 |
|
|---|
| 28 |
disabled = '' |
|---|
| 29 |
comment = hdf.getValue("args.addcomment", "") |
|---|
| 30 |
preview = hdf.getValue("args.previewaddcomment", "") |
|---|
| 31 |
cancel = hdf.getValue("args.canceladdcomment", "") |
|---|
| 32 |
submit = hdf.getValue("args.submitaddcomment", "") |
|---|
| 33 |
if not cancel: |
|---|
| 34 |
authname = hdf.getValue("args.authoraddcomment", authname) |
|---|
| 35 |
|
|---|
| 36 |
# Ensure [[AddComment]] is not present in comment, so that infinite |
|---|
| 37 |
# recursion does not occur. |
|---|
| 38 |
comment = re.sub('(^|[^!])(\[\[AddComment)', '\\1!\\2', comment) |
|---|
| 39 |
|
|---|
| 40 |
out = StringIO() |
|---|
| 41 |
if wikipreview or not (perm.has_permission(trac.perm.WIKI_MODIFY) or appendonly): |
|---|
| 42 |
disabled = ' disabled="disabled"' |
|---|
| 43 |
|
|---|
| 44 |
# If we are submitting or previewing, inject comment as it should look |
|---|
| 45 |
if cancomment and comment and (preview or submit): |
|---|
| 46 |
if preview: |
|---|
| 47 |
out.write("<div class='wikipage' id='preview'>\n") |
|---|
| 48 |
out.write("<h4 id='commentpreview'>Comment by %s on %s</h4>\n<p>\n%s\n</p>\n" % (authname, time.strftime('%c', time.localtime()), wiki_to_html(comment, hdf, env, db))) |
|---|
| 49 |
if preview: |
|---|
| 50 |
out.write("</div>\n") |
|---|
| 51 |
|
|---|
| 52 |
# When submitting, inject comment before macro |
|---|
| 53 |
if comment and submit: |
|---|
| 54 |
submitted = False |
|---|
| 55 |
newtext = StringIO() |
|---|
| 56 |
for line in page.text.splitlines(): |
|---|
| 57 |
if line.find('[[AddComment') == 0: |
|---|
| 58 |
newtext.write("==== Comment by %s on %s ====\n%s\n\n" % (authname, time.strftime('%c', time.localtime()), comment)) |
|---|
| 59 |
submitted = True |
|---|
| 60 |
newtext.write(line + "\n") |
|---|
| 61 |
if submitted: |
|---|
| 62 |
# XXX Is this the dodigest hack ever? This is needed in |
|---|
| 63 |
# "appendonly" mode when the page is readonly. XXX |
|---|
| 64 |
if appendonly: |
|---|
| 65 |
perm.expand_meta_permission('WIKI_ADMIN'); |
|---|
| 66 |
page.set_content(newtext.getvalue()) |
|---|
| 67 |
# TODO: How do we get remote_addr from a macro? |
|---|
| 68 |
page.commit(authname, 'Comment added', None) |
|---|
| 69 |
comment = "" |
|---|
| 70 |
else: |
|---|
| 71 |
out.write("<div class='system-message'><strong>ERROR: [[AddComment]] macro call must be the only content on its line. Could not add comment.</strong></div>\n") |
|---|
| 72 |
|
|---|
| 73 |
out.write("<form action='%s#commentpreview' method='post'>\n" % env.href.wiki(pagename)) |
|---|
| 74 |
out.write("<fieldset>\n<legend>Add comment</legend>\n") |
|---|
| 75 |
out.write("<div class='field'>\n<textarea id='addcomment' name='addcomment' cols='80' rows='5'%s>" % disabled) |
|---|
| 76 |
if wikipreview: |
|---|
| 77 |
out.write("Page preview...") |
|---|
| 78 |
elif not cancel: |
|---|
| 79 |
out.write(comment) |
|---|
| 80 |
out.write("</textarea>\n") |
|---|
| 81 |
out.write("</div>\n") |
|---|
| 82 |
out.write('<div class="field">\n<label for="authoraddcomment">Your email or username:</label>\n<br/><input id="authoraddcomment" type="text" name="authoraddcomment" size="30" value="%s" />\n</div>' % authname) |
|---|
| 83 |
out.write("<div class='field'>\n<input size='30' type='submit' name='submitaddcomment' value='Add comment'%s/>\n" % disabled) |
|---|
| 84 |
out.write("<input type='submit' name='previewaddcomment' value='Preview comment'%s/>\n" % disabled) |
|---|
| 85 |
out.write("<input type='submit' name='canceladdcomment' value='Cancel'%s/>\n</div>\n" % disabled) |
|---|
| 86 |
out.write("<script type='text/javascript'>\naddWikiFormattingToolbar(document.getElementById('addcomment'));\n</script>\n") |
|---|
| 87 |
out.write("</fieldset>\n</form>\n") |
|---|
| 88 |
return out.getvalue()# + "<pre>" + hdf.dump() + "</pre>" |
|---|