Location: PHPKode > projects > Netautor Professional > netautor/napro4/admin/database/transfer_data.php
<?php
/* +----------------------------------------------------------------------+
   | Netautor Professional Application Server                             |
   +----------------------------------------------------------------------+
   | Copyright (C) 1998-2005 digiconcept GmbH. <www.digiconcept.net>      |
   +----------------------------------------------------------------------+
   | This file is subject to license, that is bundled with this package   |
   | in the file LICENSE.TXT, and is available at through the             |
   | world-wide-web at http://www.netautor.com/license/                   |
   |                                                                      |
   | If you did not receive a copy of the license and are unable to       |
   | obtain it through the world-wide-web, please send a note to          |
   | hide@address.com so we can mail you a copy.                      |
   +----------------------------------------------------------------------+
   | Authors: Stefan Rottensteiner <hide@address.com>    |
   |          Marek  Kadziela      <hide@address.com>	      |
   |          Helli  Kleinhans  									      |
   |          Christian Unger                                             |
   |          Gregor Wollner                                              |
   +----------------------------------------------------------------------+
   | @version	$Revision: 1.15 $										  |
   +----------------------------------------------------------------------+*/

/**
 * Transfer data - executiv part.
 */

require_once('../../include/init.inc');
if(!$GLOBALS['USER']->check_feature('Admin') && !$GLOBALS['USER']->check_feature('ac_database'))
{
	$GLOBALS['USER']->login_call();
}

includeNaPro('array,form,mdoc,mdb');

$sql_world2 = null;
if (empty($sql_vendor))$sql_vendor = '';
if (empty($sql_sid)) 	$sql_sid = '';
if (empty($sql_dsn)) 	$sql_dsn = '';
if (empty($sql_host))	$sql_host = '';
if (empty($sql_uid)) 	$sql_uid = '';
if (empty($sql_pwd)) 	$sql_pwd = '';
if (!isset($sql_pcon)) 	$sql_pcon = false;
if (empty($mn_naid)) 	$mn_naid = '';

include 'transfer_data.msk';

ob_end_flush();

if (!empty($sql_vendor))
{
	if($sql_world->sql_vendor!=$sql_vendor)
	 includeNaPro( $sql_vendor );

	$target_config=array(	'uid'	=>$sql_uid,
							'pwd'	=>$sql_pwd,
							'host'	=>$sql_host,
							'sid'	=>$sql_sid,
							'pcon'	=>$sql_pcon);

	switch(strtolower($sql_vendor))
	{
			case 'oci':		$sql_world2 = new sql_oci($NA_SYSTEM,$target_config);		break;
			case 'oracle':	$sql_world2 = new sql_oci_817($NA_SYSTEM,$target_config);	break;
			case 'mysql':	$sql_world2 = new sql_mysql($NA_SYSTEM,$target_config);		break;
			case 'mysqli':	$sql_world2 = new sql_mysqli($NA_SYSTEM,$target_config);	break;
			case 'postgres':$sql_world2 = new sql_postgre($NA_SYSTEM,$target_config);	break;
			case 'odbc':	$sql_world2 = new sql_odbc($NA_SYSTEM,$target_config);		break;
			case 'mssql':	$sql_world2 = new sql_mssql($NA_SYSTEM,$target_config);		break;
	}

	if (!empty($sql_world2))
	{
		$sql_world2->sql_dsn    = $sql_dsn;		//Data Source Name
		$sql_world2->sql_connect();
	}
}

/**
 *  Transfer something from the internal system tables
 */
if(isset($start) && isset($system_tables))
{
	foreach($system_tables as $index=>$wert)
	{
		if($wert=='yes')
		{
			$erg=$sql_world->select('SELECT * FROM '.$index,0,0,SQL_FETCH_ALPHA);
			$max_erg = count($erg);
			echo("&raquo; Transfer {$max_erg} rows from table <b>{$index}</b><br>");
			for($n=0;$n<$max_erg;$n++)
			{
				set_time_limit(0);
				$sql_world2->bind_insert( $index ,$erg[$n]);
				if (!empty($sql_world2->org_errnr))
				{
					$error='';
				}
				else
				{
					$error = '<span class="textred">'.$sql_world2->org_errnr.':'.$sql_world2->org_errstr.'</span>';
				}
				echo( "&nbsp;Row {$n} transferred. $error<br>" );
			}
		}
	}

	flush();
}

