-
Notifications
You must be signed in to change notification settings - Fork 1
/
convertPHPMyAdminXML2UnitTestXML.CLI.php
123 lines (104 loc) · 3.48 KB
/
convertPHPMyAdminXML2UnitTestXML.CLI.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* @author Matthias Tylkowski
*/
// get all input arguments starting from first, 0 is function call itself
$mpInputFiles = array_slice($argv, 1);
// if there are no arguments print usage information
if(count($mpInputFiles) >= 1){
$oConverter = new Converter();
$oConverter->convert($mpInputFiles);
$oConverter->save();
}else{
print "Usage: php convertPHPMyAdminXML2UnitTestXML.CLI.php filename1 filename2 ... > path to file to create";
}
class Converter{
private $oDom;
private $oRootNode;
// temp variables
private $sCurrentTable;
private $oTmpTable;
public function __construct(){
// setup dom with root node
$this->oDom = new DOMDocument('1.0', 'utf-8');
$this->oRootNode = $this->oDom->createElement('dataset');
}
public function convert(array $mpInputFiles){
// run through all input arguments, create a temp dom and load the files
foreach($mpInputFiles as $sInputFileName){
$oTmpDom = new DOMDocument();
$oTmpDom->load($sInputFileName);
// get all <table> elements
$oNodeList = $oTmpDom->getElementsByTagName('table');
foreach($oNodeList as $oNode){
// ignore table elements that are in the pma namespace
if(strpos($oNode->nodeName, 'pma') === false){
// store the table name
$sCurrentTable = $oNode->getAttribute('name');
// if no table was set or current table name differs from stored one
if(is_null($this->sCurrentTable) || $sCurrentTable !== $this->sCurrentTable) {
// if there was already a stored table name append the table to the root node
if($sCurrentTable !== $this->sCurrentTable && !is_null($this->sCurrentTable)){
$this->oRootNode->appendChild($this->oTmpTable);
}
// create a new table and set its name
$this->sCurrentTable = $sCurrentTable;
$this->oTmpTable = $this->oDom->createElement('table');
$this->oTmpTable->setAttribute('name', $this->sCurrentTable);
// fetch all column names from the table
$this->defineColumns($oNode);
}
$this->formatTableData($oNode);
}
}
}
$this->oRootNode->appendChild($this->oTmpTable);
$this->oDom->appendChild($this->oRootNode);
}
/**
* collect all column elements and convert them to rows with column values
* @param DOMNode $oNode
*/
protected function formatTableData(DOMNode $oNode){
$oRow = $this->oDom->createElement('row');
foreach($oNode->getElementsByTagName('column') as $oElement){
$tmpElement = $this->oDom->createElement('value');
// create temp text node to have escaped values
$tmpTextNode = $this->oDom->createTextNode($oElement->nodeValue);
$tmpElement->appendChild($tmpTextNode);
$oRow->appendChild($tmpElement);
}
$this->oTmpTable->appendChild($oRow);
}
/**
* collect all columns from the current table and convert them to another format
* @param DOMNode $oNode
*/
protected function defineColumns(DOMNode $oNode){
foreach($oNode->getElementsByTagName('column')as $oElement){
$tmpElement = $this->oDom->createElement('column', $oElement->getAttribute('name'));
$this->oTmpTable->appendChild($tmpElement);
}
}
/**
* send the xml string to stdout
* @return string
*/
public function save(){
return $this->oDom->save('php://stdout');
}
}
/**
* Helper function for printing DOMNodes
* @param DOMNode $node
* @return string
*/
function dump_child_nodes($node)
{
$output = '';
$owner_document = $node->ownerDocument;
foreach ($node->childNodes as $el){
$output .= $owner_document->saveXML($el);
}
return $output;
}