" : '
';
return $div.MarkupToHTML($pagename, $out, array('escape' => 0)).'
';
}
########################################################################
## The functions below optimize searches by maintaining a file of
## words and link cross references (the "page index").
########################################################################
## PageIndexTerms($terms) takes an array of strings and returns a
## normalized list of associated search terms. This reduces the
## size of the index and speeds up searches.
function PageIndexTerms($terms) {
$w = array();
foreach((array)$terms as $t) {
$w = array_merge($w, preg_split('/[^\\w\\x80-\\xff]+/',
strtolower($t), -1, PREG_SPLIT_NO_EMPTY));
}
return $w;
}
## The PageIndexUpdate($pagelist) function updates the page index
## file with terms and target links for the pages in $pagelist.
## The optional $dir parameter allows this function to be called
## via register_shutdown_function (which sometimes changes directories
## on us).
function PageIndexUpdate($pagelist, $dir = '') {
global $PageIndexFile, $PageIndexTime, $Now;
$abort = ignore_user_abort(true);
if ($dir) chdir($dir);
SDV($PageIndexTime, 10);
if (!$pagelist || !$PageIndexFile) return;
$c = count($pagelist);
StopWatch("PageIndexUpdate begin ($c pages to update)");
$pagelist = (array)$pagelist;
$timeout = time() + $PageIndexTime;
$cmpfn = create_function('$a,$b', 'return strlen($b)-strlen($a);');
Lock(2);
$ofp = fopen("$PageIndexFile,new", 'w');
foreach($pagelist as $pn) {
if (time() > $timeout) break;
$page = ReadPage($pn, READPAGE_CURRENT);
if ($page) {
$targets = str_replace(',', ' ', @$page['targets']);
$terms = PageIndexTerms(array(@$page['text'], $targets, $pn));
usort($terms, $cmpfn);
$x = '';
foreach($terms as $t) { if (strpos($x, $t) === false) $x .= " $t"; }
fputs($ofp, "$pn:$Now: $targets :$x\n");
}
$updated[$pn]++;
}
$ifp = @fopen($PageIndexFile, 'r');
if ($ifp) {
while (!feof($ifp)) {
$line = fgets($ifp, 4096);
while (substr($line, -1, 1) != "\n" && !feof($ifp))
$line .= fgets($ifp, 4096);
$i = strpos($line, ':');
if ($i === false) continue;
$n = substr($line, 0, $i);
if (@$updated[$n]) continue;
fputs($ofp, $line);
}
fclose($ifp);
}
fclose($ofp);
if (file_exists($PageIndexFile)) unlink($PageIndexFile);
rename("$PageIndexFile,new", $PageIndexFile);
fixperms($PageIndexFile);
$c = count($updated);
StopWatch("PageIndexUpdate end ($c updated)");
ignore_user_abort($abort);
}
## PageIndexGrep returns a list of pages that match the strings
## provided. Note that some search terms may need to be normalized
## in order to get the desired results (see PageIndexTerms above).
## Also note that this just works for the index; if the index is
## incomplete, then so are the results returned by this list.
## (MakePageList above already knows how to deal with this.)
function PageIndexGrep($terms, $invert = false) {
global $PageIndexFile;
if (!$PageIndexFile) return array();
StopWatch('PageIndexGrep begin');
$pagelist = array();
$fp = @fopen($PageIndexFile, 'r');
if ($fp) {
$terms = (array)$terms;
while (!feof($fp)) {
$line = fgets($fp, 4096);
while (substr($line, -1, 1) != "\n" && !feof($fp))
$line .= fgets($fp, 4096);
$i = strpos($line, ':');
if (!$i) continue;
$add = true;
foreach($terms as $t)
if (strpos($line, $t) === false) { $add = false; break; }
if ($add xor $invert) $pagelist[] = substr($line, 0, $i);
}
fclose($fp);
}
StopWatch('PageIndexGrep end');
return $pagelist;
}
## PostPageIndex is inserted into $EditFunctions to update
## the linkindex whenever a page is saved.
function PostPageIndex($pagename, &$page, &$new) {
global $IsPagePosted;
if ($IsPagePosted) PageIndexUpdate($pagename);
}