Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lahiponeja/d56cbb1282585e7d5cb9680b8e54ef38 to your computer and use it in GitHub Desktop.
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.
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)
}
}
}
@lahiponeja
Copy link
Author

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(
"""

Preview mode is on: switch off the flag and execute this script
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();

println('Total de articulos a examinar: ' + articlesResourcesCount)

//Retrieve the JournalArticleResources:

articles = JournalArticleResourceLocalServiceUtil.getJournalArticleResources(0,100);

    for( i=0; i< articles.size() ; i++){
       article = articles[i]
       long aGI = article.groupId
       String ai = article.getArticleId()
       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)
               if(articleversion.version != latestversion.version){
					  trace('La ultima version de este articleId  ' + latestversion.id + ' con  ResourcePrimKey ' + latestversion.getResourcePrimKey() + ' es ' + latestversion.version)
					  trace('Esta versión sera borrada:');
                      trace(' articleId ' + articleversion.id + ' con ResourcePrimKey No. ' + articleversion.getResourcePrimKey() + ' tiene una version ' + articleversion.version)
					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