forked from dokufreaks/plugin-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax.php
235 lines (196 loc) · 8.28 KB
/
syntax.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* Cloud Plugin: shows a cloud of the most frequently used words
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Esther Brunner <[email protected]>
*/
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
class syntax_plugin_cloud extends DokuWiki_Syntax_Plugin {
function getType() { return 'substition'; }
function getPType() { return 'block'; }
function getSort() { return 98; }
function connectTo($mode) {
$this->Lexer->addSpecialPattern('~~\w*?CLOUD.*?~~', $mode, 'plugin_cloud');
}
function handle($match, $state, $pos, Doku_Handler $handler) {
$match = substr($match, 2, -2); // strip markup
if (substr($match, 0, 3) == 'TAG') {
$type = 'tag';
} elseif (substr($match, 0, 6) == 'SEARCH') {
$type = 'search';
} else {
$type = 'word';
}
list($num, $ns) = explode('>', $match, 2);
list($junk, $num) = explode(':', $num, 2);
if (!is_numeric($num)) $num = 50;
if(!is_null($ns)) $namespaces = explode('|', $ns);
else $namespaces = null;
return array($type, $num, $namespaces);
}
function render($mode, Doku_Renderer $renderer, $data) {
global $conf;
list($type, $num, $namespaces) = $data;
if ($mode == 'xhtml') {
if ($type == 'tag') { // we need the tag helper plugin
/** @var helper_plugin_tag $tag */
if (plugin_isdisabled('tag') || (!$tag = plugin_load('helper', 'tag'))) {
msg('The Tag Plugin must be installed to display tag clouds.', -1);
return false;
}
$cloud = $this->_getTagCloud($num, $min, $max, $namespaces, $tag);
} elseif($type == 'search') {
/** @var helper_plugin_searchstats $helper */
$helper = plugin_load('helper', 'searchstats');
if($helper) {
$cloud = $helper->getSearchWordArray($num);
$this->_removeFromCloud($cloud, 'search_blacklist');
// calculate min/max values
$min = PHP_INT_MAX;
$max = 0;
foreach ($cloud as $size) {
$min = min($size, $min);
$max = max($size, $max);
}
} else {
msg('You have to install the searchstats plugin to use this feature.', -1);
return false;
}
} else {
$cloud = $this->_getWordCloud($num, $min, $max);
}
if (!is_array($cloud) || empty($cloud)) return false;
$delta = ($max-$min)/16;
// prevent caching to ensure the included pages are always fresh
$renderer->info['cache'] = false;
// and render the cloud
$renderer->doc .= '<div class="cloud">'.DOKU_LF;
foreach ($cloud as $word => $size) {
if ($size < $min+round($delta)) $class = 'cloud1';
elseif ($size < $min+round(2*$delta)) $class = 'cloud2';
elseif ($size < $min+round(4*$delta)) $class = 'cloud3';
elseif ($size < $min+round(8*$delta)) $class = 'cloud4';
else $class = 'cloud5';
$name = $word;
if ($type == 'tag' && isset($tag)) {
$id = $word;
$exists = false;
resolve_pageID($tag->namespace, $id, $exists);
if($exists) {
$link = wl($id);
if($conf['useheading']) {
$name = p_get_first_heading($id, false);
if (empty($name)) {
$name = $word;
}
}
} else {
$link = wl($id, array('do'=>'showtag', 'tag'=>$word));
}
$title = $word;
$class .= ($exists ? '_tag1' : '_tag2');
} else {
if($conf['userewrite'] == 2) {
$link = wl($word, array('do'=>'search', 'id'=>$word));
$title = $size;
} else {
$link = wl($word, 'do=search');
$title = $size;
}
}
$renderer->doc .= DOKU_TAB . '<a href="' . $link . '" class="' . $class .'"'
.' title="' . $title . '">' . hsc($name) . '</a>' . DOKU_LF;
}
$renderer->doc .= '</div>' . DOKU_LF;
return true;
}
return false;
}
/**
* Removes all words in configured blacklist $balcklistName from $cloud array
*/
function _removeFromCloud(&$cloud, $balcklistName) {
$blacklist = $this->getConf($balcklistName);
if(!empty($blacklist)) {
$blacklist = explode(',', $blacklist);
$blacklist = str_replace(' ', '', $blacklist); // remove spaces
foreach ($blacklist as $word) {
if (isset($cloud[$word]))
unset($cloud[$word]);
}
}
}
/**
* Returns the sorted word cloud array
*/
function _getWordCloud($num, &$min, &$max) {
global $conf;
// load stopwords
$swfile = DOKU_INC.'inc/lang/'.$conf['lang'].'/stopwords.txt';
if (@file_exists($swfile)) $stopwords = file($swfile, FILE_IGNORE_NEW_LINES);
else $stopwords = array();
// load extra local stopwords
$swfile = DOKU_CONF.'stopwords.txt';
if (@file_exists($swfile)) $stopwords = array_merge($stopwords, file($swfile, FILE_IGNORE_NEW_LINES));
$cloud = array();
if (@file_exists($conf['indexdir'].'/page.idx')) { // new word-length based index
require_once(DOKU_INC.'inc/indexer.php');
$n = $this->getConf('minimum_word_length'); // minimum word length
$lengths = idx_indexLengths($n);
foreach ($lengths as $len) {
$idx = idx_getIndex('i', $len);
$word_idx = idx_getIndex('w', $len);
$this->_addWordsToCloud($cloud, $idx, $word_idx, $stopwords);
}
} else { // old index
$idx = file($conf['cachedir'].'/index.idx');
$word_idx = file($conf['cachedir'].'/word.idx');
$this->_addWordsToCloud($cloud, $idx, $word_idx, $stopwords);
}
$this->_removeFromCloud($cloud, 'word_blacklist');
return $this->_sortCloud($cloud, $num, $min, $max);
}
/**
* Adds all words in given index as $word => $freq to $cloud array
*/
function _addWordsToCloud(&$cloud, $idx, $word_idx, &$stopwords) {
$wcount = count($word_idx);
// collect the frequency of the words
for ($i = 0; $i < $wcount; $i++) {
$key = trim($word_idx[$i]);
if (!is_int(array_search($key, $stopwords))) {
$value = explode(':', $idx[$i]);
if (!trim($value[0])) continue;
$cloud[$key] = count($value);
}
}
}
/**
* Returns the sorted tag cloud array
*/
function _getTagCloud($num, &$min, &$max, $namespaces = NULL, helper_plugin_tag &$tag) {
$cloud = $tag->tagOccurrences(NULL, $namespaces, true, $this->getConf('list_tags_of_subns'));
$this->_removeFromCloud($cloud, 'tag_blacklist');
return $this->_sortCloud($cloud, $num, $min, $max);
}
/**
* Sorts and slices the cloud
*/
function _sortCloud($cloud, $num, &$min, &$max) {
if(empty($cloud)) return $cloud;
// sort by frequency, then alphabetically
arsort($cloud);
$cloud = array_chunk($cloud, $num, true);
$max = current($cloud[0]);
$min = end($cloud[0]);
ksort($cloud[0]);
return $cloud[0];
}
}
// vim:ts=4:sw=4:et: