Example #1
0
#! /usr/bin/php
<?php 
include '../config.php';
$hours = intval($argv[1]);
if (!$hours) {
    $hours = 4;
}
$uids = $db->get_col("select distinct(comment_user_id) from votes, comments where vote_type='comments' and vote_date > date_sub(now(), interval {$hours} hour) and comment_id = vote_link_id");
echo "Total: " . count($uids) . "\n";
foreach ($uids as $id) {
    //$affinity = User::get_affinity($id);
    $affinity = User::calculate_affinity($id, 100);
    if ($affinity) {
        echo "Calculated for {$id}, length: " . count($affinity) . "\n";
        usleep(100);
    }
}
Example #2
0
if ($links) {
    $output .= "<tr class='thead'><th>votes</th><th>anon</th><th>neg.</th><th>coef</th><th>karma</th><th>meta</th><th>title</th><th>changes</th></tr>\n";
    $i = 0;
    foreach ($links as $dblink) {
        $link = new Link();
        $link->id = $dblink->link_id;
        $db->transaction();
        $link->read();
        echo "START 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();
        if ($link->coef > 1) {
            if ($decay > 1) {
                $karma_threshold = $past_karma;
            } else {
                $karma_threshold = $min_karma;
            }
        } else {
            // Otherwise use normal decayed min_karma
            $karma_threshold = $min_karma;
        }
        //$karma_new = $link->karma * $meta_coef[$dblink->parent];
        $karma_new = $link->karma;
        $link->message = '';
Example #3
0
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 -&gt; $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;
}