<%@ page import="com.atlassian.core.ofbiz.CoreFactory"%> <%@ page import="com.atlassian.core.ofbiz.CoreFactory"%> <%@ page import="com.atlassian.core.util.map.EasyMap"%> <%@ page import="org.ofbiz.core.entity.*"%> <%@ page import="com.opensymphony.util.TextUtils"%> <%@ page import="java.io.IOException"%> <%@ page import="com.atlassian.jira.ComponentManager"%> <%@ page import="com.atlassian.core.util.collection.EasyList"%> <%@ page import="java.util.Map"%> <%@ page import="com.opensymphony.user.User"%> <%@ page import="webwork.action.ActionContext"%> <%@ page import="com.atlassian.seraph.auth.DefaultAuthenticator"%> <%@ page import="com.atlassian.jira.ManagerFactory"%> <%@ page import="com.atlassian.jira.security.Permissions"%> <%@ taglib uri="webwork" prefix="webwork" %> <%@ taglib uri="sitemesh-page" prefix="page" %> <%! private static final String COMMENT_TABLE_ENTITY_NAME = "Action"; private static final String MODE_PARAM_NAME = "mode"; private static final String COMMENT_ID_PARAM_NAME = "comment_id"; %> Clean Comment Spam <% // Check that the user has permissions User remoteUser = (User) request.getSession().getAttribute(DefaultAuthenticator.LOGGED_IN_KEY); // If the user is not logged in or does not have the global ADMIN permission then give back an error message. if (remoteUser == null || !ManagerFactory.getPermissionManager().hasPermission(Permissions.ADMINISTER, remoteUser)) { out.println("

You do not have permissions to ADMINISTER JIRA.

"); return; } %> <% String mode = request.getParameter(MODE_PARAM_NAME); if ("confirm".equals(mode)) { String[] commentIds = request.getParameterValues(COMMENT_ID_PARAM_NAME); if (commentIds != null && commentIds.length > 0) { %> <%-- Confirm Mode --%> Delete Comments

Please confirm that you would like to delete the comments shown in the table below.

The deleted comments will not be recorded into change history of their issue. Once you confirm this screen, there will be no way to restore these comments.

<% for (int i = 0; i < commentIds.length; i++) { Long commentId = new Long(commentIds[i]); printComment(out, getComment(commentId), request, false); } %>
  Comment Comment Author Created Date Comment ID Issue
<% } else { %>

No comments to delete were selected. Please select at least one comment to delete.

<< Return

<% } } else if ("delete".equals(mode)) { // Delete Mode String[] commentIds = request.getParameterValues(COMMENT_ID_PARAM_NAME); if (commentIds != null && commentIds.length > 0) { for (int i = 0; i < commentIds.length; i++) { Long commentId = new Long(commentIds[i]); int numberDeleted = deleteComment(commentId); if (numberDeleted > 0) { out.println("Deleted comment with id " + commentId + "
"); } else { out.println("Could not delete comment with id " + commentId + "
"); } } out.println("

<<Return

"); } else { %>

No comments to delete were selected. Please select at least one comment to delete.

<< Return

<% } } else { %> <%-- Default Mode --%> <% int numberOfComments = 100; String ncs = request.getParameter("numberComments"); if (ncs != null) { try { numberOfComments = Integer.parseInt(ncs); } catch (NumberFormatException e) { // Ignore and leave the variable to be 100 } } %> Clean Comment Spam 1 This page shows most recently created comments matching the filter criteria. The most recently added comment is shown first.

Please refine the filter criteria and press "Filter", or select the comments you would like to delete and press "Delete".

Note: The deleted comments will not be recorded into change history of their issue.

Max number comments: Author: " /> Min number of links: " />
<% Map conditionMap = EasyMap.build("type", "comment"); String commentAuthor = request.getParameter("commentAuthor"); if (TextUtils.stringSet(commentAuthor)) { conditionMap.put("author", commentAuthor); } EntityFieldMap entityFieldMap = new EntityFieldMap(conditionMap, EntityOperator.AND); int commentLinksNumber = 0; try { commentLinksNumber = Integer.parseInt(request.getParameter("commentLinksNumber")); } catch (NumberFormatException e) { // Ignore } EntityCondition condition; if (commentLinksNumber > 0) { StringBuffer queryString = new StringBuffer("%"); for (int i = 0; i < commentLinksNumber; i++) { queryString.append("http://%"); } EntityExpr linksCondition = new EntityExpr("body", EntityOperator.LIKE, queryString.toString()); condition = new EntityExpr(entityFieldMap, EntityOperator.AND, linksCondition); } else { condition = entityFieldMap; } boolean commentsFound = false; EntityListIterator listIterator = null; try { listIterator = CoreFactory.getGenericDelegator().findListIteratorByCondition(COMMENT_TABLE_ENTITY_NAME, condition, null, EasyList.build("created DESC")); GenericValue commentGV = (GenericValue) listIterator.next(); int i = 0; while (commentGV != null) { commentsFound = true; if (++i > numberOfComments) { break; } printComment(out, commentGV, request, true); commentGV = (GenericValue) listIterator.next(); } } finally { if (listIterator != null) { listIterator.close(); } } if (!commentsFound) { out.println(""); } %>
  Comment Comment Author Created Date Comment ID Issue
No comments were found.
<% } %> <%! private void printComment(JspWriter out, GenericValue commentGV, HttpServletRequest request, boolean input) throws IOException { out.println(""); out.println(""); if (input) { out.print(""); } else { out.print(""); } out.println(""); out.println(""); out.println(TextUtils.plainTextToHtml(commentGV.getString("body"))); out.println(""); out.println(""); out.println(TextUtils.htmlEncode(commentGV.getString("author"))); out.println(""); out.println(""); out.println(TextUtils.htmlEncode(commentGV.getString("created"))); out.println(""); out.println(""); out.println(commentGV.getLong("id")); out.println(""); out.println(""); String issueKey = getIssueKey(commentGV); if (issueKey != null) { out.print(""); out.print(issueKey); out.println(""); } else { out.println(" "); } out.println(""); out.println(""); } private GenericValue getComment(Long id) throws GenericEntityException { return CoreFactory.getGenericDelegator().findByPrimaryKey(COMMENT_TABLE_ENTITY_NAME, EasyMap.build("id", id)); } private int deleteComment(Long id) throws GenericEntityException { return CoreFactory.getGenericDelegator().removeByAnd(COMMENT_TABLE_ENTITY_NAME, EasyMap.build("id", id, "type", "comment")); } private String getIssueKey(GenericValue commentGV) { GenericValue issueGV = null; Long issueId = commentGV.getLong("issue"); try { issueGV = ComponentManager.getInstance().getIssueManager().getIssue(issueId); // This will be a GenericEntityException, but it isn't thrown in // recent versions of JIRA } catch (Exception e) { System.out.println("Error getting issue with id "+issueId+": "+e); e.printStackTrace(); } if (issueGV != null) { return issueGV.getString("key"); } return null; } %>