1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* @package sitemaker |
5
|
|
|
* @copyright (c) 2013 Daniel A. (blitze) |
6
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace blitze\sitemaker\blocks; |
11
|
|
|
|
12
|
|
|
use blitze\sitemaker\services\blocks\driver\block; |
13
|
|
|
|
14
|
|
|
class wordgraph extends block |
15
|
|
|
{ |
16
|
|
|
/** @var \phpbb\db\driver\driver_interface */ |
17
|
|
|
protected $db; |
18
|
|
|
|
19
|
|
|
/** @var \blitze\sitemaker\services\forum\data */ |
20
|
|
|
protected $forum; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
protected $phpbb_root_path; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
protected $php_ext; |
27
|
|
|
|
28
|
|
|
/** @var int */ |
29
|
|
|
protected $cache_time; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructor |
33
|
|
|
* |
34
|
|
|
* @param \phpbb\db\driver\driver_interface $db Database connection |
35
|
|
|
* @param \blitze\sitemaker\services\forum\data $forum Forum Data object |
36
|
|
|
* @param string $phpbb_root_path phpBB root path |
37
|
|
|
* @param string $php_ext phpEx |
38
|
|
|
* @param integer $cache_time Cache results for given time |
39
|
|
|
*/ |
40
|
3 |
|
public function __construct(\phpbb\db\driver\driver_interface $db, \blitze\sitemaker\services\forum\data $forum, $phpbb_root_path, $php_ext, $cache_time) |
41
|
|
|
{ |
42
|
3 |
|
$this->db = $db; |
43
|
3 |
|
$this->forum = $forum; |
44
|
3 |
|
$this->phpbb_root_path = $phpbb_root_path; |
45
|
3 |
|
$this->php_ext = $php_ext; |
46
|
3 |
|
$this->cache_time = $cache_time; |
47
|
3 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
1 |
|
public function get_config(array $settings) |
53
|
|
|
{ |
54
|
|
|
return array( |
55
|
1 |
|
'legend1' => 'SETTINGS', |
56
|
1 |
|
'show_word_count' => array('lang' => 'SHOW_WORD_COUNT', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => false, 'default' => 0), |
57
|
1 |
|
'max_num_words' => array('lang' => 'MAX_WORDS', 'validate' => 'int:0:255', 'type' => 'number:0:255', 'maxlength' => 2, 'explain' => false, 'default' => 15), |
58
|
1 |
|
'max_word_size' => array('lang' => 'WORD_MAX_SIZE', 'validate' => 'int:0:55', 'type' => 'number:0:55', 'maxlength' => 2, 'explain' => false, 'default' => 25, 'append' => 'PIXEL'), |
59
|
1 |
|
'min_word_size' => array('lang' => 'WORD_MIN_SIZE', 'validate' => 'int:0:20', 'type' => 'number:0:20', 'maxlength' => 2, 'explain' => false, 'default' => 9, 'append' => 'PIXEL'), |
60
|
1 |
|
'exclude_words' => array('lang' => 'EXCLUDE_WORDS', 'validate' => 'string', 'type' => 'textarea:5:50', 'maxlength' => 255, 'explain' => true, 'default' => ''), |
61
|
1 |
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
2 |
|
public function display(array $bdata, $edit_mode = false) |
68
|
|
|
{ |
69
|
2 |
|
$settings = $bdata['settings']; |
70
|
|
|
$block = array( |
71
|
2 |
|
'title' => 'WORDGRAPH', |
72
|
2 |
|
'content' => '', |
73
|
2 |
|
); |
74
|
|
|
|
75
|
2 |
|
$words_array = $this->get_words($settings); |
76
|
2 |
|
if (sizeof($words_array)) |
77
|
2 |
|
{ |
78
|
2 |
|
$this->show_graph($words_array, $settings); |
79
|
|
|
|
80
|
2 |
|
$block['content'] = $this->ptemplate->render_view('blitze/sitemaker', 'blocks/wordgraph.html', 'wordgraph_block'); |
81
|
2 |
|
} |
82
|
|
|
|
83
|
2 |
|
return $block; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param array $words_array |
88
|
|
|
* @param array $settings |
89
|
|
|
*/ |
90
|
2 |
|
protected function show_graph(array $words_array, array $settings) |
91
|
|
|
{ |
92
|
2 |
|
$params = $this->get_graph_params($words_array, $settings); |
93
|
|
|
|
94
|
|
|
// Sort words in result |
95
|
2 |
|
$words = array_keys($words_array); |
96
|
2 |
|
sort($words); |
97
|
|
|
|
98
|
2 |
|
foreach ($words as $word) |
99
|
|
|
{ |
100
|
2 |
|
$color = $params['min_sat'] + (($words_array[$word] - $params['min_count']) * $params['color_step']); |
101
|
2 |
|
$r = dechex($color); |
102
|
2 |
|
$b = dechex($params['max_sat'] - $color); |
103
|
2 |
|
$g = 'c'; |
104
|
|
|
|
105
|
2 |
|
$this->ptemplate->assign_block_vars('wordgraph', array( |
106
|
2 |
|
'WORD' => $this->show_word($word, $words_array[$word], $settings['show_word_count']), |
107
|
2 |
|
'WORD_SIZE' => $settings['min_word_size'] + (($words_array[$word] - $params['min_count']) * $params['size_step']), |
108
|
2 |
|
'WORD_COLOR' => $r . $g . $b, |
109
|
2 |
|
'WORD_URL' => $this->get_url($word), |
110
|
2 |
|
)); |
111
|
2 |
|
} |
112
|
2 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param array $words_array |
116
|
|
|
* @param array $settings |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
2 |
|
protected function get_graph_params(array $words_array, array $settings) |
120
|
|
|
{ |
121
|
2 |
|
$max_sat = hexdec('f'); |
122
|
2 |
|
$min_sat = hexdec(0); |
123
|
|
|
|
124
|
2 |
|
$max_count = max(array_values($words_array)); |
125
|
2 |
|
$min_count = min(array_values($words_array)); |
126
|
|
|
|
127
|
2 |
|
$spread = $max_count - $min_count; |
128
|
2 |
|
$spread = ($spread) ? $spread : 1; |
129
|
|
|
|
130
|
|
|
return array( |
131
|
2 |
|
'min_sat' => $min_sat, |
132
|
2 |
|
'max_sat' => $max_sat, |
133
|
|
|
|
134
|
2 |
|
'min_count' => $min_count, |
135
|
2 |
|
'max_count' => $max_count, |
136
|
|
|
|
137
|
2 |
|
'spread' => $spread, |
138
|
|
|
|
139
|
|
|
// determine the font-size increment |
140
|
2 |
|
'size_step' => ($settings['max_word_size'] - $settings['min_word_size']) / $spread, |
141
|
2 |
|
'color_step' => ($max_sat - $min_sat) / $spread, |
142
|
2 |
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param array $settings |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
2 |
|
protected function get_words(array $settings) |
150
|
|
|
{ |
151
|
2 |
|
$sql_array = $this->forum->query(false, false) |
152
|
2 |
|
->fetch_custom($this->get_custom_sql_array($settings), array('SELECT')) |
153
|
2 |
|
->build(true, true, false) |
154
|
2 |
|
->get_sql_array(); |
155
|
2 |
|
$sql = $this->db->sql_build_query('SELECT', $sql_array); |
156
|
2 |
|
$result = $this->db->sql_query_limit($sql, $settings['max_num_words'], 0, $this->cache_time); |
157
|
|
|
|
158
|
2 |
|
$words_array = array(); |
159
|
2 |
|
while ($row = $this->db->sql_fetchrow($result)) |
160
|
|
|
{ |
161
|
2 |
|
$word = ucwords(strtolower($row['word_text'])); |
162
|
2 |
|
$words_array[$word] = $row['word_count']; |
163
|
2 |
|
} |
164
|
2 |
|
$this->db->sql_freeresult($result); |
165
|
|
|
|
166
|
2 |
|
return $words_array; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param array $settings |
171
|
|
|
* @return array |
172
|
|
|
*/ |
173
|
2 |
|
protected function get_custom_sql_array(array $settings) |
174
|
|
|
{ |
175
|
|
|
return array( |
176
|
2 |
|
'SELECT' => array('l.word_text', 'l.word_count'), |
177
|
|
|
'FROM' => array( |
178
|
2 |
|
SEARCH_WORDLIST_TABLE => 'l', |
179
|
2 |
|
SEARCH_WORDMATCH_TABLE => 'm', |
180
|
2 |
|
TOPICS_TABLE => 't', |
181
|
2 |
|
POSTS_TABLE => 'p', |
182
|
2 |
|
), |
183
|
|
|
'WHERE' => 'l.word_common <> 1 |
184
|
|
|
AND l.word_count > 0 |
185
|
|
|
AND m.word_id = l.word_id |
186
|
|
|
AND m.post_id = p.post_id |
187
|
2 |
|
AND t.topic_id = p.topic_id' . $this->exclude_words_sql($settings['exclude_words']), |
188
|
2 |
|
'GROUP_BY' => 'l.word_text, l.word_count', |
189
|
|
|
'ORDER_BY' => 'l.word_count DESC' |
190
|
2 |
|
); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param string $exclude_words Comma separated string of words |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
2 |
|
protected function exclude_words_sql($exclude_words) |
198
|
|
|
{ |
199
|
2 |
|
$sql_where = ''; |
200
|
|
|
if ($exclude_words) |
201
|
2 |
|
{ |
202
|
1 |
|
$exclude_words = array_filter(explode(',', str_replace(' ', '', strtolower($exclude_words)))); |
203
|
1 |
|
$sql_where = (sizeof($exclude_words)) ? ' AND ' . $this->db->sql_in_set('l.word_text', $exclude_words, true) : ''; |
204
|
1 |
|
} |
205
|
|
|
|
206
|
2 |
|
return $sql_where; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param string $word |
211
|
|
|
* @param int $count |
212
|
|
|
* @param bool $show_count |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
2 |
|
protected function show_word($word, $count, $show_count) |
216
|
|
|
{ |
217
|
2 |
|
return censor_text(($show_count) ? $word . '(' . $count . ')' : $word); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param string $word |
222
|
|
|
* @return string |
223
|
|
|
*/ |
224
|
2 |
|
protected function get_url($word) |
225
|
|
|
{ |
226
|
2 |
|
return append_sid("{$this->phpbb_root_path}search.$this->php_ext", 'keywords=' . urlencode($word)); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|