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-06 01:06 pm

[dw-free] change get_daycounts from LJ function to user method

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

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

Refactor: $u->get_daycounts instead of LJ::get_daycounts( $u );

Patch by [personal profile] kareila.

Files modified:
  • cgi-bin/LJ/S2.pm
  • cgi-bin/LJ/S2/DayPage.pm
  • cgi-bin/LJ/S2/MonthPage.pm
  • cgi-bin/LJ/User.pm
  • cgi-bin/ljprotocol.pl
--------------------------------------------------------------------------------
diff -r 8f0046eee5f5 -r c4acc621f201 cgi-bin/LJ/S2.pm
--- a/cgi-bin/LJ/S2.pm	Sat Sep 04 18:17:38 2010 +0800
+++ b/cgi-bin/LJ/S2.pm	Mon Sep 06 21:06:10 2010 +0800
@@ -1653,10 +1653,11 @@ sub get_journal_day_counts
     return $s2page->{'_day_counts'} if defined $s2page->{'_day_counts'};
 
     my $u = $s2page->{'_u'};
+    return {} unless LJ::isu( $u );
     my $counts = {};
 
     my $remote = LJ::get_remote();
-    my $days = LJ::get_daycounts($u, $remote) or return {};
+    my $days = $u->get_daycounts( $remote ) or return {};
     foreach my $day (@$days) {
         $counts->{$day->[0]}->{$day->[1]}->{$day->[2]} = $day->[3];
     }
diff -r 8f0046eee5f5 -r c4acc621f201 cgi-bin/LJ/S2/DayPage.pm
--- a/cgi-bin/LJ/S2/DayPage.pm	Sat Sep 04 18:17:38 2010 +0800
+++ b/cgi-bin/LJ/S2/DayPage.pm	Mon Sep 06 21:06:10 2010 +0800
@@ -142,8 +142,7 @@ sub DayPage
     # find near days
     my ($prev, $next);
     my $here = sprintf("%04d%02d%02d", $year, $month, $day);        # we are here now
