r44342 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r44341‎ | r44342 | r44343 >
Date:15:30, 9 December 2008
Author:yaron
Status:deferred
Tags:
Comment:
New directory for math-function formats: currently, sum, average, min and max
Modified paths:
  • /trunk/extensions/SemanticResultFormats/Math (added) (history)
  • /trunk/extensions/SemanticResultFormats/Math/SRF_Math.php (added) (history)

Diff [purge]

Index: trunk/extensions/SemanticResultFormats/Math/SRF_Math.php
@@ -0,0 +1,68 @@
 2+<?php
 3+
 4+/**
 5+ * Various mathematical functions - sum, average, min and max.
 6+ *
 7+ * @file
 8+ * @ingroup SemanticResultFormats
 9+ * @author Yaron Koren
 10+ * @author Nathan Yergler
 11+ */
 12+
 13+if (!defined('MEDIAWIKI')) die();
 14+
 15+class SRFMath extends SMWResultPrinter {
 16+
 17+ public function getResult($results, $params, $outputmode) {
 18+ $this->readParameters($params, $outputmode);
 19+ return $this->getResultText($results, SMW_OUTPUT_HTML);
 20+ }
 21+
 22+ protected function getResultText($res, $outputmode) {
 23+ global $smwgIQRunningNumber, $wgUser;
 24+ $skin = $wgUser->getSkin();
 25+
 26+ // initialize all necessary variables
 27+ $sum = 0;
 28+ $count = 0;
 29+ $min = '';
 30+ $max = '';
 31+
 32+ while ( $row = $res->getNext() ) {
 33+ $last_col = array_pop($row);
 34+ $number_value = array_pop($last_col->getContent());
 35+ // if the property isn't of type Number, just ignore
 36+ // this row
 37+ if ( $number_value instanceof SMWNumberValue ) {
 38+ $count++;
 39+ $num = $number_value->getNumericValue();
 40+ if ($this->mFormat == 'sum' || $this->mFormat == 'average') {
 41+ $sum += $num;
 42+ } elseif ($this->mFormat == 'min') {
 43+ if ($min == '' || $num < $min)
 44+ $min = $num;
 45+ } elseif ($this->mFormat == 'max') {
 46+ if ($max == '' || $num > $max)
 47+ $max = $num;
 48+ }
 49+ }
 50+ }
 51+ // if there were no results, display a blank
 52+ if ($count == 0) {
 53+ $result = '';
 54+ } elseif ($this->mFormat == 'sum') {
 55+ $result = $sum;
 56+ } elseif ($this->mFormat == 'average') {
 57+ $result = $sum / $count;
 58+ } elseif ($this->mFormat == 'min') {
 59+ $result = $min;
 60+ } elseif ($this->mFormat == 'max') {
 61+ $result = $max;
 62+ } else {
 63+ $result = '';
 64+ }
 65+
 66+ return array($result, 'noparse' => 'true', 'isHTML' => 'true');
 67+ }
 68+
 69+}

Status & tagging log