Edgewall Software

Ticket #5992 (closed enhancement: fixed)

Opened 15 months ago

Last modified 7 months ago

[patch] More graceful wiki page validation with req.warning

Reported by: hyuga <hyugaricdeau@…> Owned by: cboos
Priority: high Milestone: 0.11
Component: wiki system Version: devel
Severity: normal Keywords: wiki, validation
Cc:

Description

This does away with the InvalidWikiPage exception entirely, and instead of raising an error upon finding the first invalid field, all page_manipulators are allowed to perform validation, and add warnings to req.warning. This was all warnings are displayed, and can also be displayed during preview, diff, and when trying to submit:

  • trac/wiki/web_ui.py

     
    4343from trac.wiki.model import WikiPage 
    4444 
    4545 
    46 class InvalidWikiPage(TracError): 
    47     """Exception raised when a Wiki page fails validation.""" 
    48  
    49  
    5046class WikiModule(Component): 
    5147 
    5248    implements(IContentConverter, INavigationContributor, IPermissionRequestor, 
     
    130126                    if a in req.args: 
    131127                        action = a 
    132128                        break 
    133                 if action == 'edit' and not has_collision: 
     129                valid = self._validate(context) 
     130                if action == 'edit' and not has_collision and valid: 
    134131                    self._do_save(context) 
    135132                else: 
    136133                    return self._render_editor(context, action, has_collision) 
     
    167164 
    168165    # Internal methods 
    169166 
     167    def _validate(self, context): 
     168        req = context.req 
     169        page = context.resource 
     170         
     171        valid = True 
     172        # Give the manipulators a pass at post-processing the page 
     173        for manipulator in self.page_manipulators: 
     174            for field, message in manipulator.validate_wiki_page(req, page): 
     175                valid = False 
     176                if field: 
     177                    req.warning(_("The Wiki page field '%(field)s' is " 
     178                                  "invalid: %(message)s", 
     179                                  field=field, message=message)) 
     180                else: 
     181                    req.warning(_("Invalid Wiki page: %(message)s", 
     182                                  message=message)) 
     183        return valid 
     184 
    170185    def _page_data(self, context, action=''): 
    171186        page_name = context.name() 
    172187        title = context.summary() 
     
    253268            # WIKI_ADMIN 
    254269            page.readonly = int('readonly' in req.args) 
    255270 
    256         # Give the manipulators a pass at post-processing the page 
    257         for manipulator in self.page_manipulators: 
    258             for field, message in manipulator.validate_wiki_page(req, page): 
    259                 if field: 
    260                     raise InvalidWikiPage(_("The Wiki page field '%(field)s' " 
    261                                             "is invalid: %(message)s", 
    262                                             field=field, message=message)) 
    263                 else: 
    264                     raise InvalidWikiPage(_("Invalid Wiki page: %(message)s", 
    265                                             message=message)) 
    266  
    267271        try: 
    268272            page.save(get_reporter_id(req, 'author'), req.args.get('comment'), 
    269273                      req.remote_addr) 

Attachments

wiki_validation_improvement_r5999.patch (2.6 KB) - added by hyuga <hyugaricdeau@…> 15 months ago.
Same patch as displayed in the ticket description.
wiki_graceful_validation_r7021.patch (3.1 KB) - added by ebray <hyugaricdeau@…> 7 months ago.
Here's an updated version of the patch.
wiki_graceful_validation_r7021.2.patch (3.1 KB) - added by ebray <hyugaricdeau@…> 7 months ago.
Here's an updated version of the patch (the previous version of this had a small typo).

Change History

Changed 15 months ago by hyuga <hyugaricdeau@…>

Same patch as displayed in the ticket description.

Changed 15 months ago by ThurnerRupert

  • milestone set to 0.11

could be applied immediately, no?

Changed 15 months ago by ThurnerRupert

  • milestone changed from 0.11 to 0.11.1

i mean immediately after release :)

Changed 7 months ago by cboos

  • priority changed from normal to high

Looks like this was a missing bit of #152 / #4100 related functionality, so it would be nice to get in 0.11 itself for consistency.

ebray, can you please look into updating the patch?

Changed 7 months ago by ebray <hyugaricdeau@…>

Here's an updated version of the patch.

Changed 7 months ago by ebray <hyugaricdeau@…>

Here's an updated version of the patch (the previous version of this had a small typo).

Changed 7 months ago by cboos

  • milestone changed from 0.11.1 to 0.11

Thanks for the updated patch!

Changed 7 months ago by cboos

  • status changed from new to closed
  • resolution set to fixed

Applied in r7033.

Only thing modified is that I left the InvalidWikiPage exception in, but marked it deprecated.

Changed 7 months ago by cboos

Oh, by the way, for testing the change I had to write a little IWikiPageManipulator plugin, and as I think it can be universally used, I'll donate it to the world:

from trac.core import *
from trac.wiki.api import IWikiPageManipulator

class NeedSpam(Component):
    """Enforce feeding the wiki comment with SPAM!"""

    implements(IWikiPageManipulator)

    # IWikiPageManipulator methods

    def prepare_wiki_page(self, req, page, fields):
        pass

    def validate_wiki_page(self, req, page):
        if 'spam' not in req.args.get('comment', ''):
            yield ('comment', 'No SPAM provided!')

Add/Change #5992 ([patch] More graceful wiki page validation with req.warning)

Author



Change Properties
<Author field>
Action
as closed
Next status will be 'reopened'
to The owner will change from cboos. Next status will be 'closed'
 
Note: See TracTickets for help on using tickets.