adminer/plugins/AdminerReadableTableSize.php
2018-04-28 13:25:58 +02:00

50 lines
1.4 KiB
PHP

<?php
/** This plugin replaces bytes sizes in tables list with human-readable sizes.
* Original sizes are set to the title=""
*
* @link https://www.adminer.org/plugins/#use
* @author Anonymous
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other)
*/
class AdminerReadableTableSize
{
/** @access protected */
var $prepend;
function __construct()
{
$this->prepend = <<<EOT
function AdminerReadableTableSizeConvert(a,b){if(0==a)return"0 Bytes";var c=1024,d=b||2,e=["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"],f=Math.floor(Math.log(a)/Math.log(c));return parseFloat((a/Math.pow(c,f)).toFixed(d))+" "+e[f]}
function AdminerReadableTableSize(field) {
var rows = document.querySelectorAll('[id*="'+field+'"]');
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
var bytes = row.innerText.replace(/,/g, '');
var sized = '-';
if (bytes > 0) {
sized = AdminerReadableTableSizeConvert(bytes, 2);
}
row.title = row.title+' [' + row.innerText + ' Bytes]';
row.innerText = sized;
}
}
document.addEventListener('DOMContentLoaded', function(event) {
setTimeout(function() {
AdminerReadableTableSize('Data_length');
AdminerReadableTableSize('Index_length');
AdminerReadableTableSize('Data_free');
}, 500);
});
EOT;
}
function head()
{
echo script($this->prepend);
}
}