This is version . It is not the current version, and thus it cannot be edited.
[Back to current version]   [Restore this version]

Version Overwrite#

Everytime you press "SAVE", JSPWiki saves a new version of a page. Since recently, JSPWiki only saves a new version if changes where done to the page. (good;-)

However, typical edit sequences consists of multiple edit/save/edit/save... commands executed till you are satisfied with the page. Which leads to a high number of page versions with little or no added value. (look at the first 3 version of the page ;-o )

This page descibes a hack of the current JSPWiki in order to limit the number of new page versions. It was tested on a JSPWiki,v2.3.61. -- DF

See also IdeaSingleVersionForSavesBySameUserWithinHour


When following conditions apply, the default behaviour of JSPWiki will be to overwrite the last version of the page instead of creating a new one.

  1. The user must be logged in. (authenticated)
    Anonymous or asserted user can not overwrite versions, i.e. each change will be registered with new page versions. (security)
  2. The user must be the same as the last author of the page
    A sequence of edits by different users will be registered as different page versions
  3. The change of the page must happen within one hour since the last modification When subsequent edits happen with intervals of more then one hour, a different page version will be created.

When all of these conditions hold, JSPWiki will overwrite the last version of the page. However, a checkbox will be added to the edit screen, such that the user can still decide NOT to overwrite, but save the changes to a new version.

These conditions are checked when opening the edit screen (show/hide the checkbox) and on saving the page. (press save)


You need to make changes to 2 files

  • the /templates/<your-template>/editors/plain.jsp
  • the toplevel Edit.jsp

Changes to plain.jsp (TEMPLATES LEVEL)#

This jsp generates the normal edit screen with the Save/Preview/Cancel buttons. The added stuff inserts the "Overwrite version" checkbox, if needed.

Add following stuff just after these 3 buttons:

    java.util.Date now = new java.util.Date();       
    long anHourAgo = now.getTime() - (60*60*1000);
    String lastchange = (String) pageContext.getAttribute("lastchange",PageContext.REQUEST_SCOPE);
    long lastChangeTime = Long.parseLong( lastchange );
    String lastAuthor = context.getPage().getAuthor();
    String thisAuthor = context.getWikiSession().getUserPrincipal().getName();                           
  <wiki:UserCheck status="authenticated">
    <% if( lastAuthor.equals(thisAuthor) ) { /* should be the same author */ %>
      <% if( lastChangeTime > anHourAgo ) { /* changes within one hour */ %>
        Overwrite version: <input type="checkbox" name="overwrite" checked="checked" />
      <% } /* changes within one hour */ %>
    <% } /* should be the same author */ %>

Changes to Edit.jsp (TOP LEVEL)#

The top level Edit.jsp is responsible for the actual handling of the Edit page, including the actual SAVE command.

First you need to capture the status of the OVERWRITE checkbox. Following lines can be added in the beginning of the file:

  /* #####OVERWRITE checkbox##### */
  String overwrite = request.getParameter("overwrite");

Then you need to change the actual SAVING of the text. Locate following lines:

    wiki.saveText( wikiContext, text );

and change it to

    if( overwrite != null )
      if( wikiSession.isAuthenticated() )
        if( user.equals( wikipage.getAuthor() ) )
          Date now        = new Date();       
          long anHourAgo  = now.getTime() - (60*60*1000);    
          long lastchange = 0;
          Date d = latestversion.getLastModified();
          if( d != null ) lastchange = d.getTime();
          if( lastchange > anHourAgo ) 
  "OVERWRITE : -- so first delete the latest version");
            wiki.deleteVersion( wikipage );
    wiki.saveText( wikiContext, text );

You can see that the different checks are repeated here, to make sure the overwrite flag is not spoofed in anyway. (paranoia;-)

Also notice how I implemented the actual overwrite: first the currenct version is deleted; then the new text is save. WARNING: I only tested this with the VersioningFileProvider.

Add new attachment

Only authorized users are allowed to upload new attachments.
« This particular version was published on 14-Jan-2006 00:03 by DirkFrederickx.