forked from GNUsocial/gnu-social
		
	remove obsoleted getStream, getStreamDirect, getCachedStream from Notice; use stream() instead
This commit is contained in:
		@@ -655,193 +655,6 @@ class Notice extends Memcached_DataObject
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # XXX: too many args; we need to move to named params or even a separate
 | 
					 | 
				
			||||||
    # class for notice streams
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    static function getStream($qry, $cachekey, $offset=0, $limit=20, $since_id=0, $max_id=0, $order=null, $since=null) {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (common_config('memcached', 'enabled')) {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            # Skip the cache if this is a since, since_id or max_id qry
 | 
					 | 
				
			||||||
            if ($since_id > 0 || $max_id > 0 || $since) {
 | 
					 | 
				
			||||||
                return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since);
 | 
					 | 
				
			||||||
            } else {
 | 
					 | 
				
			||||||
                return Notice::getCachedStream($qry, $cachekey, $offset, $limit, $order);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    static function getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since) {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $needAnd = false;
 | 
					 | 
				
			||||||
        $needWhere = true;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (preg_match('/\bWHERE\b/i', $qry)) {
 | 
					 | 
				
			||||||
            $needWhere = false;
 | 
					 | 
				
			||||||
            $needAnd = true;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if ($since_id > 0) {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if ($needWhere) {
 | 
					 | 
				
			||||||
                $qry .= ' WHERE ';
 | 
					 | 
				
			||||||
                $needWhere = false;
 | 
					 | 
				
			||||||
            } else {
 | 
					 | 
				
			||||||
                $qry .= ' AND ';
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            $qry .= ' notice.id > ' . $since_id;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if ($max_id > 0) {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if ($needWhere) {
 | 
					 | 
				
			||||||
                $qry .= ' WHERE ';
 | 
					 | 
				
			||||||
                $needWhere = false;
 | 
					 | 
				
			||||||
            } else {
 | 
					 | 
				
			||||||
                $qry .= ' AND ';
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            $qry .= ' notice.id <= ' . $max_id;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if ($since) {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if ($needWhere) {
 | 
					 | 
				
			||||||
                $qry .= ' WHERE ';
 | 
					 | 
				
			||||||
                $needWhere = false;
 | 
					 | 
				
			||||||
            } else {
 | 
					 | 
				
			||||||
                $qry .= ' AND ';
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            $qry .= ' notice.created > \'' . date('Y-m-d H:i:s', $since) . '\'';
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        # Allow ORDER override
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if ($order) {
 | 
					 | 
				
			||||||
            $qry .= $order;
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
            $qry .= ' ORDER BY notice.created DESC, notice.id DESC ';
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (common_config('db','type') == 'pgsql') {
 | 
					 | 
				
			||||||
            $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
            $qry .= ' LIMIT ' . $offset . ', ' . $limit;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $notice = new Notice();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $notice->query($qry);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return $notice;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # XXX: this is pretty long and should probably be broken up into
 | 
					 | 
				
			||||||
    # some helper functions
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    static function getCachedStream($qry, $cachekey, $offset, $limit, $order) {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        # If outside our cache window, just go to the DB
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if ($offset + $limit > NOTICE_CACHE_WINDOW) {
 | 
					 | 
				
			||||||
            return Notice::getStreamDirect($qry, $offset, $limit, null, null, $order, null);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        # Get the cache; if we can't, just go to the DB
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $cache = common_memcache();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (empty($cache)) {
 | 
					 | 
				
			||||||
            return Notice::getStreamDirect($qry, $offset, $limit, null, null, $order, null);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        # Get the notices out of the cache
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $notices = $cache->get(common_cache_key($cachekey));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        # On a cache hit, return a DB-object-like wrapper
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if ($notices !== false) {
 | 
					 | 
				
			||||||
            $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
 | 
					 | 
				
			||||||
            return $wrapper;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        # If the cache was invalidated because of new data being
 | 
					 | 
				
			||||||
        # added, we can try and just get the new stuff. We keep an additional
 | 
					 | 
				
			||||||
        # copy of the data at the key + ';last'
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        # No cache hit. Try to get the *last* cached version
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $last_notices = $cache->get(common_cache_key($cachekey) . ';last');
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if ($last_notices) {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            # Reverse-chron order, so last ID is last.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            $last_id = $last_notices[0]->id;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            # XXX: this assumes monotonically increasing IDs; a fair
 | 
					 | 
				
			||||||
            # bet with our DB.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            $new_notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW,
 | 
					 | 
				
			||||||
                                                  $last_id, null, $order, null);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if ($new_notice) {
 | 
					 | 
				
			||||||
                $new_notices = array();
 | 
					 | 
				
			||||||
                while ($new_notice->fetch()) {
 | 
					 | 
				
			||||||
                    $new_notices[] = clone($new_notice);
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                $new_notice->free();
 | 
					 | 
				
			||||||
                $notices = array_slice(array_merge($new_notices, $last_notices),
 | 
					 | 
				
			||||||
                                       0, NOTICE_CACHE_WINDOW);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                # Store the array in the cache for next time
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                $result = $cache->set(common_cache_key($cachekey), $notices);
 | 
					 | 
				
			||||||
                $result = $cache->set(common_cache_key($cachekey) . ';last', $notices);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                # return a wrapper of the array for use now
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                return new ArrayWrapper(array_slice($notices, $offset, $limit));
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        # Otherwise, get the full cache window out of the DB
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW, null, null, $order, null);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        # If there are no hits, just return the value
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (empty($notice)) {
 | 
					 | 
				
			||||||
            return $notice;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        # Pack results into an array
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $notices = array();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        while ($notice->fetch()) {
 | 
					 | 
				
			||||||
            $notices[] = clone($notice);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $notice->free();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        # Store the array in the cache for next time
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $result = $cache->set(common_cache_key($cachekey), $notices);
 | 
					 | 
				
			||||||
        $result = $cache->set(common_cache_key($cachekey) . ';last', $notices);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        # return a wrapper of the array for use now
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return $wrapper;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    function getStreamByIds($ids)
 | 
					    function getStreamByIds($ids)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $cache = common_memcache();
 | 
					        $cache = common_memcache();
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user