-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
modx_gen.php
229 lines (196 loc) · 5.85 KB
/
modx_gen.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
<?php
/**
*
* @package MODX Generator
* @version $Id$
* @copyright (c) tumba25
* @license http://opensource.org/licenses/gpl-license.php GNU Public License v2
*
*/
// Get the execution time.
// Should be removed before final release.
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
$phpEx = substr(strrchr(__FILE__, '.'), 1);
$script_path = dirname(__FILE__);
$dir_separator = (strpos($script_path, '/') !== false) ? '/' : '\\';
$script_path .= $dir_separator;
require($script_path . 'diff' . $dir_separator . 'Diff.' . $phpEx);
require($script_path . 'diff' . $dir_separator . 'inline.' . $phpEx);
require($script_path . 'modx_diff.' . $phpEx);
require($script_path . 'modx_writer.' . $phpEx);
require($script_path . 'constants.' . $phpEx);
require($script_path . 'generator_config.' . $phpEx);
require($script_path . 'functions.' . $phpEx);
$args = parse_args($argv, $argc, $defaults);
$slap = $diff_files = $diff_dirs = $where_changes = false;
if (empty($args['old']) || empty($args['new']) || !empty($args['help']))
{
$slap = true;
}
else if (is_dir($args['new']) && is_dir($args['old']))
{
$diff_dirs = true;
}
else if (is_file($args['new']) && is_file($args['old']))
{
$diff_files = true;
}
else
{
$slap = true;
}
if ($slap)
{
echo 'USAGE:' . "\n";
echo 'php modx_gen.php [switches] path/to/unchanged/dir path/to/changed/dir [-r path/for/root/]' . "\n";
echo 'php modx_gen.php [switches] path/to/unchanged/file path/to/changed/file [-r path/for/root/]' . "\n\n";
echo 'The parameters to modx_gen.php needs to be after "php modx_gen.php".' . "\n";
echo '-o, --old = Original files, path can be absolute or relative.' . "\n";
echo '-n, --new = Modified files, path can be absolute or relative.' . "\n";
echo ' Both old and new need to be either files or dirs.' . "\n";
echo '-r, --root = Creates a root directory containing the files missing in old.' . "\n";
echo ' You need to specify a path to where to place the root directory.' . "\n";
echo '-m, --modxfile = path and name of MODX file to generate. Defautls to stdout.' . "\n\n";
echo ' Switches' . "\n";
echo '-c, --custom = This is an install file for a addition style or language (subsilver2 is a additional style).' . "\n";
echo ' Without --custom only the prosilver style and English language will be compared.' . "\n";
echo ' Additional languages and styles need separate install files.' . "\n";
echo '-f, --force = Replaces the root directory if it exists.' . "\n";
echo '-h, --help = print this text.' . "\n";
echo '-v, --verbose = tell what happens.' . "\n";
echo '-i, --ignore-version = ignore SVN version info at the top of files.' . "\n";
if (empty($args['help']))
{
exit(20);
}
else
{
exit(0);
}
}
if ($diff_files)
{
$file = basename($args['old']);
$old_file = file($args['old']);
$new_file = file($args['new']);
if (!empty($args['ignore_version']))
{
// Ignore the version strings.
rem_ignore($old_file, $new_file);
}
if ($old_file == $new_file)
{
echo 'The files are identical' . "\n";
}
$diff = new Text_Diff('native', array($old_file, $new_file));
unset($old_file, $new_file);
$renderer = new Text_Diff_Renderer_inline();
$file_diff = $renderer->render($diff);
unset($renderer, $diff);
$parser = new modx_diff();
$file_diff = $parser->parse($file_diff);
if (!empty($file_diff))
{
$xml = new modx_writer();
$where_changes = true;
$xml->generate_xml($file, $file_diff);
}
}
else
{
// Add / to the end of the paths if it's not already there
$args['old'] .= (substr($args['old'], -1) != $dir_separator) ? $dir_separator : '';
$args['new'] .= (substr($args['new'], -1) != $dir_separator) ? $dir_separator : '';
// Get the files
if ($args['verbose'])
{
echo 'Getting files' . "\n";
}
get_dir_contents($args['old'], $old_arr);
get_dir_contents($args['new'], $new_arr);
// Sort them alphabetically
if ($args['verbose'])
{
echo 'Sorting files' . "\n";
}
$old_arr = directory_sort($old_arr);
$new_arr = directory_sort($new_arr);
$xml = new modx_writer();
$parser = new modx_diff();
// Start with a check for new files
if ($args['verbose'])
{
echo 'Checking for missing files' . "\n";
}
$where_changes = check_missing($old_arr, $new_arr);
foreach ($old_arr as $file)
{
$ext = substr(strrchr($file, '.'), 1);
if (in_array($ext, $ignore_ext) || !file_exists($args['new'] . $file))
{
continue;
}
if ($args['verbose'])
{
echo 'Comparing file: ' . $file;
}
$old_file = file($args['old'] . $file);
$new_file = file($args['new'] . $file);
if (!empty($args['ignore_version']))
{
// Ignore the version strings.
rem_ignore($old_file, $new_file);
}
if ($old_file != $new_file)
{
if (filesize($args['old'] . $file) == 0)
{
echo $args['old'] . $file . ' is empty, can\'t create a find in an empty file.' . "\n";
continue;
}
if ($args['verbose'])
{
echo '... Differences found.' . "\n";
}
$diff = new Text_Diff('native', array($old_file, $new_file));
unset($old_file, $new_file);
$renderer = new Text_Diff_Renderer_inline();
$file_diff = $renderer->render($diff);
unset($renderer, $diff);
$file_diff = $parser->parse($file_diff);
if ($file_diff)
{
$where_changes = true;
$xml->generate_xml($file, $file_diff);
}
}
else if ($args['verbose'])
{
echo '... Identical.' . "\n";
}
}
}
if (isset($xml) && $where_changes)
{
$out_file = (!empty($args['modxfile'])) ? $args['modxfile'] : '';
$xml->modx_close($out_file);
}
else
{
echo 'There where no changes' . "\n";
}
// Should be removed before final release.
if ($args['verbose'])
{
echo 'Memory peak: ' . memory_get_peak_usage(true) . "\n";
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo 'Execution time: ' . $totaltime . ' seconds' . "\n";
}
exit(0);