Location: PHPKode > projects > Obsessive Website Statistics > ows/scripts/show_info.php
#!/usr/bin/php
<?php
/*
	$Id: show_info.php 97 2007-08-25 06:10:11Z randomperson83 $

	Obsessive Web Statistics
    Copyright (C) 2007 Dustin Spicuzza <hide@address.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
	
	This tries to show information on all websites.
	
*/

	$base = realpath(dirname(__FILE__) . '/../include/');
	require "$base/base.inc.php";
	require "$base/dimensions.inc.php";
	require "$base/plugin.inc.php";
	require_cli();

	if ($argc < 2 || in_array($argv[1], array('--help', '-help', '-h', '-?'))){
		echo wordwrap("Usage: " . $argv[0] . " [domain] | ['all' for all tables]\n\nThis script displays information about OWS related tables and other stuff. May be useful for debugging issues or other things. May not be useful at all, you decide.\n");
		die;
	}
	
	$website = $argv[1];
	
	// load plugins, so we can compile dimension information
	if (!load_plugins())
		echo "Warning: could not load plugins!!\n";
	
	if ($website == 'all'){
	
		foreach (get_website_names() as $site)
			show_info($site);
	
	}else{
		if (validate_website($website))
			show_info($website);
	}
	
	

function show_info($website){
	
	echo "$website:\n\n";

	$table = str_replace('.','_',$website);
	$s_table = db_escape_string($table);
	
	// table sizes
	$result = db_query("SHOW TABLE STATUS LIKE '$s_table%'");
	
	if (!db_has_rows($result)){
		echo "\tNo tables installed.\n\n";
		return;
	}
	
	
	$idx = 0;
	$sz = 0;
	$rows = array();
	$max = array();
	
	$rows[] = array('Name','Rows','Data','Idx');
	
	while ($row = db_fetch_assoc($result)){
		
		$r = array();
		
		$r[] = $row['Name'];
		$r[] = intval($row['Rows']);
		$r[] = get_file_size(intval($row['Data_length']));
		$r[] = get_file_size(intval($row['Index_length']));
	
		$rows[] = $r;
		
		$sz += intval($row['Data_length']);
		$idx += intval($row['Index_length']);
	}
	
	show_console_table($rows,"\t");

	
	$sz = get_file_size($sz);
	$idx = get_file_size($idx);
	
	echo "\n\tTotal Data:\t$sz\n\tTotal Indexes:\t$idx\n\n\tChecking dimensions...";
	
	// this gets cached
	$dimensions = compile_dimensions();
	
	$rows = array();
	$rows[] = array('Dimension','Rows','Unique','Stale','Status');
	$good = true;
	foreach ($dimensions as $dimension => $attrs){
		
		$s_dimension = db_escape_string($table . '_' . $dimension);
		
		if (!db_has_rows(db_query("SHOW TABLES LIKE '$s_dimension'")) ||
			!db_has_rows($result = db_query("SELECT COUNT(*),COUNT(DISTINCT " . db_escape_string($dimension) . ") FROM $s_dimension"))){
			
			$rows[] = array($dimension,'-','-','N/A');
			echo "x";
			$good = false;
			continue;
		}
		
		$row = db_fetch_row($result);
		
		// show stale keys
		$key = db_escape_string($dimension . '_id');
		$result = db_query("SELECT COUNT($s_dimension.$key) FROM $s_dimension LEFT OUTER JOIN $s_table ON $s_table.$key = $s_dimension.$key WHERE $s_table.id IS NULL");
		
		$stale = array(0);
		if (db_has_rows($result))
			$stale = db_fetch_row($result);
		
		$rows[] = array($dimension,$row[0],$row[1],$stale[0],($row[0] != $row[1] ? 'ERROR' : 'OK'));
		echo '.';
	}
	
	if ($good)
		echo "OK.\n\n";
	else
		echo "error found!\n\n";
	
	show_console_table($rows,"\t");
	
	echo "\n";
}


?>

Return current item: Obsessive Website Statistics