Extension:Metrics/SpecialMetrics.php

From MediaWiki.org
Jump to navigation Jump to search
<?php
$wgExtensionCredits['specialpage'][] = array(
        'name' => 'Metrics',
        'version' => 0.1,
        'author' => 'Chris Reigrut',
        'url' => 'http://www.mediawiki.org/wiki/Extension:Metrics',
        'description' => 'Displays metrics that have been predefined.',
);

$wgExtensionFunctions[] = "wfExtensionSpecialMetrics";
$dir = dirname(__FILE__) . '/';
$wgExtensionMessagesFiles['Metrics'] = $dir . 'SpecialMetrics.i18n.php';

function wfExtensionSpecialMetrics() {
	global $wgMessageCache;
	
	require_once('includes/SpecialPage.php');
	SpecialPage::addPage(new SpecialPage('Metrics'));
	
	$wgMessageCache->addMessages(array('metrics' => 'Metrics'));
	wfLoadExtensionMessages('Metrics');
}

function wfSpecialMetrics() {
	global $wgOut, $wgRequest;
	$dbr =& wfGetDB( DB_READ );
	
	$names = getMetricNames($dbr);
	
	if (is_null( $wgRequest->getVal( 'submit' ) )) {
		// No submit - only display form
		createForm($names);
	} else {
		// Form submitted - display form and results
		$selectedNames = $wgRequest->getArray('metricNames', array());
		sort($selectedNames);
		createForm($names, $selectedNames);
		displayResults($dbr, $selectedNames);
	}
}

function getMetricNames($dbr) {
	$names = array();
		
	$result = $dbr->select(
		array('metric'),
		array( 'distinct metric_name',),
		array(),
		'metrics',
		array( 'ORDER BY'=>'metric_name ') );
	
	while ($row = $dbr->fetchObject($result)) {
		$names[] = $row->metric_name;
	}

	return $names;
}

function createForm($names, $selectedNames = array()) {
	global $wgOut, $wgRequest;
	//displayStyle();
	
	$titleObj = Title::makeTitle( NS_SPECIAL, 'Metrics' );
	$action = $titleObj->escapeLocalURL();
	$wgOut->addHTML('<form enctype="multipart/form-data" method="post"  action="'.$action.'">');
	$wgOut->addHTML('<fieldset><legend>' . wfMsg('metrics-form-legend') . '</legend>');
	
	$wgOut->addHTML('<div id="metricNames">');
	$wgOut->addHTML('<select name="metricNames[]" multiple="multiple">');
  	foreach( $names as $name ) {
  		if (in_array($name,$selectedNames)) {
  			$selected = ' selected="selected"';
  		} else {
  			$selected = '';
  		}
		$wgOut->addHTML("<option value=\"{$name}\"{$selected}>$name</option>");
	}
	$wgOut->addHTML('</select>');
	$wgOut->addHTML('</div>');
	
	$fromDate = @date_create($wgRequest->getVal('fromDate'));
	$toDate = @date_create($wgRequest->getVal('toDate'));
	
	$wgOut->addHTML('<div id="metricDates">');
	$wgOut->addHTML('<div class="metricDate"><label>' . wfMsg( 'metrics-from-label' ) . '</label><input name="fromDate" type="date" value="' . date_format($fromDate, 'n/j/Y') . '"/></div>');
	$wgOut->addHTML('<div class="metricDate"><label>' . wfMsg( 'metrics-to-label' ) . '</label><input name="toDate" type="date" value="' . date_format($toDate, 'n/j/Y') . '"/></div>');
	$wgOut->addHTML('</div>');
	
	$wgOut->addHTML('<div id="metricSubmit"><input type="submit" name="submit" value="'.wfMsg( 'metrics-form-button' ).'" /></div>');

	$wgOut->addHTML('</fieldset>');
	$wgOut->addHTML('</form>');
}

function displayResults($dbr, $selectedNames) {
	global $wgOut, $wgRequest;

	// Get results
	//list($limit, $offset) = $wgRequest->getLimitOffset();
	//$limit = 10;
	//$offset = 0;
	if (is_null($selectedNames) || count($selectedNames) == 0) {
		$wgOut->addWikiText(wfMsg( 'metrics-none-selected-error' ));
	} else {
		$fromDate = @date_create($wgRequest->getVal('fromDate'));
		$toDate = @date_create($wgRequest->getVal('toDate'));
		
		$result = $dbr->select(
			array('metric'),
			array( 'metric_date', 'metric_name', 'metric_value',),
			array( createWhereIn($dbr, 'metric_name', $selectedNames),
			       "metric_date BETWEEN '" . date_format($fromDate, 'Y-m-d') . "' AND '" . date_format($toDate, 'Y-m-d') . "'" ),
			'metrics',
			array( 'ORDER BY'=>'metric_date DESC, metric_name')
		 );
	
		// Output results
		if ( 0 == $dbr->numRows( $result ) ) {
			$wgOut->addWikiText(wfMsg( 'metrics-no-data-error' ));
		} else {
			$rows = array();
		
			$wgOut->addHTML('<table id="metricResults" class="sortable">');
			$wgOut->addHTML('<tr>');
			$wgOut->addHTML('<th>Date</th>');
			foreach( $selectedNames as $name ) {
				$wgOut->addHTML('<th>' . $name . '</th>');
			}
			$wgOut->addHTML('</tr>');
			
			$currentRowDate = NULL;
			$currentColumnIndex = 0;
			while ($row = $dbr->fetchObject($result)) {
				if ($currentRowDate != $row->metric_date) {
					while ($currentColumnIndex < count($selectedNames) && $row->metric_name != $selectedNames[$currentColumnIndex++]) {
						$wgOut->addHTML('<td>&nbsp;</td>');
					}
					if ($currentRowDate != NULL) {
						$wgOut->addHTML('</tr>');
					}
					$currentRowDate = $row->metric_date;
					$currentColumnIndex = 0;
					$wgOut->addHTML('<tr>');
					$wgOut->addHTML('<th>' . $currentRowDate . '</th>');
				}
				while ($currentColumnIndex < count($selectedNames) && $row->metric_name != $selectedNames[$currentColumnIndex++]) {
					$wgOut->addHTML('<td>&nbsp;</td>');
				}
				$wgOut->addHTML('<td>' . $row->metric_value . '</td>');
			}
			while ($currentColumnIndex < count($selectedNames) && $row->metric_name != $selectedNames[$currentColumnIndex++]) {
				$wgOut->addHTML('<td>&nbsp;</td>');
			}
			
			if ($currentRowDate != NULL) {
				$wgOut->addHTML('</tr>');
			}
				
			$wgOut->addHTML('</table>');
			
			$dbr->freeResult($result);
			$wgOut->addHTML($prevnext);
		}
	}
}

function createWhereIn($dbr, $field, $setArray) {
	$whereSet = '';
	foreach ($setArray as $key => $val) {
		if (strlen($whereSet)>0) {
			$whereSet .= ',';
		}
   	$whereSet .= $dbr->addQuotes($val); 
   }
   return "{$field} IN ({$whereSet})";
}

function displayStyle() {
	global $wgOut;
	    $style .= <<<ENDSTYLE
<style>	    
#metricNames {
	float: left;
}

.metricDate label {
	float: left;
	text-align: right;
	width: 40px;
	margin: 0 10px;
}
	

#metricSubmit {
	float: left;
	padding-left: 60px;
	margin-top: 20px;
}
  
</style>  	
ENDSTYLE;
	$wgOut->addHTML($style);
}