openbsd.app/openbsd.app.pl

371 lines
14 KiB
Perl
Raw Permalink Normal View History

#!/usr/bin/env perl
# vim: set ts=4 sw=4 tw=0:
# vim: set expandtab:
use warnings;
use strict;
use Time::HiRes qw( time );
use File::Basename;
use feature 'switch';
2022-09-23 16:45:55 -06:00
use Mojolicious::Lite -signatures;
use Mojo::SQLite;
use Text::Markdown qw{ markdown };
2022-09-23 16:45:55 -06:00
my $dbFile = "combined.db";
2022-09-26 19:51:58 -06:00
if ( $^O eq "openbsd" ) {
require OpenBSD::Pledge;
require OpenBSD::Unveil;
OpenBSD::Unveil::unveil( "/", "" ) or die;
OpenBSD::Unveil::unveil( $0, "r" ) or die;
OpenBSD::Unveil::unveil( dirname($0), "rwc" ) or die;
OpenBSD::Unveil::unveil( "/usr/local", "r" ) or die;
OpenBSD::Pledge::pledge(qw( stdio dns inet rpath proc flock wpath cpath ))
or die;
}
my $mtime = ( stat($dbFile) )[9];
$mtime = scalar localtime $mtime;
helper sqlite => sub {
state $sql = Mojo::SQLite->new;
$sql->from_filename( $dbFile, { ReadOnly => 1, no_wal => 1 } );
$sql->on(
connection => sub {
my ( $sql, $dbh ) = @_;
$dbh->do("pragma journal_mode=DELETE");
}
);
return $sql;
};
helper sqlports => sub {
state $sql = Mojo::SQLite->new;
$sql->from_filename( "/usr/local/share/sqlports",
{ ReadOnly => 1, no_wal => 1 } );
$sql->on(
connection => sub {
my ( $sql, $dbh ) = @_;
$dbh->do("pragma journal_mode=DELETE");
}
);
return $sql;
};
2022-09-23 16:45:55 -06:00
my $query = q{
SELECT
FULLPKGNAME,
FULLPKGPATH,
COMMENT,
DESCRIPTION,
highlight(%s, 2, '**', '**') AS COMMENT_MATCH,
highlight(%s, 3, '**', '**') AS DESCR_MATCH
FROM %s
WHERE %s MATCH ? ORDER BY rank;
2022-09-23 16:45:55 -06:00
};
my $depsQuery = q{
WITH RECURSIVE
under_port(name,level) AS (
VALUES(? ,0)
UNION ALL
SELECT _depends.fulldepends, under_port.level+1
FROM
_depends
JOIN _paths ON _paths.id=_depends.fullpkgpath
join under_port ON _paths.fullpkgpath = under_port.name
where
_depends.type IN (0, 1)
ORDER BY 2 DESC
)
SELECT substr('..........',1,level*3) || name FROM under_port;
};
my $title = "OpenBSD.app";
my $descr = "OpenBSD package search";
sub to_md ($results) {
foreach my $result (@$results) {
$result->{DESCR_MATCH} = markdown( $result->{DESCR_MATCH} );
$result->{COMMENT_MATCH} = markdown( $result->{COMMENT_MATCH} );
}
}
sub set_query ($is_current) {
if ($is_current) {
return sprintf( $query, ("current_ports_fts") x 4 );
}
return sprintf( $query, ("stable_ports_fts") x 4 );
}
2022-10-10 12:11:44 -06:00
sub fix_fts ($s) {
return "" unless defined $s;
$s =~ s/[^\w]/ /g;
2022-10-10 14:34:20 -06:00
$s =~ s/^\s+//g;
$s =~ s/\s+$//g;
2022-10-10 12:11:44 -06:00
return $s;
}
get '/tree' => sub ($c) {
my $v = $c->validation;
$c->stash( title => $title );
$c->stash( descr => $descr );
$c->stash( mtime => $mtime );
my $search = $c->param('name');
2022-10-06 20:25:59 -06:00
my $raw = $c->param('raw');
my $db = $c->sqlports->db;
2022-10-06 20:25:59 -06:00
if ( $raw ne "" ) {
$c->render( text => $db->query( $depsQuery, $search )->text );
}
else {
$c->render(
template => 'tree',
name => $search,
tree => $db->query( $depsQuery, $search )->text
);
}
};
2022-09-23 16:45:55 -06:00
get '/' => sub ($c) {
my $v = $c->validation;
2022-10-10 12:11:44 -06:00
my $search = fix_fts $c->param('search');
2022-09-23 20:25:02 -06:00
my $current = $c->param('current');
my $format = $c->param('format');
2022-09-23 16:45:55 -06:00
$c->stash( title => $title );
$c->stash( descr => $descr );
$c->stash( mtime => $mtime );
2022-09-23 20:25:02 -06:00
if ( defined $search && $search ne "" ) {
my $db = $c->sqlite->db;
2022-09-23 16:45:55 -06:00
my $q = set_query( defined $current );
my $start = time();
my $results = $db->query( $q, $search )->hashes;
my $end = time();
my $elapsed = sprintf( "%2f\n", $end - $start );
to_md($results);
2022-09-23 20:25:02 -06:00
given ($format) {
when ("json") {
$c->render( json => $results );
}
default {
$c->render(
template => 'results',
search => $search,
elapsed => $elapsed,
results => $results
);
}
}
2022-09-23 16:45:55 -06:00
}
else {
$c->render( template => 'index' );
}
};
get '/openbsd-app-opensearch.xml' => sub ($c) {
$c->res->headers->content_type('application/opensearchdescription+xml');
$c->render(
template => 'openbsd-app-opensearch',
format => 'xml',
title => $title,
descr => $descr
);
};
2022-09-23 16:45:55 -06:00
app->start;
__DATA__
@@ layouts/default.html.ep
<!doctype html>
<html class="no-js" lang="">
<head>
<title><%= $title %></title>
2022-09-23 16:45:55 -06:00
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="description" content="<%= $descr %>">
<link
rel="search"
type="application/opensearchdescription+xml"
2022-09-25 17:01:15 -06:00
title="<%= $title %>"
href="/openbsd-app-opensearch.xml" />
2022-11-29 21:22:42 -07:00
<link rel="stylesheet" href="https://deftly.net/pico.classless.css">
2022-09-23 16:45:55 -06:00
</head>
<body>
2022-11-29 21:22:42 -07:00
<header>
<h3><a href="/">OpenBSD.app - search packages</a></h3>
2022-11-29 21:22:42 -07:00
%= form_for '/' => begin
%= tag 'input', type => 'search', name => 'search', id => 'search', placeholder => 'Search'
%= check_box id => 'switch', role => "switch"
%= label_for 'switch' => 'Search -current'
% end
</header>
<main>
<%== content %>
</main>
2022-09-23 16:45:55 -06:00
<footer>
2022-10-06 06:50:31 -06:00
<p><a href="https://github.com/qbit/openbsd.app">OpenBSD.app</a> © 2022 - proudly hosted on <a href="https://openbsd.amsterdam/">obsd.ams</a>!</p>
2022-09-23 16:45:55 -06:00
</footer>
</body>
</html>
@@ tree.html.ep
% layout 'default';
<div>
2022-10-06 20:18:50 -06:00
<h3>Dependency tree for: <%= $name %></h3>
<p>
<pre><%= $tree %></pre>
</p>
</div>
2022-09-23 16:45:55 -06:00
@@ results.html.ep
% layout 'default';
<p>
Found <b><%= @$results %></b> results for '<b><%= $search %></b>' in <%= $elapsed %> seconds.<br />
<a href="/?search=<%= $search %>&format=json">View as JSON</a>
</p>
2022-09-23 21:45:29 -06:00
<table class="results">
2022-09-23 16:45:55 -06:00
<thead>
<tr>
2022-09-23 21:07:13 -06:00
<th>Package Name</th>
2022-09-23 21:45:29 -06:00
<th>Path</th>
2022-09-23 16:45:55 -06:00
<th>Comment</th>
<th>Description</th>
</tr>
</thead>
% foreach my $result (@$results) {
<tr>
2022-09-23 21:07:13 -06:00
<td class="nowrap"><%= $result->{FULLPKGNAME} %></td>
2022-10-10 12:51:37 -06:00
<td class="nowrap">
<a
href="/tree?name=<%= $result->{FULLPKGPATH} %>"
title="Dependencies for <%= $result->{FULLPKGNAME} %>"
><%= $result->{FULLPKGPATH} %></a>
</td>
2022-09-23 20:25:02 -06:00
<td class="nowrap"><%== $result->{COMMENT_MATCH} %></td>
2022-09-23 16:45:55 -06:00
<td><%== $result->{DESCR_MATCH} %></td>
</tr>
% }
</table>
@@ index.html.ep
% layout 'default';
2022-09-28 15:42:48 -06:00
<p>Welcome! Default search queries OpenBSD 7.2 package sets. You can search -current packages (from <i><%= $mtime %></i>) by toggling the '-current' checkbox.</p>
<p>Search syntax is limited to <a href="https://www.sqlite.org/fts5.html">SQLite's FTS5 syntax</a>. for now.</p>
2022-10-10 12:12:05 -06:00
@@ exception.html.ep
% layout 'default';
<h2>Invalid search</h2>
@@ openbsd-app-opensearch.xml.ep
2022-09-25 09:54:21 -06:00
<?xml version="1.0" encoding="utf-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/"
xmlns:moz="http://www.mozilla.org/2006/browser/search/">
<ShortName><%= $title %></ShortName>
<Description><%= $descr %></Description>
<InputEncoding>UTF-8</InputEncoding>
<Image width="32" height="32" type="image/x-icon">https://openbsd.org/favicon.ico</Image>
2022-09-25 09:54:21 -06:00
<Url type="text/html" method="GET" template="https://openbsd.app/?search={searchTerms}"/>
<moz:SearchForm>https://openbsd.app/</moz:SearchForm>
</OpenSearchDesc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AAD/////
//////////////////////////////////////////7////////////////////8j/P/////////
//////+v+O/0P/P///j/////////UY/+gh+lzuiM/zP/////jvHVFB1Bzq//ioVD/////4jzIiIk
XKqq+qpRT//////q8SIiIlqoqoylRV8x////6lIiIiJYr6qM4UMfVF///24RERIiJa+oju6KFTWq
//+BVCJEIiJeZujszzE0+v//UkG3fRIiTGzIjGikFB8//xIk2ZfEIkVszuy6oRRDNf8SIRmZsiIh
5u7suKRaFBH/0iQU3UIiIYbO7LyhX/Uk/xTNFEQiIiRWbI7IoR//M/FdlxJNVCIkV3uo7IQhr///
/XZBt3UiJBd76KiEX////1HVJWedIiTbbLtmYf////5REiRXcUIk5u7GZtX/////8SIiJVVCQcyI
zMhR//////QkIiI1IkXOjMyI/z////8VgSIiIiReyu7oM/////////zlJBQY7qherzX////////6
5UOGu+oyQ/////////////Ff+NP/8U/////////////z//8///9f////////////////////r///
////////////////////////////////////////////////////////////////////////////
/////v////5v///2Zuv/8YQDP8gAAD/IAAB/wAAAT8AAAEfAAAADwAAAC8AAAAXAAAAAwAAAAMAA
ABjAAAAcgAAAB+AAAB/AAAA/gAAAP+AAAD/gAADfwAAA//gABP/4AA///mOf//733////9//////
////////////KAAAABAAAAAgAAAAAQAIAAAAAABAAQAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAIAA
AIAAAACAgACAAAAAgACAAICAAADAwMAAgICAAAAA/wAA/wAAAP//AP8AAAD/AP8A//8AAP///wDD
AAAAzwAAANsAAADnAAAA8wAAAP8AAAD/FxcA/y8vAP9TUwD/a2cA/39/AP+LiwD/l5cA/6OjAP+v
rwD/u7sA/8fHAP/PxwD/29sA/+fnAP/z8wD/+/cAKytTADc3XwBDQ2sAT093AFdXfwBjY4sAb2+X
AH9/pwCLi7MAl5e/AKenzwCzs9sAv7/nAMfH7wDPz/cAUysrAF83NwBrQ0MAd09PAINbWwCPZ2cA
m3NzAKd/fwCzi4sAv5eXAMujowDXr68A47u7AOvDwwD709MAL1MvADtfOwBHa0cAU3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