fieldData['search'])); for($i=0; $igetAll($sql, $soundexs); //Foreach soundex found, find the levenshtein distance to provide //more accurate ranking. Also, combine the soundexes into a total //rank for each page, using $pageResults $pageResults = array(); foreach($soundexResults as &$r){ $i = array_search($r['soundex'], $soundexs); $keyword = $keywords[$i]; $distance = levenshtein(strtoupper($keyword), strtoupper($r['word'])); //Work out a rank for each soundex. This formulae may need tweaking. //Note that a small levenshtein means a closer match $rank = (1 / ($distance + 1)) * $r['count']; $page = $r['pageid']; if(isset($pageResults[$page])){ $pageResults[$page] += $rank; }else{ $pageResults[$page] = $rank; } } //Sort the results by rank arsort($pageResults, SORT_NUMERIC); //Now add all the nice, user friendly data into $niceResults $niceResults = array(); foreach ($pageResults as $pid => $rank){ $sql = 'SELECT wikipages.name AS name, cmscontent.content FROM wikipages, cmscontent WHERE wikipages.regionid = cmscontent.regionid AND wikipages.wikipageid = ? ORDER BY cmscontent.timestamp DESC LIMIT 1'; $pageInfo = $TKWikiDB->getRow($sql, array($pid)); $niceResults[$pid]['pid'] = $pid; $niceResults[$pid]['name'] = $pageInfo['name']; $niceResults[$pid]['niceName'] = TKWikiUtils::stripUnderscores($pageInfo['name']); //Get the category path $path = TKWikiUtils::getPageCategoryPath($pid); $niceResults[$pid]['categoryPath'] = (count($path) > 0) ? $path : false; //Trim down content $content = $pageInfo['content']; $content = trim(strip_tags($content)); //Conduct trimming if necessary if(strlen($content) > $cfg['TKWiki']['searchSnippetSize']){ //Trim down the cotent to the size specified by $cfg['TKWiki']['searchSnippetSize'] $content = substr($content, 0, $cfg['TKWiki']['searchSnippetSize']); //Remove any word fragments at the end of the snippet $content = strrev(strstr(strrev($content), ' ')); //Add '...' to the end $content = rtrim($content) . '...'; } $niceResults[$pid]['content'] = $content; } $this->assign('resultCount', count($niceResults)); $this->assign('searchResults', $niceResults); } } ?>