From 8b65883f9ddf1cb1b7bdec323722da351fa0cb69 Mon Sep 17 00:00:00 2001
From: Evan Prodromou <evan@controlyourself.ca>
Date: Thu, 9 Jul 2009 17:28:38 -0700
Subject: cache frequently-used subscriber, subscription, notice and fave count
 values

---
 lib/command.php       | 15 ++++-----------
 lib/profileaction.php | 21 ++++++---------------
 lib/subs.php          | 13 ++++++++++---
 lib/twitterapi.php    | 20 +++++---------------
 4 files changed, 25 insertions(+), 44 deletions(-)

(limited to 'lib')

diff --git a/lib/command.php b/lib/command.php
index 564661382..4e2280bc8 100644
--- a/lib/command.php
+++ b/lib/command.php
@@ -97,18 +97,11 @@ class StatsCommand extends Command
 {
     function execute($channel)
     {
+        $profile = $this->user->getProfile();
 
-        $subs = new Subscription();
-        $subs->subscriber = $this->user->id;
-        $subs_count = (int) $subs->count() - 1;
-
-        $subbed = new Subscription();
-        $subbed->subscribed = $this->user->id;
-        $subbed_count = (int) $subbed->count() - 1;
-
-        $notices = new Notice();
-        $notices->profile_id = $this->user->id;
-        $notice_count = (int) $notices->count();
+        $subs_count   = $profile->subscriptionCount();
+        $subbed_count = $profile->subscriberCount();
+        $notice_count = $profile->noticeCount();
 
         $channel->output($this->user, sprintf(_("Subscriptions: %1\$s\n".
                                    "Subscribers: %2\$s\n".
diff --git a/lib/profileaction.php b/lib/profileaction.php
index eeb5dbe48..9e9c79c78 100644
--- a/lib/profileaction.php
+++ b/lib/profileaction.php
@@ -163,18 +163,9 @@ class ProfileAction extends OwnerDesignAction
 
     function showStatistics()
     {
-        // XXX: WORM cache this
-        $subs = new Subscription();
-        $subs->subscriber = $this->profile->id;
-        $subs_count = (int) $subs->count() - 1;
-
-        $subbed = new Subscription();
-        $subbed->subscribed = $this->profile->id;
-        $subbed_count = (int) $subbed->count() - 1;
-
-        $notices = new Notice();
-        $notices->profile_id = $this->profile->id;
-        $notice_count = (int) $notices->count();
+        $subs_count   = $this->profile->subscriptionCount();
+        $subbed_count = $this->profile->subscriberCount();
+        $notice_count = $this->profile->noticeCount();
 
         $this->elementStart('div', array('id' => 'entity_statistics',
                                          'class' => 'section'));
@@ -199,7 +190,7 @@ class ProfileAction extends OwnerDesignAction
                                                              array('nickname' => $this->profile->nickname))),
                        _('Subscriptions'));
         $this->elementEnd('dt');
-        $this->element('dd', null, (is_int($subs_count)) ? $subs_count : '0');
+        $this->element('dd', null, $subs_count);
         $this->elementEnd('dl');
 
         $this->elementStart('dl', 'entity_subscribers');
@@ -208,12 +199,12 @@ class ProfileAction extends OwnerDesignAction
                                                              array('nickname' => $this->profile->nickname))),
                        _('Subscribers'));
         $this->elementEnd('dt');
-        $this->element('dd', 'subscribers', (is_int($subbed_count)) ? $subbed_count : '0');
+        $this->element('dd', 'subscribers', $subbed_count);
         $this->elementEnd('dl');
 
         $this->elementStart('dl', 'entity_notices');
         $this->element('dt', null, _('Notices'));
-        $this->element('dd', null, (is_int($notice_count)) ? $notice_count : '0');
+        $this->element('dd', null, $notice_count);
         $this->elementEnd('dl');
 
         $this->elementEnd('div');
