!! Version Overwrite

Everytime your press "SAVE", JSPWiki save 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.

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|DirkFrederickx]

! Approach

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.

# __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)
# 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
# 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)

!! Implementation

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:
{{{
  <%-- OVERWRITE BLOCK --%>
  <%
    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 */ %>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        Overwrite version: <input type="checkbox" name="overwrite" checked="checked" />
      <% } /* changes within one hour */ %>
    <% } /* should be the same author */ %>
  </wiki:UserCheck>
}}}


! 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:
{{{
  else
  {
    wiki.saveText( wikiContext, text );
  }
}}}

and change it to 
{{{
  else
  {
    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 ) 
          {
            log.info("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}}.