word: writer page to edit database entries
authorMischa POSLAWSKY <perl@shiar.org>
Sun, 24 May 2020 01:49:31 +0000 (03:49 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Sat, 6 Jun 2020 01:49:20 +0000 (03:49 +0200)
Initial form to maintain a larger amount of words.

tools/word.pg.sql
writer.plp [new file with mode: 0644]

index 8a5fec649224dbbbd34525c5477f289eefc9f99e..3c4f264b48c27350bcca962a882843b6477a8240 100644 (file)
@@ -1,13 +1,19 @@
 CREATE TABLE word (
        form       text        NOT NULL,
+       lang       text        NOT NULL DEFAULT 'eng',
        cat        integer              REFERENCES word (id),
+       ref        integer              REFERENCES word (id),
        source     text,
        thumb      text[],
+       wptitle    text,
        created    timestamptz          DEFAULT now(),
        id         serial      NOT NULL PRIMARY KEY
 );
 
 COMMENT ON COLUMN word.form       IS 'preferred textual representation';
+COMMENT ON COLUMN word.lang       IS 'ISO 639-3 language code';
 COMMENT ON COLUMN word.cat        IS 'hierarchical classification';
+COMMENT ON COLUMN word.ref        IS 'reference to equivalent eng translation';
 COMMENT ON COLUMN word.source     IS 'URI of downloaded image';
 COMMENT ON COLUMN word.thumb      IS 'ImageMagick convert options to create thumbnail from source image';
+COMMENT ON COLUMN word.wptitle    IS 'reference Wikipedia article';
diff --git a/writer.plp b/writer.plp
new file mode 100644 (file)
index 0000000..ce421b8
--- /dev/null
@@ -0,0 +1,66 @@
+<(common.inc.plp)><:
+
+Html({
+       title => 'words cheat sheet admin',
+       version => '1.0',
+       nocache => 1,
+});
+
+use List::Util qw( pairs pairkeys );
+
+my $db = eval {
+       my @dbinfo = (
+               'DBI:Pg:dbname=sheet;host=localhost', 'sheetadmin', 'fairuse',
+       ) or die "database not configured\n";
+       require DBIx::Simple;
+       DBIx::Simple->new(@dbinfo[0..2], {
+               RaiseError => 1,
+               pg_enable_utf8 => 1,
+       });
+} or Abort('Database error', 501, $@);
+
+my @wordcols = (
+       form    => 'Translation',
+       ref     => 'Reference',
+       cat     => 'Category',
+       lang    => 'Language',
+       source  => 'Image URL',
+       thumb   => 'Convert options',
+       wptitle => 'Wikipedia',
+);
+my $find = $Request ? {id => $Request} : undef;
+
+my $row;
+if ($ENV{REQUEST_METHOD} eq 'POST') {
+       $row = {%post{ pairkeys @wordcols }};
+       $_ = length ? $_ : undef for values %{$row};
+       eval {
+               my %res = (returning => $Request ? '*' : 'lang, cat');
+               my $query = $find ? $db->update(word => $row, $find, \%res) :
+                       $db->insert(word => $row, \%res);
+               $row = $query->hash;
+       } or Alert("Entry could not be saved", $@);
+}
+elsif ($find) {
+       $row = $db->select(word => '*', $find)->hash
+               or Abort("Word not found", 404);
+}
+
+my $title = $find ? "entry <small>#$Request</small>" : 'new entry';
+:>
+<h1>Words <:= $title :></h1>
+
+<form action="" method="post">
+<dl>
+<:
+
+for my $col (pairs @wordcols) {
+       printf '<dt><label for="%s">%s</label></dt>'
+               . '<dd><input id="%1$s" name="%1$s" value="%s" />',
+               $col->key, $col->value, $row->{$col->key} // '';
+       say '</dd>';
+}
+:>
+</dl>
+<p><input type="submit" value="Save" /></p>
+</form>