Forked from datyger/lr62-delete-web-content-versions.groovy
Created
February 20, 2020 10:22
-
-
Save lahiponeja/d56cbb1282585e7d5cb9680b8e54ef38 to your computer and use it in GitHub Desktop.
This runs through any / all web content resource objects and cycles through the historical versions of each, deleting any that are not the latest.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.liferay.portal.service.ServiceContext | |
import com.liferay.portlet.journal.service.JournalArticleServiceUtil | |
import com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil | |
import java.util.List | |
import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery | |
import com.liferay.portal.kernel.exception.PortalException | |
import com.liferay.portal.kernel.exception.SystemException | |
import com.liferay.portal.util.PropsValues | |
import com.liferay.portlet.journal.service.persistence.JournalArticleResourceActionableDynamicQuery | |
import com.liferay.portlet.journal.service.JournalArticleServiceUtil | |
import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil | |
previewMode = true; | |
if(previewMode) { | |
out.println( | |
"""<div class="portlet-msg-alert">Preview mode is on: switch off the flag and execute this script | |
again to make changes to the database</div>""") | |
} | |
def SCRIPT_ID = "Delete-Web-Content-Versions" | |
outputFile = new File( | |
"""${System.getProperty("liferay.home")}/scripting/out-${SCRIPT_ID}.txt""") | |
outputFile.getParentFile().mkdirs() | |
def trace(message) { | |
out.println(message) | |
outputFile << "${message}\n" | |
} | |
int articlesResourcesCount = JournalArticleResourceLocalServiceUtil.getJournalArticleResourcesCount(); | |
println('Total articles to process: ' + articlesResourcesCount) | |
//Retrieve the JournalArticleResources: | |
articles = JournalArticleResourceLocalServiceUtil.getJournalArticleResources(0,100); | |
for( i=0; i< articles.size() ; i++){ | |
article = articles[i] | |
long aGI = article.groupId | |
//println('article groupId=' + aGI) | |
String ai = article.getArticleId() | |
//println('The articleId=' + ai) | |
articleversions = JournalArticleServiceUtil.getArticlesByArticleId(aGI,ai, -1, -1, null) | |
for(x=0; x< articleversions.size() ; x++){ | |
try{ | |
articleversion = articleversions[x] | |
long apk = articleversion.getResourcePrimKey() | |
latestversion = JournalArticleServiceUtil.getLatestArticle(apk) | |
trace('This articleId ' + articleversion.id + ' with ResourcePrimKey of ' + articleversion.getResourcePrimKey() + ' has version of ' + articleversion.version) | |
trace('The latest version has articleId of ' + latestversion.id + ' and ResourcePrimKey of ' + latestversion.getResourcePrimKey() + ' has version of ' + latestversion.version) | |
if(articleversion.version != latestversion.version){ | |
trace('Looks like we should delete version: ' + articleversion.version + ' with GroupId=' + articleversion.groupId + ' with articleId=' + articleversion.articleId ); | |
if(!previewMode) { | |
JournalArticleLocalServiceUtil.deleteArticle(articleversion.groupId, articleversion.articleId, articleversion.version, null, null); | |
} | |
} | |
} catch (Exception e1) { | |
println(e1) | |
e1.printStackTrace(out) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import com.liferay.portal.service.ServiceContext
import com.liferay.portlet.journal.service.JournalArticleServiceUtil
import com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil
import java.util.List
import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery
import com.liferay.portal.kernel.exception.PortalException
import com.liferay.portal.kernel.exception.SystemException
import com.liferay.portal.util.PropsValues
import com.liferay.portlet.journal.service.persistence.JournalArticleResourceActionableDynamicQuery
import com.liferay.portlet.journal.service.JournalArticleServiceUtil
import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil
previewMode = true;
if(previewMode) {
out.println(
"""
again to make changes to the database
}
def SCRIPT_ID = "Delete-Web-Content-Versions"
outputFile = new File(
"""${System.getProperty("liferay.home")}/scripting/out-${SCRIPT_ID}.txt""")
outputFile.getParentFile().mkdirs()
def trace(message) {
out.println(message)
outputFile << "${message}\n"
}
int articlesResourcesCount = JournalArticleResourceLocalServiceUtil.getJournalArticleResourcesCount();
//Retrieve the JournalArticleResources:
articles = JournalArticleResourceLocalServiceUtil.getJournalArticleResources(0,100);