fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2010-06-25 11:00 am

[dw-free] replace ljmood.pl with DW::Mood.pm

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

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

Define and use is_public and ownerid methods.

Patch by [personal profile] kareila.

Files modified:
  • cgi-bin/DW/Mood.pm
  • cgi-bin/LJ/Console/Command/MoodthemePublic.pm
  • cgi-bin/LJ/Console/Command/MoodthemeSetpic.pm
  • cgi-bin/LJ/Customize.pm
--------------------------------------------------------------------------------
diff -r f85d8534fa0f -r 92e2e6b2f18e cgi-bin/DW/Mood.pm
--- a/cgi-bin/DW/Mood.pm	Fri Jun 25 15:32:49 2010 +0800
+++ b/cgi-bin/DW/Mood.pm	Fri Jun 25 19:05:56 2010 +0800
@@ -210,12 +210,43 @@ sub get_picture {
     return 0;  # couldn't find a picture anywhere in the parent chain
 }
 
-# object method: get theme description (adapted from LJ::mood_theme_des)
+# get theme description (adapted from LJ::mood_theme_des)
+# arguments: theme id (only required if called as class method)
 sub des {
-    my ( $self ) = @_;
-    my $themeid = $self->id or return undef;
+    my $self = shift;
+    return $self->prop( 'des', @_ );
+}
+
+# get named property of mood theme from cache
+sub prop {
+    my ( $self, $prop, $themeid ) = @_;
+
+    if ( defined $themeid ) {
+        # make sure the theme is valid and cached
+        $self = $self->load_theme( $themeid ) or return;
+    } else {
+        # make sure we have an object loaded
+        $themeid = $self->id or return;
+    }
+
     my $m = $LJ::CACHE_MOOD_THEME{$themeid};
-    return $m ? $m->{des} : undef;
+    return $m ? $m->{$prop} : undef;
+}
+
+# given a theme, lookup the user who owns it
+# arguments: theme id (only required if called as class method)
+# returns: userid, undef on failure
+sub ownerid {
+    my $self = shift;
+    return $self->prop( 'ownerid', @_ );
+}
+
+# given a theme, check whether it is public
+# arguments: theme id (only required if called as class method)
+# returns: Y/N/undef
+sub is_public {
+    my $self = shift;
+    return $self->prop( 'is_public', @_ );
 }
 
 # clear cached theme data from memory
diff -r f85d8534fa0f -r 92e2e6b2f18e cgi-bin/LJ/Console/Command/MoodthemePublic.pm
--- a/cgi-bin/LJ/Console/Command/MoodthemePublic.pm	Fri Jun 25 15:32:49 2010 +0800
+++ b/cgi-bin/LJ/Console/Command/MoodthemePublic.pm	Fri Jun 25 19:05:56 2010 +0800
@@ -43,10 +43,7 @@ sub execute {
         unless $public =~ /^[YN]$/;
     my $msg = ($public eq "Y") ? "public" : "not public";
 
-    my $dbh = LJ::get_db_writer();
-    my $sth = $dbh->prepare("SELECT is_public FROM moodthemes WHERE moodthemeid = ?");
-    $sth->execute($themeid);
-    my $old_value = $sth->fetchrow_array;
+    my $old_value = DW::Mood->is_public( $themeid );
 
     return $self->error("This theme doesn't seem to exist.")
         unless $old_value;
@@ -54,7 +51,12 @@ sub execute {
     return $self->error("This theme is already marked as $msg.")
         if $old_value eq $public;
 
+    my $dbh = LJ::get_db_writer() or
+        return $self->error( "Database unavailable" );
     $dbh->do("UPDATE moodthemes SET is_public = ? WHERE moodthemeid = ?", undef, $public, $themeid);
+    return $self->error( "Database error: " . $dbh->errstr ) if $dbh->err;
+    DW::Mood->clear_cache( $themeid );
+
     return $self->print("Theme #$themeid marked as $msg.");
 }
 
diff -r f85d8534fa0f -r 92e2e6b2f18e cgi-bin/LJ/Console/Command/MoodthemeSetpic.pm
--- a/cgi-bin/LJ/Console/Command/MoodthemeSetpic.pm	Fri Jun 25 15:32:49 2010 +0800
+++ b/cgi-bin/LJ/Console/Command/MoodthemeSetpic.pm	Fri Jun 25 19:05:56 2010 +0800
@@ -43,13 +43,12 @@ sub execute {
     return $self->error("Sorry, your account type doesn't let you create new mood themes")
         unless $remote->can_create_moodthemes;
 
-    my $dbh = LJ::get_db_writer();
-
-    my $sth = $dbh->prepare("SELECT ownerid FROM moodthemes WHERE moodthemeid = ?");
-    $sth->execute($themeid);
-    my $owner = $sth->fetchrow_array;
+    my $owner = DW::Mood->ownerid( $themeid );
     return $self->error("You do not own this mood theme.")
         unless $owner == $remote->id;
+
+    my $dbh = LJ::get_db_writer() or
+        return $self->error( "Database unavailable" );
 
     $width += 0;
     $height += 0;
diff -r f85d8534fa0f -r 92e2e6b2f18e cgi-bin/LJ/Customize.pm
--- a/cgi-bin/LJ/Customize.pm	Fri Jun 25 15:32:49 2010 +0800
+++ b/cgi-bin/LJ/Customize.pm	Fri Jun 25 19:05:56 2010 +0800
@@ -791,15 +791,11 @@ sub s2_implicit_style_create
 # returns: themeid if public or owned by user, false otherwise
 # </LJFUNC>
 sub validate_moodthemeid {
-    my $class = shift;
-    my ($u, $themeid) = @_;
-    my $dbr = LJ::get_db_reader();
-    if ($themeid) {
-        my ($mownerid, $mpublic) = $dbr->selectrow_array("SELECT ownerid, is_public FROM moodthemes ".
-                                                         "WHERE moodthemeid=?", undef, $themeid);
-        $themeid = 0 unless $mpublic eq 'Y' || $mownerid == $u->{'userid'};
-    }
-    return $themeid
+    my ( $class, $u, $themeid ) = @_;
+    my $theme = DW::Mood->new( $themeid );
+    return 0 unless $theme &&
+        ( $theme->is_public eq 'Y' || $theme->ownerid == $u->userid );
+    return $themeid;
 }
 
 # <LJFUNC>
--------------------------------------------------------------------------------

Post a comment in response:

This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org