package de.cdauth.jspwiki; import java.sql.*; import com.ecyrd.jspwiki.*; import com.ecyrd.jspwiki.plugin.*; import java.util.*; import javax.servlet.http.*; public class HitCounter implements WikiPlugin { private static final String databasePath = "jdbc:postgresql://localhost/jspwiki"; private static final String databaseUsername = "postgres"; private static final String databasePassword = ""; private static final String databaseDriver = "org.postgresql.Driver"; private static final int timeout = 7200; private static final String PARAM_NAME = "name"; private static final String PARAM_HIDDEN = "hidden"; private static final String PARAM_INCREASE = "increase"; private static final String PARAM_HITS = "hits"; private Connection databaseConnection = null; private String name = null; private boolean hidden = false; private boolean increase = true; private boolean hits = false; public String execute(WikiContext context, Map ParameterMap) throws PluginException { int c = 0; PreparedStatement statement = null; ResultSet result = null; HttpSession s = context.getHttpRequest().getSession(); String sid = null; if(s != null) sid = s.getId(); java.util.Date now = new java.util.Date(); name = (String) ParameterMap.get(PARAM_NAME); if(name == null) name = context.getPage().getName(); String hidden_param = (String) ParameterMap.get(PARAM_HIDDEN); if(hidden_param != null && (hidden_param.equals("yes") || hidden_param.equals("true"))) hidden = true; String increase_param = (String) ParameterMap.get(PARAM_INCREASE); if(increase_param != null && (!increase_param.equals("yes") && !increase_param.equals("true"))) increase = false; String hits_param = (String) ParameterMap.get(PARAM_HITS); if(hits_param != null && (hits_param.equals("yes") || hits_param.equals("true"))) hits = true; try { Class.forName(databaseDriver).newInstance(); databaseConnection = DriverManager.getConnection(databasePath, databaseUsername, databasePassword); statement = databaseConnection.prepareStatement("SELECT COUNT(*) AS c_count FROM t_hitcounter WHERE c_name = ?;"); statement.setString(1, name); result = statement.executeQuery(); result.next(); int in = result.getInt("c_count"); if(in > 0) { statement = databaseConnection.prepareStatement("SELECT c_hits FROM t_hitcounter WHERE c_name = ?;"); statement.setString(1, name); result = statement.executeQuery(); result.next(); c = result.getInt("c_hits"); } // Check if number has to be increased boolean must_increase = true; if(!increase) must_increase = false; else if(!hits && sid != null) { statement = databaseConnection.prepareStatement("SELECT COUNT(*) AS c_count FROM t_hittrace WHERE c_name = ? AND c_sid = ?;"); statement.setString(1, name); statement.setString(2, sid); result = statement.executeQuery(); result.next(); int seen = result.getInt("c_count"); if(seen > 0) { statement = databaseConnection.prepareStatement("UPDATE t_hittrace SET c_lastseen = ? WHERE c_name = ? AND c_sid = ?;"); must_increase = false; } else statement = databaseConnection.prepareStatement("INSERT INTO t_hittrace ( c_lastseen, c_name, c_sid ) VALUES ( ?, ?, ?);"); statement.setInt(1, Math.round(now.getTime()/1000)); statement.setString(2, name); statement.setString(3, sid); statement.executeUpdate(); } if(must_increase) { c++; if(in > 0) statement = databaseConnection.prepareStatement("UPDATE t_hitcounter SET c_hits = "+c+" WHERE c_name = ?;"); else statement = databaseConnection.prepareStatement("INSERT INTO t_hitcounter ( c_name, c_hits ) VALUES ( ?, "+c+" );"); statement.setString(1, name); statement.executeUpdate(); } // Cleanup database statement = databaseConnection.prepareStatement("DELETE FROM t_hittrace WHERE c_lastseen < ?;"); statement.setInt(1, Math.round(now.getTime()/1000)-timeout); statement.executeUpdate(); } catch(ClassNotFoundException e) { throw new PluginException("Database driver not found: "+e); } catch(InstantiationException e) { throw new PluginException("Could not load database driver: "+e); } catch(IllegalAccessException e) { throw new PluginException("Could not load database driver: "+e); } catch(SQLException e) { throw new PluginException("Database error: "+e); } String ret = ""; if(!hidden) ret = new Integer(c).toString(); return ret; } }