Created
February 11, 2013 11:01
-
-
Save mschultheiss83/4753858 to your computer and use it in GitHub Desktop.
Magento Cleanup Log-Tables and Cache-Files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*************************************************** | |
* Magento Log File Contents Monitor. GNU/GPL | |
* [email protected] | |
* provided without warranty or support | |
***************************************************/ | |
/*********************** | |
* Scan Magento local.xml file for connection information | |
***********************/ | |
if (file_exists('./app/etc/local.xml')) { | |
$xml = simplexml_load_file('./app/etc/local.xml'); | |
$tblprefix = $xml->global->resources->db->table_prefix; | |
$dbhost = $xml->global->resources->default_setup->connection->host; | |
$dbuser = $xml->global->resources->default_setup->connection->username; | |
$dbpass = $xml->global->resources->default_setup->connection->password; | |
$dbname = $xml->global->resources->default_setup->connection->dbname; | |
$tables = array( | |
'dataflow_batch_export', | |
'dataflow_batch_import', | |
'log_customer', | |
'log_quote', | |
'log_summary', | |
'log_summary_type', | |
'log_url', | |
'log_url_info', | |
'log_visitor', | |
'log_visitor_info', | |
'log_visitor_online', | |
// 'index_event', | |
'report_event', | |
// 'report_compared_product_index', | |
// 'report_viewed_product_index', | |
'catalog_compare_item', | |
// 'catalogindex_aggregation_to_tag', | |
// 'catalogindex_aggregation_tag', | |
// 'catalogindex_aggregation', | |
); | |
$dirs = array( | |
'downloader/.cache/*', | |
'downloader/pearlib/cache/*', | |
'downloader/pearlib/download/*', | |
'var/cache/', | |
'var/locks/', | |
'var/log/', | |
'var/report/', | |
'var/session/', | |
'var/tmp/' | |
); | |
} else { | |
exit('Failed to open ./app/etc/local.xml'); | |
} | |
/** Get current date, time, UTC and offset **/ | |
$date = date("Y-m-d"); | |
$time = date("H:i:s T"); | |
$offset = date("P"); | |
$utc = gmdate("H:i:s"); | |
/*********************** | |
* Start HTML output | |
***********************/ | |
echo "<html><head><title>Magento Log File Contents on " . $date . " " . $time . "</title> | |
<meta http-equiv=\"refresh\" content=\"30\"> | |
<style type=\"text/css\">html {width: 100%; font-family: Arial,Helvetica, sans-serif;} | |
body {line-height:1.0em; font-size: 100%;} | |
table {border-spacing: 1px; padding-top: 10px; } | |
th.stattitle {text-align: left; font-size: 100%; font-weight: bold; color: white; background-color: #101010;} | |
th {text-align: center; font-size: 90%; font-weight: bold; padding: 5px; border-bottom: 1px solid black; border-left: 1px solid black; } | |
td {font-size: 90%; padding: 4px; border-bottom: 1px solid black; border-left: 1px solid black;} | |
.error {color: #FF0000; } | |
</style> | |
</head><body>"; | |
/** Output title, connection info and cron job monitor runtime **/ | |
echo | |
"<h1>USE GET params \"cleanup\" VALUES all|tables|dirs</h1>" . | |
"<h2>Magento Log File Contents Report</h2>" . | |
"<h3>Connection: " . $dbuser . "@" . $dbhost . | |
" – Database: " . $dbname . "</h3>"; | |
echo "<h3>Runtime: " . $date . " " . $time . " " . $utc . " UTC</h3>"; | |
echo "<h4>Note: Your timezone offset is " . $offset . " hours from UTC</h4>"; | |
/** Connect to database **/ | |
$conn = mysql_connect($dbhost, $dbuser, $dbpass); | |
@mysql_select_db($dbname) or die("Unable to select database"); | |
/*********************** | |
* Get table record counts | |
***********************/ | |
echo '<table><tbody><tr><th class="stattitle" colspan="2">Log Tables</th></tr>'; | |
echo '<tr><th>Table</th><th>Row Count</th></tr>'; | |
foreach ($tables as $tblname) { | |
$result = mysql_query("SELECT COUNT(*) FROM " . $tblprefix . $tblname) or die(mysql_error()); | |
$numrows = mysql_fetch_array($result); | |
$num = $numrows[0]; | |
/* Table output */ | |
echo '<tr>'; | |
echo '<td>' . $tblprefix . $tblname . '</td>'; | |
echo '<td align="right">' . $num . "</td>"; | |
echo '</tr>'; | |
} | |
echo '</tbody></table>'; | |
/*********************** | |
* End of report | |
***********************/ | |
mysql_close($conn); | |
/*********************** | |
* CLEANUP $_GET['cleanup'] = all|tables|dirs | |
***********************/ | |
if (isset($_GET['cleanup'])) { | |
$cleanupType = ($_GET['cleanup'] != '' ? $_GET['cleanup'] : ''); | |
$params = array( | |
'tables' => $tables, | |
'dirs' => $dirs | |
); | |
if ($cleanupType == 'all') { | |
cleanup('tables', $params); | |
cleanup('dirs', $params); | |
} elseif ($cleanupType == 'tables') { | |
cleanup('tables', $params); | |
} elseif ($cleanupType == 'dirs') { | |
cleanup('dirs', $params); | |
} | |
} | |
function cleanup($key, $params) | |
{ | |
if ($key == 'dirs') { | |
echo '<table><tbody><tr><th class="stattitle" colspan="2">Starting Cleanup Files in</th></tr>'; | |
$dirs = $params['dirs']; | |
foreach ($dirs as $v => $k) { | |
exec('rm -rf ' . $k); | |
echo '<tr>'; | |
echo '<td>' . $k . '</td>'; | |
echo '</tr>'; | |
} | |
echo '</tbody></table>'; | |
} elseif ($key == 'tables') { | |
$tables = $params['tables']; | |
clean_log_tables($tables); | |
} | |
} | |
function clean_log_tables($tables) | |
{ | |
$xml = simplexml_load_file('./app/etc/local.xml', null, LIBXML_NOCDATA); | |
$db['host'] = $xml->global->resources->default_setup->connection->host; | |
$db['name'] = $xml->global->resources->default_setup->connection->dbname; | |
$db['user'] = $xml->global->resources->default_setup->connection->username; | |
$db['pass'] = $xml->global->resources->default_setup->connection->password; | |
$db['pref'] = $xml->global->resources->db->table_prefix; | |
mysql_connect($db['host'], $db['user'], $db['pass']) or die(mysql_error()); | |
mysql_select_db($db['name']) or die(mysql_error()); | |
echo '<table><tbody><tr><th class="stattitle" colspan="2">Starting Cleanup Files in</th></tr>'; | |
foreach ($tables as $v => $k) { | |
echo '<tr>'; | |
mysql_query('TRUNCATE `' . $db['pref'] . $k . '`') or die( | |
'<td class="error">' . mysql_error() . '</td></tr></tbody></table></body></html>'); | |
echo '<td>' . $db['pref'] . $k . '</td>'; | |
echo '</tr>'; | |
} | |
echo '</tbody></table>'; | |
} | |
echo '</body></html>'; | |
?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment