summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormorkt <>2014-12-22 11:49:08 +0100
committerYorhel <git@yorhel.nl>2014-12-22 11:49:08 +0100
commit12338003bde24a3a263abe8caca71b1836ecee83 (patch)
tree81cdb33820f949f229e84ab17233420d8cb592b8
parentdf383d117908160d1a84ce4519edc0c9bcf08c3b (diff)
bbcode: Replace [vcpgi][0-9]+ links with a [url] tag
Patch from https://vndb.org/t5564.13
-rw-r--r--lib/VNDB/DB/Chars.pm12
-rw-r--r--lib/VNDB/DB/Discussions.pm4
-rw-r--r--lib/VNDB/DB/Producers.pm13
-rw-r--r--lib/VNDB/DB/VN.pm13
-rw-r--r--lib/VNDB/Util/Misc.pm71
-rw-r--r--lib/VNDBUtil.pm2
6 files changed, 109 insertions, 6 deletions
diff --git a/lib/VNDB/DB/Chars.pm b/lib/VNDB/DB/Chars.pm
index 86b3a859..f387b044 100644
--- a/lib/VNDB/DB/Chars.pm
+++ b/lib/VNDB/DB/Chars.pm
@@ -5,7 +5,7 @@ use strict;
use warnings;
use Exporter 'import';
-our @EXPORT = qw|dbCharGet dbCharRevisionInsert dbCharImageId|;
+our @EXPORT = qw|dbCharGet dbCharRevisionInsert dbCharImageId dbCharNames|;
# options: id rev instance tagspoil trait_inc trait_exc char what results page gender bloodt
@@ -138,6 +138,16 @@ sub dbCharImageId {
}
+sub dbCharNames {
+ my ($self, @ids) = @_;
+ return $self->dbAll(q|
+ SELECT c.id, cr.name
+ FROM chars c
+ JOIN chars_rev cr ON cr.id = c.latest
+ WHERE c.id IN (!l)|, \@ids
+ );
+}
+
1;
diff --git a/lib/VNDB/DB/Discussions.pm b/lib/VNDB/DB/Discussions.pm
index 9da25384..d1462249 100644
--- a/lib/VNDB/DB/Discussions.pm
+++ b/lib/VNDB/DB/Discussions.pm
@@ -4,6 +4,7 @@ package VNDB::DB::Discussions;
use strict;
use warnings;
use Exporter 'import';
+use VNDB::Util::Misc 'bbSubstLinks';
our @EXPORT = qw|dbThreadGet dbThreadEdit dbThreadAdd dbPostGet dbPostEdit dbPostAdd dbThreadCount|;
@@ -214,7 +215,7 @@ sub dbPostEdit {
my($self, $tid, $num, %o) = @_;
my %set = (
- 'msg = ?' => $o{msg},
+ 'msg = ?' => bbSubstLinks($self, $o{msg}),
'edited = to_timestamp(?)' => $o{lastmod},
'hidden = ?' => $o{hidden}?1:0,
);
@@ -236,6 +237,7 @@ sub dbPostAdd {
my $num = $self->dbRow('SELECT num FROM threads_posts WHERE tid = ? ORDER BY num DESC LIMIT 1', $tid)->{num};
$num = $num ? $num+1 : 1;
$o{uid} ||= $self->authInfo->{id};
+ $o{msg} = bbSubstLinks($self, $o{msg});
$self->dbExec(q|
INSERT INTO threads_posts (tid, num, uid, msg)
diff --git a/lib/VNDB/DB/Producers.pm b/lib/VNDB/DB/Producers.pm
index ad6f22b2..7d4b6897 100644
--- a/lib/VNDB/DB/Producers.pm
+++ b/lib/VNDB/DB/Producers.pm
@@ -5,7 +5,7 @@ use strict;
use warnings;
use Exporter 'import';
-our @EXPORT = qw|dbProducerGet dbProducerRevisionInsert|;
+our @EXPORT = qw|dbProducerGet dbProducerRevisionInsert dbProducerNames|;
# options: results, page, id, search, char, rev
@@ -94,5 +94,16 @@ sub dbProducerRevisionInsert {
}
+sub dbProducerNames {
+ my($self, @ids) = @_;
+ return $self->dbAll(q|
+ SELECT p.id, pr.name
+ FROM producers p
+ JOIN producers_rev pr ON pr.id = p.latest
+ WHERE p.id IN (!l)|, \@ids
+ );
+}
+
+
1;
diff --git a/lib/VNDB/DB/VN.pm b/lib/VNDB/DB/VN.pm
index 090f9ecb..ecf49f96 100644
--- a/lib/VNDB/DB/VN.pm
+++ b/lib/VNDB/DB/VN.pm
@@ -7,7 +7,7 @@ use Exporter 'import';
use VNDB::Func 'gtintype', 'normalize_query';
use Encode 'decode_utf8';
-our @EXPORT = qw|dbVNGet dbVNRevisionInsert dbVNImageId dbScreenshotAdd dbScreenshotGet dbScreenshotRandom dbVNHasChar|;
+our @EXPORT = qw|dbVNGet dbVNRevisionInsert dbVNImageId dbScreenshotAdd dbScreenshotGet dbScreenshotRandom dbVNHasChar dbVNTitles|;
# Options: id, rev, char, search, length, lang, olang, plat, tag_inc, tag_exc, tagspoil,
@@ -290,5 +290,16 @@ sub dbVNHasChar {
)->{exists};
}
+
+sub dbVNTitles {
+ my ($self, @vids) = @_;
+ return $self->dbAll(q|
+ SELECT v.id, vr.title
+ FROM vn v
+ JOIN vn_rev vr ON vr.id = v.latest
+ WHERE v.id IN (!l)|, \@vids
+ );
+}
+
1;
diff --git a/lib/VNDB/Util/Misc.pm b/lib/VNDB/Util/Misc.pm
index 48bc226d..a3a156d3 100644
--- a/lib/VNDB/Util/Misc.pm
+++ b/lib/VNDB/Util/Misc.pm
@@ -7,7 +7,7 @@ use Exporter 'import';
use TUWF ':html';
use VNDB::Func;
-our @EXPORT = qw|filFetchDB ieCheck|;
+our @EXPORT = qw|filFetchDB ieCheck bbSubstLinks|;
my %filfields = (
@@ -143,5 +143,74 @@ sub ieCheck {
return 0;
}
+
+sub bbSubstLinks {
+ my ($self, $msg) = @_;
+
+ # pre-parse vndb links within message body
+ my (%lookup, %links);
+ while ($msg =~ m/(?:^|\s)\K([vcpgi])([1-9][0-9]*)(?=$|\b[^.])/g) {
+ $lookup{$1}{$2} = 1;
+ }
+ return $msg unless %lookup;
+ # lookup parsed links
+ if ($lookup{v}) {
+ $links{"v$_->{id}"} = $_->{title} for (@{$self->dbVNTitles(keys %{$lookup{v}})});
+ }
+ if ($lookup{c}) {
+ $links{"c$_->{id}"} = $_->{name} for (@{$self->dbCharNames(keys %{$lookup{c}})});
+ }
+ if ($lookup{p}) {
+ $links{"p$_->{id}"} = $_->{name} for (@{$self->dbProducerNames(keys %{$lookup{p}})});
+ }
+ if ($lookup{g}) {
+ $links{"g$_->{id}"} = $_->{name} for (@{$self->dbTagGet(id => [keys %{$lookup{g}}])});
+ }
+ if ($lookup{i}) {
+ $links{"i$_->{id}"} = $_->{name} for (@{$self->dbTraitGet(id => [keys %{$lookup{i}}])});
+ }
+ return $msg unless %links;
+ my($result, @open) = ('', 'first');
+
+ while($msg =~ m{
+ (\b[tdvprcugi][1-9][0-9]*(?=$|\b[^.])) | # 1. id
+ (\[[^\s\]]+\]) | # 2. tag
+ ((?:https?|ftp)://[^><"\n\s\]\[]+[\d\w=/-]) # 3. url
+ }x) {
+ my($match, $id, $tag) = ($&, $1, $2);
+ $result .= $`;
+ $msg = $';
+
+ if($open[$#open] ne 'raw' && $open[$#open] ne 'code') {
+ # handle tags
+ if($tag) {
+ $tag = lc $tag;
+ if($tag eq '[raw]') {
+ push @open, 'raw';
+ } elsif($tag eq '[quote]') {
+ push @open, 'quote';
+ } elsif($tag eq '[code]') {
+ push @open, 'code';
+ } elsif($tag eq '[/quote]' && $open[$#open] eq 'quote') {
+ pop @open;
+ } elsif($match =~ m{\[url=((https?://|/)[^\]>]+)\]}i) {
+ push @open, 'url';
+ } elsif($tag eq '[/url]' && $open[$#open] eq 'url') {
+ pop @open;
+ }
+ } elsif($id && !grep(/^(?:quote|url)/, @open) && $links{$match}) {
+ $match = sprintf '[url=/%s]%s[/url]', $match, $links{$match};
+ }
+ }
+ pop @open if($tag && $open[$#open] eq 'raw' && lc$tag eq '[/raw]');
+ pop @open if($tag && $open[$#open] eq 'code' && lc$tag eq '[/code]');
+
+ $result .= $match;
+ }
+ $result .= $msg;
+
+ return $result;
+}
+
1;
diff --git a/lib/VNDBUtil.pm b/lib/VNDBUtil.pm
index c864fda9..e81971a6 100644
--- a/lib/VNDBUtil.pm
+++ b/lib/VNDBUtil.pm
@@ -116,7 +116,7 @@ sub bb2html {
next;
}
# id
- if(($id || $exid || $longid) && (!$result || substr($raw, $last-1-length($match), 1) !~ /[\w]/) && substr($raw, $last, 1) !~ /[\w]/) {
+ if(($id || $exid || $longid) && !grep(/url/, @open) && (!$result || substr($raw, $last-1-length($match), 1) !~ /[\w]/) && substr($raw, $last, 1) !~ /[\w]/) {
(my $lnk = $match) =~ s/^d(\d+)\.(\d+)\.(\d+)$/d$1#$2.$3/;
$length += length $lnk;
last if $maxlength && $length > $maxlength;