Wiki source code of User Relations
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | This page calculates the Tanimoto distance between users based on common page edits. | ||
| 2 | |||
| 3 | <% | ||
| 4 | import java.util.StringTokenizer | ||
| 5 | import java.util.HashMap | ||
| 6 | import com.xpn.xwiki.doc.XWikiAttachment | ||
| 7 | import groovy.xml.MarkupBuilder | ||
| 8 | |||
| 9 | if (xwiki.hasAdminRights()) { | ||
| 10 | |||
| 11 | if(request.get("updaterelations") != null) { | ||
| 12 | |||
| 13 | //common edits user relations | ||
| 14 | def sql = ", BaseObject as obj where obj.name=doc.fullName and obj.className='XWiki.XWikiUsers'" | ||
| 15 | def usersArray = [] | ||
| 16 | |||
| 17 | for (item in xwiki.searchDocuments(sql)) { | ||
| 18 | def username = xwiki.getUserName(item) | ||
| 19 | def user = xwiki.getUser(item) | ||
| 20 | |||
| 21 | if (user.isUserInGroup("XWiki.DCNM2009")) { | ||
| 22 | usersArray.add([item, xwiki.criteriaService.revisionCriteriaFactory.createRevisionCriteria(item), [], 0]) | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | |||
| 27 | sql = ", BaseObject as obj where doc.web='DCNM2009'" | ||
| 28 | |||
| 29 | def test = 0 | ||
| 30 | |||
| 31 | for (item in xwiki.searchDocuments(sql)) { | ||
| 32 | |||
| 33 | def currDocument = xwiki.getDocument(item) | ||
| 34 | //checks if each user has editing this doc | ||
| 35 | for(currUser in usersArray) { | ||
| 36 | def revisions = currDocument.getRevisions(currUser[1]) | ||
| 37 | if(revisions.size() > 0) { | ||
| 38 | currUser[2].add(revisions.size()) | ||
| 39 | currUser[3] = currUser[3] + 1 | ||
| 40 | } | ||
| 41 | else { | ||
| 42 | currUser[2].add(0) | ||
| 43 | } | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | def distanceArray = [] | ||
| 48 | def dimension = usersArray.size() | ||
| 49 | for(i in 0..(dimension - 1)) { | ||
| 50 | def row = new double[dimension] | ||
| 51 | distanceArray.add(row) | ||
| 52 | |||
| 53 | //fill in previously calculated distances (symmetric matrix) | ||
| 54 | for(prev in 0..(i-1)) { | ||
| 55 | distanceArray[i][prev] = distanceArray[prev][i] | ||
| 56 | } | ||
| 57 | |||
| 58 | //calculate distances | ||
| 59 | if(i < (dimension - 1)) { | ||
| 60 | for(next in (i+1)..(dimension - 1)) { | ||
| 61 | |||
| 62 | //tanimoto distance | ||
| 63 | //calculate cross product of usersArray[i][2] and usersArray[next][2] | ||
| 64 | def totalShared = 0 | ||
| 65 | for(k in 0..(usersArray[i][2].size() - 1)) { | ||
| 66 | if(usersArray[i][2][k] * usersArray[next][2][k] > 0) { | ||
| 67 | totalShared += 1 | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | def denom = (double)(usersArray[next][3]+ usersArray[i][3] - totalShared) | ||
| 72 | if(denom != 0) { | ||
| 73 | distanceArray[i][next] = totalShared / denom | ||
| 74 | } | ||
| 75 | else { | ||
| 76 | distanceArray[i][next] = 0 | ||
| 77 | } | ||
| 78 | } | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | //write out connection matrix to an attachment | ||
| 83 | def writer = new StringWriter() | ||
| 84 | writer.write(distanceArray.size() + "\n") | ||
| 85 | for(i in 0..(usersArray.size()-1)) { | ||
| 86 | writer.write(usersArray[i][0]) | ||
| 87 | if(i < usersArray.size()-1) { | ||
| 88 | writer.write(",") | ||
| 89 | } | ||
| 90 | } | ||
| 91 | writer.write("\n") | ||
| 92 | for(i in 0..(distanceArray.size()-1)) { | ||
| 93 | for(j in 0..(distanceArray.size()-1)) { | ||
| 94 | writer.write("" + distanceArray[i][j]) | ||
| 95 | if(j < distanceArray.size()-1) { | ||
| 96 | writer.write(",") | ||
| 97 | } | ||
| 98 | } | ||
| 99 | writer.write("\n") | ||
| 100 | } | ||
| 101 | |||
| 102 | def attachmentName = "userdistances.txt" | ||
| 103 | def attachment = doc.getDocument().getAttachment(attachmentName) | ||
| 104 | if (!attachment){ | ||
| 105 | attachment = new XWikiAttachment(doc.getDocument(), attachmentName) | ||
| 106 | doc.getDocument().getAttachmentList().add(attachment) | ||
| 107 | } | ||
| 108 | |||
| 109 | attachment.setContent(writer.toString().getBytes()) | ||
| 110 | doc.getDocument().saveAttachmentContent(attachment, context.getContext()) | ||
| 111 | doc.save() | ||
| 112 | |||
| 113 | println "Finished updating user relations.\n" | ||
| 114 | |||
| 115 | println "[View top relations per user>" + doc.fullName + "?viewtoprelations=1]" | ||
| 116 | } | ||
| 117 | else if(request.get("viewtoprelations") != null){ | ||
| 118 | def attachmentName = "userdistances.txt" | ||
| 119 | def attachment = doc.getAttachment(attachmentName) | ||
| 120 | if (!attachment){ | ||
| 121 | println "No user relations file found." | ||
| 122 | } | ||
| 123 | else { | ||
| 124 | def content = attachment.getContentAsString() | ||
| 125 | def reader = new BufferedReader(new StringReader(content)) | ||
| 126 | def size = Integer.parseInt(reader.readLine()) | ||
| 127 | def users = [] | ||
| 128 | def userListStr = new StringTokenizer(reader.readLine(), ",") | ||
| 129 | for(i in 1..size) { | ||
| 130 | users.add(userListStr.nextToken()) | ||
| 131 | } | ||
| 132 | def sortedDistanceMatrix = [] | ||
| 133 | |||
| 134 | println "Each user's five most connected users (if they exist)\n" | ||
| 135 | for(i in 1..size) { | ||
| 136 | def distanceStr = new StringTokenizer(reader.readLine(), ",") | ||
| 137 | def row = [] | ||
| 138 | for(j in 1..size) { | ||
| 139 | row.add([users[j-1],Double.parseDouble(distanceStr.nextToken())]) | ||
| 140 | } | ||
| 141 | |||
| 142 | //sort the row | ||
| 143 | for(j in 1..(size-1)) { | ||
| 144 | def valueC = row[j][1] | ||
| 145 | def valueN = row[j][0] | ||
| 146 | def k = j-1 | ||
| 147 | while(k >= 0 && row[k][1] < valueC) { | ||
| 148 | row[k + 1][0] = row[k][0] | ||
| 149 | row[k + 1][1] = row[k][1] | ||
| 150 | k = k-1 | ||
| 151 | } | ||
| 152 | row[k+1][0] = valueN | ||
| 153 | row[k+1][1] = valueC | ||
| 154 | } | ||
| 155 | |||
| 156 | //prints users' top five connected people if they exist | ||
| 157 | print users[i-1] + ": " | ||
| 158 | for(j in 0..4) { | ||
| 159 | if(row[j][1] > 0) { | ||
| 160 | if(j > 0) { | ||
| 161 | print ", " | ||
| 162 | } | ||
| 163 | print xwiki.getUserName(row[j][0]) | ||
| 164 | } | ||
| 165 | } | ||
| 166 | println "\n" | ||
| 167 | |||
| 168 | sortedDistanceMatrix.add(row) | ||
| 169 | } | ||
| 170 | |||
| 171 | |||
| 172 | } | ||
| 173 | } | ||
| 174 | else { | ||
| 175 | println "[Update the User Relations>" + doc.fullName + "?updaterelations=1]\n" | ||
| 176 | println "[View top relations per user>" + doc.fullName + "?viewtoprelations=1]" | ||
| 177 | } | ||
| 178 | } | ||
| 179 | %> |