|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Web; |
| 5 | + |
| 6 | +namespace BlogEngine.Core.Data.Services |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Tag cloud |
| 10 | + /// </summary> |
| 11 | + public class TagCloud |
| 12 | + { |
| 13 | + private const string Link = "<a href=\"{0}\" class=\"{1}\" title=\"{2}\">{3}</a> "; |
| 14 | + private static readonly object SyncRoot = new object(); |
| 15 | + private static Dictionary<Guid, Dictionary<string, string>> weightedList = new Dictionary<Guid, Dictionary<string, string>>(); |
| 16 | + |
| 17 | + public int MinimumPosts { get; set; } |
| 18 | + public int TagCloudSize { get; set; } |
| 19 | + |
| 20 | + public TagCloud() |
| 21 | + { |
| 22 | + MinimumPosts = 1; |
| 23 | + TagCloudSize = -1; |
| 24 | + } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// List of tag links |
| 28 | + /// </summary> |
| 29 | + /// <returns></returns> |
| 30 | + public List<string> Links() |
| 31 | + { |
| 32 | + var links = new List<string>(); |
| 33 | + foreach (var key in WeightedList.Keys) |
| 34 | + { |
| 35 | + var link = string.Format( |
| 36 | + Link, |
| 37 | + string.Format("{0}?tag={1}", Utils.AbsoluteWebRoot, HttpUtility.UrlEncode(key)), |
| 38 | + WeightedList[key], string.Format("Tag: {0}", key), key); |
| 39 | + links.Add(link); |
| 40 | + } |
| 41 | + return links; |
| 42 | + } |
| 43 | + |
| 44 | + private Dictionary<string, string> WeightedList |
| 45 | + { |
| 46 | + get |
| 47 | + { |
| 48 | + Dictionary<string, string> list = null; |
| 49 | + Guid blogId = Blog.CurrentInstance.Id; |
| 50 | + |
| 51 | + if (!weightedList.TryGetValue(blogId, out list)) |
| 52 | + { |
| 53 | + lock (SyncRoot) |
| 54 | + { |
| 55 | + if (!weightedList.TryGetValue(blogId, out list)) |
| 56 | + { |
| 57 | + list = new Dictionary<string, string>(); |
| 58 | + weightedList.Add(blogId, list); |
| 59 | + |
| 60 | + this.SortList(); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return list; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private static SortedDictionary<string, int> CreateRawList() |
| 70 | + { |
| 71 | + var dic = new SortedDictionary<string, int>(); |
| 72 | + foreach (var tag in Post.Posts.Where(post => post.IsVisibleToPublic).SelectMany(post => post.Tags)) |
| 73 | + { |
| 74 | + if (dic.ContainsKey(tag)) |
| 75 | + { |
| 76 | + dic[tag]++; |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + dic[tag] = 1; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return dic; |
| 85 | + } |
| 86 | + |
| 87 | + private void SortList() |
| 88 | + { |
| 89 | + var dic = CreateRawList(); |
| 90 | + var max = dic.Values.Max(); |
| 91 | + |
| 92 | + var currentTagCount = 0; |
| 93 | + |
| 94 | + int count = currentTagCount; |
| 95 | + foreach (var key in dic.Keys.Where(key => dic[key] >= MinimumPosts).Where(key => TagCloudSize <= 0 || count <= TagCloudSize)) |
| 96 | + { |
| 97 | + currentTagCount++; |
| 98 | + |
| 99 | + var weight = ((double)dic[key] / max) * 100; |
| 100 | + if (weight >= 99) |
| 101 | + { |
| 102 | + WeightedList.Add(key, "biggest"); |
| 103 | + } |
| 104 | + else if (weight >= 70) |
| 105 | + { |
| 106 | + WeightedList.Add(key, "big"); |
| 107 | + } |
| 108 | + else if (weight >= 40) |
| 109 | + { |
| 110 | + WeightedList.Add(key, "medium"); |
| 111 | + } |
| 112 | + else if (weight >= 20) |
| 113 | + { |
| 114 | + WeightedList.Add(key, "small"); |
| 115 | + } |
| 116 | + else if (weight >= 3) |
| 117 | + { |
| 118 | + WeightedList.Add(key, "smallest"); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + } |
| 124 | +} |
0 commit comments