[Comoblog-commit] comoblog/modules/mod_search/include blog_search.inc.php,NONE,1.1
Status: Inactive
Brought to you by:
markwallis
|
From: Mark W. \(a. serialmonkey\) <mar...@us...> - 2005-10-05 00:48:34
|
Update of /cvsroot/comoblog/comoblog/modules/mod_search/include In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14620/mod_search/include Added Files: blog_search.inc.php Log Message: RFE: 1280296 - mod_search module --- NEW FILE: blog_search.inc.php --- <?php include_once (dirname(__FILE__).'/../../../include/config.inc.php'); function do_search ($search_string) { $search_string = strtolower($search_string); $posts = array(); $query = "select distinct post_id from ".CFG_MYSQL_TABPREFIX."blog_search_index where word like '%".$search_string."%'"; $res = mysql_query($query); if (!$res || !mysql_num_rows($res)) return (false); while ($row = mysql_fetch_array($res, MYSQL_NUM)) { $post_id = $row[0]; $post = post_details ($post_id); $posts[$post_id] = $post; } return $posts; } function mass_index () { // Clear index table echo "Running Mass Index Function ...\n"; echo "Removing all previous search index entries\n"; $query = "delete from ".CFG_MYSQL_TABPREFIX."blog_search_index"; $res = mysql_query($query); echo "Looping through all blog entries and indexing\n"; // Loop through all posts $query = "select post_id from ".CFG_MYSQL_TABPREFIX."posts"; $res = mysql_query($query); if (!$res || !mysql_num_rows($res)) { echo "No posts found to index\n"; return (false); } $cnt = 0; while ($row = mysql_fetch_array($res, MYSQL_NUM)) { $post_id = $row[0]; $post = post_details($post_id); $post['id'] = $post_id; index($post); $cnt = $cnt + 1; } echo "Completed indexing of $cnt posts\n"; } function unindex ($post) { $query = "delete from ".CFG_MYSQL_TABPREFIX."blog_search_index where post_id = '".$post['id']."'"; $res = mysql_query($query); } function index ($post) { process_text( $post['post_mail_body'], $post['post_id'] ); process_text( $post['post_mail_subject'], $post['post_id'] ); } function process_text ($body, $id) { $body = filter_html($body); $body = filter_punc($body); $pieces = explode(' ', $body); foreach ($pieces as $piece) { $piece = trim($piece); if (strlen($piece) > 3 && strcmp($piece, "") != 0 && !is_excluded($piece)) insert_index($id, $piece); } } function filter_html($body) { $search = array ("'<script[^>]*?>.*?</script>'si", // Strip out javascript "'<[\/\!]*?[^<>]*?>'si", // Strip out HTML tags "'([\r\n])[\s]+'", // Strip out white space "'&(quot|#34);'i", // Replace HTML entities "'&(amp|#38);'i", "'&(lt|#60);'i", "'&(gt|#62);'i", "'&(nbsp|#160);'i", "'&(iexcl|#161);'i", "'&(cent|#162);'i", "'&(pound|#163);'i", "'&(copy|#169);'i", "'&#(\d+);'e"); // evaluate as php $replace = array ("", "", " ", "\"", "&", "<", ">", " ", chr(161), chr(162), chr(163), chr(169), "chr(\\1)"); $body = preg_replace($search, $replace, $body); return $body; } function filter_punc($body) { $search = "/(,|\.|<|>|\?|\/|;|:|\"|'|{|}|\[|\]|\\\|\||=|-|_|\+|\(|\)|\*|&|\^|%|\\$|#|@|!|~|`)/"; $replace = " "; $body = preg_replace ($search, $replace, $body); return $body; } function insert_index ($post_id, $word) { $query = " insert into ".CFG_MYSQL_TABPREFIX."blog_search_index (post_id, word) values ('". $post_id ."', '". strtolower($word) ."')"; $res = mysql_query($query); } function is_excluded($word) { $excludes = explode(',', CFG_MOD_BLOG_SEARCH_EXCLUSIONS); foreach ($excludes as $exclusion) { if (strcasecmp($exclusion,$word) == 0) return(true); } return (false); } ?> |