1) array_push($keywords, mysql_real_escape_string($word)); } if(count($keywords) < 1){ if(!$useExistingConnection) mysql_close($connId); return false; } # use a maximum of four search entries if (count($keywords) > 4) array_splice($keywords, 4); for($i = 0; $i < count($keywords); $i++) $keywords[$i] = "name LIKE '%". $keywords[$i] ."%'"; $whereString = join(" OR ", $keywords); if(empty($whereString)){ if(!$useExistingConnection) mysql_close($connId); return false; } $res = mysql_query("SELECT id, name FROM manufacturers WHERE ". $whereString); if(!$res){ if(!$useExistingConnection) mysql_close($connId); return false; } $result = array(); while($manu = mysql_fetch_object($res)) $result[$manu->id] = $manu->name; # find with help of soundexes $res = mysql_query("SELECT id, name FROM manufacturers WHERE id IN(SELECT manufacturersId FROM manufacturerhassoundexes WHERE soundexesId IN(SELECT id FROM soundexes WHERE soundex=SOUNDEX('". $word ."')))"); while($manu = mysql_fetch_object($res)) $result[$manu->id] = $manu->name; if(!$useExistingConnection) mysql_close($connId); # count the score for each entry $tmp = $result; $result = array(); foreach(array_keys($tmp) as $key){ # since the max levenshtein distance is the length of the longest string of # the both compared strings, we can calculate a percent of match $score = levenshtein($name, $tmp[$key]); $score = (1 - bcdiv($score, max(strlen($name), strlen($tmp[$key])), 2)) * 100; $result[$score][$key] = $tmp[$key]; } # sort the array, so that the most relevant manufacturers are at the beginning krsort($result); return $result; } function insertSoundexes($manufacturer, $useExistingConnection = true){ if(!is_a($manufacturer, "Manufacturer")) return false; if($manufacturer->id() < 1) return false; $name = $manufacturer->name(); if(empty($name)) return false; $words = explode(" ", $name); if(!$useExistingConnection){ $connId = @ mysql_connect("localhost", Settings::value("ADMINUSER"), Settings::value("ADMINPASSWORD")); if(!$connId) return false; if(!mysql_select_db(Settings::value("DBNAME"))){ mysql_close($connId); return false; } } foreach($words as $word){ $word = mysql_real_escape_string($word); # first check whether soundex is already present $soundexId = 0; $res = mysql_query("SELECT id FROM soundexes WHERE soundex=SOUNDEX('". $word ."')"); $result = mysql_fetch_object($res); if($result) $soundexId = $result->id; if($soundexId < 1){ # finsert soundexes for current word if(!mysql_query("INSERT INTO soundexes (soundex) VALUES(SOUNDEX('". $word ."'))")){ mysql_close($connId); return false; } # select id of current insert $res = mysql_query("SELECT id FROM soundexes WHERE soundex=SOUNDEX('". $word ."')"); $result = mysql_fetch_object($res); if($result) $soundexId = $result->id; if($soundexId < 1){ mysql_close($connId); return false; } } # second, connect current manufacturer with soundex # but first check, whether they are already connected $res = mysql_query("SELECT * FROM manufacturerhassoundexes WHERE manufacturersId=". $manufacturer->id() ." AND soundexesId=". $soundexId); $result = mysql_fetch_object($res); if(!$result || ($result->id < 1)){ # insert the relation if(!mysql_query("INSERT INTO manufacturerhassoundexes (manufacturersId, soundexesId) VALUES(". $manufacturer->id() .", ". $soundexId .")")){ mysql_close($connId); return false; } } } if(!$useExistingConnection) mysql_close($connId); return true; } function stripText($text){ $text = str_replace("\n", " ", $text); $text = preg_replace('/\s\s+/', ' ', $text); $text = preg_replace("/\r|\\\\|\|/s", "", $text); return trim($text); } /** * @brief Creates an xml response with manufacturers from @p manufacturerList. * * @p manufacturerList must be an array with id as keys and names as values. * (e.g.: array(12 => "Manufacturer name")) */ function manufacturersToXml($manufacturerList, $encoding){ global $lang; if(empty($lang)) $lang = "en"; $schema = "manuserver.xsd"; # create result document $doc = new DOMDocument("1.0", $encoding); $root = $doc->createElement("manuserver"); $doc->appendChild($root); $root->setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); $root->setAttribute("xmlns", "http://mins.engsas.de/manuserver"); $root->setAttribute("xsi:schemaLocation", "http://mins.engsas.de/manuserver http://mins.engsas.de/manuserver.xsd"); $response = $doc->createElement("response"); $root->appendChild($response); $elem = $doc->createElement("result"); $response->appendChild($elem); $elem->appendChild($doc->createTextNode("true")); # add manufacturer $i = 0; foreach(array_keys($manufacturerList) as $score){ foreach(array_keys($manufacturerList[$score]) as $id){ if($i >= 20) break; $manu = new Manufacturer($manufacturerList[$score][$id]); $manu->loadFromId($id, false); $result = $manu->toXml($response, $doc, $score); if($result != true) return $result; $i++; } } // bug in php with libxml 2.6.32. Should be fixed with 2.7.3 // (see http://www.php.net/manual/de/domdocument.schemavalidate.php#89893) // if(!$doc->schemaValidate($schema)) // return "Xml is not valid"; return $doc; } ?>