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