# HG changeset patch # User Paul Boddie # Date 1383747208 -3600 # Node ID b60c5dcb85a3580e219f3010f54e067b0547b36a # Parent 1bf5a772b5f9019abf3637cb6a9e2d1d225d0037 Switched to MoinSupport search functionality from Include macro techniques. diff -r 1bf5a772b5f9 -r b60c5dcb85a3 README.txt --- a/README.txt Sun Nov 03 23:26:52 2013 +0100 +++ b/README.txt Wed Nov 06 15:13:28 2013 +0100 @@ -16,17 +16,21 @@ follows with $SCDIR referring to the SubpageComments distribution directory containing this README.txt file: +python moinsetup.py -m install_extension_package $SCDIR python moinsetup.py -m install_actions $SCDIR/actions python moinsetup.py -m install_macros $SCDIR/macros python moinsetup.py -m install_theme_resources $SCDIR python moinsetup.py -m edit_theme_stylesheet screen.css includecomments.css python moinsetup.py -m edit_theme_stylesheet print.css includecomments.css -The first command installs the PostComment action. +The first command runs the setup.py script and installs the library for the +extension. -The second command installs the IncludeComments macro. +The second command installs the PostComment action. -The third command installs the theme resources in the available theme +The third command installs the IncludeComments macro. + +The fourth command installs the theme resources in the available theme directories. The remaining commands activate the styles provided by SubpageComments by diff -r 1bf5a772b5f9 -r b60c5dcb85a3 SubpageComments.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SubpageComments.py Wed Nov 06 15:13:28 2013 +0100 @@ -0,0 +1,28 @@ +# -*- coding: iso-8859-1 -*- +""" + MoinMoin - SubpageComments common functionality + + @copyright: 2013 by Paul Boddie + @license: GNU GPL (v2 or later), see COPYING.txt for details. +""" + +from MoinSupport import getPagesForSearch + +def get_comment_numbers(pagename, request): + + """ + Return a list of comment numbers associated with the given 'pagename', using + the 'request' provided. + """ + + pagenames = [] + + for page in getPagesForSearch("title:regex:^%s/" % pagename, request): + basename, number = page.page_name.rsplit("/", 1) + if basename == pagename and number.isdigit(): + pagenames.append(int(number)) + + pagenames.sort() + return pagenames + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 1bf5a772b5f9 -r b60c5dcb85a3 actions/PostComment.py --- a/actions/PostComment.py Sun Nov 03 23:26:52 2013 +0100 +++ b/actions/PostComment.py Wed Nov 06 15:13:28 2013 +0100 @@ -10,7 +10,9 @@ from MoinMoin.PageEditor import PageEditor from MoinMoin.security import Permissions from MoinMoin.wikiutil import escape -from MoinSupport import getPagesForSearch, getPagesFromResults, ActionSupport +from MoinMoin.util import lock +from MoinSupport import ActionSupport +from SubpageComments import get_comment_numbers __version__ = "0.1" @@ -69,59 +71,53 @@ if not request.user.valid or not request.user.may.write(self.pagename): return 0, _("You are not allowed to comment on this page.") - # Determine the last comment. - - comments = get_comment_numbers(self.pagename, request) - last_comment_pagename = comments and comments[-1] or -1 + # Obtain a lock to prevent contention over comment numbers. - # Write the new page. - - comment_pagename = "%s/%04d" % (self.pagename, last_comment_pagename + 1) - new_page = PageEditor(request, comment_pagename) - username = request.user.name + writelock = lock.WriteLock(self.page.getPagePath("comment-lock")) + writelock.acquire() try: - # To add a page with an ACL, a special policy is required. + # Determine the last comment. + + comments = get_comment_numbers(self.pagename, request) + last_comment_pagename = comments and comments[-1] or -1 - may = request.user.may - request.user.may = SpecialPermissions(request.user, comment_pagename) + # Write the new page. - # Save the page, labelling it with the actual username. + comment_pagename = "%s/%04d" % (self.pagename, last_comment_pagename + 1) + new_page = PageEditor(request, comment_pagename) + username = request.user.name try: - new_page.saveText(comment_template % (username, username, comment), 0) + # To add a page with an ACL, a special policy is required. + + may = request.user.may + request.user.may = SpecialPermissions(request.user, comment_pagename) - # Restore the superusers. + # Save the page, labelling it with the actual username. + + try: + new_page.saveText(comment_template % (username, username, comment), 0) + + # Restore the original policy. - finally: - request.user.may = may + finally: + request.user.may = may - return 1, _("Comment added.") - except new_page.SaveError, exc: - return 0, unicode(exc) + return 1, _("Comment added.") + except new_page.SaveError, exc: + return 0, unicode(exc) + + # Release the lock and let others post comments now. + + finally: + writelock.release() comment_template = """\ #acl %s:read,write,delete,revert All:read #pragma comment-owner %s %s""" -def get_comment_numbers(pagename, request): - - """ - Return a list of comment numbers associated with the given 'pagename', using - the 'request' provided. - """ - - pagenames = [] - - for page in getPagesForSearch("title:regex:^%s/" % pagename, request): - basename, number = page.page_name.rsplit("/", 1) - if basename == pagename and number.isdigit(): - pagenames.append(int(number)) - - pagenames.sort() - return pagenames - # Action invocation function. def execute(pagename, request): diff -r 1bf5a772b5f9 -r b60c5dcb85a3 macros/IncludeComments.py --- a/macros/IncludeComments.py Sun Nov 03 23:26:52 2013 +0100 +++ b/macros/IncludeComments.py Wed Nov 06 15:13:28 2013 +0100 @@ -16,6 +16,7 @@ from MoinMoin.macro import Include from MoinMoin.user import User from MoinMoin.wikiutil import escape +from SubpageComments import get_comment_numbers import re import codecs @@ -68,18 +69,16 @@ """ % d) + # Add included comments. + + pages = get_comment_numbers(pagename, request) + + ownerfn = re.compile("^#pragma comment-owner (.*?)$", re.MULTILINE | re.UNICODE).search + # NOTE: Much of the code below originates from the Include macro, but it # NOTE: excludes various options of that macro and adds comment-related # NOTE: output. - # Add included comments. - - filterfn = re.compile(ur"^%s/" % re.escape(pagename), re.U).match - pages = request.rootpage.getPageList(filter=filterfn) - pages.sort() - - ownerfn = re.compile("^#pragma comment-owner (.*?)$", re.MULTILINE | re.UNICODE).search - # Track included pages. if not hasattr(page, '_macroInclude_pagelist'): @@ -88,6 +87,7 @@ # Visit each comment page. for inc_name in pages: + inc_name = "%s/%04d" % (pagename, inc_name) # Skip unreadable or already included pages. diff -r 1bf5a772b5f9 -r b60c5dcb85a3 setup.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/setup.py Wed Nov 06 15:13:28 2013 +0100 @@ -0,0 +1,13 @@ +#! /usr/bin/env python + +from distutils.core import setup + +setup( + name = "SubpageComments", + description = "Display and accept comments on MoinMoin wiki pages using subpages", + author = "Paul Boddie", + author_email = "paul@boddie.org.uk", + url = "http://moinmo.in/ActionMarket/SubpageComments", + version = "0.1", + py_modules = ["SubpageComments"] + )