From 54744af3d4ae404d28a3d5dcdc083fc2f521ecff Mon Sep 17 00:00:00 2001 From: jensp Date: Fri, 13 Feb 2015 12:24:03 +0000 Subject: [PATCH] ContentItemNameFix tool git-svn-id: https://svn.libreccm.org/ccm/trunk@3142 8810af33-2d31-482b-a856-94f89814c4df --- .../cms/ui/workflow/TaskAddUser.java | 2 +- .../cms/util/ContentItemNameFix.java | 274 ++++++++++++++++-- 2 files changed, 256 insertions(+), 20 deletions(-) diff --git a/ccm-cms/src/com/arsdigita/cms/ui/workflow/TaskAddUser.java b/ccm-cms/src/com/arsdigita/cms/ui/workflow/TaskAddUser.java index fd3231c85..010163bee 100755 --- a/ccm-cms/src/com/arsdigita/cms/ui/workflow/TaskAddUser.java +++ b/ccm-cms/src/com/arsdigita/cms/ui/workflow/TaskAddUser.java @@ -148,7 +148,7 @@ class TaskAddUser extends SimpleContainer { if (users == null) { throw new FormProcessException - ((GlobalizationUtil.globalize("cms.ui.workflow.no_users_were_selected")); + ((GlobalizationUtil.globalize("cms.ui.workflow.no_users_were_selected"))); } else { // Add each checked user to the task. diff --git a/ccm-cms/src/com/arsdigita/cms/util/ContentItemNameFix.java b/ccm-cms/src/com/arsdigita/cms/util/ContentItemNameFix.java index 78dc3fee9..e7e1fe7f1 100644 --- a/ccm-cms/src/com/arsdigita/cms/util/ContentItemNameFix.java +++ b/ccm-cms/src/com/arsdigita/cms/util/ContentItemNameFix.java @@ -22,6 +22,8 @@ import com.arsdigita.cms.ContentBundle; import com.arsdigita.cms.ContentItem; import com.arsdigita.cms.ItemCollection; import com.arsdigita.cms.Folder; +import com.arsdigita.cms.TextAsset; +import com.arsdigita.cms.contenttypes.GenericArticle; import com.arsdigita.kernel.Kernel; import com.arsdigita.kernel.KernelExcursion; import com.arsdigita.persistence.DataCollection; @@ -34,6 +36,9 @@ import com.arsdigita.util.cmd.Program; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.OptionBuilder; +import java.util.HashSet; +import java.util.Set; + /** * * @author Jens Pelzetter @@ -77,6 +82,8 @@ public class ContentItemNameFix extends Program { protected void excurse() { setEffectiveParty(Kernel.getSystemParty()); + final Set linksToCheck = new HashSet(); + final Session session = SessionManager.getSession(); final TransactionContext transactionContext = session.getTransactionContext(); @@ -84,53 +91,87 @@ public class ContentItemNameFix extends Program { final DataCollection draftFolders = session.retrieve(Folder.BASE_DATA_OBJECT_TYPE); draftFolders.addEqualsFilter(ContentItem.VERSION, "draft"); - + while (draftFolders.next()) { - checkFolder(draftFolders.getDataObject()); + checkFolder(draftFolders.getDataObject(), linksToCheck); } - + final DataCollection draftBundles = session.retrieve( ContentBundle.BASE_DATA_OBJECT_TYPE); draftBundles.addEqualsFilter(ContentItem.VERSION, "draft"); while (draftBundles.next()) { - checkBundle(draftBundles.getDataObject()); + checkBundle(draftBundles.getDataObject(), linksToCheck); } transactionContext.commitTxn(); + System.out.println("-------------------------------------------------------------"); + + System.out.println("Checking for potentially brocken links..."); + System.out.println("GenericArticle (ccm-cms-types-article, ccm-cms-types-news, ..."); + System.out.println(""); + + final DataCollection articles = session.retrieve( + GenericArticle.BASE_DATA_OBJECT_TYPE); + articles.addEqualsFilter(ContentItem.VERSION, "draft"); + while (articles.next()) { + checkArticle(articles.getDataObject(), linksToCheck); + } + + System.out.println(""); + System.out.println("MultiPartArticles..."); + System.out.println(""); + + final DataCollection mpArticles = session.retrieve( + "com.arsdigita.cms.contenttypes.MultiPartArticle"); + mpArticles.addEqualsFilter(ContentItem.VERSION, "draft"); + while (mpArticles.next()) { + checkMpArticle(mpArticles.getDataObject(), linksToCheck); + } + } }.run(); } - private void checkFolder(final DataObject folderObj) { - + private void checkFolder(final DataObject folderObj, final Set linksToCheck) { + final Folder draftFolder = new Folder(folderObj); final Folder liveFolder = (Folder) draftFolder.getLiveVersion(); - + if (liveFolder != null && !draftFolder.getName().equals(liveFolder.getName())) { - System.out.printf("Problems with folder %s:/%s (id: %s):\n", - draftFolder.getContentSection().getName(), - draftFolder.getPath(), + System.out.printf("Problems with folder %s:/%s (id: %s):\n", + draftFolder.getContentSection().getName(), + draftFolder.getPath(), draftFolder.getID().toString()); - System.out.printf("\t Live Folder has wrong name: Is '%s' but should be '%s'.", + System.out.printf("\t Live Folder has wrong name: Is '%s' but should be '%s'.", liveFolder.getName(), draftFolder.getName()); - + + linksToCheck.add(new LinkToCheck(liveFolder.getName(), + draftFolder.getName(), + String.format("%s:/%s", + liveFolder.getContentSection().getName(), + liveFolder.getPath()), + String.format("%s:/%s", + draftFolder.getContentSection().getName(), + draftFolder.getPath()))); + if (pretend) { System.out.print("\n\n"); } else { + liveFolder.setName(draftFolder.getName()); System.out.print(" Corrected.\n\n"); } - + } } - - private void checkBundle(final DataObject bundleObj) { + + private void checkBundle(final DataObject bundleObj, final Set linksToCheck) { final ContentBundle draftBundle = new ContentBundle(bundleObj); final ContentItem primaryDraftItem = draftBundle.getPrimaryInstance(); @@ -153,6 +194,14 @@ public class ContentItemNameFix extends Program { "\t Draft ContentBundle has wrong name: Is '%s' but should be '%s'.", itemName, draftBundle.getName()); + + linksToCheck.add(new LinkToCheck(draftBundle.getName(), + itemName, + String.format("%s:/%s", + draftBundle.getContentSection().getName(), + draftBundle.getPath()), + itemPath)); + if (pretend) { System.out.print("\n"); } else { @@ -161,7 +210,7 @@ public class ContentItemNameFix extends Program { } } - checkInstances(draftBundle, itemName, itemId, itemPath, headerStatus); + checkInstances(draftBundle, itemName, itemId, itemPath, headerStatus, linksToCheck); final ContentBundle liveBundle = (ContentBundle) draftBundle.getLiveVersion(); if (liveBundle != null) { @@ -173,6 +222,14 @@ public class ContentItemNameFix extends Program { itemName, liveBundle.getName()); + linksToCheck.add(new LinkToCheck(liveBundle.getName(), + itemName, + String.format("%s:/%s", + liveBundle.getContentSection() + .getName(), + liveBundle.getPath()), + itemPath)); + if (pretend) { System.out.print("\n"); } else { @@ -181,9 +238,9 @@ public class ContentItemNameFix extends Program { } } - checkInstances(liveBundle, itemName, itemId, itemPath, headerStatus); + checkInstances(liveBundle, itemName, itemId, itemPath, headerStatus, linksToCheck); } - + if (headerStatus.isHeaderPrinted()) { System.out.print("\n"); } @@ -194,7 +251,8 @@ public class ContentItemNameFix extends Program { final String itemName, final String itemId, final String itemPath, - final HeaderStatus headerStatus) { + final HeaderStatus headerStatus, + final Set linksToCheck) { final ItemCollection instances = draftBundle.getInstances(); ContentItem current; while (instances.next()) { @@ -209,6 +267,14 @@ public class ContentItemNameFix extends Program { current.getLanguage(), itemName, current.getName()); + + linksToCheck.add(new LinkToCheck(current.getName(), + itemName, + String.format("%s:/%s", + current.getContentSection().getName(), + current.getPath()), + itemPath)); + if (pretend) { System.out.print("\n"); } else { @@ -247,4 +313,174 @@ public class ContentItemNameFix extends Program { } } + private class LinkToCheck { + + private String wrongName; + private String correctName; + private String wrongPath; + private String correctPath; + + public LinkToCheck() { + //Nothing + } + + public LinkToCheck(final String wrongName, + final String correctName, + final String wrongPath, + final String correctPath) { + this.wrongName = wrongName; + this.correctName = correctName; + this.wrongPath = wrongPath; + this.correctPath = correctPath; + } + + public String getWrongName() { + return wrongName; + } + + public void setWrongName(final String wrongName) { + this.wrongName = wrongName; + } + + public String getCorrectName() { + return correctName; + } + + public void setCorrectName(final String correctName) { + this.correctName = correctName; + } + + public String getWrongPath() { + return wrongPath; + } + + public void setWrongPath(final String wrongPath) { + this.wrongPath = wrongPath; + } + + public String getCorrectPath() { + return correctPath; + } + + public void setCorrectPath(final String correctPath) { + this.correctPath = correctPath; + } + + + @Override + public int hashCode() { + int hash = 7; + if (wrongName == null) { + hash = 47 * hash; + } else { + hash = 47 * hash + wrongName.hashCode(); + } + + if (correctName == null) { + hash = 47 * hash; + } else { + hash = 47 * hash + correctName.hashCode(); + } + + return hash; + } + + @Override + public boolean equals(final Object obj) { + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final LinkToCheck other = (LinkToCheck) obj; + if (wrongName == null && other.getWrongName() != null) { + return false; + } + + if (wrongName != null && other.getWrongName() == null) { + return false; + } + + if (correctName == null && other.getCorrectName() != null) { + return false; + } + + if (correctName != null && other.getCorrectName() == null) { + return false; + } + + return ((correctName.equals(other.getCorrectName())) + && (wrongName.equals(other.getWrongName()))); + } + + } + + private void checkArticle(final DataObject articleObj, + final Set linksToCheck) { + final GenericArticle article = new GenericArticle(articleObj); + + final TextAsset textAsset = article.getTextAsset(); + + if (textAsset == null) { + return; + } + + final String text = textAsset.getText(); + + if (text == null) { + return; + } + + for (LinkToCheck linkToCheck : linksToCheck) { + if (text.contains(linkToCheck.getWrongName())) { + System.out.printf("Found a potenially brocken link in article item %s:/%s:\n", + article.getContentSection().getName(), + article.getPath()); + System.out.printf("\tLook for a link containing to path '%s' and replace it with " + + "the stable link to the target item.\n\n", + linkToCheck.getWrongPath()); + } + } + } + + private void checkMpArticle(final DataObject mpArticleObj, + final Set linksToCheck) { + final ContentItem mpItem = new ContentItem(mpArticleObj); + final DataCollection sections = (DataCollection) mpArticleObj.get("sections"); + + while (sections.next()) { + checkMpSection(mpItem, sections.getDataObject(), linksToCheck); + } + } + + private void checkMpSection(final ContentItem mpItem, + final DataObject sectionObj, + final Set linksToCheck) { + final DataObject textAssetObj = (DataObject) sectionObj.get("text"); + + if (textAssetObj == null) { + return; + } + + final String text = (String) textAssetObj.get(TextAsset.CONTENT); + + if (text == null) { + return; + } + + for (LinkToCheck linkToCheck : linksToCheck) { + if (text.contains(linkToCheck.getWrongName())) { + System.out.printf("Found a potenially brocken link in section '%s' of " + + "MultiPartArticle %s:/%s.\n", + (String) sectionObj.get("title"), + mpItem.getContentSection().getName(), + mpItem.getPath()); + System.out.printf("\tLook for a link containing to path '%s' and replace it with " + + "the stable link to the target item.\n\n", + linkToCheck.getWrongPath()); + } + } + } + }