1 26 package org.snipsnap.net.admin; 27 28 import org.radeox.util.logging.Logger; 29 import org.snipsnap.app.Application; 30 import org.snipsnap.config.Configuration; 31 import org.snipsnap.container.Components; 32 import org.snipsnap.snip.Access; 33 import org.snipsnap.snip.Links; 34 import org.snipsnap.snip.Snip; 35 import org.snipsnap.snip.SnipSpace; 36 37 import javax.servlet.http.HttpServletRequest ; 38 import javax.servlet.http.HttpServletResponse ; 39 import javax.servlet.http.HttpSession ; 40 import java.util.ArrayList ; 41 import java.util.Collections ; 42 import java.util.HashMap ; 43 import java.util.HashSet ; 44 import java.util.Iterator ; 45 import java.util.List ; 46 import java.util.Map ; 47 import java.util.Set ; 48 49 public class Maintenance implements SetupHandler { 50 public String getName() { 51 return "maintenance"; 52 } 53 54 private Map workerThreads = new HashMap (); 55 56 public Map setup(HttpServletRequest request, HttpServletResponse response, Configuration config, Map errors) { 57 String appOid = (String ) Application.get().getObject(Application.OID); 58 CheckConsistency workerThread = (CheckConsistency) workerThreads.get(appOid); 59 if (workerThread != null && workerThread.isAlive()) { 60 setRunning(workerThread, request.getSession()); 61 return errors; 62 } 63 64 if (request.getParameter("check") != null) { 65 if (workerThread != null) { 66 workerThreads.remove(appOid); 67 workerThread = null; 68 } 69 workerThread = new CheckConsistency(appOid, false); 70 workerThread.start(); 71 workerThreads.put(appOid, workerThread); 72 73 setRunning(workerThread, request.getSession()); 74 } else if (request.getParameter("dorepair") != null) { 75 CheckConsistency repairThread = new CheckConsistency(appOid, true); 76 repairThread.setParentsToFix(workerThread.getParentsToFix()); 77 repairThread.setCommentsToFix(workerThread.getCommentsToFix()); 78 repairThread.setDuplicates(workerThread.getDuplicates()); 79 repairThread.setSpamList(workerThread.getSpamList()); 80 repairThread.start(); 81 workerThreads.put(appOid, repairThread); 82 83 setRunning(repairThread, request.getSession()); 84 } else { 85 request.getSession().removeAttribute("running"); 86 if (workerThread != null && !workerThread.isAlive()) { 87 request.setAttribute("fixComments", workerThread.getCommentsToFix()); 88 request.setAttribute("fixParents", workerThread.getParentsToFix()); 89 request.setAttribute("duplicates", workerThread.getDuplicates()); 90 request.setAttribute("notFixable", workerThread.getNonFixable()); 91 request.setAttribute("spamedSnips", new Integer (workerThread.getSpamList().size())); 92 } 93 } 94 95 return errors; 96 } 97 98 private void setRunning(CheckConsistency workerThread, HttpSession session) { 99 Map statusMap = (Map ) session.getAttribute("running"); 100 if (null == statusMap) { 101 statusMap = new HashMap (); 102 } 103 statusMap.put("max", new Integer (workerThread.getMax())); 104 statusMap.put("current", new Integer (workerThread.getCurrent())); 105 if (workerThread.isRepairing()) { 106 statusMap.put("maintenance", "repairing"); 107 } else { 108 statusMap.put("maintenance", "checking"); 109 } 110 session.setAttribute("running", statusMap); 111 } 112 113 class CheckConsistency extends Thread { 114 private String appOid = null; 115 private List fixDuplicates = new ArrayList (); 116 private List fixComments = new ArrayList (); 117 private List fixParents = new ArrayList (); 118 private List noFix = new ArrayList (); 119 private List fixSpam = new ArrayList (); 120 121 private boolean repair = false; 122 123 private int currentCount = 0; 124 private int snipCount = 0; 125 126 public CheckConsistency(String appOid, boolean repair) { 127 super(); 128 this.appOid = appOid; 129 this.repair = repair; 130 } 131 132 public boolean isRepairing() { 133 return repair; 134 } 135 136 public void run() { 137 Application.get().storeObject(Application.OID, appOid); 138 SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class); 139 140 if (!repair) { 141 List allSnips = Collections.unmodifiableList(space.getAll()); 142 Set uniqeSnipNames = new HashSet (); 143 Iterator snipIt = allSnips.iterator(); 144 snipCount = allSnips.size(); 145 Logger.debug("Need to check " + snipCount + " snips."); 146 while (snipIt.hasNext()) { 147 Snip snip = (Snip) snipIt.next(); 148 check(snip, space); 149 if (!uniqeSnipNames.add(snip.getName())) { 150 fixDuplicates.add(snip); 151 } 152 currentCount++; 153 } 154 } else { 155 snipCount = fixParents.size() + 156 fixComments.size() + fixDuplicates.size() + fixSpam.size(); 157 currentCount = 0; 158 Iterator parentIt = fixParents.iterator(); 159 while (parentIt.hasNext()) { 160 Snip snip = (Snip) parentIt.next(); 161 fixParent(snip); 162 space.systemStore(snip); 163 currentCount++; 164 parentIt.remove(); 165 } 166 Iterator commentIt = fixComments.iterator(); 167 while (commentIt.hasNext()) { 168 Snip snip = (Snip) commentIt.next(); 169 fixComment(snip, space); 170 space.systemStore(snip); 171 currentCount++; 172 commentIt.remove(); 173 } 174 List blackList = Access.getReferrerBlackList(); 175 Iterator spamIt = fixSpam.iterator(); 176 while (spamIt.hasNext()) { 177 Snip snip = (Snip) spamIt.next(); 178 Links backLinks = snip.getBackLinks(); 179 180 Iterator blackListIt = blackList.iterator(); 181 boolean storeModifiedSnip = false; 182 while (blackListIt.hasNext()) { 183 String entry = ((String ) blackListIt.next()).toLowerCase(); 184 if (entry.startsWith("pattern:")) { 185 if (backLinks.removeLinkByPattern(entry.substring("pattern:".length()).trim()) > 0) { 186 storeModifiedSnip = true; 187 Logger.debug("removed link by pattern: '" + entry.substring("pattern:".length()).trim() + "'"); 188 } 189 } else { 190 if (backLinks.removeLink(entry.trim()) > 0) { 191 storeModifiedSnip = true; 192 Logger.debug("removed link by domain: " + entry); 193 } 194 } 195 } 196 if (storeModifiedSnip) { 197 space.systemStore(snip); 198 } 199 spamIt.remove(); 200 currentCount++; 201 } 202 } 203 } 204 205 public void setCommentsToFix(List snips) { 206 fixComments = snips; 207 } 208 209 public List getCommentsToFix() { 210 return fixComments; 211 } 212 213 public void setParentsToFix(List snips) { 214 fixParents = snips; 215 } 216 217 public List getParentsToFix() { 218 return fixParents; 219 } 220 221 public void setDuplicates(List snips) { 222 fixDuplicates = snips; 223 } 224 225 public List getDuplicates() { 226 return fixDuplicates; 227 } 228 229 public List getNonFixable() { 230 return noFix; 231 } 232 233 public void setSpamList(List snips) { 234 fixSpam = snips; 235 } 236 237 public List getSpamList() { 238 return fixSpam; 239 } 240 241 private void check(Snip snip, SnipSpace space) { 242 String snipName = snip.getName(); 243 if (snipName.startsWith("comment-")) { 244 if (null == snip.getCommentedSnip() && (null == snip.getCommentedName() || "".equals(snip.getCommentedName()))) { 245 String commentedName = snipName.substring("comment-".length(), snipName.lastIndexOf("-")); 246 if (!space.exists(commentedName)) { 247 Logger.warn("non-fixable snip found: '" + snipName + "' (commented snip '" + commentedName + "' missing)"); 248 noFix.add(snip); 249 } else { 250 Logger.warn("snip '" + snipName + "' is missing its commented snip '" + commentedName + "'"); 251 fixComments.add(snip); 252 } 253 } 254 } 255 256 if (snipName.matches("\\d\\d\\d\\d-\\d\\d-\\d\\d")) { 257 if (null == snip.getParent() && (null == snip.getParentName() || "".equals(snip.getParentName()))) { 258 Logger.warn("snip '" + snipName + "' is missing its parent snip"); 259 fixParents.add(snip); 260 } 261 } 262 263 Iterator backLinksIt = snip.getBackLinks().iterator(); 264 boolean foundspam = false; 265 while (backLinksIt.hasNext()) { 266 String url = (String ) backLinksIt.next(); 267 if (!Access.isValidReferrer(url)) { 268 foundspam = true; 269 fixSpam.add(snip); 270 break; 271 } 272 } 273 if (foundspam) { 274 Logger.debug("Found spam-referrer links at '" + snipName + "'"); 275 } 276 } 277 278 private void fixParent(Snip snip) { 279 String snipName = snip.getName(); 280 if (snipName.matches("\\d\\d\\d\\d-\\d\\d-\\d\\d")) { 281 if (null == snip.getParent() && (null == snip.getParentName() || "".equals(snip.getParentName()))) { 282 Logger.warn("fixing snip parent of '" + snipName + "'"); 283 snip.setParentName("start"); 284 } 285 } 286 } 287 288 private void fixComment(Snip snip, SnipSpace space) { 289 String snipName = snip.getName(); 290 if (snipName.startsWith("comment-")) { 291 if (null == snip.getCommentedSnip() && (null == snip.getCommentedName() || "".equals(snip.getCommentedName()))) { 292 String commentedName = snipName.substring("comment-".length(), snipName.lastIndexOf("-")); 293 if (!space.exists(commentedName)) { 294 Logger.warn("commented snip of snip '" + snipName + "' got lost?"); 295 } else { 296 Logger.warn("fixing commented snip '" + snipName + "' -> '" + commentedName + "'"); 297 snip.setCommentedName(commentedName); 298 } 299 } 300 } 301 } 302 303 public int getMax() { 304 return snipCount; 305 } 306 307 public int getCurrent() { 308 return currentCount; 309 } 310 } 311 } 312 313 | Popular Tags |