Location: PHPKode > projects > Netautor Professional > netautor/napro4/admin/database/reconstruct/reconstruct_action.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>	      |
   |          Gregor Wollner                                              |
   |          Christian Unger                                             |
   |          Helli  Kleinhans  									      |
   +----------------------------------------------------------------------+*/

   /**
    * A simple tool to reconstruct MDOCS from the search tables
	*
	* @author	Stefan	Rottensteiner
	* @version	$Revision: 1.6 $
	*/
	require_once('../../../include/init.inc');

	if(!$USER->check_feature('Admin') && !$USER->check_feature('ac_database'))
	{
		$USER->login_call();
	}

	includeNaPro('mdb,mdoc');

	$SQL	= $sql_world;

	/* Vom User eigegebene Werte gege. bereinigen 1034563 */
	$_POST['CFG']['MDOC_ID'] = preg_replace('/[^0-9,]/','',$_POST['CFG']['MDOC_ID']);

	@ob_end_flush();

	function reconstruct_build_document($sql_result,$SCHEMA_DEF,$AREA_DEF,&$DOCUMENT)
	{
		if (isset($AREA_DEF['me_me_id'])) $AREA_DEF = $AREA_DEF['me_me_id'];

		foreach ($AREA_DEF as $AREA_ID => $AREA_ELEMENTS)
		{
			if (empty($AREA_ID)) continue;
			$AREA_NAME	=	$SCHEMA_DEF['me_name'][ $AREA_ID ];
			if ($AREA_NAME == $SCHEMA_DEF['me_base']) $AREA_NAME= '';

			foreach ($AREA_ELEMENTS as $index => $ELEMENT_NAME)
			{
				$ELEMENT_ID	=	$SCHEMA_DEF['me_id'][ $ELEMENT_NAME ];
				if (empty($SCHEMA_DEF['me_content'][ $ELEMENT_NAME ]))
				{
					foreach($sql_result as $result_zeile => $result_data)
					{
						$AREA_INDEX	= intval( $result_data[ $ELEMENT_NAME.'_MDV_ID']);
						reconstruct_build_document(array(0=>$result_data),$SCHEMA_DEF,array( $ELEMENT_ID => $SCHEMA_DEF['me_me_id'][ $ELEMENT_ID]),$DOCUMENT[ $ELEMENT_NAME ][ $AREA_INDEX ]);
					}
				}
				else
				{
					if (empty($SCHEMA_DEF['me_multiple'][ $ELEMENT_NAME ]))
					{
						$ELEMENT_VALUE = $sql_result[0][ $ELEMENT_NAME ];
						$DOCUMENT[ $ELEMENT_NAME ][0] = $ELEMENT_VALUE;
					}
					else
					{
						foreach($sql_result as $result_zeile => $result_data)
						{
							$ELEMENT_INDEX	=	intval($result_data[ $ELEMENT_NAME.'_MDV_ID']);
							$ELEMENT_VALUE	= 	$result_data[ $ELEMENT_NAME ];
							$DOCUMENT[ $ELEMENT_NAME ][ $ELEMENT_INDEX ] = $ELEMENT_VALUE;
						}
					}
				}
			}
		}

	}

	function reconstruct_read_document(&$SQL,$SCHEMA_DEF,$mdoc_id)
	{
		$DOCUMENT	= array();
		$SCHEMA_ID	= $SCHEMA_DEF['me_mn_naid'];

		$ROOT_ID	= $SCHEMA_DEF['me_id'][ $SCHEMA_DEF['me_base'] ];

		// Verwendete SQL Tabellen
		$TABLES		=	array( $SCHEMA_ID => 'na_'.$SCHEMA_ID );

		$WHERE		=	array();
		// Zugriffspfad für jedes einzelne Element
		$PATH		= 	array();

		if (empty($GLOBALS['CACHE_ELEMENT_ACCESS'][ $SCHEMA_ID ]))
		{

			foreach ($SCHEMA_DEF['me_me_id'] as $PARENT_ID => $AREA_ELEMENTS)
			{
				foreach($AREA_ELEMENTS as $index => $ELEMENT_NAME)
				{

					if (empty($PARENT_ID)) continue;
					$ELEMENT_ID	= $SCHEMA_DEF['me_id'][ $ELEMENT_NAME ];

					// Wurzelelement ausschliessen
					if (strtoupper($ELEMENT_NAME) != $ELEMENT_NAME) continue;

					// Bereiche an sich ausklammern
					if (empty($SCHEMA_DEF['me_content'][ $ELEMENT_NAME ])) continue;

					$PARENT_ID = $SCHEMA_DEF['me_in_area'][ $ELEMENT_ID ];

					if (!empty($SCHEMA_DEF['me_multiple'][ $ELEMENT_NAME ]))
					{	// Multiples Feld
							$WHERE[]				= "na_{$SCHEMA_ID}.mdoc_id = na_{$SCHEMA_ID}_{$ELEMENT_ID}.mdoc_id";
							$TABLES[] 				= "na_{$SCHEMA_ID}_{$ELEMENT_ID}";
							$PATH[ $ELEMENT_NAME ] 	= "na_{$SCHEMA_ID}_{$ELEMENT_ID}.na_{$ELEMENT_ID} AS {$ELEMENT_NAME}";
							$PATH[ $ELEMENT_NAME.'_MDV_ID' ] 	= "na_{$SCHEMA_ID}_{$ELEMENT_ID}.mdv_id AS {$ELEMENT_NAME}_MDV_ID";
					}
					else
					{
						if ($ROOT_ID == $PARENT_ID)
						{
							$PATH[ $ELEMENT_NAME ] 	= "na_{$SCHEMA_ID}.na_{$ELEMENT_ID} AS {$ELEMENT_NAME}";
						}
						else
						{
							$WHERE[]				= "na_{$SCHEMA_ID}.mdoc_id = na_{$SCHEMA_ID}_{$PARENT_ID}.mdoc_id";
							$TABLES[] 				= "na_{$SCHEMA_ID}_{$PARENT_ID}";
							$PATH[ $ELEMENT_NAME ] 	= "na_{$SCHEMA_ID}_{$PARENT_ID}.na_{$ELEMENT_ID} AS {$ELEMENT_NAME}";
							$PARENT_NAME = $SCHEMA_DEF['me_name'][ $PARENT_ID ];
							if (!empty($SCHEMA_DEF['me_multiple'][ $PARENT_NAME ]))
							{
								$PATH[ $ELEMENT_NAME.'_MDV_ID' ] 	= "na_{$SCHEMA_ID}_{$PARENT_ID}.mdv_id AS {$PARENT_NAME}_MDV_ID";
							}
						}
					}

				}
			}
			$WHERE 		= array_unique($WHERE);
			$TABLES 	= array_unique($TABLES);
			$PATH 		= array_unique($PATH);

			$GLOBALS['CACHE_ELEMENT_ACCESS'][ $SCHEMA_ID ]['WHERE']	= $WHERE;
			$GLOBALS['CACHE_ELEMENT_ACCESS'][ $SCHEMA_ID ]['TABLES']	= $TABLES;
			$GLOBALS['CACHE_ELEMENT_ACCESS'][ $SCHEMA_ID ]['PATH']	= $PATH;
		}
		else
		{
			$WHERE	= $GLOBALS['CACHE_ELEMENT_ACCESS'][ $SCHEMA_ID ]['WHERE'];
			$TABLES	= $GLOBALS['CACHE_ELEMENT_ACCESS'][ $SCHEMA_ID ]['TABLES'];
			$PATH	= $GLOBALS['CACHE_ELEMENT_ACCESS'][ $SCHEMA_ID ]['PATH'];
		}

		$STATEMENT	=	"SELECT  na_{$SCHEMA_ID}.mdoc_id, ".implode(',',$PATH )." FROM ".implode(',', $TABLES). " WHERE  na_{$SCHEMA_ID}.mdoc_id = {$mdoc_id}";
		if (!empty($WHERE)) $STATEMENT.=" AND ".implode(' AND ', $WHERE);

		$result = $SQL->select($STATEMENT,0,0);
		if (!empty($SQL->errnr))
		{
			echo "Fehler : ".$SQL->errnr."<br>";
			return null;
		}

		reconstruct_build_document($result,$SCHEMA_DEF,$SCHEMA_DEF,$DOCUMENT);

		$converted_values	= array();
		$mdoc_content		= mdoc_make_xml('',$SCHEMA_DEF,$DOCUMENT,$CONVERTED_DOCUMENT);

		$CONVERTED_DOCUMENT['mdoc_content'] = $mdoc_content;
		return $CONVERTED_DOCUMENT;
	}

	
	function reconstruct_get_db_list(&$SQL)
	{
		$SCHEMATA	= mdb_list_dbs($SQL);
		$RESULT		= array();

		foreach ($SCHEMATA as $index => $schema)
		{
			$RESULT[ $schema['mn_naid'] ] = $schema['mn_name'];
		}
		return $RESULT;
	}

	/**
	 * Rekonstruiere alle Dokumente der gegebenen Netautor Datenbank Schemata
	 *
	 */
	function reconstruct_by_schemata(&$SQL,$CFG)
	{
		foreach ($CFG['SCHEMATA'] as $schema_index => $SCHEMA_ID)
		{
			$SCHEMA_DEF		= mdb_list_elements($SQL,$SCHEMA_ID,false,false);
			$SCHEMA_NAME 	= $SCHEMA_DEF['me_base'];

			echo "Determining document id's for schema <b>$SCHEMA_NAME</b> <br>";

			$STATEMENT = "SELECT mdoc_id FROM na_{$SCHEMA_ID}";
			//echo $STATEMENT;
			$result	= $SQL->select($STATEMENT,0,0);
			$result_count = count($result);
			
			if (empty($result[0]['mdoc_id'])) $result_count= 0;
			if (empty($result_count))
			{
				echo "The schema is empty<br>";
				continue;
			}

			echo "Found <b>$result_count</b> documents.<br>";

			foreach ($result as $result_index => $data)
			{

				@set_time_limit(0);
				@flush();

				$mdoc_id	= $data['mdoc_id'];
				echo "Check document #".($result_index+1)." with id [$mdoc_id] ... ";

				$check_result	= $SQL->select("SELECT mdoc_mn_naid FROM mdocs WHERE mdoc_id= {$mdoc_id}",0,0);

				if (!empty($check_result[0]['mdoc_mn_naid']))
				{
					$CHECK_SCHEMA_DEF	= mdb_list_elements($SQL,$check_result[0]['mdoc_mn_naid'],false,false);
					echo "<span class='text'><b>document exists in schema <u>{$CHECK_SCHEMA_DEF['me_base']}</u><br></b></span>";
					continue;
				}

				echo "reconstructing ! ";

				$DOCUMENT 	= reconstruct_read_document($SQL,$SCHEMA_DEF,$mdoc_id);

				$mdoc_content	= $DOCUMENT['mdoc_content'];
				unset($DOCUMENT['mdoc_content']);
				$SCHEMA_ID	= $SCHEMA_DEF['me_mn_naid'];

				$standard_header = array
							(	'mdoc_user_id'	=>((isset($GLOBALS['USER']) && !empty($GLOBALS['USER']->ID))?$GLOBALS['USER']->ID:10),
								'mdoc_creation'	=>time(),
								'mdoc_update'	=>time(),
								'mdoc_key'		=>md5(serialize($DOCUMENT)),
								'mdoc_mn_naid'	=>$SCHEMA_ID,
								'mdoc_mlay_id'	=>'',
								'mdoc_content'	=>$mdoc_content,
								'mdoc_id'		=>$mdoc_id
							);
				//print_r( $standard_header );
				$mdoc_id = $SQL->bind_insert('mdocs',$standard_header);
				
				echo "[".$SQL->errnr."]<br>";
			}

		}
	}

	/**
	 * Rekonstruiere die gegebenen Netautor Dokumente
	 *
	 */
	function reconstruct_by_mdoc_id(&$SQL,$CFG)
	{
		echo "Determining schemata for document id's ... <br>";
		$SCHEMATA 		= reconstruct_get_db_list($SQL);
		$MDOCS_SCHEMATA	= array();


		foreach ($SCHEMATA as $SCHEMA_ID => $SCHEMA_NAME)
		{

			@set_time_limit(0);
			@flush();
			$result	= $SQL->select("SELECT mdoc_id FROM na_{$SCHEMA_ID} WHERE mdoc_id IN ({$CFG['MDOC_ID']})",0,0);
			if (empty($result)) continue;

			echo "Found ".(count($result))." documents for schema <b>{$SCHEMA_NAME}</b><br>";

			$SCHEMA_DEF	= mdb_list_elements($SQL,$SCHEMA_ID,false,false);

			foreach ($result as $index => $data)
			{
				$mdoc_id	= $data['mdoc_id'];

				echo "Check document #".($index+1)." with id [$mdoc_id] ... ";

				$check_result	= $SQL->select("SELECT mdoc_mn_naid FROM mdocs WHERE mdoc_id= {$mdoc_id}",0,0);

				if (!empty($check_result[0]['mdoc_mn_naid']))
				{
					$CHECK_SCHEMA_DEF	= mdb_list_elements($SQL,$check_result[0]['mdoc_mn_naid'],false,false);
					echo "<span class='error'>document exists in schema <b>{$CHECK_SCHEMA_DEF['me_base']}</b><br></span>";
					continue;
				}

				echo "reconstructing !<br>";

				$DOCUMENT 	= reconstruct_read_document($SQL,$SCHEMA_DEF,$mdoc_id);

				$mdoc_content	= $DOCUMENT['mdoc_content'];
				unset($DOCUMENT['mdoc_content']);
				$SCHEMA_ID	= $SCHEMA_DEF['me_mn_naid'];

				$standard_header = array
							(	'mdoc_user_id'	=>((isset($GLOBALS['USER']) && !empty($GLOBALS['USER']->ID))?$GLOBALS['USER']->ID:10),
								'mdoc_creation'	=>time(),
								'mdoc_update'	=>time(),
								'mdoc_key'		=>md5(serialize($DOCUMENT)),
								'mdoc_mn_naid'	=>$SCHEMA_ID,
								'mdoc_mlay_id'	=>'',
								'mdoc_content'	=>$mdoc_content,
								'mdoc_id'		=>$mdoc_id
							);

				$mdoc_id = $SQL->bind_insert('mdocs',$standard_header);

			}
		}

	}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
	<title>digiconcept/netautor/reconstructor/action</title>
</head>
<link rel="stylesheet" type="TEXT/CSS" media="screen" href="style.css">
<?php

	if (empty($_POST['CFG']['MDOC_ID']))
	{
		if (empty($_POST['CFG']['SCHEMATA']))
		{
			$mode	="";
			$error	="No Netautor Schemata nor valid documents id's given";
		}
		else
		{
			$mode	="by_schemata";
			$text	="by given Netautor Schemata";
		}
	}
	else
	{
		$mode	="by_mdoc_id";
		$text	="by given document id's";
	}

?>

<body >
<br>
<b>
<?php echo ( empty($text) ? "<span class='error'>$error</span>" : "<span class='title'>Reconstruct $text</span>" );?>
<br>
</b>
<br>
<br>
<?php if(!empty($mode))
{
	$mode	= "reconstruct_$mode";
	$mode($SQL,$_POST['CFG']);
}?>
</body>
</html>
Return current item: Netautor Professional