opensearch support v0.1.0
authorChristian Weiske <cweiske@cweiske.de>
Thu, 11 Feb 2016 21:43:34 +0000 (22:43 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Thu, 11 Feb 2016 21:43:34 +0000 (22:43 +0100)
README.rst
data/templates/base.htm
data/templates/opensearch.htm [new file with mode: 0644]
data/templates/opensearchdescription.htm [new file with mode: 0644]
data/templates/search.htm
src/phinde/Helper.php
www/index.php
www/opensearchdescription.php [new file with mode: 0644]
www/www-header.php

index 3d10657..7977635 100644 (file)
@@ -24,6 +24,7 @@ Features
   - Query: ``foo bar site:example.org/dir/``
   - or use the ``site`` GET parameter:
     ``/?q=foo&site=example.org/dir``
+- OpenSearch support with HTML and Atom result lists
 
 
 ============
index f54cd0c..a497f01 100644 (file)
@@ -9,6 +9,7 @@
   <link rel="stylesheet" href="css/phinde.css"/>
   <link rel="icon" href="favicon.ico"/>
   <meta name="generator" content="phinde" />
+  <link rel="search" title="{{apptitle}}" type="application/opensearchdescription+xml" href="/opensearchdescription.php"/>
   {% block meta %}{% endblock %}
  </head>
  <body>
diff --git a/data/templates/opensearch.htm b/data/templates/opensearch.htm
new file mode 100644 (file)
index 0000000..a1000f1
--- /dev/null
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<feed xmlns="http://www.w3.org/2005/Atom" 
+      xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
+  <title>"{{query}}" search results</title> 
+  <link rel="self" href="{{fullUrl}}" type="application/atom+xml"/>
+  <updated>{{"now"|date("c")}}</updated>
+  <id>{{fullUrl}}</id>
+  <opensearch:totalResults>{{hitcount}}</opensearch:totalResults>
+  {% for hit in hits %}
+    {% set doc = hit._source %}
+    <entry>
+     <title>{{doc.title}}</title>
+     <link href="{{doc.url}}"/>
+     <id>{{doc.url}}</id>
+     {% if doc.modate %}
+     <updated>{{doc.modate|date('c')}}</updated>
+     {% endif %}
+     <content type="text">{{doc.htmlText|striptags}}</content>
+    </entry>
+  {% endfor %}
+</feed>
diff --git a/data/templates/opensearchdescription.htm b/data/templates/opensearchdescription.htm
new file mode 100644 (file)
index 0000000..6bc2f96
--- /dev/null
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
+ <ShortName>{{apptitle}}</ShortName>
+ <Description>Search</Description>
+ <Url type="application/atom+xml" rel="results"
+      template="{{absBaseUrl}}?q={searchTerms}&amp;page={startPage?}&amp;format=opensearch"/>
+ <Url type="text/html" rel="results" method="get"
+      template="{{absBaseUrl}}?q={searchTerms}"/>
+</OpenSearchDescription>
index c7b1bb7..f0481a9 100644 (file)
@@ -18,7 +18,7 @@
     <div class="container">
      <ul class="nav">
       <li>
-       <a class="brand" href="{{baseurl}}">{{apptitle}}</a>
+       <a class="brand" href="{{baseUrl}}">{{apptitle}}</a>
       </li>
      </ul>
      <form class="navbar-form pull-left">
index 312c5e5..43345ba 100644 (file)
@@ -41,5 +41,23 @@ class Helper
             )
         );
     }
+
+    /**
+     * Create a full URL with protocol and host name
+     *
+     * @param string $path Path to the file, with leading /
+     *
+     * @return string Full URL
+     */
+    public static function fullUrl($path = '/')
+    {
+        if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']) {
+            $prot = 'https';
+        } else {
+            $prot = 'http';
+        }
+        return $prot . '://' . $_SERVER['HTTP_HOST'] . $path;
+    }
+
 }
 ?>
index 54d5da8..dbdcfd2 100644 (file)
@@ -137,11 +137,20 @@ $urlSortRelevance = buildLink(
 );
 $urlSortDate = $urlSortRelevance . '&sort=date';
 
+if (isset($_GET['format']) && $_GET['format'] == 'opensearch') {
+    $template = 'opensearch';
+    $baseLink .= '&format=opensearch';
+    header('Content-type: application/atom+xml');
+} else {
+    $template = 'search';
+}
+
 render(
-    'search',
+    $template,
     array(
         'queryTime' => round($timeEnd - $timeBegin, 2) . 's',
         'query' => $query,
+        'fullUrl' => Helper::fullUrl($baseLink),
         'cleanQuery' => $cleanQuery,
         'urlNoSite' => $urlNoSite,
         'site' => $site,
diff --git a/www/opensearchdescription.php b/www/opensearchdescription.php
new file mode 100644 (file)
index 0000000..443546e
--- /dev/null
@@ -0,0 +1,17 @@
+<?php
+namespace phinde;
+/**
+ * OpenSearch XML description element
+ *
+ * @link http://www.opensearch.org/
+ */
+require 'www-header.php';
+
+header('Content-type: application/opensearchdescription+xml');
+render(
+    'opensearchdescription',
+    array(
+        'absBaseUrl' => Helper::fullUrl('/'),
+    )
+);
+?>
\ No newline at end of file
index da0d74c..e7c4542 100644 (file)
@@ -27,7 +27,7 @@ function render($tplname, $vars = array(), $return = false)
         //$vars['htmlhelper'] = new HtmlHelper();
     }
     $vars['apptitle'] = 'cweiske.de search';
-    $vars['baseurl'] = '/';
+    $vars['baseUrl'] = '/';
 
     $template = $GLOBALS['twig']->loadTemplate($tplname . '.htm');
     if ($return) {