summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--inc/JSON.php4
-rw-r--r--inc/indexer.php40
2 files changed, 21 insertions, 23 deletions
diff --git a/inc/JSON.php b/inc/JSON.php
index 332827f4c..d1fbd404a 100644
--- a/inc/JSON.php
+++ b/inc/JSON.php
@@ -130,6 +130,7 @@ class JSON {
/**
* encodes an arbitrary variable into JSON format
+ * If available the native PHP JSON implementation is used.
*
* @param mixed $var any number, boolean, string, array, or object to be encoded.
* see argument 1 to JSON() above for array-parsing behavior.
@@ -140,6 +141,7 @@ class JSON {
* @access public
*/
function encode($var) {
+ if (function_exists('json_encode')) return json_encode($var);
switch (gettype($var)) {
case 'boolean':
return $var ? 'true' : 'false';
@@ -352,6 +354,7 @@ class JSON {
/**
* decodes a JSON string into appropriate variable
+ * If available the native PHP JSON implementation is used.
*
* @param string $str JSON-formatted string
*
@@ -363,6 +366,7 @@ class JSON {
* @access public
*/
function decode($str) {
+ if (function_exists('json_decode')) return json_decode($str);
$str = $this->reduce_string($str);
switch (strtolower($str)) {
diff --git a/inc/indexer.php b/inc/indexer.php
index 32fbf4a1a..d4432026e 100644
--- a/inc/indexer.php
+++ b/inc/indexer.php
@@ -86,9 +86,7 @@ function idx_saveIndex($pre, $wlen, &$idx){
$fn = $conf['indexdir'].'/'.$pre.$wlen;
$fh = @fopen($fn.'.tmp','w');
if(!$fh) return false;
- foreach ($idx as $line) {
- fwrite($fh,$line);
- }
+ fwrite($fh,join('', $idx));
fclose($fh);
if(isset($conf['fperm'])) chmod($fn.'.tmp', $conf['fperm']);
io_rename($fn.'.tmp', $fn.'.idx');
@@ -154,7 +152,7 @@ function idx_saveIndexLine($pre, $wlen, $idx, $line){
$ih = @fopen($fn.'.idx','r');
if ($ih) {
$ln = -1;
- while (($curline = _freadline($ih)) !== false) {
+ while (($curline = fgets($ih)) !== false) {
if (++$ln == $idx) {
fwrite($fh, $line);
} else {
@@ -186,7 +184,7 @@ function idx_getIndexLine($pre, $wlen, $idx){
$fh = @fopen($fn,'r');
if(!$fh) return '';
$ln = -1;
- while (($line = _freadline($fh)) !== false) {
+ while (($line = fgets($fh)) !== false) {
if (++$ln == $idx) break;
}
fclose($fh);
@@ -252,6 +250,8 @@ function idx_getPageWords($page){
// arrive here with $words = array(wordlen => array(word => frequency))
+ $word_idx_modified = false;
+
$index = array(); //resulting index
foreach (array_keys($words) as $wlen){
$word_idx = idx_getIndex('w',$wlen);
@@ -260,6 +260,7 @@ function idx_getPageWords($page){
if(!is_int($wid)){
$wid = count($word_idx);
$word_idx[] = "$word\n";
+ $word_idx_modified = true;
}
if(!isset($index[$wlen]))
$index[$wlen] = array();
@@ -267,7 +268,7 @@ function idx_getPageWords($page){
}
// save back word index
- if(!idx_saveIndex('w',$wlen,$word_idx)){
+ if($word_idx_modified && !idx_saveIndex('w',$wlen,$word_idx)){
trigger_error("Failed to write word index", E_USER_ERROR);
return false;
}
@@ -388,26 +389,19 @@ function idx_writeIndexLine($fh,$line,$pid,$count){
* @author Andreas Gohr <andi@splitbrain.org>
*/
function idx_updateIndexLine($line,$pid,$count){
- $line = trim($line);
- $updated = array();
- 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){
- $updated[] = $part;
- }
- }
+ if ($line == ''){
+ $newLine = "\n";
+ }else{
+ $newLine = preg_replace('/(^|:)'.preg_quote($pid, '/').'\*\d*/', '', $line);
}
-
- // add doc
if ($count){
- $updated[] = "$pid*$count";
+ if (strlen($newLine) > 1){
+ return "$pid*$count:".$newLine;
+ }else{
+ return "$pid*$count".$newLine;
+ }
}
-
- return join(':',$updated)."\n";
+ return $newLine;
}
/**