aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Weiske <cweiske@cweiske.de>2016-02-11 22:43:34 +0100
committerChristian Weiske <cweiske@cweiske.de>2016-02-11 22:43:34 +0100
commitd6c817be8dfb9d41ea3f19cecd90619cde97209d (patch)
treee511903d2bfcf87f8d26da68fd4df529d33eaa3a
parente43db7fd25798616ad38a959bd378bb765b9323c (diff)
downloadphinde-d6c817be8dfb9d41ea3f19cecd90619cde97209d.tar.gz
phinde-d6c817be8dfb9d41ea3f19cecd90619cde97209d.zip
opensearch supportv0.1.0
-rw-r--r--README.rst1
-rw-r--r--data/templates/base.htm1
-rw-r--r--data/templates/opensearch.htm21
-rw-r--r--data/templates/opensearchdescription.htm9
-rw-r--r--data/templates/search.htm2
-rw-r--r--src/phinde/Helper.php18
-rw-r--r--www/index.php11
-rw-r--r--www/opensearchdescription.php17
-rw-r--r--www/www-header.php2
9 files changed, 79 insertions, 3 deletions
diff --git a/README.rst b/README.rst
index 3d10657..7977635 100644
--- a/README.rst
+++ b/README.rst
@@ -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
============
diff --git a/data/templates/base.htm b/data/templates/base.htm
index f54cd0c..a497f01 100644
--- a/data/templates/base.htm
+++ b/data/templates/base.htm
@@ -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
index 0000000..a1000f1
--- /dev/null
+++ b/data/templates/opensearch.htm
@@ -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
index 0000000..6bc2f96
--- /dev/null
+++ b/data/templates/opensearchdescription.htm
@@ -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>
diff --git a/data/templates/search.htm b/data/templates/search.htm
index c7b1bb7..f0481a9 100644
--- a/data/templates/search.htm
+++ b/data/templates/search.htm
@@ -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">
diff --git a/src/phinde/Helper.php b/src/phinde/Helper.php
index 312c5e5..43345ba 100644
--- a/src/phinde/Helper.php
+++ b/src/phinde/Helper.php
@@ -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;
+ }
+
}
?>
diff --git a/www/index.php b/www/index.php
index 54d5da8..dbdcfd2 100644
--- a/www/index.php
+++ b/www/index.php
@@ -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
index 0000000..443546e
--- /dev/null
+++ b/www/opensearchdescription.php
@@ -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
diff --git a/www/www-header.php b/www/www-header.php
index da0d74c..e7c4542 100644
--- a/www/www-header.php
+++ b/www/www-header.php
@@ -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) {