/
functions.php
213 lines (193 loc) · 4.49 KB
/
functions.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
<?php
/**
*
* @package MODX Generator
* @version $Id$
* @copyright (c) tumba25
* @license http://opensource.org/licenses/gpl-license.php GNU Public License v2
*
*/
/**
* parses the args sent to the script
*/
function parse_args($argv, $defaults)
{
$args = $defaults;
foreach ($argv as $key => $value)
{
switch($value)
{
case '-o':
case '--old':
$args['old'] = $argv[$key + 1];
break;
case '-n':
case '--new':
$args['new'] = $argv[$key + 1];
break;
case '-f':
case '--outfile':
$args['outfile'] = $argv[$key + 1];
break;
case '-v':
case '--verbose':
$args['verbose'] = ($args['verbose']) ? false : true;
break;
case '-h':
case '--help':
$args['help'] = true;
break;
case '-c':
case '--custom':
$args['custom'] = ($args['custom']) ? false : true;
break;
case '-i':
case '--ignore-version':
$args['ignore_version'] = ($args['ignore_version']) ? false : true;
break;
case '-r':
case '--root':
$args['root'] = $argv[$key + 1];
break;
default:
continue;
break;
}
}
return($args);
}
/**
* Removes ingored parts from the files before diffing.
*/
function rem_ignore(&$old_file, &$new_file)
{
// The SVN version string is usually at line 5, but it can be anywhere in the beginning.
// Let's check the 20 first lines only. And assume it's in the same place in both files.
// Otherwise let's diff that to.
for ($i = 0; $i < 20; $i++)
{
if (strpos($old_file[$i], '@version') !== false && strpos($new_file[$i], '@version') !== false)
{
// The easiest way to ignore them is to make them identical.
$new_file[$i] = $old_file[$i];
break;
}
}
}
/**
* Checks the directory arrays for missing files in $old_dir and writes a copy-tag if files are missing.
*/
function check_missing($old_arr, $new_arr, $copy = false)
{
global $xml, $where_changes, $dir_separator, $args;
$missing = false;
foreach ($new_arr as $file)
{
if (!in_array($file, $old_arr))
{
if (!$missing)
{
if ($copy)
{
if (!mkdir($copy . $dir_separator . 'root'))
{
echo 'Could not create root directory in: ' . $copy . "\n";
exit;
}
$copy .= $dir_separator . 'root' . $dir_separator;
}
$missing = true;
$where_changes = true;
$xml->startElement('copy');
}
if ($copy)
{
// Copy files to root/.
// First check that the target directory exists
if ($args['verbose'])
{
echo 'Copying: ' . $file . "\n";
}
$dir = dirname($file);
$dir_arr = explode($dir_separator, $dir);
$dir = '';
foreach ($dir_arr as $value)
{
$dir .= (($dir == '') ? '' : $dir_separator) . $value;
if(!file_exists($copy . $dir))
{
if (!mkdir($copy . $dir))
{
echo 'Could not create: ' . $copy . $dir . "\n";
}
}
}
if (!copy($args['new'] . $file, $copy . $file))
{
echo 'Could not copy: ' . $file . "\n";
exit;
}
}
// On Windows the directory separator will be \, so we need to handle that.
$file = str_replace('\\', '/', $file);
$xml->write_element('file', '', array('from' => 'root/' . $file, 'to' => $file));
}
}
if ($missing)
{
$xml->endElement();
return(true);
}
return(false);
}
/**
* Sorts the directory in alphabetical order, files first.
*/
function directory_sort($dir)
{
$filenames = $directories = $files = array();
foreach ($dir as $key => $value)
{
$files[$key] = $value;
$filenames[$key] = basename($value);
$directories[$key] = dirname($value);
}
array_multisort($directories, SORT_STRING, $filenames, SORT_STRING, $files);
return($files);
}
/**
* Reads a directory recursive and puts the result in a array.
* From evil<3s diff_tools
* Slightly modified.
*/
function get_dir_contents($dir, &$dir_arr, $base = '')
{
global $ignore, $args, $dir_separator;
$ignore = array_merge(array('.', '..'), $ignore);
$handle = opendir($dir);
while (($file = readdir($handle)) !== false)
{
if (!in_array($file, $ignore))
{
$path = $dir . $dir_separator . $file;
if (!$args['custom'] && is_dir($path))
{
// Only diff Prosilver and English
// Other styles and languages require their own install files.
if ((basename($dir) == 'styles' && $file != 'prosilver') || (basename($dir) == 'language' && $file != 'en'))
{
continue;
}
}
if (is_file($path))
{
$dir_arr[] = $base . $file;
}
else if (is_dir($path))
{
get_dir_contents($path, $dir_arr, $base . $file . $dir_separator);
}
}
}
closedir($handle);
}