From: Mark Wooding Date: Thu, 26 Nov 2015 15:45:28 +0000 (+0000) Subject: bin/pastebin.userv, lib/Odin.pm: Centralize language-tag checking. X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/odin-cgi/commitdiff_plain/49e1be4cc948adf83e0e0024ee8b5a15890e0a7c bin/pastebin.userv, lib/Odin.pm: Centralize language-tag checking. Move the actual checking logic into the main library. Do the checking in both `new' and `update' operations. --- diff --git a/bin/pastebin.userv b/bin/pastebin.userv index fe9e55d..42dc66d 100755 --- a/bin/pastebin.userv +++ b/bin/pastebin.userv @@ -60,9 +60,6 @@ EOF $op->bad if @ARGV; $op->ok or Odin::fail "usage: new [-l LANG] [-t TITLE]"; $p{content} = read_content; - @{$db->selectall_arrayref - ("SELECT lang FROM odin_pastebin_lang WHERE lang = ?", undef, $p{lang})} - or Odin::fail "unknown language `$p{lang}'"; my ($tag, $edit) = Odin::new_pastebin %p; print "$Odin::PASTEBIN/$tag $edit\n"; } elsif ($op eq "get") { diff --git a/lib/Odin.pm b/lib/Odin.pm index 154fae0..bc5947e 100644 --- a/lib/Odin.pm +++ b/lib/Odin.pm @@ -376,6 +376,16 @@ our @PASTEBIN_PROPS = keys %PASTEBIN_DEFAULTS; our $PASTEBIN_PROPCOLS = join ", ", @PASTEBIN_PROPS; our $PASTEBIN_PROPPLACES = join ", ", map "?", @PASTEBIN_PROPS; +sub check_lang ($) { + my ($lang) = @_; + + return unless defined $lang; + my $db = open_db; + @{$db->selectall_arrayref + ("SELECT lang FROM odin_pastebin_lang WHERE lang = ?", undef, $lang)} + or fail "unknown language `$lang'"; +} + sub new_pastebin (\%) { my ($new) = @_; @@ -384,6 +394,7 @@ sub new_pastebin (\%) { my $tag; merge_hash %$new, %PASTEBIN_DEFAULTS; + check_lang $new->{lang}; xact { $tag = encode_tag next_seq $db, "odin_pastebin_seq"; insert_record $db, "odin_pastebin", @@ -455,6 +466,7 @@ sub update_pastebin ($$\%) { my $db = open_db; my $editp = 0; + check_lang $new->{lang}; xact { get_pastebin_check_editkey_or_owner $db, $tag, $editkey, my %old; for my $p (@PASTEBIN_PROPS) {