/**
* Counts and returns (or displays) the number of records in a table
*
* @param bool $force_exact whether to force an exact count
*
* @return mixed the number of records if "retain" param is true,
* otherwise true
*/
public function countRecords($force_exact = false)
{
$is_view = $this->isView();
$db = $this->_db_name;
$table = $this->_name;
if ($this->_dbi->getCachedTableContent("{$db}.{$table}.ExactRows") != null) {
$row_count = $this->_dbi->getCachedTableContent("{$db}.{$table}.ExactRows");
return $row_count;
}
$row_count = false;
if (!$force_exact) {
if ($this->_dbi->getCachedTableContent("{$db}.{$table}.Rows") == null && !$is_view) {
$tmp_tables = $this->_dbi->getTablesFull($db, $table);
if (isset($tmp_tables[$table])) {
$this->_dbi->cacheTableContent("{$db}.{$table}", $tmp_tables[$table]);
}
}
if ($this->_dbi->getCachedTableContent("{$db}.{$table}.Rows") != null) {
$row_count = $this->_dbi->getCachedTableContent("{$db}.{$table}.Rows");
} else {
$row_count = false;
}
}
// for a VIEW, $row_count is always false at this point
if (false !== $row_count && $row_count >= $GLOBALS['cfg']['MaxExactCount']) {
return $row_count;
}
if (!$is_view) {
$row_count = $this->_dbi->fetchValue('SELECT COUNT(*) FROM ' . Util::backquote($db) . '.' . Util::backquote($table));
} else {
// For complex views, even trying to get a partial record
// count could bring down a server, so we offer an
// alternative: setting MaxExactCountViews to 0 will bypass
// completely the record counting for views
if ($GLOBALS['cfg']['MaxExactCountViews'] == 0) {
$row_count = 0;
} else {
// Counting all rows of a VIEW could be too long,
// so use a LIMIT clause.
// Use try_query because it can fail (when a VIEW is
// based on a table that no longer exists)
$result = $this->_dbi->tryQuery('SELECT 1 FROM ' . Util::backquote($db) . '.' . Util::backquote($table) . ' LIMIT ' . $GLOBALS['cfg']['MaxExactCountViews'], null, DatabaseInterface::QUERY_STORE);
if (!$this->_dbi->getError()) {
$row_count = $this->_dbi->numRows($result);
$this->_dbi->freeResult($result);
}
}
}
if ($row_count) {
$this->_dbi->cacheTableContent("{$db}.{$table}.ExactRows", $row_count);
}
return $row_count;
}