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
|
<?php
/**
* Common DokuWiki functions
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Andreas Gohr <andi@splitbrain.org>
*/
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
require_once(DOKU_CONF.'dokuwiki.php');
require_once(DOKU_INC.'inc/io.php');
require_once(DOKU_INC.'inc/utf8.php');
require_once(DOKU_INC.'inc/parserutils.php');
/**
* Split a page into words
*
* It is based upon PHPCMS's indexer function index_entry
*
* Returns an array of of word counts, false if an error occured
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
function idx_getPageWords($page){
global $conf;
$word_idx = file($conf['cachedir'].'/word.idx');
$swfile = DOKU_INC.'inc/lang/'.$conf['lang'].'/stopwords.txt';
if(@file_exists($swfile)){
$stopwords = file($swfile);
}else{
$stopwords = array();
}
// split page into words
$body = rawWiki($page);
$body = utf8_stripspecials($body,' ','._\-:');
$body = utf8_strtolower($body);
$body = trim($body);
$words = explode(' ',$body);
sort($words);
$index = array(); //resulting index
$old = '';
$wid = -1;
$doit = true;
$pos = 0;
//compact wordlist FIXME check for stopwords
foreach($words as $word){
if(strlen($word) == 0) continue;
// it's the same word
if($word == $old){
if($doit == false) {
// we didn't wanted it last time
continue;
}
// just increase the counter
$index[$wid]++;
continue;
}
// rememember old word
$old = $word;
$doit = true;
// checking minimum word-size (excepting numbers)
if(!is_numeric($word)) {
if(strlen($word) < 3) {
$doit = false;
continue;
}
}
// stopword check
if(is_int(array_search("$word\n",$stopwords))){
$doit = false;
continue;
}
// get word ID
$wid = array_search("$word\n",$word_idx);
if(!is_int($wid)){
$word_idx[] = "$word\n";
$wid = count($word_idx)-1;
}
// add to index
$index[$wid] = 1;
}
// save back word index
$fh = fopen($conf['cachedir'].'/word.idx','w');
if(!$fh){
trigger_error("Failed to write word.idx", E_USER_ERROR);
return false;
}
fwrite($fh,join('',$word_idx));
fclose($fh);
return $index;
}
/**
* Adds/updates the search for the given page
*
* This is the core function of the indexer which does most
* of the work. This function needs to be called with proper
* locking!
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
function idx_addPage($page){
global $conf;
// load known words and documents
$page_idx = file($conf['cachedir'].'/page.idx');
// get page id (this is the linenumber in page.idx)
$pid = array_search("$page\n",$page_idx);
if(!is_int($pid)){
$page_idx[] = "$page\n";
$pid = count($page_idx)-1;
// page was new - write back
$fh = fopen($conf['cachedir'].'/page.idx','w');
if(!$fh) return false;
fwrite($fh,join('',$page_idx));
fclose($fh);
}
// get word usage in page
$words = idx_getPageWords($page);
if($words === false) return false;
if(!count($words)) return true;
// Open index and temp file
$idx = fopen($conf['cachedir'].'/index.idx','r');
$tmp = fopen($conf['cachedir'].'/index.tmp','w');
if(!$idx || !$tmp){
trigger_error("Failed to open index files", E_USER_ERROR);
return false;
}
// copy from index to temp file, modifying were needed
$lno = 0;
$line = '';
while (!feof($idx)) {
// read full line
$line .= fgets($idx, 4096);
if(substr($line,-1) != "\n") continue;
// write a new Line to temp file
idx_writeIndexLine($tmp,$line,$pid,$words[$lno]);
$line = ''; // reset line buffer
$lno++; // increase linecounter
}
fclose($idx);
// add missing lines (usually index and word should contain
// the same number of lines, however if the page contained
// new words the word file has some more lines which need to
// be added here
$word_idx = file($conf['cachedir'].'/word.idx');
$wcnt = count($word_idx);
for($lno; $lno<$wcnt; $lno++){
idx_writeIndexLine($tmp,'',$pid,$words[$lno]);
}
// close the temp file and move it over to be the new one
fclose($tmp);
return rename($conf['cachedir'].'/index.tmp',
$conf['cachedir'].'/index.idx');
}
/**
* Write a new index line to the filehandle
*
* This function writes an line for the index file to the
* given filehandle. It removes the given document from
* the given line and readds it when $count is >0.
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
function idx_writeIndexLine($fh,$line,$pid,$count){
$line = trim($line);
if($line != ''){
$parts = explode(':',$line);
// remove doc from given line
foreach($parts as $part){
if($part == '') continue;
list($doc,$cnt) = explode('*',$part);
if($doc != $pid){
fwrite($fh,"$doc*$cnt:");
}
}
}
// add doc
if ($count){
fwrite($fh,"$pid*$count");
}
// add newline
fwrite($fh,"\n");
}
//Setup VIM: ex: et ts=4 enc=utf-8 :
|