word: validate source values as http urls
[sheet.git] / tools / word.pg.sql
1 CREATE TABLE login (
2         username   text        NOT NULL UNIQUE,
3         pass       text,
4         email      text,
5         fullname   text,
6         editlang   text[],
7         id         serial      NOT NULL PRIMARY KEY
8 );
9
10 CREATE TABLE word (
11         form       text        NOT NULL,
12         alt        text[],
13         lang       text        NOT NULL DEFAULT 'en',
14         cat        integer              REFERENCES word (id),
15         ref        integer              REFERENCES word (id),
16         prio       smallint             DEFAULT '1'
17                                         CHECK (prio >= 0 OR ref IS NOT NULL),
18         grade      integer,
19         cover      boolean     NOT NULL DEFAULT FALSE,
20         source     text                 CHECK (source ~ '^https?://'),
21         thumb      text[],
22         wptitle    text,
23         created    timestamptz          DEFAULT now(),
24         creator    integer              REFERENCES login (id),
25         updated    timestamptz,
26         id         serial      NOT NULL PRIMARY KEY
27 );
28
29 COMMENT ON COLUMN word.form       IS 'preferred textual representation';
30 COMMENT ON COLUMN word.alt        IS 'alternate forms with equivalent meaning';
31 COMMENT ON COLUMN word.lang       IS 'ISO 639 language code matching wikipedia subdomain';
32 COMMENT ON COLUMN word.cat        IS 'hierarchical classification';
33 COMMENT ON COLUMN word.ref        IS 'reference to equivalent en translation';
34 COMMENT ON COLUMN word.prio       IS 'difficulty level or importance; lower values have precedence';
35 COMMENT ON COLUMN word.grade      IS 'ascending hierarchical order, preceding default alphabetical';
36 COMMENT ON COLUMN word.cover      IS 'highlight if selected';
37 COMMENT ON COLUMN word.source     IS 'URI of downloaded image';
38 COMMENT ON COLUMN word.thumb      IS 'ImageMagick convert options to create thumbnail from source image';
39 COMMENT ON COLUMN word.wptitle    IS 'reference Wikipedia article';
40 COMMENT ON COLUMN word.updated    IS 'last significant change';
41 COMMENT ON COLUMN word.creator    IS 'user responsible for initial submit';
42
43 CREATE OR REPLACE FUNCTION exportform(word) RETURNS text AS $$
44         SELECT concat(
45                 $1.id, ':',
46                 $1.prio, CASE WHEN $1.cover THEN 'c' ELSE '' END, ':',
47                 array_to_string($1.form || $1.alt, '/')
48         );
49 $$ LANGUAGE SQL IMMUTABLE;
50
51 CREATE OR REPLACE VIEW _cat_words AS
52         SELECT exportform(word.*) form, sub.*, word.lang, word.ref
53         FROM word RIGHT JOIN (
54                 SELECT cat id, array_agg(exportform(word.*) ORDER BY grade, form) forms
55                         FROM word WHERE ref IS NULL GROUP BY cat
56         ) sub USING (id);