On Thursday 29 September 2005 17:16, Michael Wechner wrote:
> Michael Ralston wrote:
> >
> >Are there any improvements to the way StaticHTMLExporter works which can
> > be merged back into an existing 1.2.4 project?
>
> I would be great if you could improve it and contribute according
> patches
>
Well I've made some changes to
org.apache.lenya.cms.publishing.StaticHTMLExporter. Attached is a patch.
My site only has 3 levels deep in it's navigation. All top level pages appear
across the top of the screen in the lenya 'tabs' div. In the lenya 'menu' div
I have pages which are children of the current top level page. When you visit
one of these 2nd level pages, the third level pages also appear under them on
the navigation.
So for example I have published a top level page and 3 2nd level pages. I want
to publish another 2nd level page which is a child of the top level and a
sibling to the other 2nd level. These already published pages will need to be
exported again so that the new page appears on their navigation.
Also, when deactivating a page which is a child/sibling, the navigation will
need to be updated on pages so there is no dead links. And the deactivated
page will be deleted from the target by the cunningly named WDelete.
That is what this patch does :) It just adds the parent and siblings of a page
to the list of pages to export.
Oh, I also found a little bug when using StaticHTMLExport on a windows
machine, the '\' in the path name were not being replaced in the
createFileName method of wget, see last 10 line of patch.
And please excuse my System.out.println all over the place :P
Michael Ralston
diff -Nurd apache-lenya-1.2.4-orig/src/java/org/apache/lenya/cms/publishing/StaticHTMLDeactivator.java apache-lenya-1.2.4-src/src/java/org/apache/lenya/cms/publishing/StaticHTMLDeactivator.java
--- apache-lenya-1.2.4-orig/src/java/org/apache/lenya/cms/publishing/StaticHTMLDeactivator.java 1970-01-01 10:00:00.000000000 +1000
+++ apache-lenya-1.2.4-src/src/java/org/apache/lenya/cms/publishing/StaticHTMLDeactivator.java 2005-09-30 11:20:52.170708008 +1000
@@ -0,0 +1,172 @@
+/**
+ *
+ */
+package org.apache.lenya.cms.publishing;
+
+import java.io.File;
+import java.net.URL;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+import java.util.StringTokenizer;
+
+import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.lenya.cms.publication.SiteTree;
+import org.apache.lenya.cms.publication.SiteTreeNode;
+import org.apache.lenya.cms.publication.file.FilePublication;
+import org.apache.lenya.cms.task.ExecutionException;
+import org.apache.log4j.Category;
+
+/**
+ * This class will export any page which is a parent or sibling to the page being deactivated.
+ */
+public class StaticHTMLDeactivator extends StaticHTMLExporter {
+
+ private static Category log = Category.getInstance(StaticHTMLExporter.class);
+ public static final String PARAMETER_DOCUMENT_ID = "document-id";
+
+ /**
+ * This method is the complement of export,
+ * it will delete files from the destination which has been deactivated.
+ *
+ * @param serverURI The hostname of the server which lenya is running on.
+ * @param serverPort The port which lenya is running on
+ * @param publicationPath The path which the publication can be found under
+ * @param exportPath The path we want the publication exported to
+ * @param uris The pages we want deactivated
+ * @param substituteExpression What to find
+ * @param substituteReplacement What to replace
+ *
+ * @throws ExportException If anything doesn't work we throw one of these?
+ */
+ public void deactivate(URL serverURI, int serverPort, String publicationPath, String exportPath, String uri, String substituteExpression, String substituteReplacement)
+ throws ExportException {
+ try {
+ String exportDirectory = publicationPath + exportPath;
+
+ if (new File(exportPath).isAbsolute()) {
+ exportDirectory = exportPath;
+ }
+
+ log.info(".export(): Export directory: " + exportDirectory + " (" + publicationPath +
+ " , " + exportPath + ")");
+
+ org.apache.lenya.net.WDelete wdelete = new org.apache.lenya.net.WDelete();
+ wdelete.setDirectoryPrefix(exportDirectory);
+
+ String fullServerURI = serverURI + ":" + serverPort;
+
+ URL url = new URL(fullServerURI + uri);
+ log.info(".export(): Export static HTML: " + uri);
+
+ wdelete.delete(url, substituteExpression, substituteReplacement);
+ } catch (Exception e) {
+ throw new ExportException(e);
+ }
+ }
+
+
+ /**
+ * This is called by org.apache.lenya.cms.task.TaskSequence when a user deactivate.
+ *
+ * @param contextPath The directory when the lenya webapp lives
+ */
+ public void execute(String contextPath) throws ExecutionException {
+ try {
+ String publicationId = getParameters().getParameter(PARAMETER_PUBLICATION_ID);
+
+ Parameters taskParameters = new Parameters();
+
+ PublishingEnvironment environment = new PublishingEnvironment(contextPath, publicationId);
+
+ // read default parameters from PublishingEnvironment
+ taskParameters.setParameter(PublishingEnvironment.PARAMETER_EXPORT_PATH,
+ environment.getExportDirectory());
+ taskParameters.setParameter(PublishingEnvironment.PARAMETER_SUBSTITUTE_REGEXP,
+ environment.getSubstituteExpression());
+ taskParameters.setParameter(PublishingEnvironment.PARAMETER_SUBSTITUTE_REPLACEMENT,
+ environment.getSubstituteReplacement());
+
+ taskParameters.merge(getParameters());
+ parameterize(taskParameters);
+
+ String publicationPath = PublishingEnvironment.getPublicationPath(contextPath,
+ publicationId);
+
+ int serverPort = getParameters().getParameterAsInteger(PARAMETER_SERVER_PORT);
+ log.debug(".execute(): Server Port: " + serverPort);
+
+ String serverURI = getParameters().getParameter(PARAMETER_SERVER_URI);
+
+ String documentId = getParameters().getParameter(PARAMETER_DOCUMENT_ID);
+
+ // find pages that are parents or siblings of this page
+ // we look for them in the authoring tree and then check if they are also in the live tree
+ // we rewrite pages in the live tree so they no longer have links to the removed page.
+ FilePublication fp=new FilePublication(publicationId,contextPath);
+ SiteTree authoringTree=fp.getTree("authoring");
+ SiteTree liveTree=fp.getTree("live");
+
+ Set rewriteUris = new HashSet();
+
+
+
+ // look up the document in the authoring tree
+ SiteTreeNode node=authoringTree.getNode(documentId);
+
+ if (node!=null && node.visibleInNav()) {
+ // if we found it and it's visible, get it's parent
+ SiteTreeNode parent=node.getParent();
+
+ if (parent!=null && parent.visibleInNav()) {
+ System.out.println("Node has parent");
+
+ // get the children of the parent, which are
+ // obviously siblings of the original node
+ // check if the sibling is in the live tree,
+ // and add it to the list to rewrite if it is
+ SiteTreeNode[] siblings=parent.getChildren();
+ for (int i=0; i<siblings.length; i++) {
+ String relatedDocumentId=siblings[i].getAbsoluteId();
+
+ SiteTreeNode liveNode=liveTree.getNode(relatedDocumentId);
+ if (liveNode != null) {
+ String rewriteUri="/"+publicationId+"/"+"live"+relatedDocumentId+".html";
+ rewriteUris.add(rewriteUri);
+ }
+ }
+ // it's safe to assume the parent node is in the live tree
+ rewriteUris.add("/"+publicationId+"/"+"live"+parent.getAbsoluteId()+".html");
+ }
+ } else {
+ System.out.println("Node not visible");
+ }
+
+ int i=0;
+ String[] writeUris=new String[rewriteUris.size()];
+
+ for (Iterator iter=rewriteUris.iterator(); iter.hasNext();) {
+ Object obj=iter.next();
+ if (String.class.isInstance(obj))
+ writeUris[i++]=(String)obj;
+ }
+
+ // export the page we have been asked to export
+ export(new URL(serverURI), serverPort, publicationPath,
+ getParameters().getParameter(PublishingEnvironment.PARAMETER_EXPORT_PATH), writeUris,
+ getParameters().getParameter(PublishingEnvironment.PARAMETER_SUBSTITUTE_REGEXP),
+ getParameters().getParameter(PublishingEnvironment.PARAMETER_SUBSTITUTE_REPLACEMENT));
+
+ String deleteUri="/"+publicationId+"/"+"live"+documentId+".html";
+
+ deactivate(new URL(serverURI), serverPort, publicationPath,
+ getParameters().getParameter(PublishingEnvironment.PARAMETER_EXPORT_PATH), deleteUri,
+ getParameters().getParameter(PublishingEnvironment.PARAMETER_SUBSTITUTE_REGEXP),
+ getParameters().getParameter(PublishingEnvironment.PARAMETER_SUBSTITUTE_REPLACEMENT));
+
+ } catch (Exception e) {
+ throw new ExecutionException(e);
+ }
+ }
+
+}
diff -Nurd apache-lenya-1.2.4-orig/src/java/org/apache/lenya/cms/publishing/StaticHTMLExporter.java apache-lenya-1.2.4-src/src/java/org/apache/lenya/cms/publishing/StaticHTMLExporter.java
--- apache-lenya-1.2.4-orig/src/java/org/apache/lenya/cms/publishing/StaticHTMLExporter.java 2005-06-26 10:52:32.000000000 +1000
+++ apache-lenya-1.2.4-src/src/java/org/apache/lenya/cms/publishing/StaticHTMLExporter.java 2005-09-30 10:30:27.496528376 +1000
@@ -21,9 +21,15 @@
import java.io.File;
import java.net.URL;
+import java.util.Iterator;
import java.util.StringTokenizer;
+import java.util.HashSet;
+import java.util.Set;
import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.lenya.cms.publication.SiteTree;
+import org.apache.lenya.cms.publication.SiteTreeNode;
+import org.apache.lenya.cms.publication.file.FilePublication;
import org.apache.lenya.cms.task.ExecutionException;
import org.apache.log4j.Category;
@@ -46,14 +52,15 @@
/**
* DOCUMENT ME!
*
- * @param serverURI DOCUMENT ME!
- * @param serverPort DOCUMENT ME!
- * @param publicationPath DOCUMENT ME!
- * @param exportPath DOCUMENT ME!
- * @param uris DOCUMENT ME!
- * @param substituteExpression DOCUMENT ME!
- *
- * @throws ExportException DOCUMENT ME!
+ * @param serverURI The hostname of the server which lenya is running on.
+ * @param serverPort The port which lenya is running on
+ * @param publicationPath The path which the publication can be found under
+ * @param exportPath The path we want the publication exported to
+ * @param uris The pages we want exported
+ * @param substituteExpression What to find
+ * @param substituteReplacement What to replace
+ *
+ * @throws ExportException If anything doesn't work we throw one of these?
*/
public void export(URL serverURI, int serverPort, String publicationPath, String exportPath,
String[] uris, String substituteExpression, String substituteReplacement)
@@ -85,9 +92,9 @@
}
/**
- * DOCUMENT ME!
+ * This is called by org.apache.lenya.cms.task.TaskSequence when a use clicks publish.
*
- * @param contextPath DOCUMENT ME!
+ * @param contextPath The directory when the lenya webapp lives
*/
public void execute(String contextPath) throws ExecutionException {
try {
@@ -118,17 +125,60 @@
String urisString = getParameters().getParameter(PARAMETER_URIS);
StringTokenizer st = new StringTokenizer(urisString, ",");
- String[] uris = new String[st.countTokens()];
- int i = 0;
+ Set requestedUris = new HashSet();
while (st.hasMoreTokens()) {
- uris[i++] = st.nextToken();
+ requestedUris.add(st.nextToken());
+ }
+
+ // find pages that are parents or siblings of this page
+ FilePublication fp=new FilePublication(publicationId,contextPath);
+ SiteTree tree=fp.getTree("live");
+
+ Set relatedUris = new HashSet();
+
+ for (Iterator iter=requestedUris.iterator(); iter.hasNext();) {
+ String uri=(String)iter.next();
+ int endPubStartArea=uri.indexOf('/',1);
+ int endAreaStartDoc=uri.indexOf('/',endPubStartArea+1);
+ String documentIdPlusXML=uri.substring(endAreaStartDoc);
+ String documentId=documentIdPlusXML.substring(0,documentIdPlusXML.indexOf('.'));
+ SiteTreeNode node=tree.getNode(documentId);
+ if (node!=null && node.visibleInNav()) {
+ SiteTreeNode parent=node.getParent();
+ if (parent!=null && parent.visibleInNav()) {
+ System.out.println("Node has parent");
+ SiteTreeNode[] siblings=parent.getChildren();
+ for (int i=0; i<siblings.length; i++) {
+ String relatedUri="/"+publicationId+"/"+"live"+siblings[i].getAbsoluteId()+".html";
+ relatedUris.add(relatedUri);
+ }
+ relatedUris.add("/"+publicationId+"/"+"live"+parent.getAbsoluteId()+".html");
+ }
+ } else {
+ System.out.println("Node not visible");
+ }
+ }
+
+ Set joinedUris=new HashSet();
+ joinedUris.addAll(requestedUris);
+ joinedUris.addAll(relatedUris);
+
+ int i=0;
+ String[] uris=new String[joinedUris.size()];
+
+ for (Iterator iter=joinedUris.iterator(); iter.hasNext();) {
+ Object obj=iter.next();
+ if (String.class.isInstance(obj))
+ uris[i++]=(String)obj;
}
+ // export the page we have been asked to export
export(new URL(serverURI), serverPort, publicationPath,
getParameters().getParameter(PublishingEnvironment.PARAMETER_EXPORT_PATH), uris,
getParameters().getParameter(PublishingEnvironment.PARAMETER_SUBSTITUTE_REGEXP),
getParameters().getParameter(PublishingEnvironment.PARAMETER_SUBSTITUTE_REPLACEMENT));
+
} catch (Exception e) {
throw new ExecutionException(e);
}
diff -Nurd apache-lenya-1.2.4-orig/src/java/org/apache/lenya/net/WDelete.java apache-lenya-1.2.4-src/src/java/org/apache/lenya/net/WDelete.java
--- apache-lenya-1.2.4-orig/src/java/org/apache/lenya/net/WDelete.java 1970-01-01 10:00:00.000000000 +1000
+++ apache-lenya-1.2.4-src/src/java/org/apache/lenya/net/WDelete.java 2005-09-30 10:54:08.951434384 +1000
@@ -0,0 +1,63 @@
+/**
+ *
+ */
+package org.apache.lenya.net;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+
+import org.apache.log4j.Category;
+
+/**
+ * Similar to wget, but deletes files instead of writing them :)
+ *
+ */
+public class WDelete {
+
+ static Category log = Category.getInstance(WGet.class);
+ String directory_prefix = null;
+
+ /**
+ *
+ */
+ public WDelete() {
+ directory_prefix = System.getProperty("user.dir");
+ }
+
+ // the directory in which we will be deleting stuff
+ public void setDirectoryPrefix(String directory_prefix) {
+ this.directory_prefix = directory_prefix;
+ }
+
+ /**
+ * @param url The url of the resource to DELETE
+ * @param prefixSubstitute Regexp which shall be replaced
+ * @param substituteReplacement Replacement of the regexp
+ *
+ */
+ public void delete(URL url, String prefixSubstitute, String substituteReplacement)
+ throws IOException {
+ log.debug(".download(): " + url + " " + prefixSubstitute + " " + substituteReplacement);
+
+ File deleteThisFile=new File(deleteFileName(url, prefixSubstitute, substituteReplacement));
+
+ if (deleteThisFile.delete()) {
+ System.out.println("WDelete successfully deleted: "+deleteThisFile.getAbsolutePath());
+ } else {
+ System.out.println("WDelete FAILED TO DELETE: "+deleteThisFile.getAbsolutePath());
+ throw new IOException("Failed to delete"+deleteThisFile.getAbsolutePath());
+ }
+ }
+
+ /**
+ * @param url URL of resource, which has been downloaded and shall be DELETED
+ * @return Absolute substituted filename
+ */
+ public String deleteFileName(URL url, String prefixSubstitute, String substituteReplacement) {
+ File file = new File(directory_prefix + File.separator + url.getFile());
+
+ return file.getAbsolutePath().replaceAll(prefixSubstitute, substituteReplacement);
+ }
+
+}
diff -Nurd apache-lenya-1.2.4-orig/src/java/org/apache/lenya/net/WGet.java apache-lenya-1.2.4-src/src/java/org/apache/lenya/net/WGet.java
--- apache-lenya-1.2.4-orig/src/java/org/apache/lenya/net/WGet.java 2005-06-26 10:52:31.000000000 +1000
+++ apache-lenya-1.2.4-src/src/java/org/apache/lenya/net/WGet.java 2005-09-30 17:08:51.742021688 +1000
@@ -262,6 +262,7 @@
log.warn(".saveToFile(): Directory will be created: " + parent.getAbsolutePath());
parent.mkdirs();
}
+ System.out.println("Writing to: "+file.getAbsolutePath());
FileOutputStream out = new FileOutputStream(file.getAbsolutePath());
out.write(bytes);
@@ -274,8 +275,9 @@
*/
public String createFileName(URL url, String prefixSubstitute, String substituteReplacement) {
File file = new File(directory_prefix + File.separator + url.getFile());
-
- return file.getAbsolutePath().replaceAll(prefixSubstitute, substituteReplacement);
+ String fixedPrefixSubstitute=prefixSubstitute.replace('/',File.separatorChar);
+ String fixedSubstituteReplacement=substituteReplacement.replace('/',File.separatorChar);
+ return file.getAbsolutePath().replaceAll(fixedPrefixSubstitute, fixedSubstituteReplacement);
}
/**
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]