Skip to content

Commit

Permalink
tags.pl: Use real lists instead of strings
Browse files Browse the repository at this point in the history
The strings used to be concatenated elements using $FS as the
separator. Now they are references to lists.
  • Loading branch information
kensanata committed Jan 8, 2015
1 parent 7f3488b commit 6635803
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
31 changes: 15 additions & 16 deletions modules/tags.pl
Original file line number Diff line number Diff line change
Expand Up @@ -136,25 +136,25 @@ sub NewTagSave { # called within a lock!
# For each tag we list the files tagged. Add the current file for
# all those tags where it is missing.
foreach my $tag (keys %tag) {
my %file = map {$_=>1} split(/$FS/, $h{$tag});
my %file = map {$_=>1} @{$h{$tag}};
if (not $file{$id}) {
$file{$id} = 1;
$h{$tag} = join($FS, keys %file);
$h{$tag} = [keys %file];
}
}

# For each file in our hash, we have a reverse lookup of all the
# tags used. This allows us to delete the references that no longer
# show up without looping through them all. The files are indexed
# with a starting underscore because this is an illegal tag name.
foreach my $tag (split (/$FS/, $h{"_$id"})) {
foreach my $tag (@{$h{"_$id"}}) {
# If the tag we're looking at is no longer listed, we have work to
# do.
if (!$tag{$tag}) {
my %file = map {$_=>1} split(/$FS/, $h{$tag});
my %file = map {$_=>1} @{$h{$tag}};
delete $file{$id};
if (%file) {
$h{$tag} = join($FS, keys %file);
$h{$tag} = [keys %file];
} else {
delete $h{$tag};
}
Expand All @@ -164,7 +164,7 @@ sub NewTagSave { # called within a lock!
# Store the new reverse lookup of all the tags used on the current
# page. If no more tags appear on this page, delete the entry.
if (%tag) {
$h{"_$id"} = join($FS, keys %tag);
$h{"_$id"} = [keys %tag];
} else {
delete $h{"_$id"};
}
Expand All @@ -191,11 +191,11 @@ sub NewTagDeletePage { # called within a lock!
# For each file in our hash, we have a reverse lookup of all the
# tags used. This allows us to delete the references that no longer
# show up without looping through them all.
foreach my $tag (split (/$FS/, $h{"_$id"})) {
my %file = map {$_=>1} split(/$FS/, $h{$tag});
foreach my $tag (@{$h{"_$id"}}) {
my %file = map {$_=>1} @{$h{$tag}};
delete $file{$id};
if (%file) {
$h{$tag} = join($FS, keys %file);
$h{$tag} = [keys %file];
} else {
delete $h{$tag};
}
Expand Down Expand Up @@ -224,7 +224,7 @@ sub TagFind {
my %h = TagReadHash();
my %page;
foreach my $tag (@tags) {
foreach my $id (split(/$FS/, $h{lc($tag)})) {
foreach my $id (@{$h{lc($tag)}}) {
$page{$id} = 1;
}
}
Expand Down Expand Up @@ -298,7 +298,7 @@ sub TagCloud {
my $min = 0;
my %count = ();
foreach my $tag (grep !/^_/, keys %h) {
$count{$tag} = split(/$FS/, $h{$tag});
$count{$tag} = @{$h{$tag}};
$max = $count{$tag} if $count{$tag} > $max;
$min = $count{$tag} if not $min or $count{$tag} < $min;
}
Expand Down Expand Up @@ -359,13 +359,12 @@ sub DoTagsReindex {
# For each tag we list the files tagged. Add the current file for
# all tags.
foreach my $tag (keys %tag) {
$h{$tag} .= $FS if $h{$tag};
$h{$tag} .= $id;
push(@{$h{$tag}}, $id);
}

# Store the reverse lookup of all the tags used on the current
# page.
$h{"_$id"} = join($FS, keys %tag);
$h{"_$id"} = [keys %tag];
}

Storable::store(\%h, $TagFile) or print "Error saving tag file.\n";
Expand All @@ -389,8 +388,8 @@ sub TagList {
print GetHttpHeader('text/plain');
# open the DB file
my %h = TagReadHash();
foreach my $id (sort map { $_ } keys %h) {
print "$id: " . join(', ', split(/$FS/, $h{$id})) . "\n";
foreach my $id (sort keys %h) {
print "$id: " . join(', ', @{$h{$id}}) . "\n";
}
TagWriteHash(\%h);
}
Expand Down
14 changes: 7 additions & 7 deletions t/tags.t
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ update_page('Alex', 'Me! [[tag:Old School]]');
# open the DB file
my %h = TagReadHash();

%tag = map {$_=>1} split($FS, $h{"_Brilliant"});
%tag = map {$_=>1} @{$h{"_Brilliant"}};
ok($tag{podcast}, 'Brilliant page tagged podcast');
ok($tag{mag}, 'Brilliant page tagged mag');
%tag = map {$_=>1} split($FS, $h{"_Pödgecäst´s"});
%tag = map {$_=>1} @{$h{"_Pödgecäst´s"}};
ok($tag{podcast}, 'Pödgecäst´s page tagged podcast');
%file = map {$_=>1} split($FS, $h{"podcast"});
%file = map {$_=>1} @{$h{"podcast"}};
ok($file{Brilliant}, 'Tag podcast applies to page Brilliant');
ok($file{"Pödgecäst´s"}, 'Tag podcast applies to page Pödgecäst´s');
%file = map {$_=>1} split($FS, $h{"mag"});
%file = map {$_=>1} @{$h{"mag"}};
ok($file{Brilliant}, 'Tag mag applies to page Brilliant');
%file = map {$_=>1} split($FS, $h{"old_school"});
%file = map {$_=>1} @{$h{"old_school"}};
ok($file{Alex}, 'Tag Old School applies to page Alex');

# close the DB file before making changes via the wiki!
Expand All @@ -72,10 +72,10 @@ update_page('Brilliant', 'Gameologists [[tag:mag]]');
# reopen changed file
%h = TagReadHash();

%tag = map {$_=>1} split($FS, $h{"_Brilliant"});
%tag = map {$_=>1} @{$h{"_Brilliant"}};
ok(!$tag{podcast}, 'Brilliant page no longer tagged podcast');
ok($tag{mag}, 'Brilliant page still tagged mag');
%file = map {$_=>1} split($FS, $h{"podcast"});
%file = map {$_=>1} @{$h{"podcast"}};
ok(!$file{Brilliant}, 'Tag podcast no longer applies to page Brilliant');
ok($file{"Pödgecäst´s"}, 'Tag podcast still applies to page Pödgecäst´s');

Expand Down

0 comments on commit 6635803

Please sign in to comment.