ForceCommentPlugin / forcecomment /

# -*- coding: utf-8 -*-
# Copyright (C) 2009-2010 Mark Edgington
# All rights reserved.
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.

"""  ForceComment - forces the user to provide a comment for wiki changes

To customize, add the following section to your trac.ini file, and any of
the following settings (defaults are used otherwise):

min_comment_length = 10
cosmetic_change_comment = "cosmetic"
error_text = "Sorry, but a comment is required."

The 'cosmetic_change_comment' is a string or phrase which can be less than
the minimum length, but will be accepted, since it indicates that nothing
comment-worthy was changed.

from trac.core import *
from import IWikiPageManipulator

__author__ = 'Mark Edgington'
__author_email__ = ''

class ForceCommentPlugin(Component):

    def prepare_wiki_page(self, req, page, fields):
        """Not currently called, but should be provided for future

    def validate_wiki_page(self, req, page):
        """Validate a wiki page after it's been populated from user input.
        Must return a list of `(field, message)` tuples, one for each problem
        detected. `field` can be `None` to indicate an overall problem with the
        page. Therefore, a return value of `[]` means everything is OK.
        #if req.perm.has_permission('WIKI_ADMIN'):
        #    # An administrator is allowed to have an empty comment
        #    return []

        if ('preview' in req.args) or ('diff' in req.args):
            # Only a preview or diff, no need to filter the submission yet
            return []

        # get the config options set in the trac config file
        c = lambda key, default: self.env.config.get('forcecomment', key, default)
        minLen = int(c('min_comment_length', '10'))
        cosmeticString = c('cosmetic_change_comment', 'cosmetic')
        errTextDefault = "A full comment must be supplied for this change," + \
          " or use '" + cosmeticString + "' if no actual informational content has changed."
        errorText = c('error_text', errTextDefault)

        #cur_page = WikiPage(self.env,, version=page.version)
        #author = req.args.get('author', req.authname)
        comment = req.args['comment']

        if (comment.lower() == cosmeticString) or len(comment) >= minLen:
            return []
            return [(None, errorText)]