/
modx_writer.php
236 lines (207 loc) · 5.39 KB
/
modx_writer.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/**
*
* @package MODX Generator
* @version $Id$
* @copyright (c) tumba25
* @license http://opensource.org/licenses/gpl-license.php GNU Public License v2
*
*/
/**
* Writes the diff to a modx install file
*/
class modx_writer extends XMLWriter
{
/**
* These vars needs to be set by the caller.
*/
var $modx_version = 'modx-1.2.3.xsd'; // The current MODX version.
/**
* Starts the xml and writes the header.
*/
function modx_writer()
{
$this->openMemory();
$this->setIndent(true);
$this->setIndentString("\t");
// The header
$this->startDocument('1.0', 'UTF-8', 'yes');
$this->writePi('xml-stylesheet', 'type="text/xsl" href="modx.prosilver.en.xsl"');
$this->writeComment('NOTICE: Please open this file in your web browser. If presented with a security warning, you may safely tell it to allow the blocked content.');
$this->writeComment('For security purposes, please check: http://www.phpbb.com/mods/ for the latest version of this MOD.\nAlthough MODs are checked before being allowed in the MODs Database there is no guarantee that there are no security problems within the MOD.\nNo support will be given for MODs not found within the MODs Database which can be found at http://www.phpbb.com/mods/');
// <mod>
$this->startElement('mod');
$this->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$this->writeAttribute('xmlns', 'http://www.phpbb.com/mods/xml/' . $this->modx_version);
// <header>
$this->startElement('header');
// <meta>
$this->write_element('meta', '', array(
'name' => 'generator',
'content' => 'MODX file generated by MODX Generator by tumba25',
), false, false);
// </header>
$this->endElement();
$this->startElement('action-group');
}
/**
* Writes the edits to xml
*
* @param $name, the tag name.
* @param $text, text for the tag.
* @param $attributes, attributes if the are any.
*/
public function write_element($name, $text, $attributes = false)
{
if ($text == '' && $attributes == false)
{
// nothing to write
return;
}
$this->startElement($name);
if ($attributes != false)
{
foreach ($attributes as $key => $value)
{
if ($value != '')
{
$this->writeAttribute($key, $value);
}
}
}
if ($text != '')
{
// We need to check for <![CDATA[ ]]> in the strings.
// Accoring to DavidIQ this should work.
// Can't use a regular expresion for this, there might be only ]]> in the files.
$find = array(
'<![CDATA[',
']]>',
'<![CDATA[',
']]>');
$replace = array(
'<![CDATA[',
']]>',
']]><![CDATA[<![CDATA[',
']]>]]><![CDATA[');
$text = str_replace($find, $replace, $text);
$this->writeCdata($text);
}
$this->endElement();
}
/**
* Write the diff to the modx file.
*
* @param $file, file name.
* @param $file_diff, the famous diff-
*/
public function generate_xml($file, $file_diff)
{
$last_change = 0;
// On Windows the directory separator will be \, so we need to replace that.
$file = str_replace('\\', '/', $file);
$this->startElement('open');
$this->writeAttribute('src', $file);
foreach ($file_diff as $line => $value)
{
if (is_array($value))
{
if (isset($value['find']))
{
$this->startElement('edit');
// Both inlines and normal edits needs to start with a FIND
$this->write_element('find', $value['find'][0]);
if (isset($value['find'][1]))
{
$this->write_element('find', $value['find'][1]);
}
}
if ($value['type'] == INLINE)
{
// Inline edit
foreach ($value['changes'] as $change)
{
switch($change['add-type'])
{
case ADD_BEFORE:
$change_type = 'before-add';
break;
case REPLACE:
$change_type = 'replace-with';
break;
default:
$change_type = 'after-add';
break;
}
if (isset($change['inline-find']))
{
$this->startElement('inline-edit');
$this->write_element('inline-find', $change['inline-find'][0]);
if (isset($change['inline-find'][1]))
{
// In-line replaces has two finds.
$this->write_element('inline-find', $change['inline-find'][1]);
}
}
$this->write_element('inline-action', $change['add'], array('type' => $change_type));
if (isset($change['close']))
{
$this->endElement();
}
}
}
else if ($value['type'] == EDIT)
{
// Edit
if (isset($value['add']))
{
switch($value['add-type'])
{
case ADD_BEFORE:
$change_type = 'before-add';
break;
case REPLACE:
$change_type = 'replace-with';
break;
default:
$change_type = 'after-add';
break;
}
$this->write_element('action', $value['add'], array('type' => $change_type));
}
else if (!empty($value['del']))
{
$this->write_element('action', '', array('type' => 'replace-with'));
}
}
else
{
// Eh?
}
if (isset($value['close']))
{
$this->endElement();
}
}
}
$this->endElement();
}
/**
* Closes the file and either returns the modx or writes it to file
*/
public function modx_close($out_file = '')
{
$this->endElement();
$this->endElement();
$out_string = $this->outputMemory();
if ($out_file)
{
file_put_contents($out_file, $out_string);
}
else
{
echo $out_string . "\n";
}
}
}
?>