Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of git@github.com:batterseapower/prefix-search

  • Loading branch information...
commit 2193a0c2d83bbfd526e412ab0f3afcb8a6def086 2 parents 6e672ce + 6231712
@batterseapower authored
Showing with 79 additions and 27 deletions.
  1. +15 −3 index.htm
  2. +3 −1 opensearch.xml
  3. +61 −23 search.php
View
18 index.htm
@@ -1,10 +1,22 @@
<html>
<head>
<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>
<body>
- <h1>Prefix Search</h1>
- <p>This page is only here so you can get to the OpenSearch Description file.</p>
+ <h1><a href="index.htm">Prefix Search</a></h1>
+
+ <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>
</html>
View
4 opensearch.xml
@@ -4,5 +4,7 @@
<Description>Prefix Search</Description>
<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}" />
- <!--<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>
View
84 search.php
@@ -38,31 +38,69 @@
# Find user query
$query = $_GET['query'];
-# 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];
+if (!$query) {
+ # If we didn't get a query, show the user what they COULD have done
+?>
+<html>
+ <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
- $template = NULL;
- foreach ($providers as $i => $provider) {
- $provider_strings = $provider[1];
- $provider_template = $provider[2];
- if (is_numeric(array_search($provider_string, $provider_strings))) {
- $template = $provider_template;
- break;
- }
- }
+ # Find a matching template
+ $template = NULL;
+ foreach ($providers as $provider) {
+ $provider_strings = $provider[1];
+ $provider_template = $provider[2];
+ if (is_numeric(array_search($provider_string, $provider_strings))) {
+ $template = $provider_template;
+ break;
+ }
+ }
- # Redirect immediately if we recognised the template
- if ($template) {
- http_redirect(str_replace("{searchTerms}", urlencode($subquery), $template));
- return;
- }
-}
+ # Redirect immediately if we recognised the template
+ if ($template) {
+ http_redirect(str_replace("{searchTerms}", urlencode($subquery), $template));
+ return;
+ }
+ }
-# Default redirection
-http_redirect("http://www.google.com/search?q=" . urlencode($query));
+ # Default redirection
+ http_redirect("http://www.google.com/search?q=" . urlencode($query));
+}
?>
Please sign in to comment.
Something went wrong with that request. Please try again.