function mt_search($query) { //stub $sid = mt_login(); if (empty($sid)) { //phail return null; } if (empty($query) or empty($sid)) { return null; } $query = ' use "' . XML_BASE . 'yql_mp3tunes.search.xml" as mp3tunes.search; select * from mp3tunes.search where sid="' . $sid . '" AND query="' . $query . '" AND token="' . MT_TOKEN . '";'; //echo $query; $results = yql_get($query); $results = $results->results; //don't care that much about yql info var_dump($results); $results = sort_results($results, $query, array('artistname', 'tracktitle')); if (empty($results) || !is_array($results)) { return null; } $result = $results[0]; $ret = new stdClass(); $ret->title = $result->artistname . ' - ' . $result->tracktitle; $ret->url = $result->playurl; $ret->score = $result->score; //we care because we will run compare it against other service's scores return $ret; }
function search($query) { $results[] = sc_search($query); $results[] = mt_search($query); $results = sort_results($results, $query); $results = $results[0]; if (empty($results)) { return null; } return $results; }
function sc_search($query = '', $limit = 0) { $results = get_results($query, 'soundcloud'); $results = sort_results($results, $query, 'title'); if (empty($results) || !is_array($results)) { return null; } $result = $results[0]; $ret = new stdClass(); $ret->title = $result->title; $ret->url = $result->stream_url . '?client_id=' . CLIENT_ID; $ret->score = $result->score; //we care because we will run compare it against other service's scores return $ret; }
function main() { if (strcmp('4.1.0', phpversion()) > 0) { die("Error: PHP version 4.1.0 or above required!"); } if (!($file = fopen("search.idx", "rb"))) { die("Error: Search index file could NOT be opened!"); } if (readHeader($file) != "DOXS") { die("Error: Header of index file is invalid!"); } $query = ""; if (array_key_exists("query", $_GET)) { $query = $_GET["query"]; } echo "<input class=\"search\" type=\"text\" name=\"query\" value=\"{$query}\" size=\"20\" accesskey=\"s\"/>\n"; echo "</span>\n"; echo "</form>\n"; echo "</div>\n"; $results = array(); $requiredWords = array(); $forbiddenWords = array(); $foundWords = array(); $word = strtok($query, " "); while ($word) { if ($word[0] == '+') { $word = substr($word, 1); $requiredWords[] = $word; } if ($word[0] == '-') { $word = substr($word, 1); $forbiddenWords[] = $word; } if (!in_array($word, $foundWords)) { $foundWords[] = $word; search($file, $word, $results); } $word = strtok(" "); } $docs = array(); combine_results($results, $docs); // filter out documents with forbidden word or that do not contain // required words $filteredDocs = filter_results($docs, $requiredWords, $forbiddenWords); // sort the results based on rank $sorted = array(); sort_results($filteredDocs, $sorted); // report results to the user report_results($sorted); fclose($file); }
function run_the_queries($gcmaz_wpdb, $wpdb, $sql_search_terms, $rows_to_return, $pages_to_ignore_gcmaz, $pages_to_ignore_local) { /******************************************************* * 2 Queries (gcmaz domain & local site) * * I. unfinished - pages to ignore * II. Prep vars for the statement * III. To filter or not * IV. The Queries * V. Functions (merge, object to array, sort) * * Notes about the prepare statement: * a. escape the wildcards %=escape and %=wildcard * b. the C, D, E ... denote placeholders in the wpdb->prepare statement in order where * c. the sql statement has %s, %d denoting "string" or "integer" * d. blueprint of the prepared statement * $rows = $gcmaz_wpdb->get_results($gcmaz_wpdb->prepare( "the sql query", C, D, E, F, G, H, I, J, K, L, M, N, O, P) * e. variables used in "local" end with _local to avoid collision ********************************************************/ // I. array of pages on gcmaz we don't want to return // need to set this up as options in custom gcmaz plugin instead of hardcoded (select pages to ignore in search results) $pages_to_ignore_gcmaz = $pages_to_ignore_gcmaz; $pages_to_ignore_local = $pages_to_ignore_local; // II. INIT VARS FOR THE SQL PREPARE STATEMENT $var_for_publish = "publish"; $var_for_page = "page"; $var_for_post = "post"; $var_for_whats = "whats-happening"; $var_for_concert = "concert"; $var_for_community = "community-info"; $var_for_splash = "splash-post"; $var_for_publish_local = "publish"; $var_for_page_local = "page"; $var_for_post_local = "post"; $var_for_splash_local = "splash-post"; // III. FILTER OR NOT // the default search isn't filtered, but it can be refined in which case the following vars will be populated //check for search filter vars - to filter by domain, posts, pages if (isset($_POST['filter_the_search'])) { // not default search, filters in play //domain if (isset($_POST['showDomain'])) { $showDomain = sanitize_text_field($_POST['showDomain']); } else { $showDomain = 'gcmaz'; } //pages if (isset($_POST['showPages'])) { $showPages = true; } else { $showPages = false; } //posts if (isset($_POST['showPosts'])) { $showPosts = true; } else { $showPosts = false; } //news if (isset($_POST['showNews'])) { $showNews = true; } else { $showNews = false; } } else { // default search $showDomain = 'gcmaz'; $showPosts = true; $showPages = true; $showNews = true; } // IV. THE QUERIES //----------------------------------------------------- // FILTERED SEARCH TO SHOW KAFF NEWS //----------------------------------------------------- // don't bother with local search // search gcmaz.com for KAFF News "posts" only if ($showDomain == 'news' || $showDomain == 'gcmaz' && $showNews == true && $showPosts == false && $showPages == false) { $sql = "\n SELECT ID, post_date, post_title, guid, post_name, post_type\n FROM {$gcmaz_wpdb->posts}\n WHERE (" . implode(' OR ', $sql_search_terms) . ")\n AND post_status = '%s'\n AND (post_type = '%s')\n AND ID NOT IN (" . implode($pages_to_ignore_gcmaz, ", ") . ")\n ORDER BY post_date DESC\n LIMIT {$rows_to_return}\n "; //for debugging //echo "<div style='background:yellow;color:red'>NEWS</div>"; //$prepared_statement = $gcmaz_wpdb->prepare($sql, $var_for_publish, $var_for_post); //print_r($prepared_statement); $rows_gcmaz = $gcmaz_wpdb->get_results($gcmaz_wpdb->prepare($sql, $var_for_publish, $var_for_post)); // convert to array $results = convert_results_to_array($rows_gcmaz); // dont need to merge, just sort $results = sort_results($results); return $results; } //-------------------------------------------------------------------------------- // DEFAULT SEARCH & FILTERED SEARCH THAT SHOWS POSTS & PAGES //-------------------------------------------------------------------------------- if ($showPosts == true && $showPages == true || $showPosts == false && $showPages == false) { if ($showDomain == 'local') { //LOCAL ONLY //SKIPS NEWS $sql_local = "\n SELECT ID, post_date, post_title, guid, post_name, post_type\n FROM {$wpdb->posts}\n WHERE (" . implode(' OR ', $sql_search_terms) . ")\n AND post_status = '%s'\n AND (post_type = '%s' OR post_type = '%s' OR post_type = '%s')\n AND ID NOT IN (" . implode($pages_to_ignore_local, ", ") . ")\n ORDER BY post_date DESC\n LIMIT {$rows_to_return}\n "; //for debugging //echo "<div style='background:yellow;color:red'>LOCAL PAGES AND POSTS, SKIPS NEWS</div>"; //$prepared_statement_local = $wpdb->prepare($sql_local, $var_for_publish_local, $var_for_page_local, $var_for_post_local, $var_for_splash_local); //print_r($prepared_statement_local); $rows_local = $wpdb->get_results($wpdb->prepare($sql_local, $var_for_publish_local, $var_for_page_local, $var_for_post_local, $var_for_splash_local)); // convert to array $rows_local = convert_results_to_array($rows_local); //sort results $results = sort_results($rows_local); return $results; } else { // ENTIRE DOMAIN // WITH NEWS if ($showNews == true) { // QUERY1) GCMAZ.com $sql = "\n SELECT ID, post_date, post_title, guid, post_name, post_type\n FROM {$gcmaz_wpdb->posts}\n WHERE (" . implode(' OR ', $sql_search_terms) . ")\n AND post_status = '%s'\n AND (post_type = '%s' OR post_type = '%s' OR post_type = '%s' OR post_type = '%s' OR post_type = '%s' OR post_type = '%s')\n AND ID NOT IN (" . implode($pages_to_ignore_gcmaz, ", ") . ")\n ORDER BY post_date DESC\n LIMIT {$rows_to_return}\n "; //for debugging //$prepared_statement = $gcmaz_wpdb->prepare($sql, $var_for_publish, $var_for_page, $var_for_post, $var_for_whats, $var_for_concert, $var_for_community, $var_for_splash); //print_r($prepared_statement); $rows_gcmaz = $gcmaz_wpdb->get_results($gcmaz_wpdb->prepare($sql, $var_for_publish, $var_for_page, $var_for_post, $var_for_whats, $var_for_concert, $var_for_community, $var_for_splash)); // QUERY 2) LOCAL Search $sql_local = "\n SELECT ID, post_date, post_title, guid, post_name, post_type\n FROM {$wpdb->posts}\n WHERE (" . implode(' OR ', $sql_search_terms) . ")\n AND post_status = '%s'\n AND (post_type = '%s' OR post_type = '%s' OR post_type = '%s')\n AND ID NOT IN (" . implode($pages_to_ignore_local, ", ") . ")\n ORDER BY post_date DESC\n LIMIT {$rows_to_return}\n "; //for debugging //echo "<div style='background:yellow;color:red'>ALL PAGES AND POSTS WITH NEWS</div>"; //$prepared_statement_local = $wpdb->prepare($sql_local, $var_for_publish_local, $var_for_page_local, $var_for_post_local, $var_for_splash_local); //print_r($prepared_statement_local); $rows_local = $wpdb->get_results($wpdb->prepare($sql_local, $var_for_publish_local, $var_for_page_local, $var_for_post_local, $var_for_splash_local)); // convert to array $rows_gcmaz = convert_results_to_array($rows_gcmaz); $rows_local = convert_results_to_array($rows_local); // merge results $results = merge_results($rows_gcmaz, $rows_local); //sort results $results = sort_results($results); return $results; } else { // ENTIRE DOMAIN // WITHOUT NEWS // QUERY1) GCMAZ.com $sql = "\n SELECT ID, post_date, post_title, guid, post_name, post_type\n FROM {$gcmaz_wpdb->posts}\n WHERE (" . implode(' OR ', $sql_search_terms) . ")\n AND post_status = '%s'\n AND (post_type = '%s' OR post_type = '%s' OR post_type = '%s' OR post_type = '%s' OR post_type = '%s')\n AND ID NOT IN (" . implode($pages_to_ignore_gcmaz, ", ") . ")\n ORDER BY post_date DESC\n LIMIT {$rows_to_return}\n "; //for debugging //$prepared_statement = $gcmaz_wpdb->prepare($sql, $var_for_publish, $var_for_page, $var_for_whats, $var_for_concert, $var_for_community, $var_for_splash); //print_r($prepared_statement); $rows_gcmaz = $gcmaz_wpdb->get_results($gcmaz_wpdb->prepare($sql, $var_for_publish, $var_for_page, $var_for_whats, $var_for_concert, $var_for_community, $var_for_splash)); // QUERY 2) LOCAL Search $sql_local = "\n SELECT ID, post_date, post_title, guid, post_name, post_type\n FROM {$wpdb->posts}\n WHERE (" . implode(' OR ', $sql_search_terms) . ")\n AND post_status = '%s'\n AND (post_type = '%s' OR post_type = '%s' OR post_type = '%s')\n AND ID NOT IN (" . implode($pages_to_ignore_local, ", ") . ")\n ORDER BY post_date DESC\n LIMIT {$rows_to_return}\n "; //for debugging //echo "<div style='background:yellow;color:red'>ALL PAGES AND POSTS WITHOUT NEWS</div>"; //$prepared_statement_local = $wpdb->prepare($sql_local, $var_for_publish_local, $var_for_page_local, $var_for_post_local, $var_for_splash_local); //print_r($prepared_statement_local); $rows_local = $wpdb->get_results($wpdb->prepare($sql_local, $var_for_publish_local, $var_for_page_local, $var_for_post_local, $var_for_splash_local)); // convert to array $rows_gcmaz = convert_results_to_array($rows_gcmaz); $rows_local = convert_results_to_array($rows_local); // merge results $results = merge_results($rows_gcmaz, $rows_local); //sort results $results = sort_results($results); return $results; } } } //----------------------------------------------------- // FILTERED SEARCH TO SHOW POSTS NOT PAGES //----------------------------------------------------- if ($showPosts == true && $showPages == false) { if ($showDomain == 'local') { // LOCAL ONLY // SKIPS NEWS $sql_local = "\n SELECT ID, post_date, post_title, guid, post_name, post_type\n FROM {$wpdb->posts}\n WHERE (" . implode(' OR ', $sql_search_terms) . ")\n AND post_status = '%s'\n AND (post_type = '%s' OR post_type = '%s')\n AND ID NOT IN (" . implode($pages_to_ignore_local, ", ") . ")\n ORDER BY post_date DESC\n LIMIT {$rows_to_return}\n "; //for debugging //echo "<div style='background:yellow;color:red'>LOCAL POSTS, SKIPS NEWS</div>"; $rows_local = $wpdb->get_results($wpdb->prepare($sql_local, $var_for_publish_local, $var_for_post_local, $var_for_splash_local)); // convert to array $rows_local = convert_results_to_array($rows_local); //sort results $results = sort_results($rows_local); return $results; } else { // ENTIRE DOMAIN // WITH NEWS if ($showNews == true) { // QUERY 1) gcmaz.com $sql = "\n SELECT ID, post_date, post_title, guid, post_name, post_type\n FROM {$gcmaz_wpdb->posts}\n WHERE (" . implode(' OR ', $sql_search_terms) . ")\n AND post_status = '%s'\n AND (post_type = '%s' OR post_type = '%s' OR post_type = '%s' OR post_type = '%s' OR post_type = '%s')\n AND ID NOT IN (" . implode($pages_to_ignore_gcmaz, ", ") . ")\n ORDER BY post_date DESC\n LIMIT {$rows_to_return}\n "; //for debugging //$prepared_statement = $gcmaz_wpdb->prepare($sql, $var_for_publish, $var_for_post, $var_for_whats, $var_for_concert, $var_for_community, $var_for_splash); //print_r($prepared_statement); // GET RESULTS see notes (blueprint) for details $rows_gcmaz = $gcmaz_wpdb->get_results($gcmaz_wpdb->prepare($sql, $var_for_publish, $var_for_post, $var_for_whats, $var_for_concert, $var_for_community, $var_for_splash)); // QUERY 2) LOCAL Search $sql_local = "\n SELECT ID, post_date, post_title, guid, post_name, post_type\n FROM {$wpdb->posts}\n WHERE (" . implode(' OR ', $sql_search_terms) . ")\n AND post_status = '%s'\n AND (post_type = '%s' OR post_type = '%s')\n AND ID NOT IN (" . implode($pages_to_ignore_local, ", ") . ")\n ORDER BY post_date DESC\n LIMIT {$rows_to_return}\n "; //for debugging //echo "<div style='background:yellow;color:red'>ALL POSTS WITH NEWS</div>"; //$prepared_statement_local = $wpdb->prepare($sql_local, $var_for_publish_local, $var_for_post_local, $var_for_splash_local); //print_r($prepared_statement_local); $rows_local = $wpdb->get_results($wpdb->prepare($sql_local, $var_for_publish_local, $var_for_post_local, $var_for_splash_local)); // convert to array $rows_gcmaz = convert_results_to_array($rows_gcmaz); $rows_local = convert_results_to_array($rows_local); // merge results $results = merge_results($rows_gcmaz, $rows_local); //sort results $results = sort_results($results); return $results; } else { // ENTIRE DOMAIN // WITHOUT NEWS // QUERY 1) gcmaz.com $sql = "\n SELECT ID, post_date, post_title, guid, post_name, post_type\n FROM {$gcmaz_wpdb->posts}\n WHERE (" . implode(' OR ', $sql_search_terms) . ")\n AND post_status = '%s'\n AND (post_type = '%s' OR post_type = '%s' OR post_type = '%s' OR post_type = '%s')\n AND ID NOT IN (" . implode($pages_to_ignore_gcmaz, ", ") . ")\n ORDER BY post_date DESC\n LIMIT {$rows_to_return}\n "; //for debugging //$prepared_statement = $gcmaz_wpdb->prepare($sql, $var_for_publish, $var_for_whats, $var_for_concert, $var_for_community, $var_for_splash); //print_r($prepared_statement); // GET RESULTS see notes (blueprint) for details $rows_gcmaz = $gcmaz_wpdb->get_results($gcmaz_wpdb->prepare($sql, $var_for_publish, $var_for_whats, $var_for_concert, $var_for_community, $var_for_splash)); // QUERY 2) LOCAL Search $sql_local = "\n SELECT ID, post_date, post_title, guid, post_name, post_type\n FROM {$wpdb->posts}\n WHERE (" . implode(' OR ', $sql_search_terms) . ")\n AND post_status = '%s'\n AND (post_type = '%s' OR post_type = '%s')\n AND ID NOT IN (" . implode($pages_to_ignore_local, ", ") . ")\n ORDER BY post_date DESC\n LIMIT {$rows_to_return}\n "; //for debugging //echo "<div style='background:yellow;color:red'>ALL POSTS WITHOUT NEWS</div>"; //$prepared_statement_local = $wpdb->prepare($sql_local, $var_for_publish_local, $var_for_post_local, $var_for_splash_local); //print_r($prepared_statement_local); $rows_local = $wpdb->get_results($wpdb->prepare($sql_local, $var_for_publish_local, $var_for_post_local, $var_for_splash_local)); // convert to array $rows_gcmaz = convert_results_to_array($rows_gcmaz); $rows_local = convert_results_to_array($rows_local); // merge results $results = merge_results($rows_gcmaz, $rows_local); //sort results $results = sort_results($results); return $results; } } } //----------------------------------------------------- // FILTERED SEARCH TO SHOW PAGES NOT POSTS //----------------------------------------------------- if ($showPosts == false && $showPages == true) { if ($showDomain == 'local') { // LOCAL ONLY // SKIPS NEWS $sql_local = "\n SELECT ID, post_date, post_title, guid, post_name, post_type\n FROM {$wpdb->posts}\n WHERE (" . implode(' OR ', $sql_search_terms) . ")\n AND post_status = '%s'\n AND (post_type = '%s')\n AND ID NOT IN (" . implode($pages_to_ignore_local, ", ") . ")\n ORDER BY post_date DESC\n LIMIT {$rows_to_return}\n "; //for debugging //echo "<div style='background:yellow;color:red'>LOCAL PAGES, SKIPS NEWS</div>"; //$prepared_statement_local = $wpdb->prepare($sql_local, $var_for_publish_local, $var_for_post_local, $var_for_splash_local); //print_r($prepared_statement_local); $rows_local = $wpdb->get_results($wpdb->prepare($sql_local, $var_for_publish_local, $var_for_page_local)); // convert to array $rows_local = convert_results_to_array($rows_local); //sort results $results = sort_results($rows_local); return $results; } else { // ENTIRE DOMAIN // WITH NEWS if ($showNews == true) { // QUERY 1) gcmaz.com $sql = "\n SELECT ID, post_date, post_title, guid, post_name, post_type\n FROM {$gcmaz_wpdb->posts}\n WHERE (" . implode(' OR ', $sql_search_terms) . ")\n AND post_status = '%s'\n AND (post_type = '%s' OR post_type = '%s')\n AND ID NOT IN (" . implode($pages_to_ignore_gcmaz, ", ") . ")\n ORDER BY post_date DESC\n LIMIT {$rows_to_return}\n "; //for debugging //$prepared_statement = $gcmaz_wpdb->prepare($sql, $var_for_publish, $var_for_page); //print_r($prepared_statement); $rows_gcmaz = $gcmaz_wpdb->get_results($gcmaz_wpdb->prepare($sql, $var_for_publish, $var_for_post, $var_for_page)); // QUERY 2) local // -------------------------- // PREPARE THE STATEMENT $sql_local = "\n SELECT ID, post_date, post_title, guid, post_name, post_type\n FROM {$wpdb->posts}\n WHERE (" . implode(' OR ', $sql_search_terms) . ")\n AND post_status = '%s'\n AND (post_type = '%s')\n AND ID NOT IN (" . implode($pages_to_ignore_local, ", ") . ")\n ORDER BY post_date DESC\n LIMIT {$rows_to_return}\n "; //for debugging //echo "<div style='background:yellow;color:red'>ALL PAGES WITH NEWS</div>"; //$prepared_statement_local = $wpdb->prepare($sql_local, $var_for_publish_local, $var_for_post_local, $var_for_splash_local); //print_r($prepared_statement_local); //GET RESULTS see notes (blueprint) for details $rows_local = $wpdb->get_results($wpdb->prepare($sql_local, $var_for_publish_local, $var_for_page_local)); // convert to array $rows_gcmaz = convert_results_to_array($rows_gcmaz); $rows_local = convert_results_to_array($rows_local); // merge results $results = merge_results($rows_gcmaz, $rows_local); //sort results $results = sort_results($results); return $results; } else { // ENTIRE DOMAIN // WITHOUT NEWS // QUERY 1) gcmaz.com $sql = "\n SELECT ID, post_date, post_title, guid, post_name, post_type\n FROM {$gcmaz_wpdb->posts}\n WHERE (" . implode(' OR ', $sql_search_terms) . ")\n AND post_status = '%s'\n AND (post_type = '%s')\n AND ID NOT IN (" . implode($pages_to_ignore_gcmaz, ", ") . ")\n ORDER BY post_date DESC\n LIMIT {$rows_to_return}\n "; //for debugging //$prepared_statement = $gcmaz_wpdb->prepare($sql, $var_for_publish, $var_for_page); //print_r($prepared_statement); $rows_gcmaz = $gcmaz_wpdb->get_results($gcmaz_wpdb->prepare($sql, $var_for_publish, $var_for_page)); // QUERY 2) local // -------------------------- // PREPARE THE STATEMENT $sql_local = "\n SELECT ID, post_date, post_title, guid, post_name, post_type\n FROM {$wpdb->posts}\n WHERE (" . implode(' OR ', $sql_search_terms) . ")\n AND post_status = '%s'\n AND (post_type = '%s')\n AND ID NOT IN (" . implode($pages_to_ignore_local, ", ") . ")\n ORDER BY post_date DESC\n LIMIT {$rows_to_return}\n "; //for debugging //echo "<div style='background:yellow;color:red'>ALL PAGES WITHOUT NEWS</div>"; //$prepared_statement_local = $wpdb->prepare($sql_local, $var_for_publish_local, $var_for_post_local, $var_for_splash_local); //print_r($prepared_statement_local); //GET RESULTS see notes (blueprint) for details $rows_local = $wpdb->get_results($wpdb->prepare($sql_local, $var_for_publish_local, $var_for_page_local)); // convert to array $rows_gcmaz = convert_results_to_array($rows_gcmaz); $rows_local = convert_results_to_array($rows_local); // merge results $results = merge_results($rows_gcmaz, $rows_local); //sort results $results = sort_results($results); return $results; } } } }
function main() { if (strcmp('4.1.0', phpversion()) > 0) { die("Error: PHP version 4.1.0 or above required!"); } if (!($file = fopen("search.idx", "rb"))) { die("Error: Search index file could NOT be opened!"); } if (readHeader($file) != "DOXS") { die("Error: Header of index file is invalid!"); } $query = ""; if (array_key_exists("query", $_GET)) { $query = $_GET["query"]; } end_form(ereg_replace("[^[:alnum:]:\\.\\t ]", " ", $query)); echo " \n<div class=\"searchresults\">\n"; $results = array(); $requiredWords = array(); $forbiddenWords = array(); $foundWords = array(); $word = strtok($query, " "); while ($word) { if ($word[0] == '+') { $word = substr($word, 1); $requiredWords[] = $word; } if ($word[0] == '-') { $word = substr($word, 1); $forbiddenWords[] = $word; } if (!in_array($word, $foundWords)) { $foundWords[] = $word; search($file, strtolower($word), $results); } $word = strtok(" "); } $docs = array(); combine_results($results, $docs); // filter out documents with forbidden word or that do not contain // required words $filteredDocs = filter_results($docs, $requiredWords, $forbiddenWords); // sort the results based on rank $sorted = array(); sort_results($filteredDocs, $sorted); // report results to the user report_results($sorted); echo "</div>\n"; fclose($file); }
function search() { if (strcmp('4.1.0', phpversion()) > 0) { die("Error: PHP version 4.1.0 or above required!"); } $paths = paths(); $files = array(); $j = 0; for ($i = 0; $i < sizeof($paths); $i++) { if (!($f = @fopen($paths[$i] . "search.idx", "rb"))) { die("Error: Search index file could NOT be opened!"); continue; } $files[$j++] = $f; if (readHeader($f) != "DOXS") { die("Error: Header of index file is invalid!"); } } $query = ""; if (array_key_exists("query", $_GET)) { $query = $_GET["query"]; } //end_form($query); echo " \n<div class=\"searchresults\">\n"; $results = array(); $requiredWords = array(); $forbiddenWords = array(); $foundWords = array(); $word = strtok($query, " "); while ($word) { if ($word[0] == '+') { $word = substr($word, 1); $requiredWords[] = $word; } if ($word[0] == '-') { $word = substr($word, 1); $forbiddenWords[] = $word; } if (!in_array($word, $foundWords)) { $foundWords[] = $word; for ($i = 0; $i < sizeof($files); $i++) { do_search($paths[$i], $files[$i], strtolower($word), $results); } } $word = strtok(" "); } $docs = array(); combine_results($results, $docs); // filter out documents with forbidden word or that do not contain // required words $filteredDocs = filter_results($docs, $requiredWords, $forbiddenWords); // sort the results based on rank $sorted = array(); sort_results($filteredDocs, $sorted); // report results to the user report_results($sorted); echo "</div>\n"; foreach ($files as $file) { fclose($file); } }
function main($idxfile) { if (strcmp('4.1.0', phpversion()) > 0) { die("Error: PHP version 4.1.0 or above required!"); } if (!($file = fopen($idxfile, "rb"))) { die("Error: Search index file could NOT be opened!"); } if (readHeader($file) != "DOXS") { die("Error: Header of index file is invalid!"); } $query = ""; if (array_key_exists("query", $_GET)) { $query = $_GET["query"]; } $results = array(); $requiredWords = array(); $forbiddenWords = array(); $foundWords = array(); $word = strtolower(strtok($query, " ")); while ($word) { if ($word[0] == '+') { $word = substr($word, 1); $requiredWords[] = $word; } if ($word[0] == '-') { $word = substr($word, 1); $forbiddenWords[] = $word; } if (!in_array($word, $foundWords)) { $foundWords[] = $word; search($file, $word, $results); } $word = strtolower(strtok(" ")); } $docs = array(); combine_results($results, $docs); // filter out documents with forbidden word or that do not contain // required words $filteredDocs = filter_results($docs, $requiredWords, $forbiddenWords); // normalize rankings so they are in the range [0-100] normalize_ranking($filteredDocs); // sort the results based on rank $sorted = array(); sort_results($filteredDocs, $sorted); // report results to the user report_results($sorted); fclose($file); }