1 # ===================================================================== 2 # groupTagCloud: W-TW group tag cloud applet. 3 # 4 # Copyright (c) 2007-2010 Carlo Strozzi 5 # 6 # This program is free software; you can redistribute it and/or modify 7 # it under the terms of the GNU General Public License as published by 8 # the Free Software Foundation; version 2 dated June, 1991. 9 # 10 # This program is distributed in the hope that it will be useful, 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # GNU General Public License for more details. 14 # 15 # You should have received a copy of the GNU General Public License 16 # along with this program; if not, write to the Free Software 17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 # 19 # ===================================================================== 20 21 BEGIN { 22 23 tmp=tmp1=url=fmt=""; i=j=f1=f2=f3=0 24 delete a; delete b; delete t; delete f 25 26 g_uri = _rcget("tbl_group.g_uri") 27 if ((g_descr=_rcget("tbl_group.g_descr")) == _NULL) 28 g_descr = _rcget("tbl_group.g_name") 29 30 g_descr = _xmlencode(g_descr) 31 gsub(/%/,"%%",g_descr) # not to break printf(). 32 33 if (ENVIRON["TNS_TAG_CLOUD_TYPE"] == _NULL) 34 ENVIRON["TNS_TAG_CLOUD_TYPE"] = "k" # default 35 } 36 37 /^\001/ { next } # skip table header, if any. 38 39 { 40 # p_name, p_tags, p_descr 41 42 split($0,a,"\t") 43 44 # Pages with nil descriptions and pages without tags are 45 # excluded from this view, along with unwanted tags. 46 47 a[2] = " " a[2] " " 48 49 if (ENVIRON["TNS_TAG_CLOUD_TYPE"] == "k") 50 gsub(/ [gM]:[^ ]+/,_NULL,a[2]) 51 52 else if (ENVIRON["TNS_TAG_CLOUD_TYPE"] == "M") { 53 gsub(/ M:/," \001",a[2]) 54 gsub(/ [^\001][^ ]+/,_NULL,a[2]) 55 gsub(/\001/,"M:",a[2]) 56 } 57 58 else if (ENVIRON["TNS_TAG_CLOUD_TYPE"] == "g") { 59 gsub(/ g:/," \001",a[2]) 60 gsub(/ [^\001][^ ]+/,_NULL,a[2]) 61 gsub(/\001/,"g:",a[2]) 62 } 63 64 a[2] = _strip(a[2],_O_MIDDLE) 65 66 if (a[3] ~ /^ *-/ || a[2] == _NULL) next 67 68 i = split(a[2],b," ") # process each tag. 69 70 while (i > 0) t[++j] = b[i--] 71 } 72 73 END { 74 75 if (!j) exit(0) # no tags. 76 77 # Note: the following algorithm was written out from the following 78 # links: 79 # 80 # http://blog.html.it/archivi/2006/02/20/programmare-una-tag-cloud.php 81 # http://www.petefreitag.com/item/396.cfm 82 # 83 # I'm not at all sure whether I got it right, so I'll keep an eye on 84 # it until it proves itself to be correctly implemented. 85 86 _sort(t,j) 87 for (i=1; i<=j; i++) { 88 if (t[i] == tmp) { 89 f[t[i]]++ 90 if (f[t[i]] > f2) f2 = f[t[i]] # max freq. 91 } 92 else { 93 f[t[i]] = 1 94 tmp = t[i] 95 } 96 } 97 98 f1 = f2 99 100 for (tmp in f) { 101 # set min freq. 102 if (f[tmp] > ENVIRON["TNS_TAG_FREQ_MIN"] && f[tmp] < f1) f1 = f[tmp] 103 } 104 105 f3 = f2 - f1 106 107 f4 = f3 / 3 108 109 j = _uniq(t,j) 110 111 url = ENVIRON["CSA_RPC_URI"] "/" ENVIRON["CSA_LANG"] "/" g_uri "/9" 112 113 gsub(/%/,"%%",url) # needed by printf() 114 115 fmt = readfmt("tw-tag-cloud") 116 117 gsub(/%/,"%%",fmt) # turn plain '%' into '%%'. 118 gsub(/\\/,"\\\\&",fmt) # turn '\' into '\\'. 119 gsub(/[\n\r]+/,"",fmt) # just in case. 120 121 # this format string is currently not customizable. 122 for (i=1; i<=5; i++) { 123 if (!sub(//,"%s",fmt)) fmt = fmt "" 124 } 125 126 # encode any extra markers. 127 gsub(//,"\\<tw:s/\\>",fmt) 128 129 fmt = fmt "\n" 130 131 print "" # documentation. 132 133 for (i=1; i<=j; i++) { 134 value = t[i] 135 tmp = _uriencode(t[i],_O_PATHINFO) "?4=1" # use paging by default. 136 gsub(/_/," ",value) 137 value = _xmlencode(value) 138 tmp1 = value 139 sub(/^[gkM]:/,_NULL,tmp1) 140 if (f[t[i]] == f1) 141 printf(fmt,url "/" tmp,"smallestTag",g_descr,value,tmp1) 142 else if (f[t[i]] == f2) 143 printf(fmt,url "/" tmp,"largestTag",g_descr,value,tmp1) 144 else if (f[t[i]] > (f1 + f4*2)) 145 printf(fmt,url "/" tmp,"largeTag",g_descr,value,tmp1) 146 else if (f[t[i]] > (f1 + f4)) 147 printf(fmt,url "/" tmp,"mediumTag",g_descr,value,tmp1) 148 else printf(fmt,url "/" tmp,"smallTag",g_descr,value,tmp1) 149 } 150 } 151 152 # EOF