From: <jbo...@li...> - 2005-09-24 16:16:25
|
Author: szimano Date: 2005-09-24 12:16:18 -0400 (Sat, 24 Sep 2005) New Revision: 1202 Modified: trunk/forge/portal-extensions/jbosswiki/wiki-common/src/etc/org/jboss/wiki/WikiTypes.xml trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/AttachmentDataSource.java trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/FileDataSource.java trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/MediaDataSource.java trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/WikiEngine.java trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/plugins/AdminConsolePlugin.java Log: oage deleting (may be hazardous) Modified: trunk/forge/portal-extensions/jbosswiki/wiki-common/src/etc/org/jboss/wiki/WikiTypes.xml =================================================================== --- trunk/forge/portal-extensions/jbosswiki/wiki-common/src/etc/org/jboss/wiki/WikiTypes.xml 2005-09-24 16:11:48 UTC (rev 1201) +++ trunk/forge/portal-extensions/jbosswiki/wiki-common/src/etc/org/jboss/wiki/WikiTypes.xml 2005-09-24 16:16:18 UTC (rev 1202) @@ -25,13 +25,13 @@ <class>org.jboss.wiki.plugins.FriendlyLinkPlugin</class> </plugin> <plugin> + <name>adminConsole</name> + <class>org.jboss.wiki.plugins.AdminConsolePlugin</class> + </plugin> + <plugin> <name>wikiToHtmlTranslator</name> <class>org.jboss.wiki.plugins.HTMLTranslatorParts</class> </plugin> - <plugin> - <name>adminConsole</name> - <class>org.jboss.wiki.plugins.AdminConsolePlugin</class> - </plugin> </wikiType> <wikiType> <name>diff</name> Modified: trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/AttachmentDataSource.java =================================================================== --- trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/AttachmentDataSource.java 2005-09-24 16:11:48 UTC (rev 1201) +++ trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/AttachmentDataSource.java 2005-09-24 16:16:18 UTC (rev 1202) @@ -16,6 +16,8 @@ * */ public interface AttachmentDataSource { + + public static String attTrashName = "trash/attachemnts"; /**Add atachment to a given page. * @@ -53,7 +55,7 @@ * * @param pageName Name of the page to get attachment's version from. * @param attachmentName Name of the attachment. - * @return + * @return attachment version */ public int getLastAttachmentVersion(String pageName, String attachmentName); @@ -62,7 +64,22 @@ * @param pageName Name of the page to get attachment's size from. * @param attachmentName Name of the attachment. * @param version Version of the attachment to count size for. - * @return + * @return attachment size */ public long getAttachmentSize(String pageName, String attachmentName, int version); + + /** Deletes (or moves to trash) given attachment from a given page. + * + * @param pageName Name of the page to delete attachment from. + * @param attachmentName Attachment name. + * @return true if attachemnt was deleted. False otherwise. + */ + public boolean deleteAttachment(String pageName, String attachmentName); + + /** Deletes (or moves to trash) all attachments from a given page. + * + * @param pageName Name of the page to delete attachments from. + * @return true if attachemnts were deleted. False otherwise. + */ + public boolean deleteAttachments(String pageName); } Modified: trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/FileDataSource.java =================================================================== --- trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/FileDataSource.java 2005-09-24 16:11:48 UTC (rev 1201) +++ trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/FileDataSource.java 2005-09-24 16:16:18 UTC (rev 1202) @@ -38,6 +38,10 @@ public static final String propModFileName = "fileMod.properties"; + public String pathToMediaTrash; + + public String pathToAttTrash; + private String pathToMedia; private String pathToAttachments; @@ -91,6 +95,26 @@ System.err.println("Can't close input stream \n" + ioe); } } + + // create trash directory for deleted pages if it doesn't exists + + pathToMediaTrash = pathToMedia + "/" + MediaDataSource.mediaTrashName; + + File pathToTrashFile = new File(pathToMediaTrash + "/OLD"); + + if (!pathToTrashFile.exists()) { + pathToTrashFile.mkdirs(); + } + + // create trash directory for deleted attachments if it doesn't exists + + pathToAttTrash = pathToMedia + "/" + AttachmentDataSource.attTrashName; + + File pathToAttachmentsFile = new File(pathToAttTrash); + + if (!pathToAttachmentsFile.exists()) { + pathToAttachmentsFile.mkdirs(); + } } public void loadProperties(Properties fileDSProps) { @@ -373,9 +397,11 @@ } int pageMods = getPageMod(pageName); - + page = new WikiPage(pageName, author, pageContent.toString(), - i, i, new Date(pageFile.lastModified()), this, this, (pageMods & VIEWABLE) == VIEWABLE, (pageMods & EDITABLE) == EDITABLE); + i, i, new Date(pageFile.lastModified()), this, this, + (pageMods & VIEWABLE) == VIEWABLE, + (pageMods & EDITABLE) == EDITABLE); page.setLength(pageFile.length()); } @@ -738,11 +764,10 @@ if (mods != (EDITABLE + VIEWABLE)) { pageModProps.setProperty(pageName, String.valueOf(mods)); - } - else { + } else { pageModProps.remove(pageName); } - + FileOutputStream fos = null; try { @@ -758,4 +783,158 @@ } } } + + public synchronized boolean deletePage(String pageName, + boolean deleteSubpages) { + + if (pageExists(pageName)) { + // moving page file + String pageFileName = pathToMedia + "/" + pageName + ".txt"; + String pageFileTrashNameBase = pathToMediaTrash + "/" + pageName + + ".txt"; + String pageFileTrashName = pageFileTrashNameBase; + + File pageFile = new File(pageFileName); + File pageTrashFile = new File(pageFileTrashName); + +// look if page is already in trash (add i-bak) + int i = 0; + + if (pageTrashFile.exists()) { + while (pageTrashFile.exists()) { + pageFileTrashName = pageFileTrashNameBase + "-" + (i++) + + "-bak"; + pageTrashFile = new File(pageFileTrashName); + } + + } + + if (!pageFile.renameTo(pageTrashFile)) { + System.err.println("[FileDataSource]: Problems with moving " + + pageFileName + " to " + pageFileTrashName); + return false; + } + + // moving history directory + + String historyDirName = pathToMedia + "/OLD/" + pageName; + String historyTrashDirNameBase = pathToMediaTrash + "/OLD/" + pageName; + String historyTrashDirName = historyTrashDirNameBase; + + pageFile = new File(historyDirName); + pageTrashFile = new File(historyTrashDirName); + + + // look if history dir is already in trash (add i-bak) + i = 0; + + if (pageTrashFile.exists()) { + while (pageTrashFile.exists()) { + historyTrashDirName = historyTrashDirNameBase + "-" + (i++) + + "-bak"; + pageTrashFile = new File(historyTrashDirName); + } + + } + + if (!pageFile.renameTo(pageTrashFile)) { + System.err.println("[FileDataSource]: Problems with moving " + + historyDirName + " to " + historyTrashDirName); + + // return back the page history + pageFile = new File(pageFileName); + pageTrashFile = new File(pageFileTrashName); + + if (!pageTrashFile.renameTo(pageFile)) { + System.err + .println("[FileDataSource]: ERROR. I've tried moving previously moved page file from the trash, but there was some problem. This is not good and there is nothing i can do about it. The error ocured while moving " + + pageFileTrashName + " to " + pageFileName); + } + + return false; + } + + return true; + } else { + // no such page + System.err.println("There is now such page: " + pageName); + return false; + } + } + + public synchronized boolean deleteAttachment(String pageName, + String attachmentName) { + + String attName = pathToAttachments + "/" + pageName + "-att/" + + attachmentName + "-dir"; + String attTrashNameBase = pathToAttTrash + "/" + pageName + "-att/" + + attachmentName + "-dir"; + String attTrashName = attTrashNameBase; + + // moving file directory + File attFile = new File(attName); + + if (attFile.exists()) { + +// look if att dir is already in trash (add i-bak) + File attTrashFile = new File(attTrashName); + + int i = 0; + + if (attTrashFile.exists()) { + while (attTrashFile.exists()) { + attTrashName = attTrashNameBase + "-" + (i++) + "-bak"; + attTrashFile = new File(attTrashName); + } + + } + + if (!attFile.renameTo(attTrashFile)) { + System.err.println("[ATTACHMENT DS]:Problems with moving " + + attName + " to " + attTrashName); + return false; + } + + return true; + } else { + System.err.println("Page " + pageName + " has no " + attachmentName + + " attachmnet"); + return false; + } + } + + public synchronized boolean deleteAttachments(String pageName) { + String attName = pathToAttachments + "/" + pageName + "-att"; + String attTrashNameBase = pathToAttTrash + "/" + pageName + "-att"; + String attTrashName = attTrashNameBase; + + // moving file directory + File attFile = new File(attName); + + if (attFile.exists()) { +// look if att dir is already in trash (add i-bak) + File attTrashFile = new File(attTrashName); + + int i = 0; + + if (attTrashFile.exists()) { + while (attTrashFile.exists()) { + attTrashName = attTrashNameBase + "-" + (i++) + "-bak"; + attTrashFile = new File(attTrashName); + } + + } + + if (!attFile.renameTo(new File(attTrashName))) { + System.err.println("[ATTACHMENT DS]:Problems with moving " + + attName + " to " + attTrashName); + return false; + } + + return true; + } else { + System.err.println("Page " + pageName + " has no attachmnets"); + return false; + } + } } Modified: trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/MediaDataSource.java =================================================================== --- trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/MediaDataSource.java 2005-09-24 16:11:48 UTC (rev 1201) +++ trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/MediaDataSource.java 2005-09-24 16:16:18 UTC (rev 1202) @@ -19,6 +19,8 @@ public final static int EDITABLE = 100; public final static int VIEWABLE = 1; + + public static String mediaTrashName = "trash/pages"; /** * This function is executed before every save. @@ -176,4 +178,13 @@ * @param mods Sum of apropriate static variables EDITABLE and VIEWABLE */ public void setPageMod(String pageName, int mods); + + + + /**Deletes (or moves to trash) given page with or wothout optional subpages + * + * @param pageName Name of the page to delete. + * @param deleteSubpages If true - all subpages will be deleted also. + */ + public boolean deletePage(String pageName, boolean deleteSubpages); } Modified: trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/WikiEngine.java =================================================================== --- trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/WikiEngine.java 2005-09-24 16:11:48 UTC (rev 1201) +++ trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/WikiEngine.java 2005-09-24 16:16:18 UTC (rev 1202) @@ -45,6 +45,8 @@ * </p> * */ + public static String defaultPage = "Main"; + private HashMap<String, WikiPage> pages; private HashMap<String, WikiInsidePlugin> insidePlugins; @@ -481,7 +483,7 @@ String attrib = pluginLink .substring(matcher.start(), matcher.end()); - String attribName = attrib.substring(0, attrib.indexOf("'")-1); + String attribName = attrib.substring(0, attrib.indexOf("'") - 1); String attribValue = attrib.substring(attrib.indexOf("'") + 1, attrib.lastIndexOf("'")); @@ -495,4 +497,24 @@ return "<i>INSIDE PLUGIN ERROR: There is no plugin: " + pluginName + "</i>"; } + + public boolean deletePage(String pageName, boolean deleteSubpages) { + + if (getAttachementDataSource().getAttachmentsSet( + getByName(pageName, null)).size() > 0) { + if (!getAttachementDataSource().deleteAttachments(pageName)) { + return false; + } + } + + if (getMediaDataSource().deletePage(pageName, deleteSubpages)) { + + if (pages.containsKey(pageName)) { + pages.remove(pageName); + } + + return true; + } else + return false; + } } Modified: trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/plugins/AdminConsolePlugin.java =================================================================== --- trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/plugins/AdminConsolePlugin.java 2005-09-24 16:11:48 UTC (rev 1201) +++ trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/plugins/AdminConsolePlugin.java 2005-09-24 16:16:18 UTC (rev 1202) @@ -1,6 +1,7 @@ package org.jboss.wiki.plugins; import org.jboss.wiki.Credentials; +import org.jboss.wiki.WikiEngine; import org.jboss.wiki.WikiPage; import org.jboss.wiki.WikiPlugin; import org.jboss.wiki.WikiSession; @@ -9,27 +10,51 @@ @Override public WikiPage process(WikiPage wikiPage, WikiSession wikiSession) { - Credentials credentials = (Credentials) wikiSession.getAttribute("credentials"); - + Credentials credentials = (Credentials) wikiSession + .getAttribute("credentials"); + if (credentials.isAdmin()) { String actionURL = (String) wikiSession.getAttribute("actionURL"); + String errorMsg = ""; boolean editable = wikiPage.isEditable(); boolean viewable = wikiPage.isViewable(); - - if ((wikiSession.getAttribute("var1") != null) - && (wikiSession.getAttribute("var1").equals("chmod"))) { - WikiPage editingPage = wikiEngine.getByName(wikiPage.getName(), null); - - if (wikiSession.getAttribute("var2") != null) { - editingPage.setEditable(Boolean.valueOf((String) wikiSession - .getAttribute("var2"))); - editable = editingPage.isEditable(); + + if (wikiSession.getAttribute("var1") != null) { + + if (wikiSession.getAttribute("var1").equals("chmod")) { + WikiPage editingPage = wikiEngine.getByName(wikiPage + .getName(), null); + + if (wikiSession.getAttribute("var2") != null) { + editingPage.setEditable(Boolean + .valueOf((String) wikiSession + .getAttribute("var2"))); + editable = editingPage.isEditable(); + } + if (wikiSession.getAttribute("var3") != null) { + editingPage.setViewable(Boolean + .valueOf((String) wikiSession + .getAttribute("var3"))); + viewable = editingPage.isViewable(); + } } - if (wikiSession.getAttribute("var3") != null) { - editingPage.setViewable(Boolean.valueOf((String) wikiSession - .getAttribute("var3"))); - viewable = editingPage.isViewable(); + else if (wikiSession.getAttribute("var1").equals("deletePage")) { + boolean deleteSubpages = (wikiSession.getAttribute("var3") != null) ? Boolean.parseBoolean((String)wikiSession.getAttribute("var3")) : false; + + if (wikiSession.getAttribute("var2") != null) { + String nameToDelete = (String)wikiSession.getAttribute("var2"); + + if (!nameToDelete.equals(WikiEngine.defaultPage)) { + if (wikiEngine.deletePage(nameToDelete, deleteSubpages)) { + errorMsg += "Couldn't delete whole or parts of page: "+nameToDelete+" (see log for more info)"; + } + } + else { + errorMsg += "You can't delete "+WikiEngine.defaultPage+" page !"; + } + } + } } @@ -37,29 +62,46 @@ .getPageContent()); adminConsole.append("\n<hr><h3>Admin console</h3>\n"); + + if (errorMsg.length() > 0) { + adminConsole.append("<i>"+errorMsg+"</i><br />\n"); + } + + // delete page button + adminConsole.append("<a href=\"").append(actionURL) + .append("&page=Main").append( + "&var1=deletePage").append("&var2=").append( + wikiPage.getName()).append("&var3=false").append( + "\" >DELETE THIS PAGE</a><br />\n"); + + // delete page with subpages button + adminConsole.append("<a href=\"").append(actionURL) + .append("&page=Main").append(wikiPage.getName()).append( + "&var1=deletePage").append("&var2=").append( + wikiPage.getName()).append("&var3=true").append( + "\" >DELETE THIS PAGE WITH SUBPAGES</a><br />\n<br />\n"); + + // switch editable button adminConsole.append("This page ").append( - (editable) ? "is" : "isn't").append( - " editable "); + (editable) ? "is" : "isn't").append(" editable "); adminConsole.append("<a href=\"").append(actionURL) - .append("&page=").append(wikiPage.getName()).append( - "&var1=chmod").append("&var2=").append( - !editable).append("&var3=").append( - viewable).append("\" >SWITCH IT ") + .append("&page=").append( + "&var1=chmod").append("&var2=").append(!editable) + .append("&var3=").append(viewable).append("\" >SWITCH IT ") .append((editable) ? "NOT" : "").append( " EDITABLE</a><br />\n"); - + + // switch viewable button adminConsole.append("This page ").append( - (viewable) ? "is" : "isn't").append( - " viewable "); - + (viewable) ? "is" : "isn't").append(" viewable "); + adminConsole.append("<a href=\"").append(actionURL) - .append("&page=").append(wikiPage.getName()).append( - "&var1=chmod").append("&var2=").append( - editable).append("&var3=").append( - !viewable).append("\" >SWITCH IT ") - .append((viewable) ? "NOT" : "").append( - " VIEWABLE</a><br />\n"); - + .append("&page=").append(wikiPage.getName()).append( + "&var1=chmod").append("&var2=").append(editable) + .append("&var3=").append(!viewable) + .append("\" >SWITCH IT ").append((viewable) ? "NOT" : "") + .append(" VIEWABLE</a><br />\n"); + wikiPage.setPageContent(adminConsole.toString()); } return wikiPage; |