Code source wiki de Tag Cloud

Modifié par Jean-Vincent Drean le 2010/01/07 13:56

Afficher les derniers auteurs
1 {{velocity}}
2 ##
3 ## Tag cloud.
4 ##
5 ## @param tagCloudSpace - the space to display the tag cloud for. If blank, display cloud for the whole wiki. Default is blank.
6 ##
7 ## Configurable popularity levels. You must an even number of levels. Default: 6.
8 ##
9 #set ($popularityLevels = ["notPopular", "notVeryPopular", "somewhatPopular", "popular", "veryPopular", "ultraPopular"])
10 $xwiki.ssx.use("XWiki.TagCloud")
11 ##
12 ## Get tag count map. Key: tag, value: number of occurrences.
13 ## Store the minimum number of occurrences for a tag.
14 ## Store the maximum number of occurrences for a tag.
15 ##
16 #set ($tagCount = $xwiki.tag.getTagCount("$!tagCloudSpace"))
17 #set ($sortedTagCounts = $sorttool.sort($tagCount.values()))
18 #set ($maxIndex = $sortedTagCounts.size() - 1)
19 #set ($minCount = $listtool.get($sortedTagCounts, 0))
20 #set ($maxCount = $listtool.get($sortedTagCounts, $maxIndex))
21 ##
22 ## Calculate the total number of tags in the wiki.
23 ##
24 #set ($totalTagNumber = 0)
25 #foreach ($currentTagCount in $sortedTagCounts)
26 #set ($totalTagNumber = $totalTagNumber + $currentTagCount)
27 #end
28 ##
29 ## Build popularity map using the difference between minimum maximum tag occurrences counts
30 ## and the average tag occurences count.
31 ##
32 #set ($countAverage = $mathtool.div($totalTagNumber, $tagCount.size()))
33 #set ($levelsHalf = $popularityLevels.size() / 2)
34 #set ($firstHalfCountDelta = $mathtool.sub($countAverage, $minCount))
35 #set ($secondHalfCountDelta = $mathtool.sub($maxCount, $countAverage))
36 #set ($popularityMap = $util.getHashMap())
37 #set ($firstHalfIntervalSize = $mathtool.div($firstHalfCountDelta, $levelsHalf))
38 #set ($secondHalfIntervalSize = $mathtool.div($secondHalfCountDelta, $levelsHalf))
39 #set ($previousPopularityMax = $minCount)
40 #set ($intervalSize = $firstHalfIntervalSize)
41 #set ($halfPassed = false)
42 #foreach ($popularityLevel in $popularityLevels)
43 #if ($velocityCount > $levelsHalf && !$halfPassed)
44 #set ($intervalSize = $secondHalfIntervalSize)
45 #set ($halfPassed = true)
46 #end
47 #set ($currentPopularityMax = $mathtool.add($previousPopularityMax, $intervalSize))
48 #set ($discard = $popularityMap.put($currentPopularityMax, $popularityLevel))
49 #set ($previousPopularityMax = $currentPopularityMax)
50 #end
51 ##
52 ## Display tag cloud.
53 ##
54 {{html}}
55 #if ($tagCount.size() > 0)
56 <ol class="tagCloud">
57 #foreach($tag in $tagCount.keySet())
58 ## This hackish loop could be made simpler with #break (introduced in velocity 1.6)
59 #set($classSet = false)
60 #foreach($popularityMax in $sorttool.sort($popularityMap.keySet()))
61 #if($tagCount.get($tag) > $popularityMax)
62 ## Tag already processed
63 #elseif(!$classSet)
64 #set($liClass = $popularityMap.get($popularityMax))
65 #set($classSet = true)
66 #end
67 #end
68 <li class="$liClass"><a class="tag" href="$xwiki.getURL("Main.Tags", "view", "do=viewTag&amp;tag=$tag")" title="$msg.get("xe.tag.tooltip", ["${tagCount.get($tag)}"])">$tag</a></li>
69 #end
70 </ol>
71 #else
72 #if("$!tagCloudSpace" == "")
73 $msg.get("xe.tag.notags")
74 #else
75 $msg.get("xe.tag.notagsforspace", ["$tagCloudSpace"])
76 #end
77 #end
78 {{/html}}
79 {{/velocity}}
#getBlogDocument($space $blogDoc)