Location: PHPKode > projects > PhpScribe Documentation Generator > dbinstall.php
<?php

//!---------------------------------------------
// Função para verificar se a base de dados está completa
//!---------------------------------------------
function verifyDB() {

	if ($link = testConnection()) {
		$tables = array();
    	$rs = mysql_query("SHOW TABLES", $link);
		while (list($table) = mysql_fetch_row($rs)) {
			$tables[] = $table;
		}
		$missingTables = array();
		if (!in_array('class', $tables)) {
			$missingTables[] = 'class';
		}
		if (!in_array('constant', $tables)) {
			$missingTables[] = 'constant';
		}
		if (!in_array('example', $tables)) {
			$missingTables[] = 'example';
		}
		if (!in_array('file', $tables)) {
			$missingTables[] = 'file';
		}
		if (!in_array('file_type', $tables)) {
			$missingTables[] = 'file_type';
		}
		if (!in_array('function', $tables)) {
			$missingTables[] = 'function';
		}
		if (!in_array('note', $tables)) {
			$missingTables[] = 'note';
		}
		if (!in_array('package', $tables)) {
			$missingTables[] = 'package';
		}
		if (!in_array('param', $tables)) {
			$missingTables[] = 'param';
		}
		if (!in_array('project', $tables)) {
			$missingTables[] = 'project';
		}
		if (!in_array('property', $tables)) {
			$missingTables[] = 'property';
		}
		if (!in_array('revision', $tables)) {
			$missingTables[] = 'revision';
		}
		if (!in_array('see', $tables)) {
			$missingTables[] = 'see';
		}
		if (!in_array('uses', $tables)) {
			$missingTables[] = 'uses';
		}
		if (!empty($missingTables)) {
        	returnError("The following phpScribe's system tables weren't found in your database:<BR><B>" . implode(",", $missingTables) . "</B>");
		} else {
			$rs = mysql_query("SELECT * FROM file_type", $link);
			if (mysql_affected_rows() == 0) {
				returnError("The table <B>file_type</B> must have at least one entry!");
			} else {
				returnMessage("<B>Your database is OK and ready to use!!</B><BR>");
			}
		}
	}

}

