💾 Archived View for gmn.clttr.info › sources › gmnifaq.git › tree › faqs.pl.txt captured on 2024-05-10 at 10:58:36.
⬅️ Previous capture (2023-01-29)
-=-=-=-=-=-=-
#!/usr/bin/perl # Copyright René Wagner 2020 # licenced under BSD 3-Clause licence # https://src.clttr.info/rwa/gmnifaq use strict; use DBI; use URI::Escape; use lib 'lib/'; use gmnifaq; # enable UTF-8 mode for everything use utf8; binmode STDOUT, ':utf8'; binmode STDERR, ':utf8'; config(); if (!defined($ENV{'SERVER_PROTOCOL'}) || $ENV{'SERVER_PROTOCOL'} ne 'GEMINI') { write_response('CGI_ERROR', 'CGI execution error', undef); } my @body = (); push @body, header(); push @body, faqs(); push @body, footer(); write_response('SUCCESS', 'text/gemini', @body); exit; sub sql { my $query = lc(uri_unescape($ENV{'QUERY_STRING'})); if ( $query eq '' ) { return "SELECT * FROM questions q ORDER BY question;"; } if ( $query =~ /tag=([0-9]+)/i ) { return "SELECT q.* FROM questions q LEFT JOIN tags_questions tq ON q.id = tq.q_id WHERE tq.t_id = $1"; } if ( $query =~ /faq=([0-9]+)/i ) { return "SELECT q.* FROM questions q LEFT JOIN tags_questions tq ON q.id = tq.q_id WHERE q.id = $1"; } write_response('CGI_ERROR', 'invalid query string', undef); } sub faqs { my @return; my $dbh = DBI->connect($CONF{'dsn'}, '', '', { RaiseError => 1 }) or die $DBI::errstr; my @rows = $dbh->selectall_array(sql()); if ( !scalar @rows ) { push @return, ('No faqs found!', ''); } else { foreach (@rows) { push @return, sprintf("## %s", @$_[1]); push @return, (@$_[2], ''); my @tags = $dbh->selectall_array("SELECT id, name FROM tags t LEFT JOIN tags_questions tq ON tq.t_id = t.id WHERE tq.q_id = @$_[0];"); if ( scalar @tags ) { push @return, ('### tags'); foreach (@tags) { push @return, sprintf("=> ./faqs.pl?tag=%d %s", @$_[0], @$_[1]); } push @return, ''; } } } $dbh->disconnect(); return @return; } sub header { return ( '# '. $CONF{'name'}, ''); }