chiark / gitweb /
Make a licensing decision: it's all AGPLv3+.
[odin-cgi] / bin / populate-lang-table
1 #! /usr/bin/perl
2 ###
3 ### Setup program for Odin services
4 ###
5 ### (c) 2015 Mark Wooding
6 ###
7
8 ###----- Licensing notice ---------------------------------------------------
9 ###
10 ### This file is part of the `odin.gg' service, `odin-cgi'.
11 ###
12 ### `odin-cgi' is free software; you can redistribute it and/or modify
13 ### it under the terms of the GNU Affero General Public License as
14 ### published by the Free Software Foundation; either version 3 of the
15 ### License, or (at your option) any later version.
16 ###
17 ### `odin-cgi' is distributed in the hope that it will be useful,
18 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ### GNU Affero General Public License for more details.
21 ###
22 ### You should have received a copy of the GNU Affero General Public
23 ### License along with `odin-cgi'; if not, see
24 ### <http://www.gnu.org/licenses/>.
25
26 use lib "lib";
27 use Odin;
28
29 my $db = Odin::open_db;
30 my %newlang, %oldlang;
31
32 open my $fh, "-|", "highlight", "-p" or die "highlight: $!";
33 while (<$fh>) {
34   my ($descr, $lang) = /^(.*\S)\s*:\s*(\S+)(?:\s.*|)$/;
35   next if !defined $lang or $lang eq "txt";
36   $newlang{"hl:$lang"} = $descr;
37 }
38 close $fh or die "close highlight: $! $?";
39 $newlang{"txt"} = "Plain text";
40 $newlang{"md"} = "Markdown";
41
42 Odin::xact {
43   my $h = $db->selectall_hashref
44     ("SELECT lang, descr FROM odin_pastebin_lang", "lang");
45   for my $k (keys %$h) { $oldlang{$k} = $h->{$k}{descr}; }
46   for my $lang (keys %oldlang) {
47     if (!exists $newlang{$lang}) {
48       print ";; delete stale language `$lang' (`$oldlang{$lang}')\n";
49       $db->do("DELETE FROM odin_pastebin_lang WHERE lang = ?", undef, $lang);
50     }
51   }
52   for my $lang (keys %newlang) {
53     if (!exists $oldlang{$lang}) {
54       print ";; insert new language `$lang' (`$newlang{$lang}')\n";
55       $db->do("INSERT INTO odin_pastebin_lang (lang, descr) VALUES (?, ?)",
56               undef, $lang, $newlang{$lang});
57     } elsif ($oldlang{$lang} ne $newlang{$lang}) {
58       print ";; change description for `$lang' ",
59         "(`$oldlang{$lang}' -> `$newlang{$lang}')\n";
60       $db->do("UPDATE odin_pastebin_lang SET descr = ? WHERE lang = ?",
61               undef, $newlang{$lang}, $lang);
62     }
63   }
64 } $db;