//See whether this entity already has another favoriter who uses its shared event stream
//See whether this entity already has another favoriter who uses its shared event stream
$useshared=qa_db_read_one_value(qa_db_query_sub(
$useshared=qa_db_read_one_value(qa_db_query_sub(
'SELECT COUNT(*) FROM ^userfavorites WHERE entitytype=$ AND entityid=# AND nouserevents>0 LIMIT 1',
'SELECT COUNT(*) FROM ^userfavorites WHERE entitytype=$ AND entityid=# AND nouserevents>0 LIMIT 1',
$entitytype,$entityid
$entitytype,$entityid
));
));
//If not, check whether it's time to switch it over to a shared stream
//If not, check whether it's time to switch it over to a shared stream
if(!$useshared){
if(!$useshared){
$favoriters=qa_db_read_one_value(qa_db_query_sub(
$favoriters=qa_db_read_one_value(qa_db_query_sub(
'SELECT COUNT(*) FROM ^userfavorites WHERE entitytype=$ AND entityid=# LIMIT #',
'SELECT COUNT(*) FROM ^userfavorites WHERE entitytype=$ AND entityid=# LIMIT #',
$entitytype,$entityid,$threshold
$entitytype,$entityid,$threshold
));
));
$useshared=($favoriters>=$threshold);
}
// If we're going to use the shared stream...
if($useshared){
// ... for all the people for whom we're switching this to a shared stream, find the highest number of other shared streams they have
$maxshared=qa_db_read_one_value(qa_db_query_sub(
'SELECT MAX(c) FROM (SELECT COUNT(*) AS c FROM ^userfavorites AS shared JOIN ^userfavorites AS unshared '.
'WHERE shared.userid=unshared.userid AND shared.nouserevents>0 AND unshared.entitytype=$ AND unshared.entityid=# AND unshared.nouserevents=0 GROUP BY shared.userid) y',
$entitytype,$entityid
));
// ... if this number is greater than our current 'max_copy_user_updates' threshold, increase that threshold (see long comment above)
$useshared=($favoriters>=$threshold);
}
if(($maxshared+1)>$threshold)
// If we're going to use the shared stream...
qa_opt('max_copy_user_updates',$maxshared+1);
// ... now switch all unshared favoriters (including this new one) over to be shared
if($useshared){
qa_db_query_sub(
// ... for all the people for whom we're switching this to a shared stream, find the highest number of other shared streams they have
'UPDATE ^userfavorites SET nouserevents=1 WHERE entitytype=$ AND entityid=# AND nouserevents=0',
$entitytype,$entityid
);
// Otherwise if we're going to record this in user-specific streams ...
$maxshared=qa_db_read_one_value(qa_db_query_sub(
'SELECT MAX(c) FROM (SELECT COUNT(*) AS c FROM ^userfavorites AS shared JOIN ^userfavorites AS unshared '.
'WHERE shared.userid=unshared.userid AND shared.nouserevents>0 AND unshared.entitytype=$ AND unshared.entityid=# AND unshared.nouserevents=0 GROUP BY shared.userid) y',
$entitytype,$entityid
));
}else{
// ... if this number is greater than our current 'max_copy_user_updates' threshold, increase that threshold (see long comment above)
require_onceQA_INCLUDE_DIR.'db/events.php';
// ... copy across recent events from the shared stream
if(($maxshared+1)>$threshold)
qa_opt('max_copy_user_updates',$maxshared+1);
qa_db_query_sub(
// ... now switch all unshared favoriters (including this new one) over to be shared
$query="UPDATE ^posts AS x, (SELECT parents.postid, parents.created AS qcreated, COALESCE(MAX(children.created), parents.created) as acreated, COUNT(children.postid) AS acount, parents.netvotes, parents.views FROM ^posts AS parents LEFT JOIN ^posts AS children ON parents.postid=children.parentid AND children.type='A' WHERE parents.postid>=# AND parents.postid<=# AND LEFT(parents.type, 1)='Q' GROUP BY postid) AS a SET x.hotness=(".
if(qa_should_update_counts()){
'((TO_DAYS(a.qcreated)-734138)*86400.0+TIME_TO_SEC(a.qcreated))*# + '.// zero-point is Jan 1, 2010
$query="UPDATE ^posts AS x, (SELECT parents.postid, parents.created AS qcreated, COALESCE(MAX(children.created), parents.created) as acreated, COUNT(children.postid) AS acount, parents.netvotes, parents.views FROM ^posts AS parents LEFT JOIN ^posts AS children ON parents.postid=children.parentid AND children.type='A' WHERE parents.postid>=# AND parents.postid<=# AND LEFT(parents.type, 1)='Q' GROUP BY postid) AS a SET x.hotness=(".
'(a.views+0.0+#)*#'.
'((TO_DAYS(a.qcreated)-734138)*86400.0+TIME_TO_SEC(a.qcreated))*# + '.// zero-point is Jan 1, 2010
// delete PM only if both sender and receiver have hidden it
$clause=$public?'':' AND fromhidden=1 AND tohidden=1';
qa_db_query_sub(
'DELETE FROM ^messages WHERE messageid=#'.$clause,
$messageid
);
}
/**
* Recalculate the cached count of wall posts for user $userid in the database
* @param $userid
*/
functionqa_db_user_recount_posts($userid)
{
if(qa_should_update_counts()){
qa_db_query_sub(
qa_db_query_sub(
'DELETE FROM ^messages WHERE messageid=#'.$clause,
"UPDATE ^users AS x, (SELECT COUNT(*) AS wallposts FROM ^messages WHERE touserid=# AND type='PUBLIC') AS a SET x.wallposts=a.wallposts WHERE x.userid=#",
$messageid
$userid,$userid
);
);
}
}
}
functionqa_db_user_recount_posts($userid)
/*
Recalculate the cached count of wall posts for user $userid in the database
*/
{
if(qa_should_update_counts())
qa_db_query_sub(
"UPDATE ^users AS x, (SELECT COUNT(*) AS wallposts FROM ^messages WHERE touserid=# AND type='PUBLIC') AS a SET x.wallposts=a.wallposts WHERE x.userid=#",
$userid,$userid
);
}
/*
Omit PHP closing tag to help avoid accidental output