User:Robchurch/TopContributors

<?php
/**
 * Parser hook extension adds a <topcontributors /> tag to the parser,
 * giving a list of the ten most active users on a wiki
 *
 * @addtogroup Extensions
 * @author Rob Church <robchur@gmail.com>
 */
if ( defined( 'MEDIAWIKI' ) ) {

	$wgExtensionCredits['parserhook'][] = array(
		'name' => 'Top Contributors',
		'author' => 'Rob Church',
		'description' => 'Lists the ten most active contributors to a wiki',
	);

	$wgHooks['ParserFirstCallInit'][] = function( Parser &$parser ) {
		$parser->setHook( 'topcontributors', '\efTopContributors' );
	};

	/**
	 * Hook callback
	 *
	 * @param string $input
	 * @param array $args
	 * @param Parser $parser
	 * @return string
	 */
	function efTopContributors( $input, $args, $parser ) {
		$dbr = wfGetDB( DB_SLAVE );
		$res = $dbr->select(
			'revision',
			array( 'rev_user', 'rev_user_text', 'COUNT(*) AS `count`' ),
			array(),
			__METHOD__,
			array(
				'GROUP BY' => 'rev_user_text',
				'ORDER BY' => 'count DESC',
				'LIMIT' => '10',
			)
		);
		if ( $res && $dbr->numRows( $res ) > 0 ) {
			$out = '<div class="mw-top-contributors"><ul>';
			while ( $row = $dbr->fetchObject( $res ) ) {
				$out .= "\n<li>" . Linker::userLink( $row->rev_user, $row->rev_user_text )
					. ' [' . $row->count . ']</li>';
			}
			$dbr->freeResult( $res );
			return $out . "\n</ul></div>";
		} else {
			return '';
		}
	}
} else {
	echo( "This file is an extension to the MediaWiki software, and cannot be used standalone.\n" );
	exit( 1 );
}