删除具有N个以上版本的Web内容的过去版本

问题描述 投票:0回答:1

我有一个Liferay 6.2服务器,其中包含许多Web内容的站点。

Problem

多年来,服务器有时变得非常慢,甚至最近触发了OutOfMemoryException。 MySQL慢查询日志显示这是由于某些Web内容有数千个版本。

Goal

由于我们不太关心版本,我们正在考虑抛弃所有旧版本,但作为避免进一步OutOfMemory异常的第一个补救措施,我们希望这样做:

删除具有100个以上版本的所有以前版本的Web内容

怎么做?

liferay liferay-6
1个回答
0
投票

转到脚本控制台(在“服务器管理”中),将其设置为“Groovy”,粘贴下面的脚本,插入相关站点的组ID(也就是站点ID,使用Liferay Web界面找到),然后执行:

import com.liferay.portlet.journal.service.JournalArticleServiceUtil
import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil
import com.liferay.portlet.journal.model.JournalArticle

/**
 * Delete past versions of articles with too many versions.
 */

/**
 * Configuration.
 */
MAX_NUMBER_OF_VERSIONS = 100
DELETIONS_LIMIT=-1 // Only delete this number of versions, in total. Useful for tests. -1 = no limit
CHUNK=10 // In order to avoid memory load, everything is performed chunk by chunk, with this size.
sites = [20182, 21987]

/**
 * Main.
 */
sites.each {
  println "\n# Site " + it
  deletePastVersionsOfArticlesWithTooManyVersions(it)
}

/**
 * Delete past versions of articles with too many versions in the given site.
 */
def deletePastVersionsOfArticlesWithTooManyVersions(groupId) {
  List<JournalArticle> articlesWithTooManyVersions = findArticlesWithTooManyVersions(groupId);
  println "\nDeleting all past versions of " + articlesWithTooManyVersions.size() + " articles with too many versions"
  articlesWithTooManyVersions.each {
    deletePastVersions(groupId, it);
  }
}

/**
  * Returns the list of articles with to many versions in the given site, as a List<JournalArticle>
  */
def findArticlesWithTooManyVersions(groupId) {
  List<JournalArticle> result = new ArrayList<JournalArticle>()

  int count = JournalArticleLocalServiceUtil.getArticlesCount(groupId)
  for (int start=0; start<count; start+=CHUNK) {
    List<JournalArticle> versions = JournalArticleLocalServiceUtil.getArticles(groupId, start, start+CHUNK, null)
    println "Page start:" + start + " size:" + versions.size()

    // Process this page
    versions.each { version ->
      // Ignore documents that are in the recycle bin.
      if (version.isInTrash()) {
        return // Equivalent of continue for closures.
      }
      // We only need to process each Web Content once, but we get one JournalArticle per version.
      // The trick here is to only process the JournalArticle if it is the latest version, as each Web Content has exactly one latest version.
      if(JournalArticleLocalServiceUtil.isLatestVersion(groupId,version.getArticleId(), version.getVersion())){
        println "Checking article " + version.getArticleId()
        numberOfVersions = JournalArticleServiceUtil.getArticlesCountByArticleId(groupId, version.getArticleId());
        if(numberOfVersions > MAX_NUMBER_OF_VERSIONS) {
          println "JournalArticle " + version.getArticleId() + " has more than " + MAX_NUMBER_OF_VERSIONS + " versions."
          result.add(version);
        }
      }
    }
  }
  return result
}

/**
  * Delete all past versions of the given article.
  */
def deletePastVersions(groupId, article) {
  int count = JournalArticleLocalServiceUtil.getArticlesCount(groupId, article.getArticleId())
  int numberOfNecessaryIterations = 1 + count/(CHUNK-1) // Plus 1 for the remainder. Minus 1 to account for the worse case where the latest version appears in all chunks.
  println "Deleting " + (count - 1) + " versions of article " + article.getArticleId() + " in " + numberOfNecessaryIterations + " iterations."
  for (int iteration=0; iteration<=numberOfNecessaryIterations; iteration++) {
  // Paging is refreshed after deletion so we must always get the first elements.
  List<JournalArticle> versions = JournalArticleServiceUtil.getArticlesByArticleId(groupId, article.getArticleId(), 0, CHUNK, null)

    println "Iteration " + iteration + " Deleting " + versions.size() + " past versions of article " + article.getArticleId() // Actually one less if this chunk contains the latest version.
    for(version in versions) {
       if(JournalArticleLocalServiceUtil.isLatestVersion(groupId, version.getArticleId(), version.getVersion())) {
         println "Skipping the latest version." // The latest version must NOT be deleted.
         continue; // Proceed to the next version of this chunk.
      }
      // Delete this version
      println "Deleting article " + version.getArticleId() + " version " + version.getVersion() + " " + new Date()
      if (DELETIONS_LIMIT!=0) {
        DELETIONS_LIMIT--
        JournalArticleLocalServiceUtil.deleteArticle(groupId, version.getArticleId(), version.getVersion(), null,null)
        println "Deletion performed"
      }
      if (DELETIONS_LIMIT==0){
        return // Exit the method
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.