From 9ae2c70073490a253ccf2fcb567d57b07811389b Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Thu, 24 Jan 2019 17:31:38 +0100 Subject: [statistics] Minor refactoring (PHP >= 5.6) --- modules-available/statistics/inc/filter.inc.php | 16 ++++--- modules-available/statistics/page.inc.php | 58 ++++++++++++------------- 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/modules-available/statistics/inc/filter.inc.php b/modules-available/statistics/inc/filter.inc.php index 3ccea2c3..46de467b 100644 --- a/modules-available/statistics/inc/filter.inc.php +++ b/modules-available/statistics/inc/filter.inc.php @@ -14,6 +14,13 @@ class Filter public $operator; public $argument; + private static $keyCounter = 0; + + public static function getNewKey($colname) + { + return $colname . '_' . (self::$keyCounter++); + } + public function __construct($column, $operator, $argument = null) { $this->column = trim($column); @@ -24,8 +31,7 @@ class Filter /* returns a where clause and adds needed operators to the passed array */ public function whereClause(&$args, &$joins) { - global $unique_key; - $key = $this->column . '_arg' . ($unique_key++); + $key = Filter::getNewKey($this->column); $addendum = ''; /* check if we have to do some parsing*/ @@ -226,8 +232,7 @@ class StateFilter extends Filter $map = [ 'on' => ['IDLE', 'OCCUPIED'], 'off' => ['OFFLINE'], 'idle' => ['IDLE'], 'occupied' => ['OCCUPIED'], 'standby' => ['STANDBY'] ]; $neg = $this->operator == '!=' ? 'NOT ' : ''; if (array_key_exists($this->argument, $map)) { - global $unique_key; - $key = $this->column . '_arg' . ($unique_key++); + $key = Filter::getNewKey($this->column); $args[$key] = $map[$this->argument]; return " machine.state $neg IN ( :$key ) "; } else { @@ -259,8 +264,7 @@ class LocationFilter extends Filter if ($this->argument === 0) { return "machine.locationid IS $neg NULL"; } else { - global $unique_key; - $key = $this->column . '_arg' . ($unique_key++); + $key = Filter::getNewKey($this->column); if ($recursive) { $args[$key] = array_keys(Location::getRecursiveFlat($this->argument)); } else { diff --git a/modules-available/statistics/page.inc.php b/modules-available/statistics/page.inc.php index 2d86615e..c9a0cac5 100644 --- a/modules-available/statistics/page.inc.php +++ b/modules-available/statistics/page.inc.php @@ -1,7 +1,6 @@ =', '=', '<', '>']; + const OP_STRCMP = ['!~', '~', '=', '!=']; public static $columns; private $query; @@ -27,123 +26,120 @@ class Page_Statistics extends Page */ private $haveSubpage; - /* PHP sucks, no static, const array definitions... Or am I missing something? */ - public function initConstants() + /** + * Do this here instead of const since we need to check for available modules while building array. + */ + public static function initConstants() { - Page_Statistics::$op_nominal = ['!=', '=']; - Page_Statistics::$op_ordinal = ['!=', '<=', '>=', '=', '<', '>']; - Page_Statistics::$op_stringcmp = ['!~', '~', '=', '!=']; Page_Statistics::$columns = [ 'machineuuid' => [ - 'op' => Page_Statistics::$op_nominal, + 'op' => Page_Statistics::OP_NOMINAL, 'type' => 'string', 'column' => true, ], 'macaddr' => [ - 'op' => Page_Statistics::$op_nominal, + 'op' => Page_Statistics::OP_NOMINAL, 'type' => 'string', 'column' => true, ], 'firstseen' => [ - 'op' => Page_Statistics::$op_ordinal, + 'op' => Page_Statistics::OP_ORDINAL, 'type' => 'date', 'column' => true, ], 'lastseen' => [ - 'op' => Page_Statistics::$op_ordinal, + 'op' => Page_Statistics::OP_ORDINAL, 'type' => 'date', 'column' => true, ], 'logintime' => [ - 'op' => Page_Statistics::$op_ordinal, + 'op' => Page_Statistics::OP_ORDINAL, 'type' => 'date', 'column' => true, ], 'realcores' => [ - 'op' => Page_Statistics::$op_ordinal, + 'op' => Page_Statistics::OP_ORDINAL, 'type' => 'int', 'column' => true, ], 'systemmodel' => [ - 'op' => Page_Statistics::$op_stringcmp, + 'op' => Page_Statistics::OP_STRCMP, 'type' => 'string', 'column' => true, ], 'cpumodel' => [ - 'op' => Page_Statistics::$op_stringcmp, + 'op' => Page_Statistics::OP_STRCMP, 'type' => 'string', 'column' => true, ], 'hddgb' => [ - 'op' => Page_Statistics::$op_ordinal, + 'op' => Page_Statistics::OP_ORDINAL, 'type' => 'int', 'column' => false, 'map_sort' => 'id44mb' ], 'gbram' => [ - 'op' => Page_Statistics::$op_ordinal, + 'op' => Page_Statistics::OP_ORDINAL, 'type' => 'int', 'map_sort' => 'mbram', 'column' => false, ], 'kvmstate' => [ - 'op' => Page_Statistics::$op_nominal, + 'op' => Page_Statistics::OP_NOMINAL, 'type' => 'enum', 'column' => true, 'values' => ['ENABLED', 'DISABLED', 'UNSUPPORTED'] ], 'badsectors' => [ - 'op' => Page_Statistics::$op_ordinal, + 'op' => Page_Statistics::OP_ORDINAL, 'type' => 'int', 'column' => true ], 'clientip' => [ - 'op' => Page_Statistics::$op_nominal, + 'op' => Page_Statistics::OP_NOMINAL, 'type' => 'string', 'column' => true ], 'hostname' => [ - 'op' => Page_Statistics::$op_stringcmp, + 'op' => Page_Statistics::OP_STRCMP, 'type' => 'string', 'column' => true ], 'subnet' => [ - 'op' => Page_Statistics::$op_nominal, + 'op' => Page_Statistics::OP_NOMINAL, 'type' => 'string', 'column' => false ], 'currentuser' => [ - 'op' => Page_Statistics::$op_nominal, + 'op' => Page_Statistics::OP_NOMINAL, 'type' => 'string', 'column' => true ], 'state' => [ - 'op' => Page_Statistics::$op_nominal, + 'op' => Page_Statistics::OP_NOMINAL, 'type' => 'enum', 'column' => true, 'values' => ['occupied', 'on', 'off', 'idle', 'standby'] ], 'runtime' => [ - 'op' => Page_Statistics::$op_ordinal, + 'op' => Page_Statistics::OP_NOMINAL, 'type' => 'int', 'column' => true ], ]; if (Module::isAvailable('locations')) { Page_Statistics::$columns['location'] = [ - 'op' => Page_Statistics::$op_stringcmp, + 'op' => Page_Statistics::OP_STRCMP, 'type' => 'enum', 'column' => false, 'values' => array_keys(Location::getLocationsAssoc()), ]; } - /* TODO ... */ } protected function doPreprocess() { - $this->initConstants(); User::load(); if (!User::isLoggedIn()) { Message::addError('main.no-permission'); @@ -1127,3 +1123,5 @@ class Page_Statistics extends Page ), true); } } + +Page_Statistics::initConstants(); -- cgit v1.2.3-55-g7522