fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2010-09-07 08:47 am

[dw-free] cleaning up userpics code

[commit: http://hg.dwscoalition.org/dw-free/rev/ded9aadc4199]

http://bugs.dwscoalition.org/show_bug.cgi?id=513

Modernize/refactor userpics code. Use object methods rather than class
methods.

Patch by [personal profile] kareila.

Files modified:
  • cgi-bin/LJ/Console/Command/ExpungeUserpic.pm
  • cgi-bin/LJ/User.pm
  • cgi-bin/ljuserpics.pl
--------------------------------------------------------------------------------
diff -r 094089044eb8 -r ded9aadc4199 cgi-bin/LJ/Console/Command/ExpungeUserpic.pm
--- a/cgi-bin/LJ/Console/Command/ExpungeUserpic.pm	Tue Sep 07 16:41:24 2010 +0800
+++ b/cgi-bin/LJ/Console/Command/ExpungeUserpic.pm	Tue Sep 07 16:47:28 2010 +0800
@@ -48,8 +48,7 @@ sub execute {
     return $self->error("Invalid userpic URL.")
         unless $u;
 
-    # the actual expunging happens in ljlib
-    my ($rval, @hookval) = LJ::expunge_userpic($u, $picid);
+    my ( $rval, @hookval ) = $u->expunge_userpic( $picid );
 
     return $self->error("Error expunging userpic.") unless $rval;
 
diff -r 094089044eb8 -r ded9aadc4199 cgi-bin/LJ/User.pm
--- a/cgi-bin/LJ/User.pm	Tue Sep 07 16:41:24 2010 +0800
+++ b/cgi-bin/LJ/User.pm	Tue Sep 07 16:47:28 2010 +0800
@@ -6181,6 +6181,39 @@ sub clear_userpic_kw_map {
     $self->{picid_kw_map} = undef;
 }
 
+# Expunges a userpic so that the system will no longer deliver this userpic.
+# If your site has off-site caching or something similar, you can also define
+# a hook "expunge_userpic" which will be called with a picid and userid when
+# a pic is expunged.
+sub expunge_userpic {
+    my ( $u, $picid ) = @_;
+    $picid += 0;
+    return undef unless $picid && LJ::isu( $u );
+
+    # get the pic information
+    my $state;
+
+    my $dbcm = LJ::get_cluster_master( $u );
+    return undef unless $dbcm && $u->writer;
+
+    $state = $dbcm->selectrow_array( 'SELECT state FROM userpic2 WHERE userid = ? AND picid = ?',
+                                     undef, $u->userid, $picid );
+    return undef unless $state; # invalid pic
+    return $u->userid if $state eq 'X'; # already expunged
+
+    # else now mark it
+    $u->do( "UPDATE userpic2 SET state='X' WHERE userid = ? AND picid = ?", undef, $u->userid, $picid );
+    return LJ::error( $dbcm ) if $dbcm->err;
+    $u->do( "DELETE FROM userpicmap2 WHERE userid = ? AND picid = ?", undef, $u->userid, $picid );
+
+    # now clear the user's memcache picture info
+    LJ::Userpic->delete_cache( $u );
+
+    # call the hook and get out of here
+    my @rval = LJ::Hooks::run_hooks( 'expunge_userpic', $picid, $u->userid );
+    return ( $u->userid, map {$_->[0]} grep {$_ && @$_ && $_->[0]} @rval );
+}
+
 sub get_userpic_count {
     my $u = shift or return undef;
     my $count = scalar LJ::Userpic->load_user_userpics($u);
diff -r 094089044eb8 -r ded9aadc4199 cgi-bin/ljuserpics.pl
--- a/cgi-bin/ljuserpics.pl	Tue Sep 07 16:41:24 2010 +0800
+++ b/cgi-bin/ljuserpics.pl	Tue Sep 07 16:47:28 2010 +0800
@@ -106,67 +106,6 @@ sub load_userpics {
             LJ::MemCache::set([$id,"userpic.$id"], LJ::MemCache::hash_to_array("userpic", $ur));
         }
     }
-}
-
-# <LJFUNC>
-# name: LJ::expunge_userpic
-# des: Expunges a userpic so that the system will no longer deliver this userpic.  If
-#      your site has off-site caching or something similar, you can also define a hook
-#      "expunge_userpic" which will be called with a picid and userid when a pic is
-#      expunged.
-# args: u, picid
-# des-picid: ID of the picture to expunge.
-# des-u: User object
-# returns: undef on error, or the userid of the picture owner on success.
-# </LJFUNC>
-sub expunge_userpic {
-    # take in a picid and expunge it from the system so that it can no longer be used
-    my ($u, $picid) = @_;
-    $picid += 0;
-    return undef unless $picid && ref $u;
-
-    # get the pic information
-    my $state;
-
-    my $dbcm = LJ::get_cluster_master( $u );
-    return undef unless $dbcm && $u->writer;
-
-    $state = $dbcm->selectrow_array( 'SELECT state FROM userpic2 WHERE userid = ? AND picid = ?',
-                                     undef, $u->userid, $picid );
-    return undef unless $state; # invalid pic
-    return $u->userid if $state eq 'X'; # already expunged
-
-    # else now mark it
-    $u->do( "UPDATE userpic2 SET state='X' WHERE userid = ? AND picid = ?", undef, $u->userid, $picid );
-    return LJ::error( $dbcm ) if $dbcm->err;
-    $u->do( "DELETE FROM userpicmap2 WHERE userid = ? AND picid = ?", undef, $u->userid, $picid );
-
-    # now clear the user's memcache picture info
-    LJ::Userpic->delete_cache( $u );
-
-    # call the hook and get out of here
-    my @rval = LJ::Hooks::run_hooks( 'expunge_userpic', $picid, $u->userid );
-    return ( $u->userid, map {$_->[0]} grep {$_ && @$_ && $_->[0]} @rval );
-}
-
-# <LJFUNC>
-# name: LJ::activate_userpics
-# des: des: Wrapper around [func[LJ::User::activate_userpics]] for compatibility.
-# args: uuserid
-# returns: undef on failure 1 on success
-# </LJFUNC>
-sub activate_userpics
-{
-    my $u = shift;
-    return undef unless LJ::isu($u);
-
-    # if a userid was given, get a real $u object
-    $u = LJ::load_userid($u, "force") unless isu($u);
-
-    # should have a $u object now
-    return undef unless isu($u);
-
-    return $u->activate_userpics;
 }
 
 # <LJFUNC>
--------------------------------------------------------------------------------