aboutsummaryrefslogtreecommitdiffstats
path: root/build/build_helper.php
blob: 6a0fc0042e35c993caf6aedf98e93870e2670d2a (plain)
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?php
/**
*
* @package build
* @copyright (c) 2010 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/

class build_package
{
	var $package_infos;
	var $old_packages;
	var $versions;
	var $locations;

	// -c - context diff
	// -r - compare recursive
	// -N - Treat missing files as empty
	// -E - Ignore tab expansions
	//		not used: -b - Ignore space changes.
	// -w - Ignore all whitespace
	// -B - Ignore blank lines
	// -d - Try to find smaller set of changes
	var $diff_options = '-crNEBwd';
	var $diff_options_long = '-x images -crNEB'; // -x fonts -x imageset //imageset not used here, because it includes the imageset.cfg file. ;)

	var $verbose = false;
	var $status_begun = false;
	var $num_dots = 0;

	function build_package($versions, $verbose = false)
	{
		$this->versions = $versions;
		$this->verbose = $verbose;

		// Get last two entries
		$_latest = $this->versions[sizeof($this->versions) - 1];
		$_before = $this->versions[sizeof($this->versions) - 2];

		$this->locations = array(
			'new_version'	=> dirname(dirname(__FILE__)) . '/phpBB/',
			'old_versions'	=> dirname(__FILE__) . '/old_versions/',
			'root'			=> dirname(__FILE__) . '/',
			'package_dir'	=> dirname(__FILE__) . '/new_version/'
		);

		$this->package_infos = array(
			'package_name'			=> 'phpBB3',
			'name_prefix'			=> 'phpbb',
			'simple_name'			=> 'release-' . $_latest,
			'new_version_number'	=> $_latest,
			'short_version_number'	=> str_replace('.', '', $_latest),
			'release_filename'		=> 'phpBB-' . $_latest,
			'last_version'			=> 'release-' . $_before,
			'last_version_number'	=> $_before,
		);

		$this->package_infos['dest_dir'] = $this->locations['package_dir'] . $this->package_infos['package_name'];
		$this->package_infos['diff_dir'] = $this->locations['old_versions'] . $this->package_infos['simple_name'];
		$this->package_infos['patch_directory'] = $this->locations['package_dir'] . 'patches';
		$this->package_infos['files_directory'] = $this->locations['package_dir'] . 'files';
		$this->package_infos['update_directory'] = $this->locations['package_dir'] . 'update';
		$this->package_infos['release_directory'] = $this->locations['package_dir'] . 'release_files';

		// Old packages always exclude the latest version. ;)
		$this->old_packages = array();

		foreach ($this->versions as $package_version)
		{
			if ($package_version == $_latest)
			{
				continue;
			}

			$this->old_packages['release-' . $package_version] = $package_version . '_to_';
		}
	}

	function get($var)
	{
		return $this->package_infos[$var];
	}

	function begin_status($headline)
	{
		if ($this->status_begun)
		{
			echo "\nDone.\n\n";
		}

		$this->num_dots = 0;

		echo $headline . "\n    ";

		$this->status_begun = true;
	}

	function run_command($command)
	{
		$result = trim(`$command`);

		if ($this->verbose)
		{
			echo "    command : " . getcwd() . '$ ' . $command . "\n";
			echo "    result  : " . $result . "\n";
		}
		else
		{
			if ($this->num_dots > 70)
			{
				echo "\n";
				$this->num_dots = 0;
			}
			echo '.';
			$this->num_dots++;
		}

		flush();
	}

	function create_directory($directory, $dir_struct)
	{
		if (!file_exists($directory))
		{
			$this->run_command("mkdir $directory");
		}

		if (is_array($dir_struct))
		{
			foreach ($dir_struct as $_dir => $_dir_struct)
			{
				$this->create_directory($directory . '/' . $_dir, $_dir_struct);
			}
		}
	}

	function collect_diff_files($diff_filename, $package_name)
	{
		$diff_result = $binary = array();
		$diff_contents = file($diff_filename);

		$special_diff_contents = array();

		foreach ($diff_contents as $num => $line)
		{
			$line = trim($line);

			if (!$line)
			{
				continue;
			}

			// Special diff content?
			if (strpos($line, 'diff ' . $this->diff_options . ' ') === 0 || strpos($line, '*** ') === 0 || strpos($line, '--- ') === 0 || (strpos($line, ' Exp $') !== false && strpos($line, '$Id:') !== false))
			{
				$special_diff_contents[] = $line;
			}
			else if (strpos($line, 'diff ' . $this->diff_options . ' ') === 0 || strpos($line, '*** ') === 0 || strpos($line, '--- ') === 0 || (strpos($line, ' Exp $') !== false && strpos($line, '$Id:') !== false) || (strpos($line, ' $') !== false && strpos($line, '$Id:') !== false))
			{
				$special_diff_contents[] = $line;
			}

			// Is diffing line?
			if (strstr($line, 'diff ' . $this->diff_options . ' '))
			{
				$next_line = $diff_contents[$num+1];
				if (strpos($next_line, '***') === 0)
				{
	//			*** phpbb208/admin/admin_board.php	Sat Jul 10 20:16:26 2004
					$next_line = explode("\t", $next_line);
					$next_line = trim($next_line[0]);
					$next_line = str_replace('*** ' . $package_name . '/', '', $next_line);
					$diff_result[] = $next_line;
				}
			}

			// Is binary?
			if (preg_match('/^Binary files ' . $package_name . '\/(.*) and [a-z0-9._-]+\/\1 differ/i', $line, $match))
			{
				$binary[] = trim($match[1]);
			}
		}

		// Now go through the list again and find out which files have how many changes...
		$num_changes = array();

	/*  [1070] => diff -crN phpbb200/includes/usercp_avatar.php phpbb2023/includes/usercp_avatar.php
		[1071] => *** phpbb200/includes/usercp_avatar.php	Sat Jul 10 20:16:13 2004
		[1072] => --- phpbb2023/includes/usercp_avatar.php	Wed Feb  6 22:28:04 2008
		[1073] => *** 6,12 ****
		[1074] => !  *   $Id$
		[1075] => --- 6,12 ----
		[1076] => *** 51,59 ****
		[1077] => --- 51,60 ----
		[1078] => *** 62,80 ****
		[1079] => --- 63,108 ----
		[1080] => *** 87,97 ****
	*/
		while (($line = array_shift($special_diff_contents)) !== NULL)
		{
			$line = trim($line);

			if (!$line)
			{
				continue;
			}

			// Is diffing line?
			if (strstr($line, 'diff ' . $this->diff_options . ' '))
			{
				$next_line = array_shift($special_diff_contents);
				if (strpos($next_line, '*** ') === 0)
				{
	//			*** phpbb208/admin/admin_board.php	Sat Jul 10 20:16:26 2004
					$next_line = explode("\t", $next_line);
					$next_line = trim($next_line[0]);
					$next_line = str_replace('*** ' . $package_name . '/', '', $next_line);

					$is_reached = false;
					$prev_line = '';

					while (!$is_reached)
					{
						$line = array_shift($special_diff_contents);

						if (strpos($line, 'diff ' . $this->diff_options) === 0 || empty($special_diff_contents))
						{
							$is_reached = true;
							array_unshift($special_diff_contents, $line);
							continue;
						}

						if (strpos($line, '*** ') === 0 && strpos($line, ' ****') !== false)
						{
							$is_comment = false;
							while (!(strpos($line, '--- ') === 0 && strpos($line, ' ----') !== false))
							{
								$line = array_shift($special_diff_contents);
								if (strpos($line, ' Exp $') !== false || strpos($line, '$Id:') !== false)
								{
									$is_comment = true;
								}
							}

							if (!$is_comment)
							{
								if (!isset($num_changes[$next_line]))
								{
									$num_changes[$next_line] = 1;
								}
								else
								{
									$num_changes[$next_line]++;
								}
							}
						}
					}
				}
			}
		}

		// Now remove those results not having changes
		$return = array();

		foreach ($diff_result as $key => $value)
		{
			if (isset($num_changes[$value]))
			{
				$return[] = $value;
			}
		}

		foreach ($binary as $value)
		{
			$return[] = $value;
		}

		$diff_result = $return;
		unset($return);
		unset($special_diff_contents);

		$result = array(
			'files'		=> array(),
			'binary'	=> array(),
			'all'		=> $diff_result,
		);

		$binary_extensions = array('gif', 'jpg', 'jpeg', 'png', 'ttf');

		// Split into file and binary
		foreach ($diff_result as $filename)
		{
			if (strpos($filename, '.') === false)
			{
				$result['files'][] = $filename;
				continue;
			}

			$extension = explode('.', $filename);
			$extension = array_pop($extension);

			if (in_array($extension, $binary_extensions))
			{
				$result['binary'][] = $filename;
			}
			else
			{
				$result['files'][] = $filename;
			}
		}

		return $result;
	}

	/**
	* Collect the list of the deleted files from a list of deleted files and folders.
	*
	* @param string $deleted_filename   The full path to a file containing the list of deleted files and directories
	* @param string $package_name       The name of the package
	* @return array
	*/
	public function collect_deleted_files($deleted_filename, $package_name)
	{
		$result = array();
		$file_contents = file($deleted_filename);

		foreach ($file_contents as $line)
		{
			$line = trim($line);

			if (!$line)
			{
				continue;
			}

			$line = str_replace('Only in ' . $package_name, '', $line);
			$line = ltrim($line, '/');

			if (substr($line, 0, 1) == ':')
			{
				$replace = '';
			}
			else
			{
				$replace = '/';
			}

			$line = str_replace(': ', $replace, $line);

			if (is_dir("{$this->locations['old_versions']}{$package_name}/{$line}"))
			{
				$result = array_merge($result, $this->get_files_recursive("{$this->locations['old_versions']}{$package_name}/{$line}", $line));
			}
			else
			{
				$result[] = $line;
			}
		}

		return $result;
	}

	/**
	* Get recursively the list of the files contained in a directory
	*
	* @param string $directory_absolute Absolute path to the directory
	* @param string $directory          Relative path to the directory (used to prefixed the name of the files)
	* @return array
	*/
	protected function get_files_recursive($directory_absolute, $directory)
	{
		$result = array();
		$files = scandir($directory_absolute);

		foreach ($files as $file)
		{
			if (is_dir($directory_absolute . '/' . $file))
			{
				if ($file != '.' && $file != '..')
				{
					$result = array_merge($result, $this->get_files_recursive($directory_absolute . '/' . $file, $directory . '/' . $file));
				}
			}
			else
			{
				$result[] = $directory . '/' . $file;
			}
		}

		return $result;
	}
}