adding opensearch (xml description j2 template, RSS+XML search results PHP)

This commit is contained in:
Peter Molnar 2019-05-22 10:15:21 +01:00
parent 32bf4994e7
commit e18582840e
4 changed files with 92 additions and 18 deletions

34
nasg.py
View file

@ -326,6 +326,7 @@ class MarkdownDoc(object):
@property @property
def dt(self): def dt(self):
""" this returns a timestamp, not an arrow object """
maybe = self.mtime maybe = self.mtime
for key in ['published', 'date']: for key in ['published', 'date']:
t = self.meta.get(key, None) t = self.meta.get(key, None)
@ -500,7 +501,6 @@ class Singular(MarkdownDoc):
maybe = self.dt maybe = self.dt
if len(self.comments): if len(self.comments):
for c in self.comments.values(): for c in self.comments.values():
if c.dt > maybe: if c.dt > maybe:
maybe = c.dt maybe = c.dt
return maybe return maybe
@ -1574,7 +1574,7 @@ class Search(PHPFile):
return ret return ret
def append(self, post): def append(self, post):
mtime = int(post.mtime) mtime = int(post.published.timestamp)
check = self.check(post.name) check = self.check(post.name)
if (check and check < mtime): if (check and check < mtime):
self.db.execute(''' self.db.execute('''
@ -1602,24 +1602,22 @@ class Search(PHPFile):
self.is_changed = True self.is_changed = True
@property @property
def renderfile(self): def templates(self):
return os.path.join( return ['Search.j2.php', 'OpenSearch.j2.php', 'OpenSearch.j2.xml']
settings.paths.get('build'),
'search.php'
)
@property
def templatefile(self):
return 'Search.j2.php'
async def _render(self): async def _render(self):
r = J2.get_template(self.templatefile).render({ for template in self.templates:
'post': {}, r = J2.get_template(template).render({
'site': settings.site, 'post': {},
'menu': settings.menu, 'site': settings.site,
'meta': settings.meta, 'menu': settings.menu,
}) 'meta': settings.meta,
writepath(self.renderfile, r) })
target = os.path.join(
settings.paths.get('build'),
template.replace('.j2', '').lower()
)
writepath(target, r)
class IndexPHP(PHPFile): class IndexPHP(PHPFile):

View file

@ -0,0 +1,66 @@
<?php
$db = new SQLite3('./search.sqlite', SQLITE3_OPEN_READONLY);
$q = str_replace('-', '+', $_GET['q']);
$sql = $db->prepare("
SELECT
url, category, title, snippet(data, '', '', '[...]', 5, 24), mtime
FROM
data
WHERE
data MATCH :q
ORDER BY
category
");
$sql->bindValue(':q', $q);
$query = $sql->execute();
$results = array();
if($query) {
while ($row = $query->fetchArray(SQLITE3_ASSOC)) {
array_push($results, $row);
}
}
foreach ($results as $row) {
$item_node = $channel_node->appendChild($xml->createElement("item"));
$title_node = $item_node->appendChild($xml->createElement("title", $row['title']));
$link_node = $item_node->appendChild($xml->createElement("link", $row['url']));
$guid_link = $xml->createElement("guid", $row['url']);
$guid_link->setAttribute("isPermaLink","true");
$guid_node = $item_node->appendChild($guid_link);
$description_node = $item_node->appendChild($xml->createElement("description"));
$description_contents = $xml->createCDATASection(htmlentities($row["snippet(data, '', '', '[...]', 5, 24)"]));
$description_node->appendChild($description_contents);
$date_rfc = gmdate(DATE_RFC2822, $row['mtime']);
$pub_date = $xml->createElement("pubDate", $date_rfc);
$pub_date_node = $item_node->appendChild($pub_date);
}
header('Content-Type: text/xml; charset=utf-8', true);
$xml = new DOMDocument("1.0", "UTF-8");
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;
$rss = $xml->createElement("rss");
$rss_node = $xml->appendChild($rss);
$rss_node->setAttribute("version","2.0");
$rss_node->setAttribute("xmlns:dc","http://a9.com/-/spec/opensearch/1.1/");
$channel = $xml->createElement("channel");
$channel_node = $rss_node->appendChild($channel);
$channel_node->appendChild($xml->createElement("title", "Search results for: {$_GET['q']}"));
$channel_node->appendChild($xml->createElement("link", "{{ site.url }}"));
$channel_node->appendChild($xml->createElement("description", "Search {{ site.name }} for {$_GET['q']}"));
$channel_node->appendChild($xml->createElement("openSearch:totalResults", sizeof($results)));
$channel_node->appendChild($xml->createElement("openSearch:startIndex", 1));
$channel_node->appendChild($xml->createElement("openSearch:itemsPerPage", sizeof($results)));
$build_date = gmdate(DATE_RFC2822, time());
$channel_node->appendChild($xml->createElement("lastBuildDate", $build_date));
echo $xml->saveXML();

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<ShortName>{{ site.name }}</ShortName>
<Description>Search {{ site.name }}</Description>
<Url type="application/rss+xml" rel="results" method="get" template="{{ site.url }}/opensearch.php?q={searchTerms}" />
<Url type="text/html" rel="results" method="get" template="{{ site.url }}/search.php?q={searchTerms}" />
<Image>{{ site.image }}</Image>
<InputEncoding>UTF-8</InputEncoding>
</OpenSearchDescription>

View file

@ -23,6 +23,7 @@
<style media="print"> <style media="print">
{% include('style-print.css') %} {% include('style-print.css') %}
</style> </style>
<link rel="search" type="application/opensearchdescription+xml" href="/opensearch.xml" title="{{ site.name }}">
</head> </head>
<body> <body>