summaryrefslogtreecommitdiff
path: root/lib/VNDB/Handler/Tags.pm
blob: 8e4ada7a9cbad5e39a779285ec9464e377f5f8f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506

package VNDB::Handler::Tags;


use strict;
use warnings;
use YAWF ':html', ':xml';
use VNDB::Func;


YAWF::register(
  qr{g([1-9]\d*)},          \&tagpage,
  qr{g([1-9]\d*)/(edit)},   \&tagedit,
  qr{g([1-9]\d*)/(add)},    \&tagedit,
  qr{g([1-9]\d*)/del(/o)?}, \&tagdel,
  qr{g/new},                \&tagedit,
  qr{v([1-9]\d*)/tagmod},   \&vntagmod,
  qr{u([1-9]\d*)/tags},     \&usertags,
  qr{g},                    \&tagtree,
  qr{xml/tags\.xml},        \&tagxml,
);


sub tagpage {
  my($self, $tag) = @_;

  my $t = $self->dbTagGet(id => $tag, what => 'parents(0) childs(2)')->[0];
  return 404 if !$t;

  my $f = $self->formValidate(
    { name => 's', required => 0, default => 'score', enum => [ qw|score title rel pop| ] },
    { name => 'o', required => 0, default => 'd', enum => [ 'a','d' ] },
    { name => 'p', required => 0, default => 1, template => 'int' },
    { name => 'm', required => 0, default => 1, enum => [qw|0 1 2|] },
  );
  return 404 if $f->{_err};

  my($list, $np) = $t->{meta} || $t->{state} != 2 ? ([],0) : $self->dbTagVNs(
    tag => $tag,
    order => {score=>'tb.rating',title=>'vr.title',rel=>'v.c_released',pop=>'v.c_popularity'}->{$f->{s}}.($f->{o}eq'a'?' ASC':' DESC'),
    page => $f->{p},
    results => 50,
    maxspoil => $f->{m},
  );

  my $title = ($t->{meta} ? 'Meta tag: ' : 'Tag: ').$t->{name};
  $self->htmlHeader(title => $title);
  $self->htmlMainTabs('g', $t);

  if($t->{state} != 2) {
    div class => 'mainbox';
     h1 "Tag: $t->{name}";
     if($t->{state} == 1) {
       div class => 'warning';
        h2 'Tag deleted';
        p;
         lit qq|This tag has been removed from the database, and cannot be used or re-added.|.
             qq| File a request on the <a href="/t/db">discussion board</a> if you disagree with this.|;
        end;
       end;
     } else {
       div class => 'notice';
        h2 'Waiting for approval';
        p 'This tag is waiting for a moderator to approve it. You can still use it to tag VNs as you would with a normal tag.';
       end;
     }
    end;
    return $self->htmlFooter if $t->{state} == 1 && !$self->authCan('tagmod');
  }

  div class => 'mainbox';
   a class => 'addnew', href => "/g$tag/add", 'Create child tag' if $self->authCan('tagmod');
   h1 $title;
   h2 class => 'alttitle', 'a.k.a. '.join(', ', split /\n/, $t->{alias}) if $t->{alias};

   p;
    my @p = @{$t->{parents}};
    my @r;
    for (0..$#p) {
      if($_ && $p[$_-1]{lvl} < $p[$_]{lvl}) {
        pop @r for (1..($p[$_]{lvl}-$p[$_-1]{lvl}));
      }
      if($_ < $#p && $p[$_+1]{lvl} < $p[$_]{lvl}) {
        push @r, $p[$_];
      } elsif($#p == $_ || $p[$_+1]{lvl} >= $p[$_]{lvl}) {
        a href => '/g', 'Tags';
        for ($p[$_], reverse @r) {
          txt ' > ';
          a href => "/g$_->{tag}", $_->{name};
        }
        txt " > $t->{name}\n";
      }
    }
    if(!@p) {
      a href => '/g', 'Tags';
      txt " > $t->{name}\n";
    }
   end;

   if($t->{description}) {
     p class => 'center';
      lit bb2html $t->{description};
     end;
   }
  end;

  _childtags($self, $t) if @{$t->{childs}};
  _vnlist($self, $t, $f, $list, $np) if !$t->{meta} && $t->{state} == 2;

  $self->htmlFooter;
}