//!---------------------------------------------
// Função para atualizar a base de dados para uma versão mais nova
//!---------------------------------------------
function updateDB($fromVersion, $toVersion) {

	$ret = '';

	if ($link = testConnection()) {
		if ($fromVersion == '0.5' && $toVersion == '0.6')
		{
			$ret .= "There's no database updates from <B>phpScribe 0.5</B> to <B>phpScribe 0.6</B>!<BR>";
			$ret .= "<BR><font color='#0000FF'>Your phpScribe is ready to use!!</font><BR><BR>";			
			returnMessage($ret);			
		}
		else if ($fromVersion == '0.3' && $toVersion == '0.6')
		{

			$ret .= "Updating from <B>phpScribe 0.1, 0.2 or 0.3</B> to <B>phpScribe 0.5</B>...<BR><BR>";

			// TABLE 'CONSTANT'
			$constantCREATE = "CREATE TABLE constant(cod_constant INT NOT NULL AUTO_INCREMENT,cod_file INT NOT NULL DEFAULT '0',name VARCHAR(100) NOT NULL DEFAULT '',value VARCHAR(200) NOT NULL DEFAULT '',description VARCHAR(200) DEFAULT NULL,revision INT NOT NULL,FOREIGN KEY (cod_file) REFERENCES file (cod_file), PRIMARY KEY  (cod_constant), KEY revision (revision))";
			if (!mysql_query($constantCREATE, $link)) {
            	$ret .= '» \'constant\' table was already created.<BR>';
			} else {
            	$ret .= '» \'constant\' table was created successfully.<BR>';
			}

			// TABLE 'NOTE'
			$noteCREATE = "CREATE TABLE note(cod_note INT NOT NULL AUTO_INCREMENT,cod_class INT,cod_function INT,description TEXT NOT NULL,revision INT NOT NULL,FOREIGN KEY (cod_class) REFERENCES class (cod_class),FOREIGN KEY (cod_function) REFERENCES function (cod_function),PRIMARY KEY (cod_note));";
			if (!mysql_query($noteCREATE, $link)) {
            	$ret .= '» \'note\' table was already created.<BR>';
			} else {
            	$ret .= '» \'note\' table was created successfully.<BR>';
			}

			// COPY NOTE COLUMN FROM 'CLASS' TO 'NOTE' TABLE
			if ($rs = mysql_query("SELECT cod_class,note,revision FROM class WHERE note != ''", $link)) {
            	while (list($cod_class, $note, $revision) = mysql_fetch_row($rs)) {
                	$noteINSERT = "INSERT INTO note (cod_class,description,revision) VALUES ($cod_class,'$note',$revision)";
					mysql_query($noteINSERT, $link);
				}
				$noteREMOVE = "ALTER TABLE `class` DROP `note`";
				mysql_query($noteREMOVE, $link);
				$ret .= '» Notes form table \'class\' were successfully copied to table \'note\'.<BR>';
			} else {
            	$ret .= '» Notes from table \'class\' where already copied to table \'note\'.<BR>';
			}

			// COPY NOTE COLUMN FROM 'FUNCTION' TO 'NOTE' TABLE
			if ($rs = mysql_query("SELECT cod_function,note,revision FROM function WHERE note != ''", $link)) {
            	while (list($cod_function, $note, $revision) = mysql_fetch_row($rs)) {
                	$noteINSERT = "INSERT INTO note (cod_function,description,revision) VALUES ($cod_function,'$note',$revision)";
					mysql_query($noteINSERT, $link);
				}
				$noteREMOVE = "ALTER TABLE `function` DROP `note`";
				mysql_query($noteREMOVE, $link);
				$ret .= '» Notes form table \'function\' were successfully copied to table \'note\'.<BR>';
			} else {
            	$ret .= '» Notes from table \'function\' where already copied to table \'note\'.<BR>';
			}

			// OTHER UPDATES
			$colUPDATES = 	array("ALTER TABLE `file_type` CHANGE `extension` `extension` VARCHAR(20)  NOT NULL",
									"ALTER TABLE `file` CHANGE `extension` `extension` VARCHAR(20)  NOT NULL",
									"ALTER TABLE `function` ADD `since` VARCHAR(100)  AFTER description",
									"ALTER TABLE `function` ADD `deprecated` CHAR(1)  DEFAULT \"F\" NOT NULL AFTER return",
									"ALTER TABLE `function` CHANGE `access` `access` ENUM('public','private','protected')  DEFAULT \"public\"",
									"ALTER TABLE `param` ADD `default_value` VARCHAR(200)  AFTER description");
			$ok = 0;
			foreach($colUPDATES as $v) {
				if (mysql_query($v, $link)) {
					$ok++;
				}
			}
			if (!$ok) {
				$ret .= '» Updates in tables \'file_type\', \'file\', \'function\' and \'param\' were already executed.<BR>';
			} else {
				$ret .= '» Updates in tables \'file_type\', \'file\', \'function\' and \'param\' were successfully executed.<BR>';
			}

			$ret .= "<BR><font color='#0000FF'>Your phpScribe database was updated!!</font><BR><BR>";

			returnMessage($ret);
		}
		else if ($fromVersion == '0.3' && $toVersion == '0.9') {

			$ret .= "Updating from <B>phpScribe 0.1, 0.2 or 0.3</B> to <B>phpScribe 0.9</B>...<BR><BR>";

			// TABLE 'CONSTANT'
			$constantCREATE = "CREATE TABLE constant(cod_constant INT NOT NULL AUTO_INCREMENT,cod_file INT NOT NULL DEFAULT '0',name VARCHAR(100) NOT NULL DEFAULT '',value VARCHAR(200) NOT NULL DEFAULT '',description VARCHAR(200) DEFAULT NULL,revision INT NOT NULL,FOREIGN KEY (cod_file) REFERENCES file (cod_file), PRIMARY KEY  (cod_constant), KEY revision (revision))";
			if (!mysql_query($constantCREATE, $link)) {
            	$ret .= '» \'constant\' table was already created.<BR>';
			} else {
            	$ret .= '» \'constant\' table was created successfully.<BR>';
			}
			
			// TABLE 'PACKAGE'
			$packageCREATE = "CREATE TABLE package(cod_package INT NOT NULL AUTO_INCREMENT, name VARCHAR(100) NOT NULL, revision INT NOT NULL, PRIMARY KEY (cod_package))";
			if (!mysql_query($packageCREATE, $link)) {
            	$ret .= '» \'package\' table was already created.<BR>';
			} else {
            	$ret .= '» \'package\' table was created successfully.<BR>';
			}

			// TABLE 'NOTE'
			$noteCREATE = "CREATE TABLE note(cod_note INT NOT NULL AUTO_INCREMENT,cod_class INT,cod_function INT,description TEXT NOT NULL,revision INT NOT NULL,FOREIGN KEY (cod_class) REFERENCES class (cod_class),FOREIGN KEY (cod_function) REFERENCES function (cod_function),PRIMARY KEY (cod_note));";
			if (!mysql_query($noteCREATE, $link)) {
            	$ret .= '» \'note\' table was already created.<BR>';
			} else {
            	$ret .= '» \'note\' table was created successfully.<BR>';
			}

			// COPY NOTE COLUMN FROM 'CLASS' TO 'NOTE' TABLE
			if ($rs = mysql_query("SELECT cod_class,note,revision FROM class WHERE note != ''", $link)) {
            	while (list($cod_class, $note, $revision) = mysql_fetch_row($rs)) {
                	$noteINSERT = "INSERT INTO note (cod_class,description,revision) VALUES ($cod_class,'$note',$revision)";
					mysql_query($noteINSERT, $link);
				}
				$noteREMOVE = "ALTER TABLE `class` DROP `note`";
				mysql_query($noteREMOVE, $link);
				$ret .= '» Notes form table \'class\' were successfully copied to table \'note\'.<BR>';
			} else {
            	$ret .= '» Notes from table \'class\' where already copied to table \'note\'.<BR>';
			}

			// COPY NOTE COLUMN FROM 'FUNCTION' TO 'NOTE' TABLE
			if ($rs = mysql_query("SELECT cod_function,note,revision FROM function WHERE note != ''", $link)) {
            	while (list($cod_function, $note, $revision) = mysql_fetch_row($rs)) {
                	$noteINSERT = "INSERT INTO note (cod_function,description,revision) VALUES ($cod_function,'$note',$revision)";
					mysql_query($noteINSERT, $link);
				}
				$noteREMOVE = "ALTER TABLE `function` DROP `note`";
				mysql_query($noteREMOVE, $link);
				$ret .= '» Notes form table \'function\' were successfully copied to table \'note\'.<BR>';
			} else {
            	$ret .= '» Notes from table \'function\' where already copied to table \'note\'.<BR>';
			}
			
			// CLASS UPDATES
			$classUPDATES = array("ALTER TABLE `class` ADD `cod_package` INT UNSIGNED AFTER cod_file",
								"ALTER TABLE `class` ADD `since` VARCHAR(100)  AFTER author",
								"ALTER TABLE `class` ADD `version` VARCHAR(100)  AFTER since",
								"ALTER TABLE `class` ADD `static` CHAR(1)  DEFAULT \"F\" AFTER version");
			$ok = 0;
			foreach($classUPDATES as $v) {
				if (mysql_query($v, $link)) {
					$ok++;
				}
			}
			if (!$ok) {
				$ret .= '» Updates in table \'class\' were already executed.<BR>';
			} else {
				$ret .= '» Updates in table \'class\' were successfully executed.<BR>';
			}
			
			// FUNCTION UPDATES
			$functionUPDATES = 	array("ALTER TABLE `function` ADD `since` VARCHAR(100)  AFTER description",
									"ALTER TABLE `function` ADD `deprecated` CHAR(1)  DEFAULT \"F\" NOT NULL AFTER return",
									"ALTER TABLE `function` CHANGE `access` `access` ENUM('public','private','protected')  DEFAULT \"public\"",
									"ALTER TABLE `function` ADD `version` VARCHAR(100)  AFTER since",
									"ALTER TABLE `function` ADD `static` CHAR(1) DEFAULT \"F\" NOT NULL AFTER deprecated",
									"ALTER TABLE `function` ADD `ref` CHAR(1)  DEFAULT \"F\" NOT NULL AFTER static",
									"ALTER TABLE `function` ADD `return_type` VARCHAR(50)  DEFAULT \"unknown\" NOT NULL AFTER return");
			$ok = 0;
			foreach($functionUPDATES as $v) {
				if (mysql_query($v, $link)) {
					$ok++;
				}
			}
			if (!$ok) {
				$ret .= '» Updates in table \'function\' were already executed.<BR>';
			} else {
				$ret .= '» Updates in table \'function\' were successfully executed.<BR>';
			}			
			
			// PARAM UPDATES
			$paramUPDATES = array("ALTER TABLE `param` ADD `default_value` VARCHAR(200)  AFTER description",
								"ALTER TABLE `param` ADD `ref` CHAR(1)  NOT NULL AFTER default_value");
			$ok = 0;
			foreach($paramnUPDATES as $v) {
				if (mysql_query($v, $link)) {
					$ok++;
				}
			}
			if (!$ok) {
				$ret .= '» Updates in table \'param\' were already executed.<BR>';
			} else {
				$ret .= '» Updates in table \'param\' were successfully executed.<BR>';
			}

			// OTHER UPDATES
			$colUPDATES = array("ALTER TABLE `file_type` CHANGE `extension` `extension` VARCHAR(20)  NOT NULL",
								"ALTER TABLE `file` CHANGE `extension` `extension` VARCHAR(20)  NOT NULL",
								"ALTER TABLE `property` ADD `value` VARCHAR(255)  AFTER datatype");
			$ok = 0;
			foreach($colUPDATES as $v) {
				if (mysql_query($v, $link)) {
					$ok++;
				}
			}
			if (!$ok) {
				$ret .= '» Updates in tables \'file_type\', \'file\' and \'property\' were already executed.<BR>';
			} else {
				$ret .= '» Updates in tables \'file_type\', \'file\' and \'property\' were successfully executed.<BR>';
			}

			$ret .= "<BR><font color='#0000FF'>Your phpScribe database was updated!!</font><BR><BR>";

			returnMessage($ret);			
		}
		else if (($fromVersion == '0.5' || $fromVersion == '0.6') && $toVersion == '0.9') {

			$ret .= "Updating from <B>phpScribe 0.5 or 0.6</B> to <B>phpScribe 0.9</B>...<BR><BR>";
			
			// TABLE 'PACKAGE'
			$packageCREATE = "CREATE TABLE package(cod_package INT NOT NULL AUTO_INCREMENT, name VARCHAR(100) NOT NULL, revision INT NOT NULL, PRIMARY KEY (cod_package))";
			if (!mysql_query($packageCREATE, $link)) {
            	$ret .= '» \'package\' table was already created.<BR>';
			} else {
            	$ret .= '» \'package\' table was created successfully.<BR>';
			}
			
			// CLASS UPDATES
			$classUPDATES = array("ALTER TABLE `class` ADD `cod_package` INT UNSIGNED AFTER cod_file",
								"ALTER TABLE `class` ADD `since` VARCHAR(100)  AFTER author",
								"ALTER TABLE `class` ADD `version` VARCHAR(100)  AFTER since",
								"ALTER TABLE `class` ADD `static` CHAR(1)  DEFAULT \"F\" AFTER version");
			$ok = 0;
			foreach($classUPDATES as $v) {
				if (mysql_query($v, $link)) {
					$ok++;
				}
			}
			if (!$ok) {
				$ret .= '» Updates in table \'class\' were already executed.<BR>';
			} else {
				$ret .= '» Updates in table \'class\' were successfully executed.<BR>';
			}
			
			// FUNCTION UPDATES
			$functionUPDATES = 	array("ALTER TABLE `function` ADD `version` VARCHAR(100)  AFTER since",
									"ALTER TABLE `function` ADD `static` CHAR(1) DEFAULT \"F\" NOT NULL AFTER deprecated",
									"ALTER TABLE `function` ADD `ref` CHAR(1)  DEFAULT \"F\" NOT NULL AFTER static",
									"ALTER TABLE `function` ADD `return_type` VARCHAR(50)  DEFAULT \"unknown\" NOT NULL AFTER return");
			$ok = 0;
			foreach($functionUPDATES as $v) {
				if (mysql_query($v, $link)) {
					$ok++;
				}
			}
			if (!$ok) {
				$ret .= '» Updates in table \'function\' were already executed.<BR>';
			} else {
				$ret .= '» Updates in table \'function\' were successfully executed.<BR>';
			}

			// OTHER UPDATES
			$colUPDATES = 	array("ALTER TABLE `param` ADD `ref` CHAR(1)  NOT NULL AFTER default_value",
								"ALTER TABLE `property` ADD `value` VARCHAR(255)  AFTER datatype");
			$ok = 0;
			foreach($colUPDATES as $v) {
				if (mysql_query($v, $link)) {
					$ok++;
				}
			}
			if (!$ok) {
				$ret .= '» Updates in tables \'param\' and \'property\' were already executed.<BR>';
			} else {
				$ret .= '» Updates in tables \'param\' and \'property\' were successfully executed.<BR>';
			}

			$ret .= "<BR><font color='#0000FF'>Your phpScribe database was updated!!</font><BR><BR>";

			returnMessage($ret);			
		}		
		else if ($fromVersion == '0.8' && $toVersion == '0.9')
		{
			$ret .= "There's no database updates from <B>phpScribe 0.8</B> to <B>phpScribe 0.9</B>!<BR>";
			$ret .= "<BR><font color='#0000FF'>Your phpScribe is ready to use!!</font><BR><BR>";			
			returnMessage($ret);
		}		
		else {
			returnError("Unknown update: $fromVersion -> $toVersion");
		}
	}
}

