Location: PHPKode > projects > Obsessive Website Statistics > ows/scripts/installer.php
#!/usr/bin/php
<?php
/*
	$Id: installer.php 92 2007-08-17 19:09:21Z 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/>.
	
	
	
	TODO: Install/uninstall individual plugins
	
*/

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

	
	if ($argc < 3 || in_array($argv[1], array('--help', '-help', '-h', '-?'))){
		echo wordwrap("Usage: " . $argv[0] . " (install | sync | uninstall) (website | 'all' for all websites)\n\nThis script installs/uninstalls plugin table information. If you delete a plugin, you should run '" . $argv[0] . " sync' to remove any dimensional elements it left behind.\n");
		// TODO: Show possible plugins here
		die;
	}
	
	// initialize state
	$installing = false;
	$sync = false;
	
	// setup args
	if ($argv[1] == 'install')
		$installing = true;
	else if ($argv[1] == 'sync')
		$sync = true;
	else if ($argv[1] == 'uninstall'){
		if (readline("WARNING: This will permanently DELETE tables and other data associated with $argv[2]! Continue? [type 'yes'] ") != "yes")
			die();
	}else
		return show_error("You must specify whether you want to install or uninstall a plugin");
	
	echo "\n";
	
	$website = $argv[2];
	
	
	// load plugins first
	if (!load_plugins(''))
		die;
	
	// then, detect for InnoDB (transactions)
	if ($cfg['db_type'] == 'mysql' && strtolower($cfg['db_table_type']) == 'innodb'){
		$ask = true;
		$result = db_query("SHOW VARIABLES LIKE 'have_innodb'");
		if (db_has_rows($result) && $row = db_fetch_row($result))
			if ($row[1] == 'YES')
				$ask = false;
		
		if ($ask && readline("Your installation of MySQL does not have InnoDB enabled! Things may break if you do not have InnoDB enabled. Continue? [type 'yes'] ") != "yes")
			die();
	}
	
	
	if ($website == 'all'){
	
		foreach (get_website_names() as $site)
			do_install($site, $installing, $sync);
	
	}else{
	
		// first, validate the website
		if (validate_website($website))
			do_install($website, $installing, $sync);
		
	}
	

function do_install($website,$install, $sync){

	global $cfg;
	
	// lock DB
	$lock = new AnalysisLock($website);
	if ($lock->Locked())
		return false;
	
	
	if (!$sync){
	
		$plugins = get_plugins('installable');
		
		foreach ($plugins as $plugin){
		
			$installed = $plugin['plugin']->isInstalled($website);
		
			if ($install == true){
			
				// if its installed, then don't reinstall
				if ($installed)
					echo "Exists: Plugin \"" . get_plugin_name($plugin) . "\" is already installed for $website!\n";
					
				else if ($plugin['plugin']->install($website))
					echo "Success: Plugin \"" . get_plugin_name($plugin) . "\" was installed sucessfully for $website!\n";		
					
				else
					return show_error("There was an error installing the plugin \"" . get_plugin_name($plugin) . "\" for $website!\n");
					
			}else{
			
				// if its not installed, then don't uninstall
				if (!$installed)
					echo "Not Installed: Plugin \"" . get_plugin_name($plugin) . "\" is not installed for $website\n";
					
				else if ($plugin['plugin']->uninstall($website))
					echo "Success: Plugin \"" . get_plugin_name($plugin) . "\" uninstalled sucessfully for $website!\n";		
					
				else
					return show_error("There was an error uninstalling the plugin \"" . get_plugin_name($plugin) . "\" for $website!");
				
			}
		}
	}
	
	// do dimension-related installation now
	if ($install || $sync){
		
		if (($dimensions = sync_dimensions($website)) !== false)
			echo "Success: database dimensions created for $website!\n";
		else
			return show_error("Could not install dimensions for $website\n");
		
		// after the tables are synced, then anything that needs to be added should be done here
		if (count($dimensions) > 0){
			$analysis = new Analysis();
			if (!$analysis->Initialize($website,$dimensions))
				return false;
				
			if (!$analysis->reAnalyze())
				return show_error("Reanalysis failed! You may need to use reanalysis.php to rerun analysis once you correct the above error!");
		}
			
	}else
		if (delete_all_dimensions($website))
			echo "Success: all database dimensions deleted for $website!\n";
		else
			return show_error("Could not delete all dimensions for $website");
	
	echo "\n";
	
	return true;
}


?>
Return current item: Obsessive Website Statistics