#!/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 Mojolicious::Lite -signatures; use Mojo::SQLite; use HTML::Escape qw/escape_html/; my $dbFile = "combined.db"; my $sqlPortsFile = "/usr/local/share/sqlports"; my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime(); $year = $year + 1900; 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; } else { $sqlPortsFile = "/tmp/openbsd_app/stable/share/sqlports"; } 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( $sqlPortsFile, { ReadOnly => 1, no_wal => 1 } ); $sql->on( connection => sub { my ( $sql, $dbh ) = @_; $dbh->do("pragma journal_mode=DELETE"); } ); return $sql; }; my $query = q{ SELECT FULLPKGNAME, FULLPKGPATH, COMMENT, DESCRIPTION, HOMEPAGE, highlight(%s, 2, '**', '**') AS COMMENT_MATCH, highlight(%s, 3, '**', '**') AS DESCR_MATCH FROM %s WHERE %s MATCH ? ORDER BY rank; }; 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 $reverseQuery = q{ WITH RECURSIVE d (fullpkgpath, dependspath, type) as (select root.fullpkgpath, root.dependspath, root.type from _canonical_depends root join _paths on root.dependspath=_paths.canonical join _paths p2 on p2.fullpkgpath = ? and p2.id=_paths.pkgpath where root.type!=3 union select child.fullpkgpath, child.dependspath, child.type from d parent, _canonical_depends child where parent.fullpkgpath=child.dependspath and child.type!=3) select distinct _paths.fullpkgpath from d join _paths on _paths.id=d.fullpkgpath order by _paths.fullpkgpath; }; my $pathQuery = q{ SELECT FULLPKGNAME, FULLPKGPATH, COMMENT, DESCRIPTION, HOMEPAGE FROM %s WHERE FULLPKGPATH = ?; }; my $title = "OpenBSD.app"; my $descr = "OpenBSD package search"; sub markdown ($str) { $str = escape_html($str); $str =~ s/\*\*(\w+)\*\*/$1<\/strong>/g; $str =~ s/\n/
/g; return $str; } 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 ); } sub fix_fts ($s) { return "" unless defined $s; $s =~ s/[^\w]/ /g; $s =~ s/^\s+//g; $s =~ s/\s+$//g; return $s; } get '/reverse' => sub ($c) { my $v = $c->validation; $c->stash( title => $title ); $c->stash( descr => $descr ); $c->stash( mtime => $mtime ); $c->stash( year => (localtime)[5] + 1900 ); my $search = $c->param('name'); my $raw = $c->param('raw'); my $db = $c->sqlports->db; if ( defined $raw && $raw ne "" ) { $c->render( text => $db->query( $reverseQuery, $search )->text ); } else { $c->render( template => 'reverse', name => $search, list => $db->query( $reverseQuery, $search )->text ); } }; get '/tree' => sub ($c) { my $v = $c->validation; $c->stash( title => $title ); $c->stash( descr => $descr ); $c->stash( mtime => $mtime ); $c->stash( year => (localtime)[5] + 1900 ); my $search = $c->param('name'); my $raw = $c->param('raw'); my $db = $c->sqlports->db; if ( defined $raw && $raw ne "" ) { $c->render( text => $db->query( $depsQuery, $search )->text ); } else { $c->render( template => 'tree', name => $search, tree => $db->query( $depsQuery, $search )->text ); } }; get '/path/*' => sub ($c) { my $p = $c->req->url->path; $p =~ s/^\/path\///; my $current = $c->param('current'); $c->stash( title => $title ); $c->stash( descr => $descr ); $c->stash( mtime => $mtime ); $c->stash( year => (localtime)[5] + 1900 ); my $db = $c->sqlite->db; my $q = sprintf( $pathQuery, defined($current) ? "current_ports_fts" : "stable_ports_fts" ); $c->render( template => 'path', name => $p, info => $db->query( $q, $p )->hash ); }; get '/' => sub ($c) { my $v = $c->validation; my $search = fix_fts $c->param('search'); my $current = $c->param('current'); my $link = defined($current) ? "?current=on" : ""; my $format = $c->param('format'); $c->stash( current => $current ); $c->stash( title => $title ); $c->stash( descr => $descr ); $c->stash( mtime => $mtime ); $c->stash( year => (localtime)[5] + 1900 ); if ( defined $search && $search ne "" ) { my $db = $c->sqlite->db; 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); if ( $format eq "json" ) { $c->render( json => $results ); } else { $c->render( template => 'results', search => $search, elapsed => $elapsed, link => $link, results => $results ); } } else { $c->render( template => 'index' ); } }; get '/pico.classless.css' => sub ($c) { $c->reply->static('pico.classless.css'); }; 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 ); }; app->start; __DATA__ @@ layouts/default.html.ep <%= $title %>

OpenBSD.app - search packages

%= form_for '/' => begin %= label_for search => 'Search', class => 'none' %= search_field 'search', id => 'search', placeholder => 'Search', value => undef %= check_box 'current', id => 'current', role => "switch" %= label_for current => "Search -current" (<%= $mtime %>) % end
<%== content %>
@@ tree.html.ep % layout 'default';

Dependency tree for: <%= $name %>

<%= $tree %>

@@ path.html.ep % layout 'default';

Info for: <%= $name %>

% if ($info->{FULLPKGNAME} ne "") {
Package Name Details Comment Description
<%= $info->{FULLPKGNAME} %> <%== $info->{COMMENT} %> <%== $info->{DESCRIPTION} %>
% } else { Nothing found. % }

@@ reverse.html.ep % layout 'default';

Reverse dependency list for: <%= $name %>

<%= $list %>

@@ results.html.ep % layout 'default';

Found <%= @$results %> results for '<%= $search %>' in <%= $elapsed %> seconds.
View as JSON

% foreach my $result (@$results) { % }
Package Name Details Comment Description
<%= $result->{FULLPKGNAME} %> <%== $result->{COMMENT_MATCH} %> <%== $result->{DESCR_MATCH} %>
@@ index.html.ep % layout 'default';

Welcome! Default search queries OpenBSD 7.5 package sets.

@@ exception.html.ep % layout 'default';

Invalid search

@@ openbsd-app-opensearch.xml.ep <%= $title %> <%= $descr %> UTF-8 https://openbsd.org/favicon.ico https://openbsd.app/ @@ favicon.ico (base64) AAABAAQAICAAAAAAAACoCAAARgAAACAgEAAAAAAA6AIAAO4IAAAQEAAAAAAAAGgFAADWCwAAEBAQ AAAAAAAoAQAAPhEAACgAAAAgAAAAQAAAAAEACAAAAAAAgAQAAAAAAAAAAAAAAAEAAAAAAAAAAAAA AACAAACAAAAAgIAAgAAAAIAAgACAgAAAwMDAAICAgAAAAP8AAP8AAAD//wD/AAAA/wD/AP//AAD/ //8AwwAAAM8AAADbAAAA5wAAAPMAAAD/AAAA/xcXAP8vLwD/U1MA/2tnAP9/fwD/i4sA/5eXAP+j owD/r68A/7u7AP/HxwD/z8cA/9vbAP/n5wD/8/MA//v3ACsrUwA3N18AQ0NrAE9PdwBXV38AY2OL AG9vlwB/f6cAi4uzAJeXvwCnp88As7PbAL+/5wDHx+8Az8/3AFMrKwBfNzcAa0NDAHdPTwCDW1sA j2dnAJtzcwCnf38As4uLAL+XlwDLo6MA16+vAOO7uwDrw8MA+9PTAC9TLwA7XzsAR2tHAFN3UwBf g18Aa49rAHebdwCDp4MAj7OPAJu/mwCny6cAs9ezAL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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP////////// /m///+5O7//2BMHf0YABH8AAAD/AAABvwAAAR8AAAAPAAAADwAAAAcAAAADAAAAAgAAAAMAAABCA AAAIAAAAAMAAAA+AAAAfAAAAP8AAAB/AAAAPgAAA7xgAAP/wAA5//gEPf/znn//859/////f//// ////////KAAAACAAAABAAAAAAQAEAAAAAACAAgAAAAAAAAAAAAAQAAAAAAAAAAQCBAAMhpwABNL0 ABxGTAAEqswAHGZ0AIyCfADMxswATEZEAPz+/AAsKiQApJ6cAGxqZABMjpwAXFZMAAAAAAD///// //////////////////////////////////////////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////// ////////////KAAAABAAA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