diff --git a/lib/subs.php b/lib/subs.php
index 3bd67b39c..e76023752 100644
--- a/lib/subs.php
+++ b/lib/subs.php
@@ -44,7 +44,6 @@ function subs_subscribe_user($user, $other_nickname)
 
 function subs_subscribe_to($user, $other)
 {
-
     if ($user->isSubscribed($other)) {
         return _('Already subscribed!.');
     }
@@ -60,12 +59,16 @@ function subs_subscribe_to($user, $other)
 
     subs_notify($other, $user);
 
-        $cache = common_memcache();
+    $cache = common_memcache();
 
     if ($cache) {
         $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
 	}
 
+    $profile = $user->getProfile();
+
+    $profile->blowSubscriptionsCount();
+    $other->blowSubscribersCount();
 
     if ($other->autosubscribe && !$other->isSubscribed($user) && !$user->hasBlocked($other)) {
         if (!$other->subscribeTo($user)) {
@@ -117,7 +120,6 @@ function subs_unsubscribe_user($user, $other_nickname)
 
 function subs_unsubscribe_to($user, $other)
 {
-
     if (!$user->isSubscribed($other))
         return _('Not subscribed!.');
 
@@ -139,6 +141,11 @@ function subs_unsubscribe_to($user, $other)
         $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
 	}
 
+    $profile = $user->getProfile();
+
+    $profile->blowSubscriptionsCount();
+    $other->blowSubscribersCount();
+
     return true;
 }
 
diff --git a/lib/twitterapi.php b/lib/twitterapi.php
index 8f902cbca..f48513e67 100644
--- a/lib/twitterapi.php
+++ b/lib/twitterapi.php
@@ -89,7 +89,7 @@ class TwitterapiAction extends Action
 
         $twitter_user['url'] = ($profile->homepage) ? $profile->homepage : null;
         $twitter_user['protected'] = false; # not supported by Laconica yet
-        $twitter_user['followers_count'] = $this->count_subscriptions($profile);
+        $twitter_user['followers_count'] = $profile->subscriberCount();
 
         // To be supported soon...
         $twitter_user['profile_background_color'] = '';
@@ -98,17 +98,11 @@ class TwitterapiAction extends Action
         $twitter_user['profile_sidebar_fill_color'] = '';
         $twitter_user['profile_sidebar_border_color'] = '';
 
-        $subbed = DB_DataObject::factory('subscription');
-        $subbed->subscriber = $profile->id;
-        $subbed_count = (int) $subbed->count() - 1;
-        $twitter_user['friends_count'] = (is_int($subbed_count)) ? $subbed_count : 0;
+        $twitter_user['friends_count'] = $profile->subscriptionCount();
 
         $twitter_user['created_at'] = $this->date_twitter($profile->created);
 
-        $faves = DB_DataObject::factory('fave');
-        $faves->user_id = $user->id;
-        $faves_count = (int) $faves->count();
-        $twitter_user['favourites_count'] = $faves_count; // British spelling!
+        $twitter_user['favourites_count'] = $profile->faveCount(); // British spelling!
 
         // Need to pull up the user for some of this
         $user = User::staticGet($profile->id);
@@ -129,11 +123,7 @@ class TwitterapiAction extends Action
         $twitter_user['profile_background_image_url'] = '';
         $twitter_user['profile_background_tile'] = false;
 
-        $notices = DB_DataObject::factory('notice');
-        $notices->profile_id = $profile->id;
-        $notice_count = (int) $notices->count();
-
-        $twitter_user['statuses_count'] = (is_int($notice_count)) ? $notice_count : 0;
+        $twitter_user['statuses_count'] = $profile->noticeCount();
 
         // Is the requesting user following this user?
         $twitter_user['following'] = false;
@@ -396,7 +386,7 @@ class TwitterapiAction extends Action
             $enclosure = $entry['enclosures'][0];
             $this->element('enclosure', array('url'=>$enclosure['url'],'type'=>$enclosure['mimetype'],'length'=>$enclosure['size']), null);
         }
-        
+
         $this->elementEnd('item');
     }
 
-- 
cgit v1.2.3-54-g00ecf


From f527b8a8d7cf38e21c0ceb8d54cfae2d679e1564 Mon Sep 17 00:00:00 2001
From: Evan Prodromou <evan@controlyourself.ca>
Date: Thu, 9 Jul 2009 17:40:11 -0700
Subject: wrong order for span and a in profilesection

---
 lib/profilesection.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

(limited to 'lib')

diff --git a/lib/profilesection.php b/lib/profilesection.php
index 8ed290e03..9ff243fb5 100644
--- a/lib/profilesection.php
+++ b/lib/profilesection.php
@@ -94,8 +94,8 @@ class ProfileSection extends Section
                                     $profile->fullname :
                                     $profile->nickname));
         $this->out->element('span', 'fn nickname', $profile->nickname);
-        $this->out->elementEnd('span');
         $this->out->elementEnd('a');
+        $this->out->elementEnd('span');
         $this->out->elementEnd('td');
         if ($profile->value) {
             $this->out->element('td', 'value', $profile->value);
-- 
cgit v1.2.3-54-g00ecf


From 15f6309deacf64e6b408d45a4eb19852d36f9f72 Mon Sep 17 00:00:00 2001
From: Evan Prodromou <evan@controlyourself.ca>
Date: Thu, 9 Jul 2009 23:12:53 -0700
Subject: add a little syntactical sugar for adding plugins

---
 lib/common.php | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

(limited to 'lib')

diff --git a/lib/common.php b/lib/common.php
index 832667d81..c47702779 100644
--- a/lib/common.php
+++ b/lib/common.php
@@ -282,6 +282,39 @@ if (function_exists('date_default_timezone_set')) {
     date_default_timezone_set('UTC');
 }
 
+function addPlugin($name, $attrs = null)
+{
+    $name = ucfirst($name);
+    $pluginclass = "{$name}Plugin";
+
+    if (!class_exists($pluginclass)) {
+
+        $files = array("local/plugins/{$pluginclass}.php",
+                       "local/plugins/{$name}/{$pluginclass}.php",
+                       "local/{$pluginclass}.php",
+                       "local/{$name}/{$pluginclass}.php",
+                       "plugins/{$pluginclass}.php",
+                       "plugins/{$name}/{$pluginclass}.php");
+
+        foreach ($files as $file) {
+            $fullpath = INSTALLDIR.'/'.$file;
+            if (@file_exists($fullpath)) {
+                include_once($fullpath);
+                break;
+            }
+        }
+    }
+
+    $inst = new $pluginclass();
+
+    if (!empty($attrs)) {
+        foreach ($attrs as $aname => $avalue) {
+            $inst->$aname = $avalue;
+        }
+    }
+    return $inst;
+}
+
 // From most general to most specific:
 // server-wide, then vhost-wide, then for a path,
 // finally for a dir (usually only need one of the last two).
-- 
cgit v1.2.3-54-g00ecf