-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcreateCSVduplicateTags.groovy
94 lines (80 loc) · 2.94 KB
/
createCSVduplicateTags.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
Create a CSV File for Duplicate Tags List in the Application.
This script can be used to generate a CSV output and store into filesystem . It lists down all the tags which are Duplicate and all the pages where they are being used. It will help to analyse the System Taxonomy.
/** @author Hashim Khan */
import org.apache.sling.api.resource.Resource
import com.day.cq.tagging.Tag
import org.apache.sling.api.resource.ResourceResolver
def filePath = "/opt/adobe/output.csv"
def tagLocation = "/etc/tags/geometrixx-media"
def buildQuery(tagLocation) {
def queryManager = session.workspace.queryManager;
def statement = "/jcr:root" + tagLocation + "//element(*, cq:Tag)"
def query = queryManager.createQuery(statement, 'xpath')
}
def findDuplicateTags(tagLocation, tagNodeName) {
def queryManager = session.workspace.queryManager;
def statement = "/jcr:root" + tagLocation + "//element(*, cq:Tag) [fn:name() = '" + tagNodeName + "' ]"
def query = queryManager.createQuery(statement, 'xpath')
}
def findPagesWithTag(tagId, tagPath) {
def queryManager = session.workspace.queryManager;
def statement = "//element(*, cq:Page)[(jcr:content/@cq:tags = '" + tagId + "' or jcr:content/@cq:tags = '" + tagPath + "' )]"
def query = queryManager.createQuery(statement, 'xpath')
}
final def query = buildQuery(tagLocation);
final def result = query.execute()
def tagList = []
f = new File(filePath)
result.nodes.each {
node - >
String nodeTitle = node.name;
tagList.add(nodeTitle);
}
def duplicates = tagList.findAll {
tagList.count(it) > 1
}
def uniqueUsers = duplicates.unique(mutate = false)
print 'TAGTITLE ,TAGID , Pages , Count' + '\n'
f.append('TAGTITLE ,TAGID , Pages , Count' + '\n')
uniqueUsers.each {
def tagquery = findDuplicateTags(tagLocation, it);
def pathresult = tagquery.execute()
pathresult.nodes.each {
node - >
Resource r = resourceResolver.getResource(node.path)
Tag t1 = r.adaptTo(com.day.cq.tagging.Tag)
print t1.getTitle() + ','
f.append(t1.getTitle() + ',')
def pagequery = findPagesWithTag(t1.getTagID(), node.path);
def pageresult = pagequery.execute()
print t1.getTagID() + ','
f.append(t1.getTagID() + ',')
count = 0;
def totalResults = pageresult.getTotalSize()
pageresult.nodes.each {
pagenode - >
if (count > 0) {
print ','
f.append(',')
}
print pagenode.path + ','
f.append(pagenode.path + ',')
if (count == 0) {
print t1.getCount() + ','
f.append(t1.getCount()) + ','
}
count++;
if (totalResults != count) {
print '\n'
f.append('\n')
}
print ','
f.append(',')
}
print '\n'
f.append('\n')
}
print '\n'
f.append('\n')
}