[dw-free] interests / keywords cleanup
[commit: http://hg.dwscoalition.org/dw-free/rev/cc157cc361f6]
http://bugs.dwscoalition.org/show_bug.cgi?id=1776
Move LJ::get_interests() to LJ::User->get_interests method.
Patch by
kareila.
Files modified:
http://bugs.dwscoalition.org/show_bug.cgi?id=1776
Move LJ::get_interests() to LJ::User->get_interests method.
Patch by
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Files modified:
- cgi-bin/DW/Logic/ProfilePage.pm
- cgi-bin/LJ/User.pm
- cgi-bin/ljfeed.pl
- htdocs/interests.bml
- htdocs/manage/profile/index.bml
- htdocs/misc/interestdata.bml
-------------------------------------------------------------------------------- diff -r 490a701da881 -r cc157cc361f6 cgi-bin/DW/Logic/ProfilePage.pm --- a/cgi-bin/DW/Logic/ProfilePage.pm Mon Oct 26 02:28:07 2009 +0000 +++ b/cgi-bin/DW/Logic/ProfilePage.pm Mon Oct 26 02:32:34 2009 +0000 @@ -687,7 +687,7 @@ sub interests { my $remote = $self->{remote}; my @ret; - my $ints = LJ::get_interests( $u ); # arrayref of: [ intid, intname, intcount ] + my $ints = $u->get_interests(); # arrayref of: [ intid, intname, intcount ] if ( @$ints ) { foreach my $int ( @$ints ) { my $intid = $int->[0]; @@ -699,7 +699,7 @@ sub interests { if ( $intcount > 1 ) { # bold shared interests on all profiles except your own if ( $remote && !$remote->equals( $u ) ) { - my %remote_intids = map { $_ => 1 } @{ LJ::get_interests( $remote, { justids => 1 } ) }; + my %remote_intids = map { $_ => 1 } @{ $remote->get_interests( { justids => 1 } ) }; $intname = "<strong>$intname</strong>" if $remote_intids{$intid}; } push @ret, { url => "$LJ::SITEROOT/interests?int=$eint", text => $intname }; diff -r 490a701da881 -r cc157cc361f6 cgi-bin/LJ/User.pm --- a/cgi-bin/LJ/User.pm Mon Oct 26 02:28:07 2009 +0000 +++ b/cgi-bin/LJ/User.pm Mon Oct 26 02:32:34 2009 +0000 @@ -4059,25 +4059,104 @@ sub third_party_notify_list_remove { ######################################################################## ### 17. Interest-Related Functions +# $opts is optional, with keys: +# forceids => 1 : don't use memcache for loading the intids +# forceints => 1 : don't use memcache for loading the interest rows +# justids => 1 : return arrayref of intids only, not names/counts +# returns otherwise an arrayref of interest rows, sorted by interest name +sub get_interests { + my ( $u, $opts ) = @_; + $opts ||= {}; + return undef unless LJ::isu( $u ); + + # first check request cache inside $u + if ( my $ints = $u->{_cache_interests} ) { + return [ map { $_->[0] } @$ints ] if $opts->{justids}; + return $ints; + } + + my $uid = $u->userid; + my $uitable = $u->is_community ? 'comminterests' : 'userinterests'; + + # load the ids + my $mk_ids = [$uid, "intids:$uid"]; + my $ids = LJ::MemCache::get($mk_ids) unless $opts->{forceids}; + unless ( $ids && ref $ids eq "ARRAY" ) { + $ids = []; + my $dbh = LJ::get_db_writer(); + my $sth = $dbh->prepare( "SELECT intid FROM $uitable WHERE userid=?" ); + $sth->execute( $uid ); + push @$ids, $_ while ($_) = $sth->fetchrow_array; + LJ::MemCache::add( $mk_ids, $ids, 3600*12 ); + } + + # FIXME: set a 'justids' $u cache key in this case, then only return that + # later if 'justids' is requested? probably not worth it. + return $ids if $opts->{justids}; + + # load interest rows + my %need; + $need{$_} = 1 foreach @$ids; + my @ret; + + unless ( $opts->{forceints} ) { + if ( my $mc = LJ::MemCache::get_multi( map { [$_, "introw:$_"] } @$ids ) ) { + while ( my ($k, $v) = each %$mc ) { + next unless $k =~ /^introw:(\d+)/; + delete $need{$1}; + push @ret, $v; + } + } + } + + if ( %need ) { + my $ids = join( ",", map { $_ + 0 } keys %need ); + my $dbr = LJ::get_db_reader(); + my $sth = $dbr->prepare( "SELECT intid, interest, intcount FROM interests ". + "WHERE intid IN ($ids)" ); + $sth->execute; + my $memc_store = 0; + while ( my ($intid, $int, $count) = $sth->fetchrow_array ) { + # minimize latency... only store 25 into memcache at a time + # (too bad we don't have set_multi.... hmmmm) + my $aref = [$intid, $int, $count]; + if ( $memc_store++ < 25 ) { + # if the count is fairly high, keep item in memcache longer, + # since count's not so important. + my $expire = $count < 10 ? 3600*12 : 3600*48; + LJ::MemCache::add( [$intid, "introw:$intid"], $aref, $expire ); + } + push @ret, $aref; + } + } + + @ret = sort { $a->[1] cmp $b->[1] } @ret; + return $u->{_cache_interests} = \@ret; +} + + sub interest_count { my $u = shift; + return undef unless LJ::isu( $u ); # FIXME: fall back to SELECT COUNT(*) if not cached already? - return scalar @{LJ::get_interests($u, { justids => 1 })}; + return scalar @{ $u->get_interests( { justids => 1 } ) }; } sub interest_list { my $u = shift; - - return map { $_->[1] } @{ LJ::get_interests($u) }; + return undef unless LJ::isu( $u ); + + return map { $_->[1] } @{ $u->get_interests() }; } # return hashref with intname => intid sub interests { my $u = shift; - my $uints = LJ::get_interests($u); + return undef unless LJ::isu( $u ); + my $uints = $u->get_interests(); my %interests; foreach my $int (@$uints) { @@ -7576,86 +7655,6 @@ sub can_view ######################################################################## ### 17. Interest-Related Functions -# $opts is optional, with keys: -# forceids => 1 : don't use memcache for loading the intids -# forceints => 1 : don't use memcache for loading the interest rows -# justids => 1 : return arrayref of intids only, not names/counts -# returns otherwise an arrayref of interest rows, sorted by interest name -sub get_interests -{ - my ($u, $opts) = @_; - $opts ||= {}; - return undef unless $u; - - # first check request cache inside $u - if (my $ints = $u->{_cache_interests}) { - if ($opts->{justids}) { - return [ map { $_->[0] } @$ints ]; - } - return $ints; - } - - my $uid = $u->userid; - my $uitable = $u->is_community ? 'comminterests' : 'userinterests'; - - # load the ids - my $ids; - my $mk_ids = [$uid, "intids:$uid"]; - $ids = LJ::MemCache::get($mk_ids) unless $opts->{'forceids'}; - unless ($ids && ref $ids eq "ARRAY") { - $ids = []; - my $dbh = LJ::get_db_writer(); - my $sth = $dbh->prepare("SELECT intid FROM $uitable WHERE userid=?"); - $sth->execute($uid); - push @$ids, $_ while ($_) = $sth->fetchrow_array; - LJ::MemCache::add($mk_ids, $ids, 3600*12); - } - - # FIXME: set a 'justids' $u cache key in this case, then only return that - # later if 'justids' is requested? probably not worth it. - return $ids if $opts->{'justids'}; - - # load interest rows - my %need; - $need{$_} = 1 foreach @$ids; - my @ret; - - unless ($opts->{'forceints'}) { - if (my $mc = LJ::MemCache::get_multi(map { [$_, "introw:$_"] } @$ids)) { - while (my ($k, $v) = each %$mc) { - next unless $k =~ /^introw:(\d+)/; - delete $need{$1}; - push @ret, $v; - } - } - } - - if (%need) { - my $ids = join(",", map { $_+0 } keys %need); - my $dbr = LJ::get_db_reader(); - my $sth = $dbr->prepare("SELECT intid, interest, intcount FROM interests ". - "WHERE intid IN ($ids)"); - $sth->execute; - my $memc_store = 0; - while (my ($intid, $int, $count) = $sth->fetchrow_array) { - # minimize latency... only store 25 into memcache at a time - # (too bad we don't have set_multi.... hmmmm) - my $aref = [$intid, $int, $count]; - if ($memc_store++ < 25) { - # if the count is fairly high, keep item in memcache longer, - # since count's not so important. - my $expire = $count < 10 ? 3600*12 : 3600*48; - LJ::MemCache::add([$intid, "introw:$intid"], $aref, $expire); - } - push @ret, $aref; - } - } - - @ret = sort { $a->[1] cmp $b->[1] } @ret; - return $u->{_cache_interests} = \@ret; -} - - sub interest_string_to_list { my $intstr = shift; diff -r 490a701da881 -r cc157cc361f6 cgi-bin/ljfeed.pl --- a/cgi-bin/ljfeed.pl Mon Oct 26 02:28:07 2009 +0000 +++ b/cgi-bin/ljfeed.pl Mon Oct 26 02:32:34 2009 +0000 @@ -764,7 +764,7 @@ sub create_view_foaf { # interests, please! # arrayref of interests rows: [ intid, intname, intcount ] - my $intu = LJ::get_interests($u); + my $intu = $u->get_interests(); foreach my $int (@$intu) { LJ::text_out(\$int->[1]); # 1==interest $ret .= " <foaf:interest dc:title=\"". LJ::exml($int->[1]) . "\" " . diff -r 490a701da881 -r cc157cc361f6 htdocs/interests.bml --- a/htdocs/interests.bml Mon Oct 26 02:28:07 2009 +0000 +++ b/htdocs/interests.bml Mon Oct 26 02:32:34 2009 +0000 @@ -99,7 +99,7 @@ body<= return $ret; } - my $rints = LJ::get_interests($remote); + my $rints = $remote->get_interests(); my $count = scalar(@$rints); # account for the case where we bypass the POST due to the referer @@ -220,7 +220,7 @@ body<= my %uint; my %fromint; - my $fints = LJ::get_interests($fromu); + my $fints = $fromu->get_interests(); foreach (@$fints) { $fromint{$_->[1]} = $_->[0]+0; } @@ -239,7 +239,7 @@ body<= } else { my $in = join (",", map { $fromint{$_} } keys %fromint); - my $uints = LJ::get_interests($u); + my $uints = $u->get_interests(); foreach (@$uints) { $uint{$_->[1]} = $_->[0]; } @@ -301,7 +301,7 @@ body<= my %uint; my $intcount = 0; - my $uints = LJ::get_interests($u); + my $uints = $u->get_interests(); foreach (@$uints) { $uint{$_->[0]} = $_->[1]; # uint{intid} = interest $intcount++; diff -r 490a701da881 -r cc157cc361f6 htdocs/manage/profile/index.bml --- a/htdocs/manage/profile/index.bml Mon Oct 26 02:28:07 2009 +0000 +++ b/htdocs/manage/profile/index.bml Mon Oct 26 02:32:34 2009 +0000 @@ -65,7 +65,7 @@ body<= LJ::text_out(\$u->{'bio'}, "force"); # load interests - my $uints = LJ::get_interests($u, { forceids => 1 }); + my $uints = $u->get_interests( { forceids => 1 } ); my %interests = (); foreach (@$uints) { $interests{$_->[1]} = $_->[0]; # $interests{name} = intid diff -r 490a701da881 -r cc157cc361f6 htdocs/misc/interestdata.bml --- a/htdocs/misc/interestdata.bml Mon Oct 26 02:28:07 2009 +0000 +++ b/htdocs/misc/interestdata.bml Mon Oct 26 02:32:34 2009 +0000 @@ -13,7 +13,7 @@ my $ret; - my $ints = LJ::get_interests($u) || []; + my $ints = $u->get_interests() || []; foreach my $int (@$ints) { $ret .= "$int->[0] $int->[2] $int->[1]\n"; } --------------------------------------------------------------------------------