User:Kleptomik/Sandbox
From MediaWiki.org
A small hack that changes the Metrics extension to generate Google Chart code (I use the Gchart4mw extension).
<?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 ASC, 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>'); $theChart = '<br /><b>Google Chart Code</b>:'; $theChart .= '<pre><lines title="'; if((count($selectedNames)) > 1) { $theChart .= 'GRAPH_NAME_HERE'; } else { $theChart .= $selectedNames[0]; } $theChart .= '"<br /> ymin=0 ymax=500 '; $theChart .= '<br />colors=FF0000'; if((count($selectedNames)) > 1) { $theChart .= ',00FF00,0000FF,993300,FFFF00,CCFFFF,FF66FF,CCCCCC,006600,336666 '; } $theChart .= '<br />ylabel=10 xlabel '; $theChart .= 'grid=xy size=500x600'; if((count($selectedNames)) > 1) { $theChart .= ' legend'; } $theChart .= '><br />'; foreach( $selectedNames as $name ) { $wgOut->addHTML('<th>' . $name . '</th>'); if((count($selectedNames)) > 1) { $theChart .= ',' . $name; } } $wgOut->addHTML('</tr>'); $currentRowDate = NULL; $currentColumnIndex = 0; $highMark = 0; while ($row = $dbr->fetchObject($result)) { if ($currentRowDate != $row->metric_date) { while ($currentColumnIndex < count($selectedNames) && $row->metric_name != $selectedNames[$currentColumnIndex++]) { $wgOut->addHTML('<td> </td>'); } if ($currentRowDate != NULL) { $wgOut->addHTML('</tr>'); } $currentRowDate = $row->metric_date; $currentColumnIndex = 0; $wgOut->addHTML('<tr>'); $wgOut->addHTML('<th>' . $currentRowDate . '</th>'); $theChart .= '<br />'; $theChart .= preg_replace('/[0-9][0-9][0-9][0-9]-/', '', $currentRowDate); } while ($currentColumnIndex < count($selectedNames) && $row->metric_name != $selectedNames[$currentColumnIndex++]) { $wgOut->addHTML('<td> </td>'); } $wgOut->addHTML('<td>' . $row->metric_value . '</td>'); $theChart .= ', ' . $row->metric_value; if($row->metric_value > $highMark) { $highMark = $row->metric_value; } } while ($currentColumnIndex < count($selectedNames) && $row->metric_name != $selectedNames[$currentColumnIndex++]) { $wgOut->addHTML('<td> </td>'); } if ($currentRowDate != NULL) { $wgOut->addHTML('</tr>'); } $wgOut->addHTML('</table>'); $theChart .= '<br /></lines></pre>'; $theChart = preg_replace('/ymax=500/', 'ymax=' . ($highMark + ($highMark / 5)), $theChart ); $dbr->freeResult($result); $wgOut->addHTML($theChart); } } } 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); }