Location: PHPKode > projects > Obsessive Website Statistics > ows/scripts/optimize.php
#!/usr/bin/php
<?php
/*
	$Id: optimize.php 86 2007-08-13 02:40:57Z 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 runs MySQL's optimize on the specified website, or you can run it
	on all tables. I'm not quite sure how often you should run this yet... 
	
*/

	$base = realpath(dirname(__FILE__) . '/../include/');
	require "$base/base.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 runs the MySQL command 'OPTIMIZE' on each website specified. If you specify the domain as 'all', then all tables will have 'OPTIMIZE' run on them. See the database documentation for more information about OPTIMIZE.\n");
		die;
	}
	
	$website = $argv[1];
	
	if ($website == 'all'){
	
		foreach (get_website_names() as $site)
			optimize($site);
	
	}else{
		if (validate_website_table($website))
			optimize($website);
	}
		

// runs the 'optimize' query.. can take awhile	
function optimize($website){

	$table = str_replace('.','_',$website);

	$result = db_query("SHOW TABLES LIKE '" . db_escape_string($table) . "%'");
	if (!db_has_rows($result))
		return show_error("Could not get list of tables for $website. That site may not have been installed yet.");
		
	while($row = db_fetch_row($result)){
		echo "Optimizing table $row[0] at " . date("D M j G:i:s T Y") . "...\n";
		if (!db_is_valid_result(db_query("OPTIMIZE TABLE " . db_escape_string($row[0]))))
			return;
	}
}

?>

Return current item: Obsessive Website Statistics