]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Notice.php
Memcache returns false on cache miss
[quix0rs-gnu-social.git] / classes / Notice.php
index ef8f7046e61d17bef693b33745241dc2ce2d4041..c60237ffb0b429647a60834b80c48f98d7d65989 100644 (file)
@@ -22,9 +22,9 @@ if (!defined('LACONICA')) { exit(1); }
 /**
  * Table Definition for notice
  */
-require_once 'DB/DataObject.php';
+require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
 
-class Notice extends DB_DataObject 
+class Notice extends Memcached_DataObject 
 {
     ###START_AUTOCODE
     /* the code below is auto generated do not remove the above tag */
@@ -40,10 +40,10 @@ class Notice extends DB_DataObject
     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
     public $reply_to;                        // int(4)  
     public $is_local;                        // tinyint(1)  
-    public $source;                          // varchar(8)  
+    public $source;                          // varchar(32)  
 
     /* Static get */
-    function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Notice',$k,$v); }
+    function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Notice',$k,$v); }
 
     /* the code above is auto generated do not remove the tag below */
     ###END_AUTOCODE
@@ -54,10 +54,13 @@ class Notice extends DB_DataObject
 
        function saveTags() {
                /* extract all #hastags */
-               $count = preg_match_all('/(?:^|\s)#([a-z0-9]{1,64})/', strtolower($this->content), $match);
+               $count = preg_match_all('/(?:^|\s)#([A-Za-z0-9_\-\.]{1,64})/', strtolower($this->content), $match);
                if (!$count) {
                        return true;
                }
+               
+               /* elide characters we don't want in the tag */
+               $match[1] = str_replace(array('-', '_', '.'), '', $match[1]);
 
                /* Add them to the database */
                foreach(array_unique($match[1]) as $hashtag) {
@@ -68,41 +71,72 @@ class Notice extends DB_DataObject
                        $id = $tag->insert();
                        if (!$id) {
                                $last_error = PEAR::getStaticProperty('DB_DataObject','lastError');
-                               common_log(LOG_ERROR, 'DB error inserting hashtag: ' . $last_error->message);
+                               common_log(LOG_ERR, 'DB error inserting hashtag: ' . $last_error->message);
                                common_server_error(sprintf(_('DB error inserting hashtag: %s'), $last_error->message));
                                return;
                        }
                }
                return true;
        }
-       
-       static function saveNew($profile_id, $content, $source=NULL, $is_local=1) {
+
+       static function saveNew($profile_id, $content, $source=NULL, $is_local=1, $reply_to=NULL, $uri=NULL) {
                
                $notice = new Notice();
                $notice->profile_id = $profile_id;
                $notice->is_local = $is_local;
-               $notice->created = DB_DataObject_Cast::dateTime();
-               # Default theme uses 'content' for something else
+               $notice->reply_to = $reply_to;
+               $notice->created = common_sql_now();
                $notice->content = $content;
-
                $notice->rendered = common_render_content($notice->content, $notice);
-
+               $notice->source = $source;
+               $notice->uri = $uri;
+               
                $id = $notice->insert();
 
                if (!$id) {
                        return _('Problem saving notice.');
                }
 
-               $orig = clone($notice);
-               $notice->uri = common_notice_uri($notice);
+               # Update the URI after the notice is in the database
+               if (!$uri) {
+                       $orig = clone($notice);
+                       $notice->uri = common_notice_uri($notice);
 
-               if (!$notice->update($orig)) {
-                       return _('Problem saving notice.');
+                       if (!$notice->update($orig)) {
+                               return _('Problem saving notice.');
+                       }
                }
 
+               # XXX: do we need to change this for remote users?
+               
                common_save_replies($notice);
                $notice->saveTags();
+
+               # Clear the cache for subscribed users, so they'll update at next request
+               # XXX: someone clever could prepend instead of clearing the cache
+               
+               if (common_config('memcached', 'enabled')) {
+                       $notice->blowSubsCache();
+               }
                
                return $notice;
        }
+       
+       function blowSubsCache() {
+               $cache = new Memcache();
+               if ($cache->connect(common_config('memcached', 'server'), common_config('memcached', 'port'))) {
+                       $user = new User();
+                       
+                       $user->query('SELECT id ' .
+                                                'FROM user JOIN subscription ON user.id = subscription.subscriber ' .
+                                                'WHERE subscription.subscribed = ' . $this->profile_id);
+                       
+                       while ($user->fetch()) {
+                               $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
+                       }
+                       
+                       $user->free();
+                       unset($user);
+               }
+       }
 }