/**
 * Transfer data
 */
if(isset($start) && isset($mn_naid))
{
		for($i=0;$i<count($mn_naid);$i++)
		{
			/**
			 * Transfer database definitions
			 * Note :
			 * No entries in melements
			 * No tables will be created
			 */
	       	if(isset($mnames))
			{
				$erg = $sql_world->select('SELECT * FROM mnames WHERE mn_naid='.$mn_naid[$i],0,0,SQL_FETCH_ALPHA);

				echo("&raquo; Transfer Netautor Professional database <b>".$erg[0]['mn_name']."</b><br>");

				$sql_world2->bind_insert('mnames',$erg[0]);

				if (empty($sql_world2->org_errnr))
				{
					$error='';
				}
				else
				{
					$error = '<span class="textred">'.$sql_world2->org_errnr.':'.$sql_world2->org_errstr.'</span>';
				}
				echo("&nbsp;Transfered. $error<br>");
			}

			/**
			 * Transfer database field definitions
			 */
			if(isset($melements) && !empty($mn_naid[$i]))
			{
				$db_id		 = $mn_naid[$i];
				$db_elements = mdb_list_elements($sql_world,$db_id);
				$max_erg= count($db_elements['me_name']);

				echo('&raquo; Transfer '.$max_erg.' elements<br>');

				$db_name	 	= $db_elements['me_base'];
				$base_id		= $db_elements['me_id'][ $db_name ];
				$search_tables 	= array();

				/**
				 * Create array with needed search table names
				 */
				foreach ($db_elements['me_in_area'] as $_what => $_where){
					if (empty($_where) || $_what==$_where) continue;
					$field_name		= $db_elements['me_name'][$_what];

					$search = 'na_'.$db_id;
					$is_area	= (isset($db_elements['me_content'][$field_name]) && empty($db_elements['me_content'][$field_name]));
					$is_multiple= (!empty($db_elements['me_multiple'][$field_name]));

					if ($is_area || $is_multiple){
						$search.='_'.$_what;
					}
					else if ($_where != $base_id){
						$search.='_'.$_where;
					}
					$search_tables[$search]=$search;
				}
				$search_tables = array_unique($search_tables);
				ksort($search_tables);

				// Create search tables if needed
				foreach($search_tables as $key => $table_name) {
					$table_def = array();
					$sql_world->get_table_def($table_name,$table_def);
					
					echo("&nbsp;&nbsp;Create table <b>{$table_name}</b><br>");
					if (empty($table_def)) continue;

					/* Correct some very strage effects. Default & Remark returned as '0' AND a hidden special char ..*/
					foreach($table_def['columns'] as $column_name => $column_def) {
						if (!empty($column_def['default'])) {
							if (substr(trim($column_def['default']),0,1)=="'" && substr(trim($column_def['default']),-1)=="'")
							{
								$table_def['columns'][$column_name]['default']= substr(trim($column_def['default']),1,-1);
							}
						}
						if (!empty($column_def['remark'])) {
							if (substr(trim($column_def['remark']),0,1)=="'" && substr(trim($column_def['remark']),-1)=="'")
							{
								$table_def['remark'][$column_name]['remark']= substr(trim($column_def['remark']),1,-1);
							}
						}
					}

					$sql_world2->sql_create_table($table_def);
					/* Create error message */
					if (empty($sql_world2->org_errnr)) {	$error='';	}
					else {	$error = '&nbsp;&nbsp;<span class="textred">'.$sql_world2->org_errnr.':'.$sql_world2->org_errstr.'</span><br>';}

					echo($error);

				}

				/* Transfer melements */
				$sub_erg =$sql_world->select('SELECT * FROM melements WHERE me_mn_naid = '.$db_id,0,0,SQL_FETCH_ALPHA);
				if(!empty($sub_erg)) 
				{
					foreach ($sub_erg as $key => $invalues)
					{
						echo("&raquo; Transfer definition of element <b>".$invalues['me_name'].'</b><br>');
						$sql_world2->bind_insert('melements',$invalues);
						/* Create error message */
						if (empty($sql_world2->org_errnr)) {	$error='Element transferred.<br>';}
						else {	$error = '&nbsp;&nbsp;<span class="textred">'.$sql_world2->org_errnr.':'.$sql_world2->org_errstr.'</span><br>';}
						echo($error);
						
						$element_id = $invalues['me_id'];
						/* Transfer me_settings  */
						$settings_erg =$sql_world->select('SELECT * FROM me_settings WHERE mes_me_id = '.$element_id,0,0,SQL_FETCH_ALPHA);
						if(!empty($settings_erg)) 
						{
							echo('&raquo; Transfer settings for element <b>'.$invalues['me_name'].'</b><br>');
							foreach ($settings_erg as $settings_key => $settings_invalues)
							{
								$sql_world2->bind_insert('me_settings',$settings_invalues);
								/*Create error message */
								if (empty($sql_world2->org_errnr)) {	$error='Settings transferred.<br>';	}
								else {	$error = '&nbsp;&nbsp;<span class="textred">'.$sql_world2->org_errnr.':'.$sql_world2->org_errstr.'</span><br>';}
								echo($error);
							}
						}
					}
				}
			}

		/**
		 * Transfer layouts
		 */
		if(isset($mlayouts) && !empty($mn_naid[$i]))
		{
			$erg=$sql_world->select('SELECT * FROM mlayouts WHERE mlay_mn_naid='.$mn_naid[$i],0,0,SQL_FETCH_ALPHA);

			$max_erg = count($erg);
			echo("&raquo; Transfer {$max_erg} layouts.<br>");
			for($n=0;$n<$max_erg;$n++)
			{
				set_time_limit(0);

				$layout_name 	= $erg[$n]['mlay_name'];
				$layout_id 		= $erg[$n]['mlay_id'];
				$layout_size	= strlen($erg[$n]['mlay_definition']);
				$erg[$n]['mlay_definition']=str_replace('\\','\\\\',$erg[$n]['mlay_definition']);

				$sql_world2->bind_insert('mlayouts',$erg[$n]);
				if (empty($sql_world2->org_errnr)) {
					$error='';
				}
				else {
					$error = '   <span class="textred">'.$sql_world2->org_errnr.':'.$sql_world2->org_errstr.'</span>';
				}

				echo("\n[$n] Layout <b>$layout_name</b> with id <b>$layout_id</b> transferred ({$layout_size} bytes) {$error}<br>");
				if($sql_world2->org_errnr == 1064)
				{
					echo(htmlspecialchars($sql_world2->statement));
					echo('<hr>'.htmlspecialchars($erg[$n]['mlay_definition']));
				}
				flush();
			}
		}

		/**
		 * Transfer documents
		 */
		if(!empty($mdocs) && !empty($mn_naid[$i]))
		{
			$max		= $sql_world->select('SELECT count(*) as counter FROM mdocs WHERE mdoc_mn_naid='.$mn_naid[$i],0,0,SQL_FETCH_ALPHA);

			$set_size	= 20;

			echo('&raquo; Transfer '.$max[0]['counter'].' documents<br>');

			$max = round($max[0]['counter'] / $set_size);

			$db_elements 	= mdb_list_elements($sql_world,$mn_naid[$i]);
			$db_name	 	= $db_elements['me_base'];
			$base_id		= $db_elements['me_id'][ $db_name ];
			$search_tables 	= array();
			foreach ($db_elements['me_in_area'] as $_what => $_where){
				if (empty($_where) || $_what==$_where) continue;
				$field_name		= $db_elements['me_name'][$_what];

				$search = 'na_'.$mn_naid[$i];
				$is_area	= (isset($db_elements['me_content'][$field_name]) && empty($db_elements['me_content'][$field_name]));
				$is_multiple= (!empty($db_elements['me_multiple'][$field_name]));

				if ($is_area || $is_multiple){
					$search.='_'.$_what;
				}
				else if ($_where != $base_id){
					$search.='_'.$_where;
				}
				$search_tables[$search]=$search;
			}
			$search_tables = array_unique($search_tables);
			ksort($search_tables);

			for($r=0;$r<=$max;$r++)
			{
				$temp_erg		= $sql_world->select('SELECT mdoc_id FROM mdocs WHERE mdoc_mn_naid='.$mn_naid[$i],$r*$set_size,$set_size,SQL_FETCH_ALPHA);
				$temp_erg_list = '';
				foreach($temp_erg as $key => $value)
				{
					if($temp_erg_list != '') $temp_erg_list.= ',';
					$temp_erg_list .= "'".$value['mdoc_id']."'";
				}
				$erg			= $sql_world->select('SELECT * FROM mdocs WHERE mdoc_mn_naid='.$mn_naid[$i].' AND mdoc_id IN ('.$temp_erg_list.')',0,$set_size,SQL_FETCH_ALPHA);
				$max_erg 		= count($erg);

				echo ("Transfer $max_erg documents of database <b>".$db_name.'</b><br>');

				for($n=0;$n<$max_erg;$n++)
				{
					set_time_limit(30);
					$doc_id			= $erg[$n]['mdoc_id'];
					$doc_content	= $erg[$n]['mdoc_content'];
					$doc_len		= strlen($doc_content);
					$doc_parsed		= true;

					echo("<br>[$n]DocumentID: {$doc_id} Content-Length: {$doc_len} Parsing: ".($doc_parsed?'<span class="textgreen">ok':'<span class="textred">error').'</span>');

					$exists	= $sql_world2->select('SELECT count(*) as anzahl FROM mdocs WHERE mdoc_id='.$doc_id,0,0,SQL_FETCH_ALPHA);
					$exists = !(empty($exists[0]['anzahl']));

					if ($exists) 
					{
						echo(' ... <span class="textred">Document already exists (re-new)</span>');
						/* Treat mdoc*/
						$mdoc_result= $sql_world2->bind_update('mdocs',$erg[$n],'mdoc_id='.$doc_id);
						/* Tread search tables */
						foreach ($search_tables as $key => $target_table) {
							@set_time_limit(30);
							/*Get search values */
							$sub_erg	= $sql_world->select('SELECT * FROM '.$target_table.' WHERE mdoc_id='.$doc_id,0,0,SQL_FETCH_ALPHA);
							/*Delete values in target table*/
							$sql_world2->exec('DELETE FROM '.$target_table.' WHERE mdoc_id='.$doc_id);
							/*Insert new search values in target table*/
							if(empty($sub_erg)) continue;

							foreach ($sub_erg as $sub_erg_index => $sub_erg_data) 
							{
								@set_time_limit(20);
								$sql_world2->bind_insert($target_table,$sub_erg_data);
							}
						}
					}
					else
					{
						echo(' ... <span class="textgreen">Document does not exist (create)</span>');
						/* Treat mdoc */
						$mdoc_result= $sql_world2->bind_insert('mdocs',$erg[$n]);
						if($mdoc_result > 0)
						{
							$doc_id = $erg[$n]['mdoc_id'];

							/* Treat search tables */
							foreach ($search_tables as $key => $target_table) {
								@set_time_limit(30);
								/*Get search values */

								$sub_erg	= $sql_world->select('SELECT * FROM '.$target_table.' WHERE mdoc_id='.$doc_id,0,0,SQL_FETCH_ALPHA);
								/*Insert new search values in target table*/
								if(empty($sub_erg)) continue;

								foreach ($sub_erg as $sub_erg_index => $sub_erg_data) 
								{
									@set_time_limit(20);
									$sql_world2->bind_insert($target_table,$sub_erg_data);
								}
							}
						}
					}
				}
				flush();
			}
		}
	}
}
?>
</body>
</html>
Return current item: Netautor Professional