summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2018-09-29 12:51:54 +0200
committerYorhel <git@yorhel.nl>2018-09-29 12:52:47 +0200
commit4e3081f530526601d82fbe792ea4c0f9bc9a9c9a (patch)
tree44eeb9323c7e1af549c5dfa6f540e9c37ef90a0a
parent706f447961d18c36ffdc6be34a6eae40acd01574 (diff)
Add default spoiler level to tags and use it in spoiler calculation
https://vndb.org/t11296.3 (And I forgot to add update_20180929.sql in the previous commit)
-rw-r--r--lib/VNDB/DB/Tags.pm10
-rw-r--r--lib/VNDB/Handler/Tags.pm6
-rw-r--r--util/sql/func.sql24
-rw-r--r--util/sql/schema.sql3
-rw-r--r--util/updates/update_20180929.sql2
5 files changed, 28 insertions, 17 deletions
diff --git a/lib/VNDB/DB/Tags.pm b/lib/VNDB/DB/Tags.pm
index 8c27e55f..fb4717d8 100644
--- a/lib/VNDB/DB/Tags.pm
+++ b/lib/VNDB/DB/Tags.pm
@@ -39,7 +39,7 @@ sub dbTagGet {
't.meta = ?' => $o{meta}?1:0 ) : (),
);
my @select = (
- qw|t.id t.meta t.name t.description t.state t.cat t.c_items|,
+ qw|t.id t.meta t.name t.description t.state t.cat t.c_items t.defaultspoil|,
q|extract('epoch' from t.added) as added|,
$o{what} =~ /addedby/ ? ('t.addedby', 'u.username') : (),
);
@@ -129,7 +129,7 @@ sub dbTagEdit {
$self->dbExec('UPDATE tags !H WHERE id = ?', {
$o{upddate} ? ('added = NOW()' => 1) : (),
- map exists($o{$_}) ? ("$_ = ?" => $o{$_}) : (), qw|name meta description state cat|
+ map exists($o{$_}) ? ("$_ = ?" => $o{$_}) : (), qw|name meta description state cat defaultspoil|
}, $id);
if($o{aliases}) {
$self->dbExec('DELETE FROM tags_aliases WHERE tag = ?', $id);
@@ -147,8 +147,8 @@ sub dbTagEdit {
# returns the id of the new tag
sub dbTagAdd {
my($self, %o) = @_;
- my $id = $self->dbRow('INSERT INTO tags (name, meta, description, state, cat, addedby) VALUES (!l, ?) RETURNING id',
- [ map $o{$_}, qw|name meta description state cat| ], $o{addedby}||$self->authInfo->{id}
+ my $id = $self->dbRow('INSERT INTO tags (name, meta, description, state, cat, defaultspoil, addedby) VALUES (!l, ?) RETURNING id',
+ [ map $o{$_}, qw|name meta description state cat defaultspoil| ], $o{addedby}||$self->authInfo->{id}
)->{id};
$self->dbExec('INSERT INTO tags_parents (tag, parent) VALUES (?, ?)', $id, $_) for(@{$o{parents}});
$self->dbExec('INSERT INTO tags_aliases (tag, alias) VALUES (?, ?)', $id, $_) for (@{$o{aliases}});
@@ -257,7 +257,7 @@ sub dbTagStats {
my($r, $np) = $self->dbPage(\%o, qq|
SELECT t.id, t.name, t.cat, count(*) as cnt, $rating as rating,
- COALESCE(avg(CASE WHEN tv.ignore THEN NULL ELSE tv.spoiler END), 0) as spoiler,
+ COALESCE(avg(CASE WHEN tv.ignore THEN NULL ELSE tv.spoiler END), t.defaultspoil) as spoiler,
bool_or(tv.ignore) AS overruled
FROM tags t
JOIN tags_vn tv ON tv.tag = t.id
diff --git a/lib/VNDB/Handler/Tags.pm b/lib/VNDB/Handler/Tags.pm
index 141c75aa..6b02a6dd 100644
--- a/lib/VNDB/Handler/Tags.pm
+++ b/lib/VNDB/Handler/Tags.pm
@@ -162,6 +162,7 @@ sub tagedit {
{ post => 'meta', required => 0, default => 0 },
{ post => 'alias', required => 0, maxlength => 1024, default => '', regex => [ qr/^[^,]+$/s, 'No comma allowed in aliases' ] },
{ post => 'description', required => 0, maxlength => 10240, default => '' },
+ { post => 'defaultspoil',required => 0, default => 0, enum => [ 0..2 ] },
{ post => 'parents', required => !$self->authCan('tagmod'), default => '' },
{ post => 'merge', required => 0, default => '' },
);
@@ -187,6 +188,7 @@ sub tagedit {
cat => $frm->{cat},
description => $frm->{description},
meta => $frm->{meta}?1:0,
+ defaultspoil => $frm->{defaultspoil},
aliases => \@aliases,
parents => \@parents,
);
@@ -203,7 +205,7 @@ sub tagedit {
}
if($tag) {
- $frm->{$_} ||= $t->{$_} for (qw|name meta description state cat|);
+ $frm->{$_} ||= $t->{$_} for (qw|name meta description state cat defaultspoil|);
$frm->{alias} ||= join "\n", @{$t->{aliases}};
$frm->{parents} ||= join ', ', map $_->{name}, @{$t->{parents}};
}
@@ -249,6 +251,8 @@ sub tagedit {
[ textarea => short => 'alias', name => "Aliases\n(separated by newlines)", cols => 30, rows => 4 ],
[ textarea => short => 'description', name => 'Description' ],
[ static => content => 'What should the tag be used for? Having a good description helps users choose which tags to link to a VN.' ],
+ [ select => short => 'defaultspoil', name => 'Default spoiler level', options => [ map [$_, fmtspoil $_], 0..2 ] ],
+ [ static => content => 'This is the spoiler level that will be used by default when everyone has voted "neutral".' ],
[ input => short => 'parents', name => 'Parent tags' ],
[ static => content => 'Comma separated list of tag names to be used as parent for this tag.' ],
$self->authCan('tagmod') ? (
diff --git a/util/sql/func.sql b/util/sql/func.sql
index e36d46e8..4d52d1bf 100644
--- a/util/sql/func.sql
+++ b/util/sql/func.sql
@@ -133,12 +133,13 @@ BEGIN
-- All votes for all tags, including votes inherited by child tags if the
-- parent tag itself does not have any votes.
-- (also includes meta tags, because they could have a normal tag as parent)
- WITH RECURSIVE tags_vn_all(lvl, tag, vid, uid, vote, spoiler, meta) AS (
- SELECT 15, tag, vid, uid, vote, spoiler, false
- FROM tags_vn
- WHERE NOT ignore
+ WITH RECURSIVE tags_vn_all(lvl, tag, vid, uid, vote, spoiler, defaultspoil, meta) AS (
+ SELECT 15, tv.tag, tv.vid, tv.uid, tv.vote, tv.spoiler, t.defaultspoil, false
+ FROM tags_vn tv
+ JOIN tags t ON t.id = tv.tag
+ WHERE NOT tv.ignore
UNION ALL
- SELECT lvl-1, tp.parent, ta.vid, ta.uid, ta.vote, ta.spoiler, t.meta
+ SELECT lvl-1, tp.parent, ta.vid, ta.uid, ta.vote, ta.spoiler, t.defaultspoil, t.meta
FROM tags_vn_all ta
JOIN tags_parents tp ON tp.tag = ta.tag
JOIN tags t ON t.id = tp.parent
@@ -148,15 +149,18 @@ BEGIN
)
-- grouped by (tag, vid)
SELECT tag, vid, COUNT(uid) AS users, AVG(vote)::real AS rating,
- (CASE WHEN AVG(spoiler) > 1.3 THEN 2 WHEN AVG(spoiler) > 0.7 THEN 1 ELSE 0 END)::smallint AS spoiler
+ (CASE WHEN COUNT(spoiler) = 0 THEN defaultspoil
+ WHEN AVG(spoiler) > 1.3 THEN 2
+ WHEN AVG(spoiler) > 0.7 THEN 1 ELSE 0
+ END)::smallint AS spoiler
FROM (
-- grouped by (tag, vid, uid), so only one user votes on one parent tag per VN entry (also removing meta tags)
- SELECT tag, vid, uid, MAX(vote)::real, AVG(spoiler)::real
+ SELECT tag, vid, uid, MAX(vote)::real, AVG(spoiler)::real, defaultspoil
FROM tags_vn_all
WHERE NOT meta
- GROUP BY tag, vid, uid
- ) AS t(tag, vid, uid, vote, spoiler)
- GROUP BY tag, vid
+ GROUP BY tag, vid, uid, defaultspoil
+ ) AS t(tag, vid, uid, vote, spoiler, defaultspoil)
+ GROUP BY tag, vid, defaultspoil
HAVING AVG(vote) > 0;
-- recreate index
CREATE INDEX tags_vn_inherit_tag_vid ON tags_vn_inherit (tag, vid);
diff --git a/util/sql/schema.sql b/util/sql/schema.sql
index b3669dd5..6c2d785b 100644
--- a/util/sql/schema.sql
+++ b/util/sql/schema.sql
@@ -461,7 +461,8 @@ CREATE TABLE tags (
state smallint NOT NULL DEFAULT 0,
c_items integer NOT NULL DEFAULT 0,
addedby integer NOT NULL DEFAULT 0,
- cat tag_category NOT NULL DEFAULT 'cont'
+ cat tag_category NOT NULL DEFAULT 'cont',
+ defaultspoil smallint NOT NULL DEFAULT 0
);
-- tags_aliases
diff --git a/util/updates/update_20180929.sql b/util/updates/update_20180929.sql
new file mode 100644
index 00000000..bd854f59
--- /dev/null
+++ b/util/updates/update_20180929.sql
@@ -0,0 +1,2 @@
+ALTER TABLE traits ADD COLUMN defaultspoil smallint NOT NULL DEFAULT 0;
+ALTER TABLE tags ADD COLUMN defaultspoil smallint NOT NULL DEFAULT 0;