Code cleanup: use $u->bio, instead of LJ::get_bio or $u->{bio}, so we
only have one path through the code, and we're sure that we get updated data.
Also, clean up $u->set_bio and add $u->has_bio.
Patch by kareila.
diffs (202 lines):
diff -r f355fddd7005 -r 6698797cb8dd cgi-bin/LJ/User.pm
--- a/cgi-bin/LJ/User.pm Thu Jun 17 17:37:49 2010 +0800
+++ b/cgi-bin/LJ/User.pm Thu Jun 17 18:07:12 2010 +0800
@@ -2876,9 +2876,29 @@ sub ajax_auth_token {
}
+# gets a user bio, from DB or memcache.
+# optional argument: boolean, true to skip memcache and use cluster master.
sub bio {
- my $u = shift;
- return LJ::get_bio($u);
+ my ( $u, $force ) = @_;
+ return unless $u && $u->has_bio;
+
+ my $bio;
+
+ $bio = $u->memc_get( 'bio' ) unless $force;
+ return $bio if defined $bio;
+
+ # not in memcache, fall back to disk
+ my $db = @LJ::MEMCACHE_SERVERS || $force
+ ? LJ::get_cluster_def_reader( $u )
+ : LJ::get_cluster_reader( $u );
+ return unless $db;
+ $bio = $db->selectrow_array( "SELECT bio FROM userbio WHERE userid=?",
+ undef, $u->userid );
+
+ # set in memcache
+ LJ::MemCache::add( [$u->id, "bio:" . $u->id], $bio );
+
+ return $bio;
}
@@ -2917,6 +2937,11 @@ sub display_name {
sub equals {
return LJ::u_equals( @_ );
+}
+
+
+sub has_bio {
+ return $_[0]->{has_bio} eq "Y" ? 1 : 0;
}
@@ -3050,6 +3075,32 @@ sub new_from_url {
}
+# if bio_absent is set to "yes", bio won't be updated
+sub set_bio {
+ my ( $u, $text, $bio_absent ) = @_;
+ $bio_absent = "" unless $bio_absent;
+
+ my $oldbio = $u->bio;
+ my $newbio = $bio_absent eq "yes" ? $oldbio : $text;
+ my $has_bio = ( $newbio =~ /\S/ ) ? "Y" : "N";
+
+ LJ::update_user( $u, { has_bio => $has_bio } );
+
+ # update their bio text
+ return if ( $oldbio eq $text ) || ( $bio_absent eq "yes" );
+
+ if ( $has_bio eq "N" ) {
+ $u->do( "DELETE FROM userbio WHERE userid=?", undef, $u->id );
+ $u->dudata_set( 'B', 0, 0 );
+ } else {
+ $u->do( "REPLACE INTO userbio (userid, bio) VALUES (?, ?)",
+ undef, $u->id, $text );
+ $u->dudata_set( 'B', 0, length( $text ) );
+ }
+ $u->memc_set( 'bio', $text );
+}
+
+
sub url {
my $u = shift;
@@ -3077,35 +3128,6 @@ sub url {
*username = \&user;
sub user {
return $_[0]->{user};
-}
-
-
-# if bio_absent is set to "yes", bio won't be updated
-sub set_bio {
- my ($u, $text, $bio_absent) = @_;
- $bio_absent = "" unless $bio_absent;
-
- my $oldbio = $u->bio;
- my $newbio = $bio_absent eq "yes" ? $oldbio : $text;
- my $has_bio = ($newbio =~ /\S/) ? "Y" : "N";
-
- my %update = (
- 'has_bio' => $has_bio,
- );
- LJ::update_user($u, \%update);
-
- # update their bio text
- if (($oldbio ne $text) && $bio_absent ne "yes") {
- if ($has_bio eq "N") {
- $u->do("DELETE FROM userbio WHERE userid=?", undef, $u->id);
- $u->dudata_set('B', 0, 0);
- } else {
- $u->do("REPLACE INTO userbio (userid, bio) VALUES (?, ?)",
- undef, $u->id, $text);
- $u->dudata_set('B', 0, length($text));
- }
- LJ::MemCache::set([$u->id, "bio:" . $u->id], $text);
- }
}
@@ -7277,38 +7299,6 @@ sub ljuser
=cut
# <LJFUNC>
-# name: LJ::get_bio
-# des: gets a user bio, from DB or memcache.
-# args: u, force
-# des-force: true to get data from cluster master.
-# returns: string
-# </LJFUNC>
-sub get_bio {
- my ($u, $force) = @_;
- return unless $u && $u->{'has_bio'} eq "Y";
-
- my $bio;
-
- my $memkey = [$u->userid, "bio:" . $u->userid];
- unless ($force) {
- my $bio = LJ::MemCache::get($memkey);
- return $bio if defined $bio;
- }
-
- # not in memcache, fall back to disk
- my $db = @LJ::MEMCACHE_SERVERS || $force ?
- LJ::get_cluster_def_reader($u) : LJ::get_cluster_reader($u);
- $bio = $db->selectrow_array( "SELECT bio FROM userbio WHERE userid=?",
- undef, $u->userid );
-
- # set in memcache
- LJ::MemCache::add($memkey, $bio);
-
- return $bio;
-}
-
-
-# <LJFUNC>
# name: LJ::modify_caps
# des: Given a list of caps to add and caps to remove, updates a user's caps.
# args: uuid, cap_add, cap_del, res
diff -r f355fddd7005 -r 6698797cb8dd cgi-bin/ljfeed.pl
--- a/cgi-bin/ljfeed.pl Thu Jun 17 17:37:49 2010 +0800
+++ b/cgi-bin/ljfeed.pl Thu Jun 17 18:07:12 2010 +0800
@@ -732,11 +732,11 @@ sub create_view_foaf {
}
# user bio
- if ($u->{'has_bio'} eq "Y") {
- $u->{'bio'} = LJ::get_bio($u);
- LJ::text_out(\$u->{'bio'});
- LJ::CleanHTML::clean_userbio(\$u->{'bio'});
- $ret .= " <ya:bio>" . LJ::exml($u->{'bio'}) . "</ya:bio>\n";
+ if ( $u->has_bio ) {
+ my $bio = $u->bio;
+ LJ::text_out( \$bio );
+ LJ::CleanHTML::clean_userbio( \$bio );
+ $ret .= " <ya:bio>" . LJ::exml( $bio ) . "</ya:bio>\n";
}
# icbm/location info
diff -r f355fddd7005 -r 6698797cb8dd htdocs/manage/profile/index.bml
--- a/htdocs/manage/profile/index.bml Thu Jun 17 17:37:49 2010 +0800
+++ b/htdocs/manage/profile/index.bml Thu Jun 17 18:07:12 2010 +0800
@@ -75,10 +75,10 @@ body<=
foreach (values %$u) { LJ::text_out(\$_); }
# load and clean bio
- $u->{'bio'} = LJ::get_bio($u);
- $saved{'bio'} = $u->{'bio'};
- LJ::EmbedModule->parse_module_embed($u, \$u->{'bio'}, edit => 1);
- LJ::text_out(\$u->{'bio'}, "force");
+ my $bio = $u->bio;
+ $saved{bio} = $bio;
+ LJ::EmbedModule->parse_module_embed( $u, \$bio, edit => 1 );
+ LJ::text_out( \$bio, "force" );
# load interests: $interests{name} = intid
my %interests = %{ $u->interests( { forceids => 1 } ) };
@@ -466,7 +466,7 @@ body<=
rows => '10', cols => '50',
wrap => 'soft',
style => "width: 90%",
- value => $u->{bio} } );
+ value => $bio } );
} else {
$ret .= LJ::html_hidden('bio_absent', 'yes');
$ret .= "<?p <?inerr " . BML::ml('.error.invalidbio', {'aopts' =>
"href='$LJ::SITEROOT/utf8convert'"}) . " inerr?> p?>\n";
_______________________________________________
dw-changelog mailing list
[email protected]
http://lists.dwscoalition.org/mailman/listinfo/dw-changelog