Skip to content

Instantly share code, notes, and snippets.

@datyger
Last active June 10, 2022 19:58
Show Gist options
  • Save datyger/5d3f65bfc2841d5a503aafcc4e1b3a85 to your computer and use it in GitHub Desktop.
Save datyger/5d3f65bfc2841d5a503aafcc4e1b3a85 to your computer and use it in GitHub Desktop.
Script to delete all old versions of web content besides the latest version.
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.portal.kernel.dao.orm.DynamicQueryFactoryUtil
import com.liferay.portal.kernel.dao.orm.DynamicQuery
import com.liferay.portal.kernel.dao.orm.Conjunction
import com.liferay.portlet.journal.service.persistence.JournalArticleResourceActionableDynamicQuery
import com.liferay.portlet.journal.service.JournalArticleServiceUtil
import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil
import com.liferay.portlet.journal.model.JournalArticleResourceModel
import com.liferay.portlet.journal.model.JournalArticleResource
import com.liferay.portlet.journal.model.JournalArticleModel
final 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-WC-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"
}
try {
final sc = new com.liferay.portal.service.ServiceContext()
ActionableDynamicQuery actionableDynamicQuery =
new JournalArticleResourceActionableDynamicQuery() {
@Override
protected void performAction(Object object)
throws PortalException, SystemException {
JournalArticleResource article = (JournalArticleResource)object
//trace("Made it this far" + article.articleId)
long aGI = article.groupId
//println('article groupId=' + aGI)
String ai = article.getArticleId()
//println('The articleId=' + ai)
List<JournalArticleResource> articleversions = JournalArticleServiceUtil.getArticlesByArticleId(aGI,ai, -1, -1, null)
for(JournalArticleResource articleversion : articleversions) {
long apk = articleversion.getResourcePrimKey()
JournalArticleModel 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 latest version=' + latestversion.version + ' - So 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);
}
}
}
}
}
actionableDynamicQuery.performActions()
}
catch (Exception e) {
println(e)
e.printStackTrace(out)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment