Merge branch '0.9.x' of git@gitorious.org:statusnet/mainline into 0.9.x

This commit is contained in:
Sarven Capadisli 2010-01-05 01:16:48 +00:00
commit 48289e607b
22 changed files with 319 additions and 125 deletions

View File

@ -185,11 +185,7 @@ class FavoritedAction extends Action
function showContent()
{
if (common_config('db', 'type') == 'pgsql') {
$weightexpr='sum(exp(-extract(epoch from (now() - fave.modified)) / %s))';
} else {
$weightexpr='sum(exp(-(now() - fave.modified) / %s))';
}
$weightexpr = common_sql_weight('fave.modified', common_config('popular', 'dropoff'));
$qry = 'SELECT notice.*, '.
$weightexpr . ' as weight ' .
@ -207,7 +203,7 @@ class FavoritedAction extends Action
}
$notice = Memcached_DataObject::cachedQuery('Notice',
sprintf($qry, common_config('popular', 'dropoff')),
$qry,
600);
$nl = new NoticeList($notice, $this);

View File

@ -105,12 +105,8 @@ class PublictagcloudAction extends Action
#Add the aggregated columns...
$tags->selectAdd('max(notice_id) as last_notice_id');
if(common_config('db','type')=='pgsql') {
$calc='sum(exp(-extract(epoch from (now()-created))/%s)) as weight';
} else {
$calc='sum(exp(-(now() - created)/%s)) as weight';
}
$tags->selectAdd(sprintf($calc, common_config('tag', 'dropoff')));
$calc = common_sql_weight('created', common_config('tag', 'dropoff'));
$tags->selectAdd($calc . ' as weight');
$tags->groupBy('tag');
$tags->orderBy('weight DESC');
@ -136,7 +132,12 @@ class PublictagcloudAction extends Action
$this->elementStart('dd');
$this->elementStart('ul', 'tags xoxo tag-cloud');
foreach ($tw as $tag => $weight) {
$this->showTag($tag, $weight, $weight/$sum);
if ($sum) {
$weightedSum = $weight/$sum;
} else {
$weightedSum = 0.5;
}
$this->showTag($tag, $weight, $weightedSum);
}
$this->elementEnd('ul');
$this->elementEnd('dd');

View File

@ -59,7 +59,7 @@ class Config extends Memcached_DataObject
if (!empty($c)) {
$settings = $c->get(common_cache_key(self::settingsKey));
if (!empty($settings)) {
if ($settings !== false) {
return $settings;
}
}

View File

@ -19,8 +19,6 @@
if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
class Memcached_DataObject extends DB_DataObject
{
/**
@ -37,6 +35,42 @@ class Memcached_DataObject extends DB_DataObject
}
}
/**
* Magic function called at serialize() time.
*
* We use this to drop a couple process-specific references
* from DB_DataObject which can cause trouble in future
* processes.
*
* @return array of variable names to include in serialization.
*/
function __sleep()
{
$vars = array_keys(get_object_vars($this));
$skip = array('_DB_resultid', '_link_loaded');
return array_diff($vars, $skip);
}
/**
* Magic function called at unserialize() time.
*
* Clean out some process-specific variables which might
* be floating around from a previous process's cached
* objects.
*
* Old cached objects may still have them.
*/
function __wakeup()
{
// Refers to global state info from a previous process.
// Clear this out so we don't accidentally break global
// state in *this* process.
$this->_DB_resultid = null;
// We don't have any local DBO refs, so clear these out.
$this->_link_loaded = false;
}
/**
* Wrapper for DB_DataObject's static lookup using memcached
* as backing instead of an in-process cache array.
@ -57,7 +91,7 @@ class Memcached_DataObject extends DB_DataObject
unset($i);
}
$i = Memcached_DataObject::getcached($cls, $k, $v);
if ($i) {
if ($i !== false) { // false == cache miss
return $i;
} else {
$i = DB_DataObject::factory($cls);
@ -69,6 +103,12 @@ class Memcached_DataObject extends DB_DataObject
$i->encache();
return $i;
} else {
// save the fact that no such row exists
$c = self::memcache();
if (!empty($c)) {
$ck = self::cachekey($cls, $k, $v);
$c->set($ck, null);
}
return false;
}
}
@ -77,10 +117,13 @@ class Memcached_DataObject extends DB_DataObject
function &pkeyGet($cls, $kv)
{
$i = Memcached_DataObject::multicache($cls, $kv);
if ($i) {
if ($i !== false) { // false == cache miss
return $i;
} else {
$i = new $cls();
$i = DB_DataObject::factory($cls);
if (empty($i)) {
return false;
}
foreach ($kv as $k => $v) {
$i->$k = $v;
}
@ -88,6 +131,11 @@ class Memcached_DataObject extends DB_DataObject
$i->encache();
} else {
$i = null;
$c = self::memcache();
if (!empty($c)) {
$ck = self::multicacheKey($cls, $kv);
$c->set($ck, null);
}
}
return $i;
}
@ -152,65 +200,88 @@ class Memcached_DataObject extends DB_DataObject
function encache()
{
$c = $this->memcache();
if (!$c) {
return false;
} else {
$pkey = array();
$pval = array();
$types = $this->keyTypes();
ksort($types);
foreach ($types as $key => $type) {
if ($type == 'K') {
$pkey[] = $key;
$pval[] = $this->$key;
} else {
$c->set($this->cacheKey($this->tableName(), $key, $this->$key), $this);
}
}
# XXX: should work for both compound and scalar pkeys
$pvals = implode(',', $pval);
$pkeys = implode(',', $pkey);
$c->set($this->cacheKey($this->tableName(), $pkeys, $pvals), $this);
$keys = $this->_allCacheKeys();
foreach ($keys as $key) {
$c->set($key, $this);
}
}
function decache()
{
$c = $this->memcache();
if (!$c) {
return false;
} else {
$pkey = array();
$pval = array();
}
$keys = $this->_allCacheKeys();
foreach ($keys as $key) {
$c->delete($key, $this);
}
}
function _allCacheKeys()
{
$ckeys = array();
$types = $this->keyTypes();
ksort($types);
$pkey = array();
$pval = array();
foreach ($types as $key => $type) {
if ($type == 'K') {
assert(!empty($key));
if ($type == 'U') {
if (empty($this->$key)) {
continue;
}
$ckeys[] = $this->cacheKey($this->tableName(), $key, $this->$key);
} else if ($type == 'K' || $type == 'N') {
$pkey[] = $key;
$pval[] = $this->$key;
} else {
$c->delete($this->cacheKey($this->tableName(), $key, $this->$key));
throw new Exception("Unknown key type $key => $type for " . $this->tableName());
}
}
# should work for both compound and scalar pkeys
# XXX: comma works for now but may not be safe separator for future keys
assert(count($pkey) > 0);
// XXX: should work for both compound and scalar pkeys
$pvals = implode(',', $pval);
$pkeys = implode(',', $pkey);
$c->delete($this->cacheKey($this->tableName(), $pkeys, $pvals));
}
$ckeys[] = $this->cacheKey($this->tableName(), $pkeys, $pvals);
return $ckeys;
}
function multicache($cls, $kv)
{
ksort($kv);
$c = Memcached_DataObject::memcache();
$c = self::memcache();
if (!$c) {
return false;
} else {
return $c->get(self::multicacheKey($cls, $kv));
}
}
static function multicacheKey($cls, $kv)
{
ksort($kv);
$pkeys = implode(',', array_keys($kv));
$pvals = implode(',', array_values($kv));
return $c->get(Memcached_DataObject::cacheKey($cls, $pkeys, $pvals));
}
return self::cacheKey($cls, $pkeys, $pvals);
}
function getSearchEngine($table)
@ -247,7 +318,8 @@ class Memcached_DataObject extends DB_DataObject
$key_part = common_keyize($cls).':'.md5($qry);
$ckey = common_cache_key($key_part);
$stored = $c->get($ckey);
if ($stored) {
if ($stored !== false) {
return new ArrayWrapper($stored);
}

View File

@ -1207,7 +1207,7 @@ class Notice extends Memcached_DataObject
$idstr = $cache->get($idkey);
if (!empty($idstr)) {
if ($idstr !== false) {
// Cache hit! Woohoo!
$window = explode(',', $idstr);
$ids = array_slice($window, $offset, $limit);
@ -1216,7 +1216,7 @@ class Notice extends Memcached_DataObject
$laststr = $cache->get($idkey.';last');
if (!empty($laststr)) {
if ($laststr !== false) {
$window = explode(',', $laststr);
$last_id = $window[0];
$new_ids = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
@ -1376,7 +1376,7 @@ class Notice extends Memcached_DataObject
$ids = $this->_repeatStreamDirect($limit);
} else {
$idstr = $cache->get(common_cache_key('notice:repeats:'.$this->id));
if (!empty($idstr)) {
if ($idstr !== false) {
$ids = explode(',', $idstr);
} else {
$ids = $this->_repeatStreamDirect(100);

View File

@ -572,5 +572,5 @@ created = 142
modified = 384
[user_location_prefs__keys]
user_id = U
user_id = K

View File

@ -454,7 +454,6 @@ function showForm()
<dd>
<div class="instructions">
<p>Enter your database connection information below to initialize the database.</p>
<p>StatusNet bundles a number of libraries for ease of installation. <a href="?checklibs=true">You can see what bundled libraries you are using, versus what libraries are installed on your server.</a>
</div>
</dd>
</dl>

View File

@ -116,7 +116,7 @@ class Cache
function get($key)
{
$value = null;
$value = false;
if (Event::handle('StartCacheGet', array(&$key, &$value))) {
if (array_key_exists($key, $this->_items)) {

View File

@ -74,6 +74,7 @@ class ColumnDef
* @param string $key type of key
* @param value $default default value
* @param value $extra unused
* @param boolean $auto_increment
*/
function __construct($name=null, $type=null, $size=null,

View File

@ -58,11 +58,7 @@ class GroupTagCloudSection extends TagCloudSection
function getTags()
{
if (common_config('db', 'type') == 'pgsql') {
$weightexpr='sum(exp(-extract(epoch from (now() - notice_tag.created)) / %s))';
} else {
$weightexpr='sum(exp(-(now() - notice_tag.created) / %s))';
}
$weightexpr = common_sql_weight('notice_tag.created', common_config('tag', 'dropoff'));
$names = $this->group->getAliases();
@ -99,7 +95,6 @@ class GroupTagCloudSection extends TagCloudSection
$tag = Memcached_DataObject::cachedQuery('Notice_tag',
sprintf($qry,
common_config('tag', 'dropoff'),
$this->group->id,
$namestring),
3600);

View File

@ -58,11 +58,7 @@ class PersonalTagCloudSection extends TagCloudSection
function getTags()
{
if (common_config('db', 'type') == 'pgsql') {
$weightexpr='sum(exp(-extract(epoch from (now() - notice_tag.created)) / %s))';
} else {
$weightexpr='sum(exp(-(now() - notice_tag.created) / %s))';
}
$weightexpr = common_sql_weight('notice_tag.created', common_config('tag', 'dropoff'));
$qry = 'SELECT notice_tag.tag, '.
$weightexpr . ' as weight ' .
@ -83,7 +79,6 @@ class PersonalTagCloudSection extends TagCloudSection
$tag = Memcached_DataObject::cachedQuery('Notice_tag',
sprintf($qry,
common_config('tag', 'dropoff'),
$this->user->id),
3600);
return $tag;

View File

@ -48,17 +48,17 @@ class PopularNoticeSection extends NoticeSection
{
function getNotices()
{
// @fixme there should be a common func for this
if (common_config('db', 'type') == 'pgsql') {
$weightexpr='sum(exp(-extract(epoch from (now() - fave.modified)) / %s))';
if (!empty($this->out->tag)) {
$tag = pg_escape_string($this->out->tag);
}
} else {
$weightexpr='sum(exp(-(now() - fave.modified) / %s))';
if (!empty($this->out->tag)) {
$tag = mysql_escape_string($this->out->tag);
}
}
$weightexpr = common_sql_weight('fave.modified', common_config('popular', 'dropoff'));
$qry = "SELECT notice.*, $weightexpr as weight ";
if(isset($tag)) {
$qry .= 'FROM notice_tag, notice JOIN fave ON notice.id = fave.notice_id ' .
@ -78,7 +78,7 @@ class PopularNoticeSection extends NoticeSection
$qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
$notice = Memcached_DataObject::cachedQuery('Notice',
sprintf($qry, common_config('popular', 'dropoff')),
$qry,
1200);
return $notice;
}

View File

@ -524,6 +524,10 @@ class Schema
$sql .= ($cd->nullable) ? "null " : "not null ";
}
if (!empty($cd->auto_increment)) {
$sql .= " auto_increment ";
}
return $sql;
}
}

View File

@ -908,6 +908,26 @@ function common_sql_date($datetime)
return strftime('%Y-%m-%d %H:%M:%S', $datetime);
}
/**
* Return an SQL fragment to calculate an age-based weight from a given
* timestamp or datetime column.
*
* @param string $column name of field we're comparing against current time
* @param integer $dropoff divisor for age in seconds before exponentiation
* @return string SQL fragment
*/
function common_sql_weight($column, $dropoff)
{
if (common_config('db', 'type') == 'pgsql') {
// PostgreSQL doesn't support timestampdiff function.
// @fixme will this use the right time zone?
// @fixme does this handle cross-year subtraction correctly?
return "sum(exp(-extract(epoch from (now() - $column)) / $dropoff))";
} else {
return "sum(exp(timestampdiff(second, utc_timestamp(), $column) / $dropoff))";
}
}
function common_redirect($url, $code=307)
{
static $status = array(301 => "Moved Permanently",

View File

@ -99,6 +99,23 @@ abstract class AuthenticationPlugin extends Plugin
}
}
/**
* Internal AutoRegister event handler
* @param nickname
* @param provider_name
* @param user - the newly registered user
*/
function onAutoRegister($nickname, $provider_name, &$user)
{
if($provider_name == $this->provider_name && $this->autoregistration){
$user = $this->autoregister($nickname);
if($user){
User_username::register($user,$nickname,$this->provider_name);
return false;
}
}
}
function onStartCheckPassword($nickname, $password, &$authenticatedUser){
//map the nickname to a username
$user_username = new User_username();
@ -127,13 +144,10 @@ abstract class AuthenticationPlugin extends Plugin
}
}
}else{
if($this->autoregistration){
$authenticated = $this->checkPassword($nickname, $password);
if($authenticated){
$user = $this->autoregister($nickname);
if($user){
$authenticatedUser = $user;
User_username::register($authenticatedUser,$nickname,$this->provider_name);
if(Event::handle('AutoRegister', array($nickname, $this->provider_name, &$authenticatedUser))){
if($authenticatedUser){
return false;
}
}

View File

@ -61,7 +61,7 @@ class CacheLogPlugin extends Plugin
function onEndCacheGet($key, &$value)
{
if (is_null($value)) {
if ($value === false) {
$this->log(LOG_INFO, "Cache MISS for key '$key'");
} else {
$this->log(LOG_INFO, "Cache HIT for key '$key'");
@ -71,7 +71,21 @@ class CacheLogPlugin extends Plugin
function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
{
$this->log(LOG_INFO, "Setting cache value for key '$key'");
if (empty($value)) {
if (is_array($value)) {
$this->log(LOG_INFO, "Setting empty array for key '$key'");
} else if (is_null($value)) {
$this->log(LOG_INFO, "Setting null value for key '$key'");
} else if (is_string($value)) {
$this->log(LOG_INFO, "Setting empty string for key '$key'");
} else if (is_integer($value)) {
$this->log(LOG_INFO, "Setting integer 0 for key '$key'");
} else {
$this->log(LOG_INFO, "Setting empty value '$value' for key '$key'");
}
} else {
$this->log(LOG_INFO, "Setting non-empty value for key '$key'");
}
return true;
}

View File

@ -40,6 +40,7 @@ class CasAuthenticationPlugin extends AuthenticationPlugin
public $server;
public $port = 443;
public $path = '';
public $takeOverLogin = false;
function checkPassword($username, $password)
{
@ -62,6 +63,14 @@ class CasAuthenticationPlugin extends AuthenticationPlugin
}
}
function onArgsInitialize(&$args)
{
if($this->takeOverLogin && $args['action'] == 'login')
{
$args['action'] = 'caslogin';
}
}
function onStartInitializeRouter($m)
{
$m->connect('main/cas', array('action' => 'caslogin'));

View File

@ -21,6 +21,9 @@ password_changeable*: must be set to false. This plugin does not support changin
server*: CAS server to authentication against
port (443): Port the CAS server listens on. Almost always 443
path (): Path on the server to CAS. Usually blank.
takeOverLogin (false): Take over the main login action. If takeOverLogin is
set, anytime the standard username/password login form would be shown,
a CAS login will be done instead.
* required
default values are in (parenthesis)
@ -33,6 +36,7 @@ addPlugin('casAuthentication', array(
'autoregistration'=>true,
'server'=>'sso-cas.univ-rennes1.fr',
'port'=>443,
'path'=>''
'path'=>'',
'takeOverLogin'=>true
));

View File

@ -105,12 +105,11 @@ class FeedSubPlugin extends Plugin
return true;
}
/*
// auto increment seems to be broken
function onCheckSchema() {
// warning: the autoincrement doesn't seem to set.
// alter table feedinfo change column id id int(11) not null auto_increment;
$schema = Schema::get();
$schema->ensureDataObject('Feedinfo');
$schema->ensureTable('feedinfo', Feedinfo::schemaDef());
return true;
}
*/
}

View File

@ -31,7 +31,7 @@ class FeedDBException extends FeedSubException
}
}
class Feedinfo extends Plugin_DataObject
class Feedinfo extends Memcached_DataObject
{
public $__table = 'feedinfo';
@ -56,14 +56,38 @@ class Feedinfo extends Plugin_DataObject
return parent::staticGet(__CLASS__, $k, $v);
}
function tableDef()
/**
* return table definition for DB_DataObject
*
* DB_DataObject needs to know something about the table to manipulate
* instances. This method provides all the DB_DataObject needs to know.
*
* @return array array of column definitions
*/
function table()
{
class_exists('Schema'); // autoload hack
// warning: the autoincrement doesn't seem to set.
// alter table feedinfo change column id id int(11) not null auto_increment;
return new TableDef($this->__table,
array(new ColumnDef('id', 'integer',
null, false, 'PRI', '0', null, true),
return array('id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
'profile_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
'feeduri' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
'homeuri' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
'huburi' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
'verify_token' => DB_DATAOBJECT_STR,
'sub_start' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME,
'sub_end' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME,
'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL,
'lastupdate' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
}
static function schemaDef()
{
return array(new ColumnDef('id', 'integer',
/*size*/ null,
/*nullable*/ false,
/*key*/ 'PRI',
/*default*/ '0',
/*extra*/ null,
/*auto_increment*/ true),
new ColumnDef('profile_id', 'integer',
null, false),
new ColumnDef('feeduri', 'varchar',
@ -81,9 +105,41 @@ class Feedinfo extends Plugin_DataObject
new ColumnDef('created', 'datetime',
null, false),
new ColumnDef('lastupdate', 'datetime',
null, false)));
null, false));
}
/**
* return key definitions for DB_DataObject
*
* DB_DataObject needs to know about keys that the table has; this function
* defines them.
*
* @return array key definitions
*/
function keys()
{
return array('id' => 'P'); //?
}
/**
* return key definitions for Memcached_DataObject
*
* Our caching system uses the same key definitions, but uses a different
* method to get them.
*
* @return array key definitions
*/
function keyTypes()
{
return $this->keys();
}
/**
* Fetch the StatusNet-side profile for this feed
* @return Profile
*/
public function getProfile()
{
return Profile::staticGet('id', $this->profile_id);

View File

@ -54,6 +54,9 @@ class MemcachePlugin extends Plugin
private $_conn = null;
public $servers = array('127.0.0.1;11211');
public $compressThreshold = 20480;
public $compressMinSaving = 0.2;
/**
* Initialize the plugin
*
@ -156,6 +159,16 @@ class MemcachePlugin extends Plugin
}
$this->_conn->addServer($host, $port);
}
// Compress items stored in the cache if they're over threshold in size
// (default 2KiB) and the compression would save more than min savings
// ratio (default 0.2).
// Allows the cache to store objects larger than 1MB (if they
// compress to less than 1MB), and improves cache memory efficiency.
$this->_conn->setCompressThreshold($this->compressThreshold,
$this->compressMinSaving);
}
}
}

View File

@ -63,8 +63,10 @@ class XCachePlugin extends Plugin
function onStartCacheGet(&$key, &$value)
{
if (!xcache_isset($key)) {
$value = false;
} else {
$value = xcache_get($key);
if (!is_null($value)) {
$value = unserialize($value);
}
Event::handle('EndCacheGet', array($key, &$value));