# used for on both /g and /g+
sub _childtags {
  my($self, $t, $index) = @_;

  my @l = @{$t->{childs}};
  my @tags;
  for (0..$#l) {
    if($l[$_]{lvl} == $l[0]{lvl}) {
      $l[$_]{childs} = [];
      push @tags, $l[$_];
    } else {
      push @{$tags[$#tags]{childs}}, $l[$_];
    }
  }

  div class => 'mainbox';
   if(!$index) {
     h1 'Child tags';
   } else {
     h1 'Browse tags';
   }
   ul class => 'tagtree';
    for my $p (sort { @{$b->{childs}} <=> @{$a->{childs}} } @tags) {
      li;
       a href => "/g$p->{tag}", $p->{name};
       b class => 'grayedout', " ($p->{c_vns})" if $p->{c_vns};
       end, next if !@{$p->{childs}};
       ul;
        for (0..$#{$p->{childs}}) {
          last if $_ >= 5 && @{$p->{childs}} > 6;
          li;
           txt '> ';
           a href => "/g$p->{childs}[$_]{tag}", $p->{childs}[$_]{name};
           b class => 'grayedout', " ($p->{childs}[$_]{c_vns})" if $p->{childs}[$_]{c_vns};
          end;
        }
        if(@{$p->{childs}} > 6) {
          li;
           txt '> ';
           a href => "/g$p->{tag}", style => 'font-style: italic', sprintf '%d more tags...', @{$p->{childs}}-5;
          end;
        }
       end;
      end;
    }
   end;
   clearfloat;
   br;
  end;
}

sub _vnlist {
  my($self, $t, $f, $list, $np) = @_;
  div class => 'mainbox';
   h1 'Visual novels';
   p class => 'browseopts';
    a href => "/g$t->{id}?m=0", $f->{m} == 0 ? (class => 'optselected') : (), 'Hide spoilers';
    a href => "/g$t->{id}?m=1", $f->{m} == 1 ? (class => 'optselected') : (), 'Show minor spoilers';
    a href => "/g$t->{id}?m=2", $f->{m} == 2 ? (class => 'optselected') : (), 'Show major spoilers';
   end;
   if(!@$list) {
     p "\n\nThis tag has not been linked to any visual novels yet, or they were hidden because of the spoiler settings.";
   }
  end;
  return if !@$list;
  $self->htmlBrowse(
    class    => 'tagvnlist',
    items    => $list,
    options  => $f,
    nextpage => $np,
    pageurl  => "/g$t->{id}?m=$f->{m};o=$f->{o};s=$f->{s}",
    sorturl  => "/g$t->{id}?m=$f->{m}",
    header   => [
      [ 'Score',    'score' ],
      [ 'Title',    'title' ],
      [ '',         0       ],
      [ '',         0       ],
      [ 'Released', 'rel'   ],
      [ 'Popularity', 'pop' ],
    ],
    row     => sub {
      my($s, $n, $l) = @_;
      Tr $n % 2 ? (class => 'odd') : ();
       td class => 'tc1';
        tagscore $l->{rating};
        i sprintf '(%d)', $l->{users};
       end;
       td class => 'tc2';
        a href => '/v'.$l->{vid}, title => $l->{original}||$l->{title}, shorten $l->{title}, 100;
       end;
       td class => 'tc3';
        $_ ne 'oth' && cssicon $_, $self->{platforms}{$_}
          for (sort split /\//, $l->{c_platforms});
       end;
       td class => 'tc4';
        cssicon "lang $_", $self->{languages}{$_}
          for (reverse sort split /\//, $l->{c_languages});
       end;
       td class => 'tc5';
        lit monthstr $l->{c_released};
       end;
       td class => 'tc6', sprintf '%.2f', $l->{c_popularity}*100;
      end;
    }
  );
}


sub tagedit {
  my($self, $tag, $act) = @_;

  return $self->htmlDenied if !$self->authCan('tagmod');

  my($frm, $par);
  if($act && $act eq 'add') {
    $par = $self->dbTagGet(id => $tag)->[0];
    return 404 if !$par;
    $frm->{parents} = $tag;
    $tag = undef;
  }

  my $t = $tag && $self->dbTagGet(id => $tag, what => 'parents(1)')->[0];
  return 404 if $tag && !$t;

  if($self->reqMethod eq 'POST') {
    $frm = $self->formValidate(
      { name => 'name',        required => 1, maxlength => 250 },
      { name => 'state',       required => 1, enum => [ 0..2 ] },
      { name => 'meta',        required => 0, default => 0 },
      { name => 'alias',       required => 0, maxlength => 1024, default => '' },
      { name => 'description', required => 0, maxlength => 1024, default => '' },
      { name => 'parents',     required => 0, regex => [ qr/^(\d+)(\s\d+)*$/, 'Parents must be a list of tag IDs' ], default => '' }
    );
    if(!$frm->{_err}) {
      my $c = $self->dbTagGet(name => $frm->{name});
      $frm->{_err} = [ 'tagexists' ] if !$t && @$c || $t && (@$c > 1 || @$c && $c->[0]{id} != $tag);
    }
    if(!$frm->{_err}) {
      $frm->{meta} = $frm->{meta} ? 1 : 0;
      $frm->{parents} = [ split / /, $frm->{parents} ];
      $self->dbTagEdit($tag, %$frm, upddate => $frm->{state} == 2 && $t->{state} != 2) if $tag;
      $tag = $self->dbTagAdd(%$frm) if !$tag;
      $self->resRedirect("/g$tag", 'post');
    }
  }

  if($tag) {
    $frm->{$_} ||= $t->{$_} for (qw|name meta alias description state|);
    $frm->{parents} ||= join ' ', map $_->{tag}, @{$t->{parents}};
  }

  my $title = $par ? "Add child tag to $par->{name}" : $tag ? "Edit tag: $t->{name}" : 'Add new tag';
  $self->htmlHeader(title => $title, noindex => 1);
  $self->htmlMainTabs('g', $par || $t, 'edit') if $t || $par;
  $self->htmlForm({ frm => $frm, action => $par ? "/g$par->{id}/add" : $tag ? "/g$tag/edit" : '/g/new' }, $title => [
    [ input    => short => 'name',     name => 'Primary name' ],
    [ select   => short => 'state',    name => 'State', options => [
      [ 0, 'Awaiting moderation' ], [ 1, 'Deleted/hidden' ], [ 2, 'Approved' ] ] ],
    [ checkbox => short => 'meta',     name => 'This is a meta-tag (only to be used as parent for other tags, not for linking to VN entries)' ],
    $tag ?
      [ static => content => 'WARNING: Checking this option or selecting "Deleted" as state will permanently delete all existing VN relations!' ] : (),
    [ 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.' ],
    [ input    => short => 'parents',  name => 'Parent tags' ],
    [ static   => content => "Space separated list of tag IDs to be used as parent for this tag. A proper user interface will come in the future...<br /><br />...probably." ],
  ]);
  $self->htmlFooter;
}


sub tagdel {
  my($self, $tag, $act) = @_;
  return $self->htmlDenied if !$self->authCan('tagmod');

  # confirm
  if(!$act || $act ne '/o') {
    my $t = $self->dbTagGet(id => $tag)->[0];
    return 404 if !$t->{id};
    $self->htmlHeader(title => 'Delete tag', noindex => 1);
    $self->htmlMainTabs('g', $t, 'del');
    div class => 'mainbox';
     div class => 'warning';
      h2 'Delete tag';
      p;
       lit qq|Are you sure you want to delete the <a href="/g$tag">$t->{name}</a> tag? |
          .qq|All VN relations will be permanently deleted as well!<br /><br />|
          .qq|<a href="/g$tag/del/o">Yes, I'm not kidding!</a>|;
      end;
     end;
    end;
    $self->htmlFooter;
  }
  # delete
  else {
    $self->dbTagDel($tag);
    $self->resRedirect('/g', 'post');
  }
}


sub vntagmod {
  my($self, $vid) = @_;

  my $v = $self->dbVNGet(id => $vid)->[0];
  return 404 if !$v;

  return $self->htmlDenied if !$self->authCan('tag');

  if($self->reqMethod eq 'POST') {
    my $frm = $self->formValidate(
      { name => 'taglinks', required => 0, default => '', maxlength => 10240, regex => [ qr/^[1-9][0-9]*,-?[1-3],-?[0-2]( [1-9][0-9]*,-?[1-3],-?[0-2])*$/, 'meh' ] }
    );
    return 404 if $frm->{_err};
    $self->dbTagLinkEdit($self->authInfo->{id}, $vid, [ map [ split /,/ ], split / /, $frm->{taglinks}]);
  }

  my $my = $self->dbTagLinks(vid => $vid, uid => $self->authInfo->{id});
  my $tags = $self->dbTagStats(vid => $vid, result => 9999);

  my $frm;

  $self->htmlHeader(title => "Add/remove tags for $v->{title}", noindex => 1, js => 'forms');
  $self->htmlMainTabs('v', $v, 'tagmod');
  div class => 'mainbox';
   h1 "Add/remove tags for $v->{title}";
   div class => 'warning';
    h2 'Tagging';
    ul;
     li "Don't forget to hit the submit button on the bottom of the page after changing anything here!";
     li 'Tag guidelines?';
     li 'Some tag information on the site is cached, it can take up to an hour for your changes to be visible everywhere.';
    end;
   end;
  end;
  $self->htmlForm({ frm => $frm, action => "/v$vid/tagmod", hitsubmit => 1 }, 'Tags' => [
    [ hidden => short => 'taglinks', value => '' ],
    [ static => nolabel => 1, content => sub {
      table id => 'tagtable';
       thead;
        Tr;
         td '';
         td colspan => 2, class => 'tc2_1', 'Others';
         td colspan => 2, class => 'tc3_1', 'You';
        end;
        Tr;
         my $i=0;
         td class => 'tc'.++$i, $_ for(qw|Tag Rating Spoiler Rating Spoiler|);
        end;
       end;
       tfoot; Tr;
        td colspan => 5;
         input type => 'text', class => 'text', name => 'addtag', value => '';
         input type => 'button', class => 'submit', value => 'Add tag';
        end;
       end; end;
       tbody;
        for my $t (sort { $a->{name} cmp $b->{name} } @$tags) {
          my $m = (grep $_->{tag} == $t->{id}, @$my)[0] || {};
          Tr;
           td class => 'tc1';
            a href => "/g$t->{id}", $t->{name};
           end;
           td class => 'tc2';
            tagscore !$m->{vote} ? $t->{rating} : $t->{cnt} == 1 ? 0 : ($t->{rating}*$t->{cnt} - $m->{vote}) / ($t->{cnt}-1);
            i ' ('.($t->{cnt} - ($m->{vote} ? 1 : 0)).')';
           end;
           td class => 'tc3', sprintf '%.2f', $t->{spoiler};
           td class => 'tc4', $m->{vote}||0;
           td class => 'tc5', defined $m->{spoiler} ? $m->{spoiler} : -1;
          end;
        }
       end;
      end;
    } ],
  ]);
  $self->htmlFooter;
}


sub usertags {
  my($self, $uid) = @_;

  my $u = $self->dbUserGet(uid => $uid)->[0];
  return 404 if !$u;

  my $f = $self->formValidate(
    { name => 's', required => 0, default => 'cnt', enum => [ qw|cnt name| ] },
    { name => 'o', required => 0, default => 'd', enum => [ 'a','d' ] },
    { name => 'p', required => 0, default => 1, template => 'int' },
  );
  return 404 if $f->{_err};

  # TODO: might want to use AJAX to load the VN list on request
  my($list, $np) = $self->dbTagStats(
    uid => $uid,
    page => $f->{p},
    order => ($f->{s}eq'cnt'?'COUNT(*)':'name').($f->{o}eq'a'?' ASC':' DESC'),
    what => 'vns',
  );

  $self->htmlHeader(title => "Tags by $u->{username}", noindex => 1);
  $self->htmlMainTabs('u', $u, 'tags');
  div class => 'mainbox';
   h1 "Tags by $u->{username}";
   if(@$list) {
     p 'Warning: spoilery tags are not hidden in this list!';
   } else {
     p "$u->{username} doesn't seem to have used the tagging system yet...";
   }
  end;

  if(@$list) {
    $self->htmlBrowse(
      class    => 'tagstats',
      options  => $f,
      nextpage => $np,
      items    => $list,
      pageurl  => "/u$u->{id}/tags?s=$f->{s};o=$f->{o}",
      sorturl  => "/u$u->{id}/tags",
      header   => [
        sub {
          td class => 'tc1';
           b id => 'relhidall';
            lit '<i>&#9656;</i> #VNs ';
           end;
           lit $f->{s} eq 'cnt' && $f->{o} eq 'a' ? "\x{25B4}" : qq|<a href="/u$u->{id}/tags?o=a;s=cnt">\x{25B4}</a>|;
           lit $f->{s} eq 'cnt' && $f->{o} eq 'd' ? "\x{25BE}" : qq|<a href="/u$u->{id}/tags?o=d;s=cnt">\x{25BE}</a>|;
          end;
        },
        [ 'Tag',  'name' ],
        [ ' ', '' ],
      ],
      row     => sub {
        my($s, $n, $l) = @_;
        Tr $n % 2 ? (class => 'odd') : ();
         td class => 'tc1 relhid_but', id => "tag$l->{id}";
          lit "<i>&#9656;</i> $l->{cnt}";
         end;
         td class => 'tc2', colspan => 2;
          a href => "/g$l->{id}", $l->{name};
         end;
        end;
        for(@{$l->{vns}}) {
          Tr class => "relhid tag$l->{id}";
           td class => 'tc1_1';
            tagscore $_->{vote};
           end;
           td class => 'tc1_2';
            a href => "/v$_->{vid}", title => $_->{original}||$_->{title}, shorten $_->{title}, 50;
           end;
           td class => 'tc1_3', !defined $_->{spoiler} ? ' ' : ['No spoiler', 'Minor spoiler', 'Major spoiler']->[$_->{spoiler}];
          end;
        }
      },
    );
  }
  $self->htmlFooter;
}


sub tagtree {
  my $self = shift;

  $self->htmlHeader(title => 'Browse tags');
  my $t = $self->dbTagTree(0, 2, 1);
  _childtags($self, {childs => $t}, 1);
  $self->htmlFooter;
}


sub tagxml {
  my $self = shift;

  my $q = $self->formValidate({ name => 'q', maxlength => 500 });
  return 404 if $q->{_err};
  $q = $q->{q};

  my($list, $np) = $self->dbTagGet(
    $q =~ /^g([1-9]\d*)/ ? (id => $1) : $q =~ /^name:(.+)$/ ? (name => $1) : (search => $q),
    results => 10,
    page => 1,
  );

  $self->resHeader('Content-type' => 'text/xml; charset=UTF-8');
  xml;
  tag 'tags', more => $np ? 'yes' : 'no', query => $q;
   for(@$list) {
     tag 'item', id => $_->{id}, meta => $_->{meta} ? 'yes' : 'no', state => $_->{state}, $_->{name};
   }
  end;
}

1;