[dw-free] interests / keywords cleanup
[commit: http://hg.dwscoalition.org/dw-free/rev/f1133de2d63a]
http://bugs.dwscoalition.org/show_bug.cgi?id=1776
Move LJ::get_keyword_id to a LJ::User method.
Patch by
kareila.
Files modified:
http://bugs.dwscoalition.org/show_bug.cgi?id=1776
Move LJ::get_keyword_id to a LJ::User method.
Patch by
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Files modified:
- cgi-bin/LJ/User.pm
- cgi-bin/LJ/Userpic.pm
- cgi-bin/ljlib.pl
- cgi-bin/ljmemories.pl
- cgi-bin/taglib.pl
- htdocs/tools/memadd.bml
-------------------------------------------------------------------------------- diff -r 4d8bd333d94e -r f1133de2d63a cgi-bin/LJ/User.pm --- a/cgi-bin/LJ/User.pm Sun Sep 20 09:15:19 2009 +0000 +++ b/cgi-bin/LJ/User.pm Sun Sep 20 18:24:59 2009 +0000 @@ -4624,6 +4624,57 @@ sub can_add_tags_to { } +# <LJFUNC> +# name: LJ::User::get_keyword_id +# class: +# des: Get the id for a keyword. +# args: uuid, keyword, autovivify? +# des-uuid: User object or userid to use. +# des-keyword: A string keyword to get the id of. +# returns: Returns a kwid into [dbtable[userkeywords]]. +# If the keyword doesn't exist, it is automatically created for you. +# des-autovivify: If present and 1, automatically create keyword. +# If present and 0, do not automatically create the keyword. +# If not present, default behavior is the old +# style -- yes, do automatically create the keyword. +# </LJFUNC> +sub get_keyword_id +{ + my ( $u, $kw, $autovivify ) = @_; + $u = LJ::want_user( $u ); + return undef unless $u; + $autovivify = 1 unless defined $autovivify; + + # setup the keyword for use + return 0 unless $kw =~ /\S/; + $kw = LJ::text_trim( $kw, LJ::BMAX_KEYWORD, LJ::CMAX_KEYWORD ); + + # get the keyword and insert it if necessary + my $kwid = $u->selectrow_array( 'SELECT kwid FROM userkeywords WHERE userid = ? AND keyword = ?', + undef, $u->userid, $kw ) + 0; + if ( $autovivify && ! $kwid ) { + # create a new keyword + $kwid = LJ::alloc_user_counter( $u, 'K' ); + return undef unless $kwid; + + # attempt to insert the keyword + my $rv = $u->do( "INSERT IGNORE INTO userkeywords (userid, kwid, keyword) VALUES (?, ?, ?)", + undef, $u->userid, $kwid, $kw ) + 0; + return undef if $u->err; + + # at this point, if $rv is 0, the keyword is already there so try again + unless ( $rv ) { + $kwid = $u->selectrow_array( 'SELECT kwid FROM userkeywords WHERE userid = ? AND keyword = ?', + undef, $u->userid, $kw ) + 0; + } + + # nuke cache + $u->memc_delete( 'kws' ); + } + return $kwid; +} + + sub tags { my $u = shift; diff -r 4d8bd333d94e -r f1133de2d63a cgi-bin/LJ/Userpic.pm --- a/cgi-bin/LJ/Userpic.pm Sun Sep 20 09:15:19 2009 +0000 +++ b/cgi-bin/LJ/Userpic.pm Sun Sep 20 18:24:59 2009 +0000 @@ -879,7 +879,7 @@ sub set_keywords { my $picid = $self->{picid}; foreach my $kw (@keywords) { - my $kwid = LJ::get_keyword_id( $u, $kw ); + my $kwid = $u->get_keyword_id( $kw ); next unless $kwid; # TODO: fire some warning that keyword was bogus if (++$c > $LJ::MAX_USERPIC_KEYWORDS) { diff -r 4d8bd333d94e -r f1133de2d63a cgi-bin/ljlib.pl --- a/cgi-bin/ljlib.pl Sun Sep 20 09:15:19 2009 +0000 +++ b/cgi-bin/ljlib.pl Sun Sep 20 18:24:59 2009 +0000 @@ -1501,66 +1501,6 @@ sub cmd_buffer_add } -# <LJFUNC> -# name: LJ::get_keyword_id -# class: -# des: Get the id for a keyword. -# args: uuid?, keyword, autovivify? -# des-uuid: User object or userid to use. -# des-keyword: A string keyword to get the id of. -# returns: Returns a kwid into [dbtable[userkeywords]]. -# If the keyword doesn't exist, it is automatically created for you. -# des-autovivify: If present and 1, automatically create keyword. -# If present and 0, do not automatically create the keyword. -# If not present, default behavior is the old -# style -- yes, do automatically create the keyword. -# </LJFUNC> -sub get_keyword_id -{ - &nodb; - - # see if we got a user? if so we use userkeywords on a cluster - my $u; - if (@_ >= 2) { - $u = LJ::want_user(shift); - return undef unless $u; - } - - my ($kw, $autovivify) = @_; - $autovivify = 1 unless defined $autovivify; - - # setup the keyword for use - unless ($kw =~ /\S/) { return 0; } - $kw = LJ::text_trim($kw, LJ::BMAX_KEYWORD, LJ::CMAX_KEYWORD); - - # get the keyword and insert it if necessary - my $kwid; - if ( $u ) { - $kwid = $u->selectrow_array('SELECT kwid FROM userkeywords WHERE userid = ? AND keyword = ?', - undef, $u->{userid}, $kw) + 0; - if ($autovivify && ! $kwid) { - # create a new keyword - $kwid = LJ::alloc_user_counter($u, 'K'); - return undef unless $kwid; - - # attempt to insert the keyword - my $rv = $u->do("INSERT IGNORE INTO userkeywords (userid, kwid, keyword) VALUES (?, ?, ?)", - undef, $u->{userid}, $kwid, $kw) + 0; - return undef if $u->err; - - # at this point, if $rv is 0, the keyword is already there so try again - unless ($rv) { - $kwid = $u->selectrow_array('SELECT kwid FROM userkeywords WHERE userid = ? AND keyword = ?', - undef, $u->{userid}, $kw) + 0; - } - - # nuke cache - LJ::MemCache::delete([ $u->{userid}, "kws:$u->{userid}" ]); - } - } - return $kwid; -} - sub get_interest { my $intid = shift or return undef; diff -r 4d8bd333d94e -r f1133de2d63a cgi-bin/ljmemories.pl --- a/cgi-bin/ljmemories.pl Sun Sep 20 09:15:19 2009 +0000 +++ b/cgi-bin/ljmemories.pl Sun Sep 20 18:24:59 2009 +0000 @@ -56,7 +56,7 @@ sub create { $journalid += 0; $ditemid += 0; $security ||= 'public'; - $kwids ||= [ LJ::get_keyword_id($u, '*') ]; # * means no category + $kwids ||= [ $u->get_keyword_id( '*' ) ]; # * means no category $des = LJ::trim($des); return undef unless $userid && $journalid && $ditemid && $des && $security && @$kwids; return undef unless $security =~ /^(?:public|friends|private)$/; diff -r 4d8bd333d94e -r f1133de2d63a cgi-bin/taglib.pl --- a/cgi-bin/taglib.pl Sun Sep 20 09:15:19 2009 +0000 +++ b/cgi-bin/taglib.pl Sun Sep 20 18:24:59 2009 +0000 @@ -730,7 +730,7 @@ sub update_logtags { # and turn everything into ids $opts->{"${verb}_ids"} ||= []; foreach my $kw (@{$opts->{$verb} || []}) { - my $kwid = LJ::get_keyword_id($u, $kw, $can_control); + my $kwid = $u->get_keyword_id( $kw, $can_control ); if ($can_control) { # error if we failed to create return undef unless $kwid; @@ -1065,7 +1065,7 @@ sub create_usertag { my %res; foreach my $tag (@$tags) { - my $kwid = LJ::get_keyword_id($u, $tag); + my $kwid = $u->get_keyword_id( $tag ); return undef unless $kwid; $res{$tag} = $kwid; @@ -1125,7 +1125,7 @@ sub delete_usertag { my $tag = LJ::Tags::validate_tag($val); return undef unless $tag; - $kwid = LJ::get_keyword_id($u, $tag, 0); + $kwid = $u->get_keyword_id( $tag, 0 ); } elsif ($type eq 'id') { $kwid = $val + 0; } @@ -1213,14 +1213,14 @@ sub rename_usertag { my $val = LJ::Tags::validate_tag($oldkw); return $err->( LJ::Lang::ml( 'taglib.error.invalid', { tagname => LJ::ehtml( $oldkw ) } ) ) unless $val; - $kwid = LJ::get_keyword_id($u, $val, 0); + $kwid = $u->get_keyword_id( $val, 0 ); } elsif ($type eq 'id') { $kwid = $oldkw + 0; } return $err->() unless $kwid; # see if this is already a keyword - my $newkwid = LJ::get_keyword_id($u, $newname); + my $newkwid = $u->get_keyword_id( $newname ); return undef unless $newkwid; # see if the tag we're renaming TO already exists as a keyword, @@ -1293,7 +1293,7 @@ sub set_usertag_display { return undef unless $var; # do not auto-vivify but get the keyword id - $kwid = LJ::get_keyword_id($u, $var, 0); + $kwid = $u->get_keyword_id( $var, 0 ); } return undef unless $kwid; diff -r 4d8bd333d94e -r f1133de2d63a htdocs/tools/memadd.bml --- a/htdocs/tools/memadd.bml Sun Sep 20 09:15:19 2009 +0000 +++ b/htdocs/tools/memadd.bml Sun Sep 20 18:24:59 2009 +0000 @@ -313,7 +313,7 @@ return; } - my $kwid = LJ::get_keyword_id($memoryu, $kw); + my $kwid = $memoryu->get_keyword_id( $kw ); $needflush = 1 unless defined $exist_kw->{$kwid}; push @kwid, $kwid; } --------------------------------------------------------------------------------