From 8dc2b92d667f1401ab9f1315a36add61658f899c Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Thu, 6 May 2021 10:26:09 +0200 Subject: Moderize Database handling * Auto-convert to utf8mb4_unicode_520_ci * Use foreach instead of while to loop over results * Drop useless statement caching * Keep emulated prepares, as we sometimes loop over nested queries --- modules-available/statistics/pages/list.inc.php | 2 +- modules-available/statistics/pages/machine.inc.php | 8 ++++---- modules-available/statistics/pages/projectors.inc.php | 2 +- modules-available/statistics/pages/replace.inc.php | 2 +- modules-available/statistics/pages/summary.inc.php | 12 ++++++------ 5 files changed, 13 insertions(+), 13 deletions(-) (limited to 'modules-available/statistics/pages') diff --git a/modules-available/statistics/pages/list.inc.php b/modules-available/statistics/pages/list.inc.php index e9af994a..d3c8069e 100644 --- a/modules-available/statistics/pages/list.inc.php +++ b/modules-available/statistics/pages/list.inc.php @@ -59,7 +59,7 @@ class SubPage // Only make client clickable if user is allowed to view details page $detailsAllowedLocations = User::getAllowedLocations("machine.view-details"); $location = self::buildLocationLookup(); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $row) { if ($singleMachine === 'none') { $singleMachine = $row['machineuuid']; } else { diff --git a/modules-available/statistics/pages/machine.inc.php b/modules-available/statistics/pages/machine.inc.php index 87e8ad14..677ff580 100644 --- a/modules-available/statistics/pages/machine.inc.php +++ b/modules-available/statistics/pages/machine.inc.php @@ -36,7 +36,7 @@ class SubPage 'end' => $row['logintime'] + 300, )); $session = false; - while ($r = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $r) { if ($session === false || abs($session['dateline'] - $row['logintime']) > abs($r['dateline'] - $row['logintime'])) { $session = $r; } @@ -201,7 +201,7 @@ class SubPage array('screen' => DeviceType::SCREEN, 'uuid' => $uuid)); $client['screens'] = array(); $ports = array(); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $row) { if ($row['disconnecttime'] != 0) continue; $ports[] = $row['connector']; @@ -227,7 +227,7 @@ class SubPage $spans['graph'] = ''; $last = false; $first = true; - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $row) { if (!$client['isclient'] && $row['typeid'] === Statistics::SESSION_LENGTH) continue; // Don't differentiate between session and idle for non-clients if ($first && $row['dateline'] > $cutoff && $client['lastboot'] > $cutoff) { @@ -325,7 +325,7 @@ class SubPage . ' WHERE machineuuid = :uuid ORDER BY logid DESC LIMIT 25', array('uuid' => $client['machineuuid'])); $count = 0; $log = array(); - while ($row = $lres->fetch(PDO::FETCH_ASSOC)) { + foreach ($lres as $row) { if (substr($row['description'], -5) === 'on :0' && strpos($row['description'], 'root logged') === false) { continue; } diff --git a/modules-available/statistics/pages/projectors.inc.php b/modules-available/statistics/pages/projectors.inc.php index cc808cf0..97a21ebd 100644 --- a/modules-available/statistics/pages/projectors.inc.php +++ b/modules-available/statistics/pages/projectors.inc.php @@ -52,7 +52,7 @@ class SubPage 'screen' => DeviceType::SCREEN, )); $data = array( - 'projectors' => $res->fetchAll(PDO::FETCH_ASSOC) + 'projectors' => $res->fetchAll() ); Render::addTemplate('projector-list', $data); } diff --git a/modules-available/statistics/pages/replace.inc.php b/modules-available/statistics/pages/replace.inc.php index 9c16aed7..77f311ea 100644 --- a/modules-available/statistics/pages/replace.inc.php +++ b/modules-available/statistics/pages/replace.inc.php @@ -106,7 +106,7 @@ class SubPage FROM machine old INNER JOIN machine new ON (old.clientip = new.clientip AND old.lastseen < new.firstseen AND old.lastseen > $oldCutoff AND new.firstseen > $newCutoff) ORDER BY oldhost ASC, oldip ASC"); $list = []; - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $row) { $row['oldlastseen_s'] = Util::prettyTime($row['oldlastseen']); $row['newfirstseen_s'] = Util::prettyTime($row['newfirstseen']); $list[] = $row; diff --git a/modules-available/statistics/pages/summary.inc.php b/modules-available/statistics/pages/summary.inc.php index ce67070e..4599b8b6 100644 --- a/modules-available/statistics/pages/summary.inc.php +++ b/modules-available/statistics/pages/summary.inc.php @@ -67,7 +67,7 @@ class SubPage $points1 = array('data' => array(), 'label' => 'Online', 'fillColor' => '#efe', 'strokeColor' => '#aea', 'pointColor' => '#7e7', 'pointStrokeColor' => '#fff', 'pointHighlightFill' => '#fff', 'pointHighlightStroke' => '#7e7'); $points2 = array('data' => array(), 'label' => 'In use', 'fillColor' => '#fee', 'strokeColor' => '#eaa', 'pointColor' => '#e77', 'pointStrokeColor' => '#fff', 'pointHighlightFill' => '#fff', 'pointHighlightStroke' => '#e77'); $sum = 0; - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $row) { $x = explode('#', $row['data']); if ($sum === 0) { $labels[] = date('H:i', $row['dateline']); @@ -103,7 +103,7 @@ class SubPage $lines = array(); $json = array(); $id = 0; - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $row) { if (empty($row['systemmodel'])) { continue; } @@ -131,7 +131,7 @@ class SubPage $res = Database::simpleQuery("SELECT mbram, Count(*) AS `count` FROM machine m $join WHERE $where GROUP BY mbram", $args); $lines = array(); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $row) { $gb = (int)ceil($row['mbram'] / 1024); for ($i = 1; $i < count(StatisticsFilter::SIZE_RAM); ++$i) { if (StatisticsFilter::SIZE_RAM[$i] < $gb) { @@ -178,7 +178,7 @@ class SubPage WHERE $where GROUP BY kvmstate ORDER BY `count` DESC", $args); $lines = array(); $json = array(); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $row) { $lines[] = $row; $json[] = array( 'color' => isset($colors[$row['kvmstate']]) ? $colors[$row['kvmstate']] : '#000', @@ -198,7 +198,7 @@ class SubPage $res = Database::simpleQuery("SELECT id44mb, Count(*) AS `count` FROM machine m $join WHERE $where GROUP BY id44mb", $args); $lines = array(); $total = 0; - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $row) { $total += $row['count']; $gb = (int)ceil($row['id44mb'] / 1024); for ($i = 1; $i < count(StatisticsFilter::SIZE_ID44); ++$i) { @@ -251,7 +251,7 @@ class SubPage . " WHERE firstseen > :cutoff AND $where ORDER BY firstseen DESC LIMIT 32", $args); $rows = array(); $count = 0; - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $row) { if (empty($row['hostname'])) { $row['hostname'] = $row['clientip']; } -- cgit v1.2.3-55-g7522