diff options
| author | Christian Weiske <cweiske@cweiske.de> | 2016-02-11 22:43:34 +0100 |
|---|---|---|
| committer | Christian Weiske <cweiske@cweiske.de> | 2016-02-11 22:43:34 +0100 |
| commit | d6c817be8dfb9d41ea3f19cecd90619cde97209d (patch) | |
| tree | e511903d2bfcf87f8d26da68fd4df529d33eaa3a | |
| parent | e43db7fd25798616ad38a959bd378bb765b9323c (diff) | |
| download | phinde-d6c817be8dfb9d41ea3f19cecd90619cde97209d.tar.gz phinde-d6c817be8dfb9d41ea3f19cecd90619cde97209d.zip | |
opensearch supportv0.1.0
| -rw-r--r-- | README.rst | 1 | ||||
| -rw-r--r-- | data/templates/base.htm | 1 | ||||
| -rw-r--r-- | data/templates/opensearch.htm | 21 | ||||
| -rw-r--r-- | data/templates/opensearchdescription.htm | 9 | ||||
| -rw-r--r-- | data/templates/search.htm | 2 | ||||
| -rw-r--r-- | src/phinde/Helper.php | 18 | ||||
| -rw-r--r-- | www/index.php | 11 | ||||
| -rw-r--r-- | www/opensearchdescription.php | 17 | ||||
| -rw-r--r-- | www/www-header.php | 2 |
9 files changed, 79 insertions, 3 deletions
@@ -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}&page={startPage?}&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) { |
