File:
1.00.4a/server/bin/import.php (
View as Code)
1: 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: $parser=xml_parser_create();
172: xml_set_element_handler($parser,Array( $this, "startElement" ),
173: Array( $this, "endElement" ) );
174: xml_set_character_data_handler($parser,Array( $this, "charData" ));
175:
176: $return=true;
177:
178: if (!xml_parse($parser,$xml,true))
179: {
180: $this->LastError="XML Error ".xml_error_string(xml_get_error_code($parser));
181: $this->LastError.=" at line ".xml_get_current_line_number($parser);
182: $return=false;
183: }
184:
185: xml_parser_free($parser);
186:
187: return true;
188: }
189:
190: function ParseFile($xmlfile)
191: {
192: $fp=fopen($xmlfile,"r")
193: or die("Could not open XML file ".$xmlfile);
194: $data="";
195: while (!feof($fp))
196: $data.=fgets($fp,4096);
197: fclose($fp);
198: return $this->Parse($data);
199: }
200:
201:
202: }
203:
204:
205: $xml=new TImportXML();
206: if ($xml->ParseFile($file)===false)
207: {
208: echo "Error Encountered!\n";
209: echo $xml->LastError;
210: echo "\n\n";
211: exit();
212: }
213:
214: echo "Nodes: ".$xml->Nodes." Local Tests: ".$xml->Localtests."\n";
215: if ($live)
216: {
217: echo "Starting FreeNATS for live import... ";
218: @include("include.php");
219: if (!isset($BaseDir)) $BaseDir="../base/";
220:
221: require($BaseDir."nats.php");
222: $NATS->Start();
223: echo "Ok\n";
224: }
225: foreach($xml->NodeList as $nodeid=>$node)
226: {
227:
228: echo "NodeID: ".$nodeid." - Parsing Properties...\n";
229: $fields="(";
230: $values="(";
231: $first=true;
232: foreach($node as $key=>$val)
233: {
234: if ($first) $first=false;
235: else
236: {
237: $fields.=",";
238: $values.=",";
239: }
240: $fields.=$key;
241: $values.="\"".$val."\"";
242: echo " ".$key." => ".$val."\n";
243: }
244: $fields.=")";
245: $values.=")";
246:
247: $q="INSERT INTO fnnode".$fields." VALUES".$values;
248: if ($debug) echo " SQL: ".$q."\n";
249:
250: if ($live)
251: { // actual import
252: $NATS->DB->Query($q);
253: if ($NATS->DB->Error())
254: {
255: echo "Failed: SQL Error: ".$NATS->DB->Error_String()."\n";
256: $xml->NodeList[$nodeid]['import_success']=false;
257: }
258: else if ($NATS->DB->Affected_Rows()<=0)
259: {
260: echo "Failed: SQL INSERT Failed: Duplicate or Blank?\n";
261: $xml->NodeList[$nodeid]['import_success']=false;
262: }
263: else
264: {
265: echo "Succeeded: SQL INSERT Success\n";
266: $xml->NodeList[$nodeid]['import_success']=true;
267: }
268: }
269: else
270: { // dummy run
271: echo " Live Import: No\n";
272: $xml->NodeList[$nodeid]['import_success']=true;
273: }
274: echo "\n";
275: }
276:
277: foreach($xml->LocaltestList as $test)
278: {
279: //print_r($test);
280: //exit();
281: echo "Test ".$test['testtype']." for ".$test['nodeid']."\n";
282: $process=false;
283: if (isset($xml->NodeList[$test['nodeid']]))
284: {
285: if ( isset($xml->NodeList[$test['nodeid']]['import_success']) && $xml->NodeList[$test['nodeid']]['import_success'] )
286: {
287: echo "Processing Test\n";
288: $process=true;
289: }
290: else
291: {
292: echo "Skipping Test: Node failed to import in this data\n";
293: }
294: }
295: else
296: {
297: echo "WARNING: Node not created in this script for this test\n";
298: $process=true;
299: }
300: if ($process)
301: {
302: $fields="(";
303: $values="(";
304: $first=true;
305: foreach($test as $key=>$val)
306: {
307: if ($first) $first=false;
308: else { $fields.=","; $values.=","; }
309: $fields.=$key;
310: $values.="\"".$val."\"";
311: echo " ".$key." => ".$val."\n";
312: }
313: $fields.=")";
314: $values.=")";
315: $q="INSERT INTO fnlocaltest".$fields." VALUES".$values;
316: if ($debug) echo " SQL: ".$q."\n";
317: if ($live)
318: { // actual import
319: $NATS->DB->Query($q);
320: if ($NATS->DB->Error())
321: {
322: echo "Failed: SQL Error: ".$NATS->DB->Error_String()."\n";
323:
324: }
325: else if ($NATS->DB->Affected_Rows()<=0)
326: {
327: echo "Failed: SQL INSERT Failed: Duplicate or Blank?\n";
328:
329: }
330: else
331: {
332: echo "Succeeded: SQL INSERT Success\n";
333:
334: }
335: }
336: }
337: echo "\n";
338: }
339:
340: if ($live) $NATS->Stop();
341: ?>