summaryrefslogtreecommitdiffstats
path: root/inc
diff options
context:
space:
mode:
authorSimon Rettberg2017-12-01 13:42:04 +0100
committerSimon Rettberg2017-12-01 13:42:04 +0100
commit5393703f6e1485ddff94a50f63bcdd216ab629f4 (patch)
treece2db7130302bd4fb78b170e1fbd75496a1b4510 /inc
parentMerge remote-tracking branch 'origin/permission-manager' into permission-manager (diff)
parent[roomplanner] Sort already placed machines to the bottom (diff)
downloadslx-admin-5393703f6e1485ddff94a50f63bcdd216ab629f4.tar.gz
slx-admin-5393703f6e1485ddff94a50f63bcdd216ab629f4.tar.xz
slx-admin-5393703f6e1485ddff94a50f63bcdd216ab629f4.zip
Merge branch 'master' into permission-manager
Diffstat (limited to 'inc')
-rw-r--r--inc/database.inc.php17
-rw-r--r--inc/paginate.inc.php23
-rw-r--r--inc/pagination.inc.php47
-rw-r--r--inc/render.inc.php5
4 files changed, 29 insertions, 63 deletions
diff --git a/inc/database.inc.php b/inc/database.inc.php
index 150f828a..79c945b8 100644
--- a/inc/database.inc.php
+++ b/inc/database.inc.php
@@ -17,6 +17,7 @@ class Database
private static $statements = array();
private static $returnErrors;
private static $lastError = false;
+ private static $explainList = array();
/**
* Connect to the DB if not already connected.
@@ -37,6 +38,11 @@ class Database
return false;
Util::traceError('Connecting to the local database failed: ' . $e->getMessage());
}
+ if (CONFIG_DEBUG) {
+ register_shutdown_function(function() {
+ self::examineLoggedQueries();
+ });
+ }
return true;
}
@@ -115,7 +121,7 @@ class Database
{
self::init();
if (CONFIG_DEBUG && preg_match('/^\s*SELECT/is', $query)) {
- self::explainQuery($query, $args);
+ self::$explainList[] = [$query, $args];
}
// Support passing nested arrays for IN statements, automagically refactor
self::handleArrayArgument($query, $args);
@@ -141,6 +147,13 @@ class Database
return false;
}
+ public static function examineLoggedQueries()
+ {
+ foreach (self::$explainList as $e) {
+ self::explainQuery($e[0], $e[1]);
+ }
+ }
+
private static function explainQuery($query, $args)
{
$res = self::simpleQuery('EXPLAIN ' . $query, $args, true);
@@ -155,7 +168,7 @@ class Database
$lens[$key] = strlen($key);
}
foreach ($rows as $row) {
- if (!$log && preg_match('/filesort|temporary/i', $row['Extra'])) {
+ if (!$log && $row['rows'] > 20 && preg_match('/filesort|temporary/i', $row['Extra'])) {
$log = true;
}
foreach ($row as $key => $col) {
diff --git a/inc/paginate.inc.php b/inc/paginate.inc.php
index 91f52077..cdb4adf1 100644
--- a/inc/paginate.inc.php
+++ b/inc/paginate.inc.php
@@ -38,11 +38,9 @@ class Paginate
if (preg_match('/(\-\-|;)(\s|[^\'"`])*$/is', $query)) {
Util::traceError("Your query must not end in a comment or semi-colon!");
}
- $query .= ' LIMIT ' . ($this->currentPage * $this->perPage) . ', ' . $this->perPage;
- // Use SQL_CALC_FOUND_ROWS
- if (!preg_match('/^\s*SELECT\s+SQL_CALC_FOUND_ROWS/is', $query)) {
- $query = preg_replace('/^\s*SELECT/is', 'SELECT SQL_CALC_FOUND_ROWS ', $query);
- }
+ // Don't use SQL_CALC_FOUND_ROWS as it leads to filesort frequently thus being slower than two queries
+ // See https://www.percona.com/blog/2007/08/28/to-sql_calc_found_rows-or-not-to-sql_calc_found_rows/
+
} else {
Util::traceError('Unsupported database engine');
}
@@ -55,7 +53,7 @@ class Paginate
if (substr($url, -1) !== '&') $url .= '&';
}
//
- $this->query =$query;
+ $this->query = $query;
$this->url = $url;
}
@@ -64,11 +62,14 @@ class Paginate
*/
public function exec($args = array())
{
- $args[':limit_start'] = $this->currentPage;
- $args[':limit_count'] = $this->perPage;
- $retval = Database::simpleQuery($this->query, $args);
- $res = Database::queryFirst('SELECT FOUND_ROWS() AS rowcount');
- $this->totalRows = (int)$res['rowcount'];
+ $countQuery = preg_replace('/ORDER\s+BY\s.*?(\sASC|\sDESC|$)/is', '', $this->query);
+ $countQuery = preg_replace('/SELECT\s.*?\sFROM\s/is', 'SELECT Count(*) AS rowcount FROM ', $countQuery);
+ $countRes = Database::queryFirst($countQuery, $args);
+ $args['limit_start'] = $this->currentPage;
+ $args['limit_count'] = $this->perPage;
+ $query = $this->query . ' LIMIT ' . ($this->currentPage * $this->perPage) . ', ' . $this->perPage;
+ $retval = Database::simpleQuery($query, $args);
+ $this->totalRows = (int)$countRes['rowcount'];
return $retval;
}
diff --git a/inc/pagination.inc.php b/inc/pagination.inc.php
deleted file mode 100644
index 65785a36..00000000
--- a/inc/pagination.inc.php
+++ /dev/null
@@ -1,47 +0,0 @@
-<?php
-
-/**
- * TODO: Why does this class exist?
- * There's already the Paginate class which works more efficient by using the LIMIT statement
- * for the query, and has more options. Consider refactoring the places where this class is
- * used (see syslog or eventlog for usage examples), then get rid of this one.
- */
-class Pagination
-{
- private $items;
- private $page;
- private $maxItems;
-
- public function __construct($par1, $par2)
- {
- $this->items = $par1;
- $this->page = $par2;
-
- $this->maxItems = 5;
- }
-
- public function getPagination()
- {
- $ret = array();
- $n = ceil(count($this->items) / $this->maxItems);
- for ($i = 1; $i <= $n; $i++) {
- $class = ($i == $this->page) ? 'active' : '';
- $ret[] = array(
- 'class' => $class,
- 'page' => $i
- );
- }
- return $ret;
- }
-
- public function getItems()
- {
- $ret = array();
- $first = ($this->page - 1) * $this->maxItems;
- for ($i = 0; $i < $this->maxItems; $i++) {
- if ($first + $i < count($this->items))
- $ret[] = $this->items[$first + $i];
- }
- return $ret;
- }
-} \ No newline at end of file
diff --git a/inc/render.inc.php b/inc/render.inc.php
index 53e2f314..13262c1d 100644
--- a/inc/render.inc.php
+++ b/inc/render.inc.php
@@ -107,8 +107,7 @@ class Render
<script src="script/jquery.js"></script>
<script src="script/bootstrap.min.js"></script>
<script src="script/taskmanager.js"></script>
- <script src="script/fileselect.js"></script>
- <script src="script/collapse.js"></script>
+ <script src="script/slx-fixes.js"></script>
';
foreach ($modules as $module) {
$files = $module->getScripts($module != $pageModule);
@@ -213,7 +212,7 @@ class Render
*/
public static function parse($template, $params = false, $module = false)
{
- if ($module === false) {
+ if ($module === false && class_exists('Page')) {
$module = Page::getModule()->getIdentifier();
}
// Load html snippet