//!---------------------------------------------
// Função para testar a conexão ao banco
//!---------------------------------------------
function testConnection() {
	global $PS_USER_CFG;
	if ( (isset($PS_USER_CFG['DATABASE_HOST'])) && (isset($PS_USER_CFG['DATABASE_USER'])) && (isset($PS_USER_CFG['DATABASE_PASS'])) ) {
		if (!function_exists("mysql_connect")) {
			returnError("Your server doesn't have mySQL support.<BR>phpScribe's not able to run without a functional mySQL database.<BR><BR><A HREF='http://www.mysql.com' STYLE='color:#ff0000;text-decoration:underline;font-weight:bold' TARGET='_blank'>Wanna download mySQL?</A>");
			return FALSE;
		} else if (!$link = @mysql_connect($PS_USER_CFG['DATABASE_HOST'],$PS_USER_CFG['DATABASE_USER'],$PS_USER_CFG['DATABASE_PASS'])) {
			returnError("It wasn't possible to connect to yout mySQL database using the current settings!<BR>Check your <B>DATABASE_HOST</B>, <B>DATABASE_USER</B> and <B>DATABASE_PASS</B>!");
			return FALSE;
		} else if (!@mysql_select_db($PS_USER_CFG['DATABASE_BASE'],$link)) {
			returnError("It wasn't possible to select the provided database <B>".$PS_USER_CFG['DATABASE_BASE']."</B>!");
			return FALSE;
		} else {
			if ((!isset($PS_USER_CFG)) || (!file_exists($PS_USER_CFG['GLOBAL_PATH']."/index.php"))) {
				returnError("The system couldn't find the phpScribe's main file index.php.<BR>Is the server path value right? (e.g.: \www\phpscribe)</B>");
				return FALSE;
			} else {
				return $link;
			}
		}
	} else {
    	returnError("The system couldn't find the database settings (host, user or password)...");
		return FALSE;
	}
}

//!---------------------------------------------
// Função para imprimir uma mensagem de erro
//!---------------------------------------------
function returnError($msg) {
	echo "<TR HEIGHT='40'><TD WIDTH='15' BGCOLOR='#F2F2F2'>&nbsp;</TD><TD COLSPAN='3' CLASS='bignormal' BGCOLOR='#F2F2F2'><BR><FONT COLOR='#FF0000'><B>FATAL ERROR!</B><BR><BR>" . $msg . "</FONT><BR><BR></TD></TR>";
}

//!---------------------------------------------
// Função geral para impressão de mensagens da instalação/configuração
//!---------------------------------------------
function returnMessage($msg) {
	echo "<TR HEIGHT='40'><TD WIDTH='15' BGCOLOR='#F2F2F2'>&nbsp;</TD><TD COLSPAN='3' CLASS='bignormal' BGCOLOR='#F2F2F2'><BR><FONT COLOR='#007700'>" . $msg . "</FONT><BR></TD></TR>";
}

?>
Return current item: PhpScribe Documentation Generator