Skip to content

Commit

Permalink
Merge branch 'master' of git@github.com:batterseapower/prefix-search
Browse files Browse the repository at this point in the history
  • Loading branch information
batterseapower committed May 17, 2009
2 parents 6e672ce + 6231712 commit 2193a0c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 27 deletions.
18 changes: 15 additions & 3 deletions index.htm
Original file line number Original file line Diff line number Diff line change
@@ -1,10 +1,22 @@
<html> <html>
<head> <head>
<title>Prefix Search</title> <title>Prefix Search</title>
<link rel="search" type="application/opensearchdescription+xml" href="/opensearch.xml" title="Prefix Search" /> <link rel="search" type="application/opensearchdescription+xml" href="opensearch.xml" title="Prefix Search" />
</head> </head>
<body> <body>
<h1>Prefix Search</h1> <h1><a href="index.htm">Prefix Search</a></h1>
<p>This page is only here so you can get to the OpenSearch Description file.</p>
<p>I am a simple search provider that easily lets you use multiple different search
engines by adding a prefix to your searches. For example, you could search for
<a href="search.php?query=rt+fight+club"><tt>rt Fight Club</tt></a> to run a search
for that film on <a href="http://www.rottentomatoes.com">Rotten Tomatoes</a>.</p>

<p>A full list of prefixes you can use is available by searching for
<a href="search.php">nothing at all</a>.</p>

<form action="search.php">
<input type="text" name="query" />
<input type="submit" />
</form>
</body> </body>
</html> </html>
4 changes: 3 additions & 1 deletion opensearch.xml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
<Description>Prefix Search</Description> <Description>Prefix Search</Description>
<Image height="16" width="16" type="image/x-icon">http://prefix-search.omega-prime.co.uk/favicon.ico</Image> <Image height="16" width="16" type="image/x-icon">http://prefix-search.omega-prime.co.uk/favicon.ico</Image>
<Url type="text/html" method="get" template="http://prefix-search.omega-prime.co.uk/search.php?query={searchTerms}" /> <Url type="text/html" method="get" template="http://prefix-search.omega-prime.co.uk/search.php?query={searchTerms}" />
<!--<moz:SearchForm>http://en.wikipedia.org/wiki/Special:Search</moz:SearchForm>--> <!-- I'm going to cheat and use Google suggestions (since that is the default prefix, after all) -->
<Url type="application/x-suggestions+json" template="http://suggestqueries.google.com/complete/search?hl=en&amp;q={searchTerms}"/>
<moz:SearchForm>http://prefix-search.omega-prime.co.uk</moz:SearchForm>
</OpenSearchDescription> </OpenSearchDescription>
84 changes: 61 additions & 23 deletions search.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -38,31 +38,69 @@
# Find user query # Find user query
$query = $_GET['query']; $query = $_GET['query'];


# Parse query term into initial subquery and actual query text if (!$query) {
if (preg_match("/^([^\\s]+)\\s+(.*)$/", $query, $matches)) { # If we didn't get a query, show the user what they COULD have done
# Extract relevant stuff from the regex gubbins ?>
$provider_string = strtolower($matches[1]); <html>
$subquery = $matches[2]; <head>
<title>Prefix Search</title>
<link rel="search" type="application/opensearchdescription+xml" href="opensearch.xml" title="Prefix Search" />
</head>
<body>
<h1><a href="index.htm">Prefix Search</a></h1>

<table>
<tr>
<th>Prefix</th>
<th>Template</th>
</tr>
<?php
foreach ($providers as $provider) {
$provider_strings = $provider[1];
$encoded_provider_template = htmlentities($provider[2]);

foreach ($provider_strings as $i => $provider_string) {
$encoded_provider_string = htmlentities($provider_string);
echo "<tr><td>$encoded_provider_string</td>";
if ($i == 0) {
$provider_strings_count = sizeof($provider_strings);
echo "<td rowspan=\"$provider_strings_count\">$encoded_provider_template</td>";
}
echo "</tr>";
}
}
?>
</table>
</body>
</html>
<?php
} else {
# Parse query term into initial subquery and actual query text
if (preg_match("/^([^\\s]+)\\s+(.*)$/", $query, $matches)) {
# Extract relevant stuff from the regex gubbins
$provider_string = strtolower($matches[1]);
$subquery = $matches[2];


# Find a matching template # Find a matching template
$template = NULL; $template = NULL;
foreach ($providers as $i => $provider) { foreach ($providers as $provider) {
$provider_strings = $provider[1]; $provider_strings = $provider[1];
$provider_template = $provider[2]; $provider_template = $provider[2];
if (is_numeric(array_search($provider_string, $provider_strings))) { if (is_numeric(array_search($provider_string, $provider_strings))) {
$template = $provider_template; $template = $provider_template;
break; break;
} }
} }


# Redirect immediately if we recognised the template # Redirect immediately if we recognised the template
if ($template) { if ($template) {
http_redirect(str_replace("{searchTerms}", urlencode($subquery), $template)); http_redirect(str_replace("{searchTerms}", urlencode($subquery), $template));
return; return;
} }
} }


# Default redirection # Default redirection
http_redirect("http://www.google.com/search?q=" . urlencode($query)); http_redirect("http://www.google.com/search?q=" . urlencode($query));
}


?> ?>

0 comments on commit 2193a0c

Please sign in to comment.