function depublish($site_id) { // send back to queue links with too many negatives global $db, $globals; $days = 4; echo "STARTING depublish for {$site_id}\n"; $site_info = SitesMgr::get_info($site_id); $links = $db->get_col("select SQL_NO_CACHE link_id as id from links, sub_statuses where id = {$site_id} and status = 'published' and date > date_sub(now(), interval {$days} day) and date < date_sub(now(), interval 14 minute) and link = link_id and link_negatives > link_votes / 5"); if ($links) { $votes_clicks = $db->get_col("select SQL_NO_CACHE link_votes/counter from links, sub_statuses, link_clicks where sub_statuses.id = {$site_id} and status = 'published' and date > date_sub(now(), interval {$days} day) and link = link_id and link_clicks.id = link"); sort($votes_clicks); foreach ($links as $link) { $l = Link::from_db($link); $vc = $l->votes / $l->clicks; $prob = cdf($votes_clicks, $vc); // Count only those votes with karma > 6 to avoid abuses with new accounts with new accounts $negatives = (int) $db->get_var("select SQL_NO_CACHE sum(user_karma) from votes, users where vote_type='links' and vote_link_id={$l->id} and vote_date > from_unixtime({$l->date}) and vote_date > date_sub(now(), interval 24 hour) and vote_value < 0 and vote_user_id > 0 and user_id = vote_user_id and user_karma > " . $globals['depublish_negative_karma']); $positives = (int) $db->get_var("select SQL_NO_CACHE sum(user_karma) from votes, users where vote_type='links' and vote_link_id={$l->id} and vote_date > from_unixtime({$l->date}) and vote_value > 0 and vote_date > date_sub(now(), interval 24 hour) and vote_user_id > 0 and user_id = vote_user_id and user_karma > " . $globals['depublish_positive_karma']); echo "Candidate {$l->uri}\n karma: {$l->sub_karma} ({$l->karma}) negative karma: {$negatives} positive karma: {$positives}\n"; // Adjust positives to the probability of votes/clicks $c = 1 + (1 - $prob) * 0.5; $positives = $positives * $c; echo " probability: {$prob} New positives: {$positives} ({$c})\n"; if ($negatives > 10 && $negatives > $c * $l->sub_karma / 6 && $l->negatives > $c * $l->votes / 6 && $l->negatives > 5 && ($negatives > $positives || $negatives > $c * $l->sub_karma / 2 && $negatives > $positives / 2)) { echo "Queued again: {$l->id} negative karma: {$negatives} positive karma: {$positives}\n"; $karma_old = $l->sub_karma; $karma_new = intval($l->sub_karma / $globals['depublish_karma_divisor']); $l->status = 'queued'; $l->sub_karma = $l->karma = $karma_new; $db->query("update links set link_status='queued', link_date = link_sent_date, link_karma={$karma_new} where link_id = {$l->id}"); SitesMgr::deploy($l); // Add an annotation to show it in the logs $l->karma_old = $karma_old; $l->karma = $karma_new; $l->annotation = _('Retirada de portada'); $l->save_annotation('link-karma'); Log::insert('link_depublished', $l->id, $l->author); if (!$site_info->sub) { // Add the discard to log/event $user = new User($l->author); if ($user->read) { echo "{$user->username}: {$user->karma}\n"; $user->add_karma(-$globals['instant_karma_per_depublished'], _('Retirada de portada')); } // Increase karma to users that voted negative $ids = $db->get_col("select vote_user_id from votes where vote_type = 'links' and vote_link_id = {$l->id} and vote_user_id > 0 and vote_value < 0"); foreach ($ids as $id) { $u = new User($id); if ($u->read) { // Avoid abuse of users voting negative just to get more karma $voted = $db->get_var("select count(*) from logs where log_type = 'user_depublished_vote' and log_user_id = {$id} and log_date > date_sub(now(), interval 48 hour)"); if ($voted < 5) { $u->add_karma(0.2, _('Negativo a retirada de portada')); Log::insert('user_depublished_vote', $l->id, $id); } } } } /*********** * TODO: call for every site (as in promote) if ($globals['twitter_token'] || $globals['jaiku_user']) { if ($globals['url_shortener']) { $short_url = $l->get_short_permalink(); } else { $short_url = fon_gs($l->get_permalink()); } $text = _('Retirada de portada') . ': ' . $l->title; if ($globals['twitter_user'] && $globals['twitter_token']) { twitter_post($text, $short_url); } if ($globals['jaiku_user'] && $globals['jaiku_key']) { jaiku_post($text, $short_url); } } *******/ } } } }
public function getTimezoneActivity($corpArray) { if (!is_array($corpArray)) { return false; } $tzActivityArray = array(); foreach ($corpArray as $aCorp) { // Add this corps TZ activity. Note: Kills and losses are treated the same // - we just want to know when they are active if (is_array($corpActivityDB = $this->getKillsInvolvingCorporation($aCorp))) { foreach ($corpActivityDB as $aCorpA) { $tzActivityArray[] = $aCorpA["kill"]->timestamp; } } } // Calculate TZ spread. Note: This is based on people working during the day, and // is weighted towards the mid to upper end (prime time). Note also that EVE // timestamps are constant - adjustments have to be made for timezones. The // calculations are not binary, a kill at midnight EVE time would be prime time // US, and still within EU. $tzArray = array("eu" => array("name" => "Euro", "score" => 0, "z-score" => 0, "r-perc" => 0, "z-perc" => 0, "shift" => 1), "us" => array("name" => "US", "score" => 0, "z-score" => 0, "r-perc" => 0, "z-perc" => 0, "shift" => -5), "au_nz" => array("name" => "AU/NZ", "score" => 0, "z-score" => 0, "r-perc" => 0, "z-perc" => 0, "shift" => 10)); $tzWeight = array(0 => 0.35, 1 => 0.1, 2 => 0.05, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0, 8 => 0, 9 => 0, 10 => 0, 11 => 0.05, 12 => 0.1, 13 => 0.1, 14 => 0.1, 15 => 0.1, 16 => 0.1, 17 => 0.1, 18 => 0.2, 19 => 0.45, 20 => 0.7, 21 => 0.9, 22 => 1.0, 23 => 0.7); foreach ($tzActivityArray as $aTime) { // 6pm to midnight is the spread for everyone //dprintf("kill time: %s, EU: %s, US: %s, AU/NZ: %s", $aTime, date("H:i:s", strtotime("+1 hour", strtotime($aTime))), date("H:i:s", strtotime("-5 hours", strtotime($aTime))), date("H:i:s", strtotime("+10 hours", strtotime($aTime)))); $eTZhour = intval(date("G", strtotime("+1 hour", strtotime($aTime)))); $eUShour = intval(date("G", strtotime("-5 hours", strtotime($aTime)))); $eAUhour = intval(date("G", strtotime("+10 hours", strtotime($aTime)))); $tzArray["eu"]["score"] += $tzWeight[$eTZhour] + abs($tzWeight[$eTZhour + 1 > 23 ? 0 : $eTZhour + 1] - $tzWeight[$eTZhour]) * (intval(date("i", strtotime($aTime))) / 59 * 100); $tzArray["us"]["score"] += $tzWeight[$eUShour] + abs($tzWeight[$eUShour + 1 > 23 ? 0 : $eUShour + 1] - $tzWeight[$eUShour]) * (intval(date("i", strtotime($aTime))) / 59 * 100); $tzArray["au_nz"]["score"] += $tzWeight[$eAUhour] + abs($tzWeight[$eAUhour + 1 > 23 ? 0 : $eAUhour + 1] - $tzWeight[$eAUhour]) * (intval(date("i", strtotime($aTime))) / 59 * 100); } // Calculate TZ percentiles $tzZAry = array($tzArray["eu"]["score"], $tzArray["us"]["score"], $tzArray["au_nz"]["score"]); $tzStdDev = sd($tzZAry); $tzArray["eu"]["z-score"] = ($tzArray["eu"]["score"] - array_sum($tzZAry) / 3) / $tzStdDev; $tzArray["eu"]["z-perc"] = cdf($tzArray["eu"]["z-score"]) * 100; $tzArray["eu"]["r-perc"] = $tzArray["eu"]["score"] / array_sum($tzZAry) * 100; $tzArray["us"]["z-score"] = ($tzArray["us"]["score"] - array_sum($tzZAry) / 3) / $tzStdDev; $tzArray["us"]["z-perc"] = cdf($tzArray["us"]["z-score"]) * 100; $tzArray["us"]["r-perc"] = $tzArray["us"]["score"] / array_sum($tzZAry) * 100; $tzArray["au_nz"]["z-score"] = ($tzArray["au_nz"]["score"] - array_sum($tzZAry) / 3) / $tzStdDev; $tzArray["au_nz"]["z-perc"] = cdf($tzArray["au_nz"]["z-score"]) * 100; $tzArray["au_nz"]["r-perc"] = $tzArray["au_nz"]["score"] / array_sum($tzZAry) * 100; // Sort array by score descending $aryScore = array(); foreach ($tzArray as $key => $row) { $aryScore[$key] = $row["score"]; } array_multisort($aryScore, SORT_DESC, $tzArray); return $tzArray; }
function update_link_karma($site, $link) { global $db, $globals; if (time() - $link->time_annotation('link-karma') < 75) { echo "ALREADY CALCULATED {$link->uri}, ignoring\n"; return 0; } $site_info = SitesMgr::get_info($site); echo "START {$site_info->name} WITH {$link->uri}\n"; $user = new User(); $user->id = $link->author; $user->read(); $karma_pos_user = 0; $karma_neg_user = 0; $karma_pos_ano = 0; User::calculate_affinity($link->author, $past_karma * 0.3); // Calculate the real karma for the link $link->calculate_karma(); $karma_new = $link->karma; $link->message = ''; $changes = 0; // TODO: $subs_coef is not available // if (DEBUG ) $link->message .= "Sub: $link->sub_id coef: ".$subs_coef[$link->sub_id]." Init values: previous: $link->old_karma calculated: $link->karma new: $karma_new<br>\n"; // Verify last published from the same site $hours = 8; $min_pub_coef = 0.8; $last_site_published = (int) $db->get_var("select SQL_NO_CACHE UNIX_TIMESTAMP(max(link_date)) from sub_statuses, links where id = {$site} and status = 'published' and date > date_sub(now(), interval {$hours} hour) and link_id = link and link_blog = {$link->blog} "); if ($last_site_published > 0) { $pub_coef = $min_pub_coef + (1 - $min_pub_coef) * (time() - $last_site_published) / (3600 * $hours); $karma_new *= $pub_coef; $link->message .= 'Last published: ' . intval((time() - $last_site_published) / 3600) . ' hours ago.<br/>'; } if ($ban = check_ban($link->url, 'hostname', false, true)) { // Check if the domain is banned $karma_new *= 0.5; $link->message .= 'Domain banned.<br/>'; $link->annotation .= _('dominio baneado') . ": " . $ban['comment'] . "<br/>"; } elseif ($user->level == 'disabled' || $user->level == 'autodisabled') { // Check if the user is banned disabled if ($user->level == 'autodisabled') { $link->message .= "{$user->username} disabled herself, penalized.<br/>"; $karma_new *= 0.2; } else { $link->message .= "{$user->username} disabled, probably due to abuses, penalized.<br/>"; $karma_new *= 0.4; } $link->annotation .= _('cuenta deshabilitada') . "<br/>"; } elseif (check_ban($link->url, 'punished_hostname', false, true)) { // Check domain and user punishments $karma_new *= 0.75; $link->message .= $globals['ban_message'] . '<br/>'; } // Check if it was depubished before $depublished = (int) $db->get_var("select count(*) from logs where log_type = 'link_depublished' and log_ref_id = {$link->id}"); if ($depublished > 0) { $karma_new *= 0.4; $link->message .= 'Previously depublished' . '<br/>'; $link->annotation .= _('previamente quitada de portada') . "<br/>"; } // Check if the are previously published during last hours from the same sub if ($link->sub_id > 0 && $link->is_sub && $link->sub_owner > 0 && $link->sub_id != $site && $site_info->owner == 0) { $sub_published = $db->get_var("select UNIX_TIMESTAMP(date) from sub_statuses where id = {$site} and origen = {$link->sub_id} and status = 'published' and date > date_sub(now(), interval 24 hour) order by date desc limit 1"); if ($sub_published > 0) { $m_diff = intval((time() - $sub_published) / 60); $c = min(1, max(0.3, $m_diff / 1440)); $karma_new *= $c; $link->message .= 'Published from the same sub, c' . sprintf(': %4.2f <br/>', $c); $link->annotation .= _('publicada del mismo sub recientemente, coeficiente') . sprintf(': %4.2f <br/>', $c); } } $link->karma = round($karma_new); /// Commons votes if ($link->karma > 20) { $days = 7; $commons_votes = $db->get_col("select SQL_NO_CACHE value from sub_statuses, link_commons where id = {$site} and status = 'published' and sub_statuses.date > date_sub(now(), interval {$days} day) and link_commons.link = sub_statuses.link order by value asc"); $common = $link->calculate_common_votes(); echo "Calculating diversity ({$common}-" . count($commons_votes) . ")\n"; if ($common != false && $commons_votes && count($commons_votes) > 5) { $common_probability = cdf($commons_votes, $common); $p = round($common_probability, 2); echo "common: {$common} common_probability: {$common_probability}\n"; $link->common_probability = $common_probability; $link->message .= 'Voters density: ' . sprintf("%5.2f", $common) . ' diversity coef: ' . sprintf("%3.2f%%", (1 - $common_probability) * 100) . " Probability: {$p}<br/>"; $link->annotation .= _('Densidad diversidad') . ': ' . sprintf("%5.2f", $common) . ' ' . _('coeficiente') . ": " . sprintf("%3.2f%%", (1 - $common_probability) * 100) . " (" . _('probabilidad') . ": {$p})<br/>"; // Bonus for diversity $c = $common_probability / 0.5; if ($c <= 1) { $c = 1 - $c; if ($link->low_karma_perc > 60) { $low_karma_coef = (50 - ($link->low_karma_perc - 50)) / 50; } else { $low_karma_coef = 1; } $bonus = round($c * 0.5 * $link->karma * $low_karma_coef * (1 - 5 * $link->negatives / $link->votes)); echo "BONUS: {$link->karma} {$p}, {$c} -> {$bonus} ({$link->low_karma_perc}, {$low_karma_coef}, {$link->negatives}/{$link->votes})\n"; } else { // Decrease for high affinity between voters $c = $c - 1; $bonus = -round($c * 0.5 * $link->karma); echo "PENALIZATION: {$link->karma} {$p}, {$c} -> {$bonus}\n"; } if (abs($bonus) > 10) { $old = $link->karma; $link->karma += $bonus; $link->annotation .= _('Karma por diversidad') . ": {$old} -> {$link->karma}<br/>"; } } } // check differences, if > 4 store it if (abs($link->old_karma - $link->karma) > 6) { // Check percentage of low karma votes if difference > 20 (to avoid sending too many messages if ($link->old_karma > $link->karma + 20 && !empty($globals['adm_email']) && intval($link->low_karma_perc) >= 90 && $link->votes > 50) { echo "LOW KARMA WARN {$link->uri}\n"; $subject = _('AVISO: enlace con muchos votos de karma menor que la media'); $body = "Perc: {$link->low_karma_perc}% User votes: {$link->votes} Negatives: {$link->negatives}\n\n"; $body .= $link->get_permalink(); mail($globals['adm_email'], $subject, $body); } $link->message = sprintf("updated karma: %6d (%d, %d, %d) -> %-6d<br/>\n", $link->old_karma, $link->votes, $link->anonymous, $link->negatives, $link->karma) . $link->message; //$link->annotation .= _('ajuste'). ": $link->old_karma -> $link->karma <br/>"; if ($link->old_karma > $link->karma) { $changes = 1; } else { $changes = 2; } // increase if (!DEBUG) { $link->save_annotation('link-karma', $site_info->name); // Update relevant values $db->query("UPDATE links set link_karma={$link->karma}, link_votes_avg={$link->votes_avg} WHERE link_id={$link->id}"); } else { $link->message .= "To store: previous: {$link->old_karma} new: {$link->karma}<br>\n"; } } return $changes; }
function Trueskill_update($epsilon, $beta, $tau, $A_mu, $A_sigma, $A_rank, $B_mu, $B_sigma, $B_rank) { $output = ''; if ($epsilon == 0) { $epsilon = 1.0; } if ($tau == 0) { $tau = 0.0833333; } // dynamic factor $A_sigma = sqrt(pow($A_sigma, 2) + pow($tau, 2)); $B_sigma = sqrt(pow($B_sigma, 2) + pow($tau, 2)); if ($A_rank == $B_rank) { // Draw $c_ij = sqrt(2 * pow($beta, 2) + pow($A_sigma, 2) + pow($B_sigma, 2)); $t = ($A_mu - $B_mu) / $c_ij; $alpha = $epsilon / $c_ij; $d = $alpha - $t; $s = $alpha + $t; $N_d = 1 / sqrt(2 * M_PI) * exp(-pow($d, 2) / 2); $Psi_d = cdf($d); $N_s = 1 / sqrt(2 * M_PI) * exp(-pow($s, 2) / 2); $Psi_s = cdf(-$s); $v = ($N_s - $N_d) / ($Psi_d - $Psi_s); $w = pow($v, 2) + ($d * $N_d + $s * $N_s) / ($Psi_d - $Psi_s); $A_delta_mu = pow($A_sigma, 2) * $v / $c_ij; $B_delta_mu = -pow($B_sigma, 2) * $v / $c_ij; $A_delta_sigma = sqrt(1 - pow($A_sigma, 2) * $w / pow($c_ij, 2)); $B_delta_sigma = sqrt(1 - pow($B_sigma, 2) * $w / pow($c_ij, 2)); $output .= "A: {$A_mu}, {$A_sigma}, {$A_delta_mu}, {$A_delta_sigma}<br>"; $output .= "B: {$B_mu}, {$B_sigma}, {$B_delta_mu}, {$B_delta_sigma}<br>"; return array($A_delta_mu, $A_delta_sigma, $B_delta_mu, $B_delta_sigma); } else { if ($A_rank < $B_rank) { $winner_mu = $A_mu; $winner_sigma = $A_sigma; $looser_mu = $B_mu; $looser_sigma = $B_sigma; } else { $winner_mu = $B_mu; $winner_sigma = $B_sigma; $looser_mu = $A_mu; $looser_sigma = $A_sigma; } $c_ij = sqrt(2 * pow($beta, 2) + pow($winner_sigma, 2) + pow($looser_sigma, 2)); $t = ($winner_mu - $looser_mu) / $c_ij; $alpha = $epsilon / $c_ij; $d = $t - $alpha; $output .= "c_ij = {$c_ij}<br>"; $output .= "t = {$t}<br>"; $output .= "alpha = {$alpha}<br>"; $output .= "d = {$d}<br>"; $N = 1 / sqrt(2 * M_PI) * exp(-pow($d, 2) / 2); $erf1 = erf($d / sqrt(2)); $output .= "erf1 = {$erf1}<br>"; $Psi = cdf($d); $output .= "N = {$N}<br>"; $output .= "Psi = {$Psi}<br>"; $v = $N / $Psi; $w = $v * ($v + $d); $output .= "v = {$v}<br>"; $output .= "w = {$w}<br>"; $winner_delta_mu = pow($winner_sigma, 2) * $v / $c_ij; $looser_delta_mu = -pow($looser_sigma, 2) * $v / $c_ij; $winner_delta_sigma = sqrt(1 - pow($winner_sigma, 2) * $w / pow($c_ij, 2)); $looser_delta_sigma = sqrt(1 - pow($looser_sigma, 2) * $w / pow($c_ij, 2)); $output .= "Winner: {$winner_mu}, {$winner_sigma}, {$winner_delta_mu}, {$winner_delta_sigma}<br>"; $output .= "Looser: {$looser_mu}, {$looser_sigma}, {$looser_delta_mu}, {$looser_delta_sigma}<br>"; if ($A_rank < $B_rank) { return array($winner_delta_mu, $winner_delta_sigma, $looser_delta_mu, $looser_delta_sigma); } else { return array($looser_delta_mu, $looser_delta_sigma, $winner_delta_mu, $winner_delta_sigma); } } }