Jump to content

Topic on Extension talk:Ratings/Flow

Vlsergey (talkcontribs)
<?php
/**
 * Get top N elements for some rating
 *
 * @file
 * @ingroup Extensions
 */
if ( !defined( 'MEDIAWIKI' ) ) {
	die();
}

$wgHooks['ParserFirstCallInit'][] = 'wfRatingsTop';

/**
 * Register the new <ratingstop /> parser hook with the Parser.
 *
 * @param $parser Parser: instance of Parser (not necessarily $wgParser)
 * @return Boolean: true
 */
function wfRatingsTop( &$parser ) {
	$parser->setHook( 'ratingstop', 'getRatingsTop' );
	return true;
}

/**
 * Get top N elements for some rating
 *
 * @return String: HTML
 */
function getRatingsTop( $input, $args, $parser ) {
	global $wgMemc, $wgUploadPath;


	$dbr = wfGetDB( DB_SLAVE );

	if (!isset($args['tag'])) {
		return "<b>Tag must be specified</b>";
	}
	$tag = $args['tag'];
	$limit = !isset($args['limit']) ? 10 : intval($args['limit']);
	$desc = isset($args['desc']) ? ' DESC' : ''; 

	$res = $dbr->select(
			array( 'votes', 'vote_props', 'page' ),
			array(
					'page_title', 'avg(vote_value) vv', 'count(vote_value) vc'
			),
			array(
					'page_id=vote_page_id','vote_prop_id=prop_id','prop_name' => $tag,
			),
			__METHOD__,
			array( 'GROUP BY' => 'page_namespace, page_title', 'ORDER BY' => 'vv' . $desc . ', vc DESC', 'LIMIT' => $limit )
	);

	$ratingsTopItems = array();
	foreach ( $res as $row ) {
		$ratingsTopItems[] = array(
				'page_namespace' => $row->page_namespace,
				'page_title' => $row->page_title,
				'vv' => $row->vv,
				'vc' => $row->vc
		);
	}

	$comments = '<ul>';

	foreach ( $ratingsTopItems as $item ) {
		$title = Title::makeTitle(
				$item['page_namespace'],
				$item['page_title']
		);

		$comments .= '<li class="ratings-top-item" data-mark="' . round($item['vv'] + 1, 2) . '" ' . 
				'data-count="' . $item['vc'] . '" >';
		$comments .= '<a href="' . $title->getFullURL() . '"' .
				' title="' . $title->getText() . '">' . $title->getText() . '</a>';
		$comments .= '</li>';
	}
	$comments .= '</ul>';

	return $comments;
}
Reply to "Top10 ratings output"