19 * @license GNU General Public License, version 2 or at your option any later versionc |
19 * @license GNU General Public License, version 2 or at your option any later versionc |
20 */ |
20 */ |
21 |
21 |
22 class TagCloud |
22 class TagCloud |
23 { |
23 { |
24 |
24 |
25 /** |
25 /** |
26 * The list of words in the cloud. |
26 * The list of words in the cloud. |
27 * @var array |
27 * @var array |
28 */ |
28 */ |
29 |
29 |
30 var $words = array(); |
30 var $words = array(); |
31 |
31 |
32 /** |
32 /** |
33 * Constructor. |
33 * Constructor. |
34 * @param array Optional. An initial list of words, just a plain old array. |
34 * @param array Optional. An initial list of words, just a plain old array. |
35 */ |
35 */ |
36 |
36 |
37 function __construct($words = array()) |
37 function __construct($words = array()) |
38 { |
38 { |
39 if ( count($words) > 0 ) |
39 if ( count($words) > 0 ) |
40 { |
40 { |
41 foreach ( $words as $word ) |
41 foreach ( $words as $word ) |
42 $this->add_word($word); |
42 $this->add_word($word); |
43 } |
43 } |
44 } |
44 } |
45 |
45 |
46 /** |
46 /** |
47 * Adds a word into the word list. |
47 * Adds a word into the word list. |
48 * @param string The word to add |
48 * @param string The word to add |
49 */ |
49 */ |
50 |
50 |
51 function add_word($word) |
51 function add_word($word) |
52 { |
52 { |
53 $word = sanitize_tag($word); |
53 $word = sanitize_tag($word); |
54 |
54 |
55 if ( isset($this->words[$word]) ) |
55 if ( isset($this->words[$word]) ) |
56 $this->words[$word] += 1; |
56 $this->words[$word] += 1; |
57 else |
57 else |
58 $this->words[$word] = 1; |
58 $this->words[$word] = 1; |
59 } |
59 } |
60 |
60 |
61 /** |
61 /** |
62 * Returns the total size of the cloud. |
62 * Returns the total size of the cloud. |
63 * @return int |
63 * @return int |
64 */ |
64 */ |
65 |
65 |
66 function get_cloud_size() |
66 function get_cloud_size() |
67 { |
67 { |
68 return array_sum($this->words); |
68 return array_sum($this->words); |
69 } |
69 } |
70 |
70 |
71 /** |
71 /** |
72 * Shuffles the cloud. |
72 * Shuffles the cloud. |
73 */ |
73 */ |
74 |
74 |
75 function shuffle_cloud() |
75 function shuffle_cloud() |
76 { |
76 { |
77 $keys = array_keys($this->words); |
77 $keys = array_keys($this->words); |
78 if ( !$keys || empty($keys) || !is_array($keys) ) |
78 if ( !$keys || empty($keys) || !is_array($keys) ) |
79 return null; |
79 return null; |
80 |
80 |
81 shuffle($keys); |
81 shuffle($keys); |
82 if ( !$keys || empty($keys) || !is_array($keys) ) |
82 if ( !$keys || empty($keys) || !is_array($keys) ) |
83 return null; |
83 return null; |
84 |
84 |
85 $temp = $this->words; |
85 $temp = $this->words; |
86 $this->words = array(); |
86 $this->words = array(); |
87 foreach ( $keys as $word ) |
87 foreach ( $keys as $word ) |
88 { |
88 { |
89 $this->words[$word] = $temp[$word]; |
89 $this->words[$word] = $temp[$word]; |
90 } |
90 } |
91 |
91 |
92 unset($temp); |
92 unset($temp); |
93 } |
93 } |
94 |
94 |
95 /** |
95 /** |
96 * Returns the popularity index (scale class) for a 1-100 number. |
96 * Returns the popularity index (scale class) for a 1-100 number. |
97 * @param int |
97 * @param int |
98 * @return int |
98 * @return int |
99 */ |
99 */ |
100 |
100 |
101 function get_scale_class($val) |
101 function get_scale_class($val) |
102 { |
102 { |
103 $ret = 0; |
103 $ret = 0; |
104 if ( $val >= 99 ) |
104 if ( $val >= 99 ) |
105 $ret = 1; |
105 $ret = 1; |
106 else if ( $val >= 70 ) |
106 else if ( $val >= 70 ) |
107 $ret = 2; |
107 $ret = 2; |
108 else if ( $val >= 60 ) |
108 else if ( $val >= 60 ) |
109 $ret = 3; |
109 $ret = 3; |
110 else if ( $val >= 50 ) |
110 else if ( $val >= 50 ) |
111 $ret = 4; |
111 $ret = 4; |
112 else if ( $val >= 40 ) |
112 else if ( $val >= 40 ) |
113 $ret = 5; |
113 $ret = 5; |
114 else if ( $val >= 30 ) |
114 else if ( $val >= 30 ) |
115 $ret = 6; |
115 $ret = 6; |
116 else if ( $val >= 20 ) |
116 else if ( $val >= 20 ) |
117 $ret = 7; |
117 $ret = 7; |
118 else if ( $val >= 10 ) |
118 else if ( $val >= 10 ) |
119 $ret = 8; |
119 $ret = 8; |
120 else if ( $val >= 5 ) |
120 else if ( $val >= 5 ) |
121 $ret = 9; |
121 $ret = 9; |
122 return $ret; |
122 return $ret; |
123 } |
123 } |
124 |
124 |
125 /** |
125 /** |
126 * Generates and returns HTML for the cloud. |
126 * Generates and returns HTML for the cloud. |
127 * @param string Optional. The CSS class applied to all <span> tags. Can be "normal" or "small". Defaults to "normal". |
127 * @param string Optional. The CSS class applied to all <span> tags. Can be "normal" or "small". Defaults to "normal". |
128 * @param string Optional. The alignment for the div. Defaults to "center". |
128 * @param string Optional. The alignment for the div. Defaults to "center". |
129 * @return string |
129 * @return string |
130 */ |
130 */ |
131 |
131 |
132 function make_html($span_class = 'normal', $div_align = 'center') |
132 function make_html($span_class = 'normal', $div_align = 'center') |
133 { |
133 { |
134 global $lang; |
134 global $lang; |
135 $html = array(); |
135 $html = array(); |
136 $max = max($this->words); |
136 $max = max($this->words); |
137 $size = $this->get_cloud_size(); |
137 $size = $this->get_cloud_size(); |
138 $inc = 0; |
138 $inc = 0; |
139 if ( count($this->words) > 0 ) |
139 if ( count($this->words) > 0 ) |
140 { |
140 { |
141 foreach ( $this->words as $word => $popularity ) |
141 foreach ( $this->words as $word => $popularity ) |
142 { |
142 { |
143 $inc++; |
143 $inc++; |
144 $word = htmlspecialchars($word); |
144 $word = htmlspecialchars($word); |
145 $percent = ( $popularity / $max ) * 100; |
145 $percent = ( $popularity / $max ) * 100; |
146 $index = $this->get_scale_class($percent); |
146 $index = $this->get_scale_class($percent); |
147 $newline = ( $inc == 5 ) ? "<br />" : ''; |
147 $newline = ( $inc == 5 ) ? "<br />" : ''; |
148 ( $inc == 5 ) ? $inc = 0 : null; |
148 ( $inc == 5 ) ? $inc = 0 : null; |
149 $url = makeUrlNS('Special', 'TagCloud/' . htmlspecialchars($word)); |
149 $url = makeUrlNS('Special', 'TagCloud/' . htmlspecialchars($word)); |
150 $popstring = ( $popularity == 1 ) ? $lang->get('pagetools_tagcloud_tip_popularity_one') : $lang->get('pagetools_tagcloud_tip_popularity_plural', array('popularity' => $popularity)); |
150 $popstring = ( $popularity == 1 ) ? $lang->get('pagetools_tagcloud_tip_popularity_one') : $lang->get('pagetools_tagcloud_tip_popularity_plural', array('popularity' => $popularity)); |
151 $html[] = "<span class='tc_word_{$span_class} tc_{$span_class}_index_{$index}'><a href='$url' title='$popstring'>$word</a></span>"; // $newline"; |
151 $html[] = "<span class='tc_word_{$span_class} tc_{$span_class}_index_{$index}'><a href='$url' title='$popstring'>$word</a></span>"; // $newline"; |
152 } |
152 } |
153 } |
153 } |
154 $html = '<div style="text-align: ' . $div_align . '; margin: 0 auto; max-width: 400px;">' . implode("\n", $html) . '</div>'; |
154 $html = '<div style="text-align: ' . $div_align . '; margin: 0 auto; max-width: 400px;">' . implode("\n", $html) . '</div>'; |
155 return $html; |
155 return $html; |
156 } |
156 } |
157 |
157 |
158 |
158 |
159 } |
159 } |
160 |
160 |
161 ?> |
161 ?> |