xref: /web-php/bin/news2html (revision 97f5bbe6)
1#!/usr/bin/env php
2<?php
3PHP_SAPI == 'cli' or die("Please run this script using the cli sapi");
4
5// get args
6$cmd = array_shift($_SERVER['argv']);
7
8if (count($_SERVER['argv']) < 2) {
9	echo "Use: $cmd /path/to/php-5.4.16/NEWS 5.4.16 [path/to/ChangeLog.php]\n";
10	exit(1);
11}
12$news_file = array_shift($_SERVER['argv']);
13$version = array_shift($_SERVER['argv']);
14$changelog = @array_shift($_SERVER['argv']);
15
16// find NEWS entry
17$fp = fopen($news_file, "r");
18if(!$fp) {
19	die("Can not open {$news_file}\n");
20}
21
22$inside = false;
23$entries = [];
24while(($ln = fgets($fp)) !== false) {
25	if (preg_match("/(.. ... ....),? PHP $version/", $ln, $m)) {
26		// got entry start
27		$inside = true;
28		$date = strtr($m[1], " ", "-");
29		continue;
30	}
31	if (!$inside) { continue; }
32
33	if (preg_match('/, PHP \d+.\d+.\d+/', $ln)) {
34		// next entry - we're done
35		break;
36	}
37
38	if ($ln == "\n") {
39		$module = 'Core';
40		continue;
41	}
42	if ($ln[0] == '-') {
43		// module
44		$module = trim(substr($ln, 1), " \t\n:");
45	} elseif (preg_match('/^\s+\.\s/',$ln)) {
46		$entries[$module][] = trim(preg_replace('/^\s+\.\s+/', '', $ln));
47	} else {
48		// continued line
49		$c = count($entries[$module])-1;
50		$entries[$module][$c] = trim($entries[$module][$c] )." ".trim($ln);
51	}
52}
53
54if ($changelog) { ob_start(); }
55
56echo <<<HEAD
57<section class="version" id="$version"><!-- {{{ $version -->
58<h3>Version $version</h3>
59<b><?php release_date('$date'); ?></b>
60<ul>
61HEAD;
62
63$bug_map = [
64    '/Fixed bug #([0-9]+)/'      => '<?php bugfix(\1); ?'.'>',
65    '/Fixed PECL bug #([0-9]+)/' => '<?php peclbugfix(\1); ?'.'>',
66    '/Implemented FR #([0-9]+)/' => '<?php implemented(\1); ?'.'>',
67    '/GitHub PR #([0-9]+)/'      => '<?php githubissuel(\'php/php-src\', \1); ?'.'>',
68    '/GH-([0-9]+)/'              => '<?php githubissuel(\'php/php-src\', \1); ?'.'>',
69];
70
71foreach($entries as $module => $items) {
72	echo "<li>$module:\n<ul>\n";
73	foreach($items as $item) {
74		// strip author
75		$item = preg_replace('/(\.(\s+\(CVE-\d+-\d+\))?)\s+\(.+?\)\s*$/', '\\1', $item);
76		// encode HTML
77		$item = htmlspecialchars($item, ENT_NOQUOTES);
78		// convert bug numbers
79		$item = preg_replace(array_keys($bug_map), array_values($bug_map), $item);
80		echo "  <li>$item</li>\n";
81	}
82	echo "</ul></li>\n";
83}
84echo "</ul>\n<!-- }}} --></section>\n\n";
85
86if ($changelog) {
87	$contents = ob_get_clean();
88
89	$log = file_get_contents($changelog);
90	if (empty($log)) {
91		fprintf(STDERR, "Unable to read $changelog\n");
92		exit(1);
93	}
94
95	$parts = explode('.', $version, 3);
96	if (count($parts) < 2) {
97		fprintf(STDERR, "Unable to parse branch from $version\n");
98		exit(1);
99	}
100
101	$tag = "<a id=\"PHP_{$parts[0]}_{$parts[1]}\"></a>";
102	if (strpos($log, $tag) === false) {
103		fprintf(STDERR, "Unable to find branch tag in ChangeLog\n");
104		exit(1);
105	}
106
107	$log = str_replace($tag, "$tag\n\n$contents", $log);
108	file_put_contents($changelog, $log);
109}
110