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 3d10657a381b155ae8d76808a5ebad7ce8de1ca2..7977635eb0a7d415281fe9ed4da28cee410459eb 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``
   - 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 f54cd0cf9d909f3adbf9af417fbc285942112d42..a497f017ba23e06d90041cdfbb4121615eceb6ee 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="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>
   {% 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 c7b1bb7c5420b4c3a4950fe9288428c4f2e4e6e7..f0481a9c73ab4f954b14c0f5863a8de418dab453 100644 (file)
@@ -18,7 +18,7 @@
     <div class="container">
      <ul class="nav">
       <li>
     <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">
       </li>
      </ul>
      <form class="navbar-form pull-left">
index 312c5e54a35ba4790c15842ca349504f241468ac..43345ba22899c890966c5a659bda068b0daebbe6 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 54d5da80f4ba2afcdb455a4294854acd6b77d9a6..dbdcfd20d7e1ccaa72a99d2e9aab8bd5f82f88e1 100644 (file)
@@ -137,11 +137,20 @@ $urlSortRelevance = buildLink(
 );
 $urlSortDate = $urlSortRelevance . '&sort=date';
 
 );
 $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(
 render(
-    'search',
+    $template,
     array(
         'queryTime' => round($timeEnd - $timeBegin, 2) . 's',
         'query' => $query,
     array(
         'queryTime' => round($timeEnd - $timeBegin, 2) . 's',
         'query' => $query,
+        'fullUrl' => Helper::fullUrl($baseLink),
         'cleanQuery' => $cleanQuery,
         'urlNoSite' => $urlNoSite,
         'site' => $site,
         '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 da0d74cd8cf60a3187b008707b6065147eff7bc8..e7c45420c6026835050efd428f6bc0d2d1aadc62 100644 (file)
@@ -27,7 +27,7 @@ function render($tplname, $vars = array(), $return = false)
         //$vars['htmlhelper'] = new HtmlHelper();
     }
     $vars['apptitle'] = 'cweiske.de search';
         //$vars['htmlhelper'] = new HtmlHelper();
     }
     $vars['apptitle'] = 'cweiske.de search';
-    $vars['baseurl'] = '/';
+    $vars['baseUrl'] = '/';
 
     $template = $GLOBALS['twig']->loadTemplate($tplname . '.htm');
     if ($return) {
 
     $template = $GLOBALS['twig']->loadTemplate($tplname . '.htm');
     if ($return) {