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