-    foreach (@{LJ::get_daycounts($u, $remote)})
-    {
+    foreach ( @{ $u->get_daycounts( $remote ) } ) {
         $_ = sprintf("%04d%02d%02d", (@$_)[0 .. 2]);    # map each date as YYYYMMDD number
         if ($_ < $here && (!$prev || $_ > $prev)) {     # remember in $prev less then $here last date
             $prev = $_;
diff -r 8f0046eee5f5 -r c4acc621f201 cgi-bin/LJ/S2/MonthPage.pm
--- a/cgi-bin/LJ/S2/MonthPage.pm	Sat Sep 04 18:17:38 2010 +0800
+++ b/cgi-bin/LJ/S2/MonthPage.pm	Mon Sep 06 21:06:10 2010 +0800
@@ -66,7 +66,7 @@ sub MonthPage
         ( $viewall, $viewsome ) =
             $remote->view_priv_check( $u, $get->{viewall}, 'month' );
 
-        if ( $viewall || $remote->equals( $u ) || $remote->can_manage( $u ) ) {
+        if ( $viewall || $remote->can_manage( $u ) ) {
             $secwhere = "";   # see everything
         } elsif ( $remote->is_individual ) {
             my $gmask = $u->is_community ? $remote->member_of( $u ) : $u->trustmask( $remote );
@@ -152,7 +152,7 @@ sub MonthPage
 
     $p->{'months'} = [];
 
-    my $days = LJ::get_daycounts($u, $remote) || [];
+    my $days = $u->get_daycounts( $remote ) || [];
     my $lastmo;
     foreach my $day (@$days) {
         my ($oy, $om) = ($day->[0], $day->[1]);
diff -r 8f0046eee5f5 -r c4acc621f201 cgi-bin/LJ/User.pm
--- a/cgi-bin/LJ/User.pm	Sat Sep 04 18:17:38 2010 +0800
+++ b/cgi-bin/LJ/User.pm	Mon Sep 06 21:06:10 2010 +0800
@@ -5544,6 +5544,77 @@ sub revoke_priv_all {
 =head2 Styles and S2-Related Functions
 =cut
 
+# returns undef on error, or otherwise arrayref of arrayrefs,
+# each of format [ year, month, day, count ] for all days with
+# non-zero count.  examples:
+#  [ [ 2003, 6, 5, 3 ], [ 2003, 6, 8, 4 ], ... ]
+#
+sub get_daycounts {
+    my ( $u, $remote, $not_memcache ) = @_;
+    return undef unless LJ::isu( $u );
+    my $uid = $u->id;
+
+    my $memkind = 'p'; # public only, changed below
+    my $secwhere = "AND security='public'";
+    my $viewall = 0;
+
+    if ( LJ::isu( $remote ) ) {
+        # do they have the viewall priv?
+        my $r = eval { Apache->request; }; # web context
+        my %getargs = $r ? $r->args : undef;
+        if ( defined $getargs{'viewall'} and $getargs{'viewall'} eq '1' ) {
+            $viewall = $remote->has_priv( 'canview', '*' );
+            LJ::statushistory_add( $u->userid, $remote->userid,
+                "viewall", "archive" ) if $viewall;
+        }
+
+        if ( $viewall || $remote->can_manage( $u ) ) {
+            $secwhere = "";   # see everything
+            $memkind = 'a'; # all
+        } elsif ( $remote->is_individual ) {
+            my $gmask = $u->is_community ? $remote->member_of( $u ) : $u->trustmask( $remote );
+            if ( $gmask ) {
+                $secwhere = "AND (security='public' OR (security='usemask' AND allowmask & $gmask))";
+                $memkind = 'g' . $gmask; # friends case: allowmask == gmask == 1
+            }
+        }
+    }
+
+    my $memkey = [$uid, "dayct2:$uid:$memkind"];
+    unless ($not_memcache) {
+        my $list = LJ::MemCache::get($memkey);
+        if ($list) {
+            # this was an old version of the stored memcache value
+            # where the first argument was the list creation time
+            # so throw away the first argument
+            shift @$list unless ref $list->[0];
+            return $list;
+        }
+    }
+
+    my $dbcr = LJ::get_cluster_def_reader($u) or return undef;
+    my $sth = $dbcr->prepare("SELECT year, month, day, COUNT(*) ".
+                             "FROM log2 WHERE journalid=? $secwhere GROUP BY 1, 2, 3");
+    $sth->execute($uid);
+    my @days;
+    while (my ($y, $m, $d, $c) = $sth->fetchrow_array) {
+        # we force each number from string scalars (from DBI) to int scalars,
+        # so they store smaller in memcache
+        push @days, [ int($y), int($m), int($d), int($c) ];
+    }
+
+    if ( $memkind ne "g1" && $memkind =~ /^g\d+$/ ) {
+        # custom groups are cached for only 15 minutes
+        LJ::MemCache::set( $memkey, [@days], 15 * 60 );
+    } else {
+        # all other security levels are cached indefinitely
+        # because we clear them when there are updates
+        LJ::MemCache::set( $memkey, [@days]  );
+    }
+    return \@days;
+}
+
+
 sub journal_base {
     return LJ::journal_base( @_ );
 }
@@ -8151,82 +8222,6 @@ sub set_password {
 =head2 Styles and S2-Related Functions (LJ)
 =cut
 
-# returns undef on error, or otherwise arrayref of arrayrefs,
-# each of format [ year, month, day, count ] for all days with
-# non-zero count.  examples:
-#  [ [ 2003, 6, 5, 3 ], [ 2003, 6, 8, 4 ], ... ]
-#
-sub get_daycounts
-{
-    my ($u, $remote, $not_memcache) = @_;
-    # NOTE: $remote not yet used.  one of the oldest LJ shortcomings is that
-    # it's public how many entries users have per-day, even if the entries
-    # are protected.  we'll be fixing that with a new table, but first
-    # we're moving everything to this API.
-
-    $u = LJ::want_user( $u ) or return undef;
-    my $uid = $u->id;
-
-    my $memkind = 'p'; # public only, changed below
-    my $secwhere = "AND security='public'";
-    my $viewall = 0;
-    if ($remote) {
-        # do they have the viewall priv?
-        my $r = eval { Apache->request; }; # web context
-        my %getargs = $r ? $r->args : undef;
-        if ( defined $getargs{'viewall'} and $getargs{'viewall'} eq '1' and ( $remote && $remote->has_priv( 'canview', '*' ) ) ) {
-            $viewall = 1;
-            LJ::statushistory_add( $u->userid, $remote->userid,
-                "viewall", "archive" );
-        }
-
-        if ( $remote->userid == $uid || $viewall || $remote->can_manage( $u ) ) {
-            $secwhere = "";   # see everything
-            $memkind = 'a'; # all
-        } elsif ( $remote->is_individual ) {
-            my $gmask = $u->is_community ? $remote->member_of( $u ) : $u->trustmask( $remote );
-            if ( $gmask ) {
-                $secwhere = "AND (security='public' OR (security='usemask' AND allowmask & $gmask))";
-                $memkind = 'g' . $gmask; # friends case: allowmask == gmask == 1
-            }
-        }
-    }
-
-    my $memkey = [$uid, "dayct2:$uid:$memkind"];
-    unless ($not_memcache) {
-        my $list = LJ::MemCache::get($memkey);
-        if ($list) {
-            # this was an old version of the stored memcache value
-            # where the first argument was the list creation time
-            # so throw away the first argument
-            shift @$list unless ref $list->[0];
-            return $list;
-        }
-    }
-
-    my $dbcr = LJ::get_cluster_def_reader($u) or return undef;
-    my $sth = $dbcr->prepare("SELECT year, month, day, COUNT(*) ".
-                             "FROM log2 WHERE journalid=? $secwhere GROUP BY 1, 2, 3");
-    $sth->execute($uid);
-    my @days;
-    while (my ($y, $m, $d, $c) = $sth->fetchrow_array) {
-        # we force each number from string scalars (from DBI) to int scalars,
-        # so they store smaller in memcache
-        push @days, [ int($y), int($m), int($d), int($c) ];
-    }
-
-    if ( $memkind ne "g1" && $memkind =~ /^g\d+$/ ) {
-        # custom groups are cached for only 15 minutes
-        LJ::MemCache::set( $memkey, [@days], 15 * 60 );
-    } else {
-        # all other security levels are cached indefinitely
-        # because we clear them when there are updates
-        LJ::MemCache::set( $memkey, [@days]  );
-    }
-    return \@days;
-}
-
-
 # <LJFUNC>
 # name: LJ::journal_base
 # des: Returns URL of a user's journal.
diff -r 8f0046eee5f5 -r c4acc621f201 cgi-bin/ljprotocol.pl
--- a/cgi-bin/ljprotocol.pl	Sat Sep 04 18:17:38 2010 +0800
+++ b/cgi-bin/ljprotocol.pl	Mon Sep 06 21:06:10 2010 +0800
@@ -907,9 +907,10 @@ sub getdaycounts
     my $u = $flags->{'u'};
     my $uowner = $flags->{'u_owner'} || $u;
     my $ownerid = $flags->{'ownerid'};
-
-    my $res = {};
-    my $daycts = LJ::get_daycounts($uowner, $u);
+    return fail($err,502) unless LJ::isu( $uowner );
+
+    my $res = {};
+    my $daycts = $uowner->get_daycounts( $u );
     return fail($err,502) unless $daycts;
 
     foreach my $day (@$daycts) {
--------------------------------------------------------------------------------