File: 1.01.0a/server/bin/import.php (View as HTML)

  1: <?php
  2: /* -------------------------------------------------------------
  3: This file is part of FreeNATS
  4: 
  5: FreeNATS is (C) Copyright 2008 PurplePixie Systems
  6: 
  7: FreeNATS is free software: you can redistribute it and/or modify
  8: it under the terms of the GNU General Public License as published by
  9: the Free Software Foundation, either version 3 of the License, or
 10: (at your option) any later version.
 11: 
 12: FreeNATS is distributed in the hope that it will be useful,
 13: but WITHOUT ANY WARRANTY; without even the implied warranty of
 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15: GNU General Public License for more details.
 16: 
 17: You should have received a copy of the GNU General Public License
 18: along with FreeNATS.  If not, see www.gnu.org/licenses
 19: 
 20: For more information see www.purplepixie.org/freenats
 21: -------------------------------------------------------------- */
 22: 
 23: 
 24: // FreeNATS Bulk Importer
 25: // Usage php import.php filename.xml
 26: 
 27: echo "FreeNATS Bulk Importer - www.purplepixie.org/freenats\n";
 28: 
 29: if ($argc<2)
 30: 	{
 31: 	echo "Usage: php import.php filename.xml [--live] [--debug]\n\n";
 32: 	exit();
 33: 	}
 34: 	
 35: 
 36: 
 37: 	
 38: $live=false;
 39: $debug=false;
 40: for ($a=1; $a<$argc; $a++)
 41: 	{
 42: 	if ($argv[$a]=="--live") $live=true;
 43: 	else if ($argv[$a]=="--debug") $debug=true;
 44: 	else $file=$argv[$a];
 45: 	}
 46: 	
 47: echo "Importing File: ".$file." => Live Import: ";
 48: if ($live) echo "Yes";
 49: else echo "No";
 50: echo "\n\n";
 51: 
 52: 
 53: class TImportXML
 54: {
 55: var $Init=false;
 56: var $Elements=0;
 57: var $Nodes=0;
 58: var $NodeList=array();
 59: var $Localtests=0;
 60: var $LocaltestList=array();
 61: 
 62: var $NodeDefaults=array();
 63: var $LocaltestDefaults=array();
 64: 
 65: var $in_freenats=false;
 66: var $in_node=false;
 67: var $in_localtest=false;
 68: var $in_nodedefault=false;
 69: var $in_localtestdefault=false;
 70: var $in_test=false;
 71: var $depth=0;
 72: var $last_element="";
 73: var $cur_id="";
 74: 
 75: function startElement($parser, $name, $attrs)
 76: {
 77: if ($name=="FREENATS-DATA") $this->in_freenats=true;
 78: if (!$this->in_freenats) return 0;
 79: 
 80: if ($name=="NODE")
 81: 	{
 82: 	$this->in_node=true;
 83: 	$this->Nodes++;
 84: 	$this->NodeList[$attrs['NODEID']]=$this->NodeDefaults;
 85: 	$this->cur_id=$attrs['NODEID'];
 86: 	}
 87: else if ($name=="DEFAULT")
 88: 	{
 89: 	if ($attrs['TYPE']=="node") 
 90: 		{
 91: 		echo "Node Default Section Starting\n";
 92: 		$this->in_nodedefault=true;
 93: 		}
 94: 	else if ($attrs['TYPE']=="localtest") 
 95: 		{
 96: 		echo "Localtest Default Section Starting\n";
 97: 		$this->in_localtestdefault=true;
 98: 		}
 99: 	}
100: else if ($name=="LOCALTEST") 
101: 	{
102: 	$this->in_localtest=true;
103: 	//echo "In Local Test\n";
104: 	$this->LocaltestList[$this->Localtests]=$this->LocaltestDefaults;
105: 	$this->Localtests;
106: 	}
107: $this->last_element=$name;
108: $this->depth++;
109: $this->Elements++;
110: }
111: 
112: function endElement($parser, $name)
113: {
114: $this->depth--;
115: if ($name=="LOCALTEST") 
116: 	{
117: 	//echo "Finished Localtest\n";
118: 	$this->in_localtest=false;
119: 	$this->Localtests++;
120: 	}
121: else if ($name=="NODE") $this->in_node=false;
122: else if ($name=="DEFAULT") 
123: 	{
124: 	$this->in_nodedefault=false;
125: 	$this->in_localtestdefault=false;
126: 	echo "Default Section End\n\n";
127: 	}
128: else if ($name=="FREENATS-DATA") $in_freenats=false;
129: }
130: 
131: function charData($parser,$data)
132: {
133: if (!$this->in_freenats) return 0;
134: $data=trim($data);
135: if ($data!="")
136: 	{
137: 	if ($this->in_node)
138: 		{
139: 		$this->NodeList[$this->cur_id][strtolower($this->last_element)]=$data;
140: 		echo "Node Data: ".$data."\n";
141: 		}
142: 	else if ($this->in_nodedefault)
143: 		{
144: 		echo " Default: ".strtolower($this->last_element)." => ".$data."\n";
145: 		$this->NodeDefaults[strtolower($this->last_element)]=$data;
146: 		}
147: 	else if ($this->in_localtestdefault)
148: 		{
149: 		echo " Default: ".strtolower($this->last_element)." => ".$data."\n";
150: 		$this->LocaltestDefaults[strtolower($this->last_element)]=$data;
151: 		}
152: 	else if ($this->in_localtest)
153: 		{
154: 		$test=$this->Localtests;
155: 		//echo "***".$data;
156: 		$this->LocaltestList[$test][strtolower($this->last_element)]=$data;
157: 		}
158: 	//else echo $data;
159: 	
160: 	}
161: }
162: 
163: function Error()
164: {
165: return $this->LastError;
166: }
167: 
168: function Parse($xml)
169: {
170: $this->Init=true;
171: if (get_magic_quotes_gpc()) $xml=stripslashes($xml);
172: $parser=xml_parser_create();
173: xml_set_element_handler($parser,Array( $this, "startElement" ),
174: 	Array( $this, "endElement" ) );
175: xml_set_character_data_handler($parser,Array( $this, "charData" ));
176: 
177: $return=true;
178: 
179: if (!xml_parse($parser,$xml,true))
180: 	{
181: 	$this->LastError="XML Error ".xml_error_string(xml_get_error_code($parser));
182: 	$this->LastError.=" at line ".xml_get_current_line_number($parser);
183: 	$return=false;
184: 	}
185: 
186: xml_parser_free($parser);
187: 
188: return true;
189: }
190: 
191: function ParseFile($xmlfile)
192: {
193: $fp=fopen($xmlfile,"r")
194:  or die("Could not open XML file ".$xmlfile);
195: $data="";
196: while (!feof($fp))
197: 	$data.=fgets($fp,4096);
198: fclose($fp);
199: return $this->Parse($data);
200: }
201: 
202: 
203: }
204: 
205: 
206: $xml=new TImportXML();
207: if ($xml->ParseFile($file)===false)
208: 	{
209: 	echo "Error Encountered!\n";
210: 	echo $xml->LastError;
211: 	echo "\n\n";
212: 	exit();
213: 	}
214: 	
215: echo "Nodes: ".$xml->Nodes."     Local Tests: ".$xml->Localtests."\n";
216: if ($live)
217: 	{
218: 	echo "Starting FreeNATS for live import... ";
219: 	@include("include.php");
220: 	if (!isset($BaseDir)) $BaseDir="../base/";
221: 
222: 	require($BaseDir."nats.php");
223: 	$NATS->Start();
224: 	echo "Ok\n";
225: 	}
226: foreach($xml->NodeList as $nodeid=>$node)
227: 	{
228: 
229: 	echo "NodeID: ".$nodeid." - Parsing Properties...\n";
230: 	$fields="(";
231: 	$values="(";
232: 	$first=true;
233: 	foreach($node as $key=>$val)
234: 		{
235: 		if ($first) $first=false;
236: 		else
237: 			{
238: 			$fields.=",";
239: 			$values.=",";
240: 			}
241: 		$fields.=$key;
242: 		$values.="\"".$val."\"";
243: 		echo "  ".$key." => ".$val."\n";
244: 		}
245: 	$fields.=")";
246: 	$values.=")";
247: 	
248: 	$q="INSERT INTO fnnode".$fields." VALUES".$values;
249: 	if ($debug) echo "  SQL: ".$q."\n";
250: 	
251: 	if ($live)
252: 		{ // actual import
253: 		$NATS->DB->Query($q);
254: 		if ($NATS->DB->Error())
255: 			{
256: 			echo "Failed: SQL Error: ".$NATS->DB->Error_String()."\n";
257: 			$xml->NodeList[$nodeid]['import_success']=false;
258: 			}
259: 		else if ($NATS->DB->Affected_Rows()<=0)
260: 			{
261: 			echo "Failed: SQL INSERT Failed: Duplicate or Blank?\n";
262: 			$xml->NodeList[$nodeid]['import_success']=false;
263: 			}
264: 		else
265: 			{
266: 			echo "Succeeded: SQL INSERT Success\n";
267: 			$xml->NodeList[$nodeid]['import_success']=true;
268: 			}
269: 		}
270: 	else
271: 		{ // dummy run
272: 		echo "  Live Import: No\n";
273: 		$xml->NodeList[$nodeid]['import_success']=true;
274: 		}
275: 	echo "\n";
276: 	}
277: 
278: foreach($xml->LocaltestList as $test)
279: 	{
280: 	//print_r($test);
281: 	//exit();
282: 	echo "Test ".$test['testtype']." for ".$test['nodeid']."\n";
283: 	$process=false;
284: 	if (isset($xml->NodeList[$test['nodeid']]))
285: 		{
286: 		if ( isset($xml->NodeList[$test['nodeid']]['import_success']) && $xml->NodeList[$test['nodeid']]['import_success'] )
287: 			{
288: 			echo "Processing Test\n";
289: 			$process=true;
290: 			}
291: 		else
292: 			{
293: 			echo "Skipping Test: Node failed to import in this data\n";
294: 			}
295: 		}
296: 	else
297: 		{
298: 		echo "WARNING: Node not created in this script for this test\n";
299: 		$process=true;
300: 		}
301: 	if ($process)
302: 		{
303: 		$fields="(";
304: 		$values="(";
305: 		$first=true;
306: 		foreach($test as $key=>$val)
307: 			{
308: 			if ($first) $first=false;
309: 			else { $fields.=","; $values.=","; }
310: 			$fields.=$key;
311: 			$values.="\"".$val."\"";
312: 			echo "  ".$key." => ".$val."\n";
313: 			}
314: 		$fields.=")";
315: 		$values.=")";
316: 		$q="INSERT INTO fnlocaltest".$fields." VALUES".$values;
317: 		if ($debug) echo "  SQL: ".$q."\n";
318: 		if ($live)
319: 			{ // actual import
320: 			$NATS->DB->Query($q);
321: 			if ($NATS->DB->Error())
322: 				{
323: 				echo "Failed: SQL Error: ".$NATS->DB->Error_String()."\n";
324: 				
325: 				}
326: 			else if ($NATS->DB->Affected_Rows()<=0)
327: 				{
328: 				echo "Failed: SQL INSERT Failed: Duplicate or Blank?\n";
329: 				
330: 				}
331: 			else
332: 				{
333: 				echo "Succeeded: SQL INSERT Success\n";
334: 				
335: 				}
336: 			}
337: 		}
338: 	echo "\n";
339: 	}
340: 	
341: if ($live) $NATS->Stop();
342: ?>