$memcache_key = 'topcommented_' . $from . '_' . get_current_page(); } if (!($memcache_key && ($rows = memcache_mget($memcache_key . 'rows')) && ($links = memcache_mget($memcache_key)))) { // It's not in memcache if ($time_link) { $rows = min(100, $db->get_var("SELECT count(*) FROM links WHERE {$time_link}")); } else { $rows = min(100, $db->get_var("SELECT count(*) FROM links")); } if ($rows == 0) { do_error(_('no hay noticias seleccionadas'), 500); } $links = $db->get_results("{$sql} LIMIT {$offset},{$page_size}"); if ($memcache_key) { memcache_madd($memcache_key . 'rows', $rows, 1800); memcache_madd($memcache_key, $links, 1800); } } if ($links) { foreach ($links as $dblink) { $link->id = $dblink->link_id; $link->read(); $link->print_summary('short'); } } do_pages($rows, $page_size); echo '</div>'; do_footer_menu(); do_footer(); function print_period_tabs() {
function do_best_posts() { global $db, $globals, $dblang; if ($globals['mobile']) { return; } $output = ''; $key = 'best_posts_' . $globals['css_main']; if (memcache_mprint($key)) { return; } $min_date = date("Y-m-d H:i:00", $globals['now'] - 86400); // about 24 hours $res = $db->get_results("select post_id from posts, users where post_date > '{$min_date}' and post_user_id = user_id and post_karma > 0 order by post_karma desc limit 10"); if ($res) { $output .= '<div class="sidebox"><div class="header"><h4><a href="' . post_get_base_url('_best') . '">' . _('mejores notas') . '</a></h4></div><div class="comments"><ul>' . "\n"; foreach ($res as $p) { $post = new Post(); $post->id = $p->post_id; $post->read(); $output .= '<li><img src="' . get_avatar_url($post->author, $post->avatar, 20) . '" alt="" width="20" height="20" class="avatar"/>'; $output .= '<p><strong>' . $post->username . '</strong>: <a onmouseout="tooltip.clear(event);" onclick="tooltip.clear(this);" onmouseover="return tooltip.ajax_delayed(event, \'get_post_tooltip.php\', \'' . $post->id . '\', 10000);" href="' . post_get_base_url($post->username) . '/' . $post->id . '"><em>' . text_to_summary($post->clean_content(), 80) . '</em></a></p></li>' . "\n"; } $output .= '</ul></div></div>'; echo $output; memcache_madd($key, $output, 300); } }
} if ($links) { $dict['status'] = 'OK'; $dict['data'] = array(); foreach ($links as $dblink) { if ($json) { $data = array(); $data['url'] = 'http://' . get_server_name() . '/story.php?id=' . $dblink->link_id; $data['status'] = $dblink->link_status; $data['votes'] = intval($dblink->link_votes); $data['anonymous'] = intval($dblink->link_anonymous); $data['karma'] = intval($dblink->link_karma); array_push($dict['data'], $data); } else { $response = 'OK http://' . get_server_name() . '/story.php?id=' . $dblink->link_id . ' ' . ($dblink->link_votes + $dblink->link_anonymous) . ' ' . $dblink->link_status . "\n"; } } } else { if ($json) { $dict['status'] = 'KO'; $dict['submit_url'] = 'http://' . get_server_name() . '/submit.php?url=' . $url; } else { $response = 'KO http://' . get_server_name() . '/submit.php?url=' . $url; } } if ($json) { $response = json_encode($dict) . $ending; } echo $response; memcache_madd($cache_key, $response, 5);
function do_last_subs($status = 'published', $count = 10, $order = 'date') { global $db, $globals, $dblang; if ($globals['mobile'] || $globals['submnm']) { return; } $output = ' '; $key = "last_subs_{$status}-{$count}-{$order_}" . $globals['v']; //if(memcache_mprint($key)) return; echo '<!-- Calculating ' . __FUNCTION__ . ' -->'; $ids = $db->get_col("select link from sub_statuses, subs, links where date > date_sub(now(), interval 48 hour) and status = '{$status}' and sub_statuses.id = origen and subs.id = sub_statuses.id and owner > 0 and not nsfw and link_id = link order by {$order} desc limit {$count}"); if ($ids) { $links = array(); $title = _('en subs de usuarios'); foreach ($ids as $id) { $link = Link::from_db($id); $link->print_subname = true; $link->url = $link->get_permalink(); $link->thumb = $link->has_thumb(); $link->total_votes = $link->votes + $link->anonymous; if ($link->thumb) { $link->thumb_x = round($link->thumb_x / 2); $link->thumb_y = round($link->thumb_y / 2); } $links[] = $link; } $subclass = 'brown'; $url = $globals['base_url_general'] . 'subs'; $vars = compact('links', 'title', 'subclass', 'url'); $output = Haanga::Load('best_stories.html', $vars, true); echo $output; } memcache_madd($key, $output, 300); }
/*** END SIDEBAR ***/ echo '<div id="newswrap">' . "\n"; echo '<div class="topheading"><h2>' . _('noticias más comentadas') . '</h2></div>'; $link = new Link(); // Use memcache if available if ($globals['memcache_host'] && get_current_page() < 4) { $memcache_key = 'topcommented_' . $globals['site_shortname'] . $from . '_' . get_current_page(); } if (!($memcache_key && ($rows = memcache_mget($memcache_key . 'rows')) && ($links = unserialize(memcache_mget($memcache_key))))) { // It's not in memcache $rows = -1; // min(100, $db->get_var("SELECT count(*) FROM links")); $links = $db->get_results("{$sql} LIMIT {$offset},{$page_size}"); if ($memcache_key) { memcache_madd($memcache_key . 'rows', $rows, 1800); memcache_madd($memcache_key, serialize($links), 1800); } } if ($links) { foreach ($links as $dblink) { $link->id = $dblink->link_id; $link->read(); $link->print_summary('short'); } } do_pages($rows, $page_size); echo '</div>'; do_footer_menu(); do_footer(); function print_period_tabs() {
function do_best_comments() { global $db, $globals, $dblang; $foo_link = new Link(); if (memcache_mprint('best_comments')) { return; } $min_date = date("Y-m-d H:i:00", $globals['now'] - 22000); // about 6 hours $res = $db->get_results("select comment_id, comment_order, user_login, link_id, link_uri, link_title, link_comments from comments, links, users where comment_date > '{$min_date}' and comment_karma > 10 and comment_link_id = link_id and comment_user_id = user_id order by comment_karma desc limit 12"); if ($res) { $output = '<div class="vertical-box">'; $output .= '<h4><a href="' . $globals['base_url'] . 'topcomments.php">' . _('mejores comentarios') . '</a></h4><ul>' . "\n"; foreach ($res as $comment) { $foo_link->uri = $comment->link_uri; $link = $foo_link->get_relative_permalink() . get_comment_page_suffix($globals['comments_page_size'], $comment->comment_order, $comment->link_comments) . '#comment-' . $comment->comment_order; $output .= '<li>' . $comment->user_login . ' ' . _('en') . ' <a onmouseout="tooltip.clear(event);" onclick="tooltip.clear(this);" onmouseover="return tooltip.ajax_delayed(event, \'get_comment_tooltip.php\', \'' . $comment->comment_id . '\', 10000);" href="' . $link . '">' . $comment->link_title . '</a></li>' . "\n"; } $output .= '</ul></div>'; echo $output; memcache_madd('best_comments', $output, 300); } }
break; case 4: $select = "SELECT user_id, count(*) as count "; $from_where = " FROM comments, users WHERE user_level not in ('disabled', 'autodisabled') and comment_user_id=user_id GROUP BY comment_user_id"; $order_by = " ORDER BY count DESC "; break; case 5: $select = "SELECT user_id, count(*) as count "; $from_where = " FROM votes, users WHERE vote_type='links' and vote_user_id=user_id GROUP BY vote_user_id"; $order_by = " ORDER BY count DESC "; break; } $sql = "{$select} {$from_where} {$order_by} LIMIT {$page_size}"; if (!($users = unserialize(memcache_mget($sql)))) { $users = $db->get_results($sql); memcache_madd($sql, serialize($users), 3600); } do_header(_('usuarios') . ' | ' . $globals['site_name']); echo '<div id="singlewrap">' . "\n"; echo '<div class="topheading"><h2>' . _('estadísticas de usuarios') . '</h2></div>'; echo '<table class="decorated"><tr>'; // Print headers for ($i = 0; $i < count($items); $i++) { echo '<th class="short">'; if ($i == $sortby) { echo '<span class="info_s">' . $items[$i] . '</span>'; } elseif ($i <= 3 && $i > 0) { // Don't show order by votes or comment // Too much CPU and disk IO consuption echo '<a href="topusers.php?sortby=' . $i . '">' . $items[$i] . '</a>'; } else {
$time_link = ''; } } if (!($memcache_key && ($rows = memcache_mget($memcache_key . 'rows')) && ($links = unserialize(memcache_mget($memcache_key))))) { // It's not in cache, or memcache is disabled $rows = $db->get_var("SELECT count(*) FROM sub_statuses WHERE id = " . SitesMgr::my_id() . " AND {$time_link} status = 'published'"); if ($rows > 0) { $links = $db->get_results("{$sql} LIMIT {$offset},{$page_size}"); if ($memcache_key) { if ($range_values[$from] > 2) { $ttl = 86400; } else { $ttl = 1800; } memcache_madd($memcache_key . 'rows', $rows, $ttl); memcache_madd($memcache_key, serialize($links), $ttl); } } } do_header(_('más votadas') . ' | ' . $globals['site_name'], _('populares')); $globals['tag_status'] = 'published'; do_tabs('main', 'popular'); print_period_tabs(); /*** SIDEBAR ****/ echo '<div id="sidebar">'; do_banner_right(); do_active_stories(); do_banner_promotions(); do_last_subs('published', 5, 'link_votes'); do_best_comments(); do_vertical_tags('published');
function print_relevant_comments($link, $no_page) { global $globals, $db; if ($globals['bot'] || $link->comments < 10) { return; } if ($link->comments > 30 && $globals['now'] - $link->date < 86400 * 4) { $do_cache = true; } else { $do_cache = false; } if ($do_cache) { $key = 'relevant_story_comments_' . $globals['v'] . '_' . $link->id; if (memcache_mprint($key)) { return; } } $karma = intval($globals['comment_highlight_karma'] / 2); $limit = min(15, intval($link->comments / 10)); // For the SQL $extra_limit = $limit * 2; $min_len = 32; $min_karma = max(20, $karma / 2); $min_votes = 4; $check_vote = $link->date - ($globals['now'] - $globals['time_enabled_votes']); $now = intval($globals['now'] / 60) * 60; $res = $db->get_results("select comment_id, comment_order, comment_karma, comment_karma + comment_order * 0.7 as val, length(comment_content) as comment_len, user_id, user_avatar, vote_value from comments LEFT JOIN votes ON ({$check_vote} > 0 and vote_type = 'links' and vote_link_id = comment_link_id and vote_user_id = comment_user_id), users where comment_link_id = {$link->id} and comment_votes >= {$min_votes} and comment_karma > {$min_karma} and length(comment_content) > {$min_len} and comment_user_id = user_id order by val desc limit {$extra_limit}"); function cmp_comment_val($a, $b) { if ($a->val == $b->val) { return 0; } return $a->val < $b->val ? 1 : -1; } if ($res) { $objects = array(); $self = false; $link_url = $link->get_relative_permalink(); foreach ($res as $comment) { // The commenter has voted negative if ($comment->vote_value < 0 && $comment->comment_len > 60) { $comment->val *= 2; // If the link has many negatives ("warned"), add little more weight to criticism if ($link->has_warning) { $comment->val *= 1.5; } } // Gives a little advantage to larger comments $comment->val *= min(1.5, log($comment->comment_len, 10) / 1.8); } usort($res, "cmp_comment_val"); foreach ($res as $comment) { $obj = new stdClass(); $obj->id = $comment->comment_id; $obj->order = $comment->comment_order; $obj->link_id = $link->id; $obj->link_url = $link_url; $obj->user_id = $comment->user_id; $obj->avatar = $comment->user_avatar; $obj->vote = $comment->vote_value; $obj->val = $comment->val; $obj->karma = $comment->comment_karma; $objects[] = $obj; if ($no_page && !$self && $obj->vote < 0 && $link->negatives < $link->votes * 0.5 && (count($objects) < 6 || $comment->comment_karma > $globals['comment_highlight_karma']) && count($res) >= count($objects) * 2) { $self = get_highlighted_comment($obj); $obj->summary = true; } if (count($objects) > $limit) { break; } } if ($no_page && !$self && count($objects) > 5 && $objects[0]->val > $globals['comment_highlight_karma'] * 1.5) { $self = get_highlighted_comment($objects[0]); $objects[0]->summary = true; } $output = Haanga::Load('relevant_comments.html', compact('objects', 'link_url', 'self'), true); echo $output; if ($do_cache) { memcache_madd($key, $output, 300); } } }
function do_last_blogs() { global $db, $globals, $dblang; if ($globals['mobile']) { return; } $foo = new Comment(); $output = ''; $key = 'last_blogs_' . $globals['v']; if (memcache_mprint($key)) { return; } echo '<!-- Calculating ' . __FUNCTION__ . ' -->'; $entries = $db->get_results("select rss.blog_id, rss.user_id, title, url, user_login, user_avatar from rss, users where rss.user_id = users.user_id order by rss.date desc limit 10"); if ($entries) { $objects = array(); $title = _('apuntes de blogs'); $url = $globals['base_url'] . 'rsss.php'; foreach ($entries as $entry) { $obj = new stdClass(); $obj->user_id = $entry->user_id; $obj->avatar = $entry->user_avatar; $obj->title = text_to_summary($entry->title, 75); $obj->link = $entry->url; $obj->username = $entry->user_login; $objects[] = $obj; } $vars = compact('objects', 'title', 'url'); $output = Haanga::Load('last_blogs.html', $vars, true); echo $output; memcache_madd($key, $output, 300); } }
static function store_clicks() { global $globals, $db; if (!self::$clicked) { return false; } $id = self::$clicked; self::$clicked = 0; if (!memcache_menabled()) { $db->query("UPDATE link_clicks SET counter=counter+1 WHERE id = {$id}"); return true; } $key = 'clicks_cache'; $cache = memcache_mget($key); if (!$cache || !is_array($cache)) { $cache = array(); $cache['time'] = $globals['start_time']; $cache[$id] = 1; $in_cache = false; } else { $cache[$id]++; $in_cache = true; } if ($globals['start_time'] - $cache['time'] > 3.0 + rand(0, 100) / 100) { // We use random to minimize race conditions for deleting the cache if ($in_cache && !memcache_mdelete($key)) { memcache_madd($key, array()); syslog(LOG_INFO, "store_clicks: Delete failed"); } ksort($cache); // To avoid transaction's deadlocks $show_errors = $db->show_errors; $db->show_errors = false; // we know there can be lock timeouts :( $tries = 0; // By the way, freaking locking timeouts with few updates per second with this technique while ($tries < 3) { $error = false; $db->transaction(); $total = 0; $r = true; foreach ($cache as $id => $counter) { if ($id > 0 && $counter > 0) { $r = $db->query("INSERT INTO link_clicks (id, counter) VALUES ({$id},{$counter}) ON DUPLICATE KEY UPDATE counter=counter+{$counter}"); // $r = $db->query("UPDATE link_clicks SET counter=counter+$counter WHERE id = $id"); if (!$r) { break; } $total += $counter; } } if ($r) { $db->commit(); $tries = 100000; // Stop it } else { $tries++; syslog(LOG_INFO, "failed {$tries} attempts in store_clicks"); $db->rollback(); } } $db->show_errors = $show_errors; } else { memcache_madd($key, $cache); } }
// Default $sql = "SELECT SQL_CACHE link_id, link_votes-link_negatives as votes FROM links WHERE link_status = 'published' ORDER BY votes DESC "; $time_link = ''; } } if (!($memcache_key && ($rows = memcache_mget($memcache_key.'rows')) && ($links = memcache_mget($memcache_key))) ) { // It's not in cache, or memcache is disabled $rows = $db->get_var("SELECT count(*) FROM links WHERE $time_link link_status = 'published'"); if ($rows > 0) { $links = $db->get_results("$sql LIMIT $offset,$page_size"); if ($memcache_key) { if ($range_values[$from] > 2) $ttl = 86400; else $ttl = 1800; memcache_madd($memcache_key.'rows', $rows, $ttl); memcache_madd($memcache_key, $links, $ttl); } } } do_header(_('más votadas') . ' | ' . _('menéame')); $globals['tag_status'] = 'published'; do_tabs('main', 'popular'); print_period_tabs(); /*** SIDEBAR ****/ echo '<div id="sidebar">'; do_banner_right(); do_banner_promotions(); do_best_comments();
if (!is_dir($globals['cache_dir'])) { ping_error('cache directory not available'); } if (!is_writeable($globals['cache_dir'])) { ping_error('cache directory not writeble'); } // Check access to DB $db->connect(); if (!$db->connected) { // Force DB access ping_error('DB not available'); } // Check memcache if (memcache_menabled()) { $data = array(1, 2, 3); memcache_madd('ping', $data, 10); $result = memcache_mget('ping'); if (!$result || $data != $result) { ping_error('memcache failed'); } } } echo "pong\n"; function ping_error($log) { header('HTTP/1.1 500 Server error'); if (!empty($log)) { echo "ERROR ping: {$log}"; syslog(LOG_INFO, "ERROR ping: {$log}"); } die;