File: build-chms.php

package info (click to toggle)
php-doc 20241205~git.dfcbb86%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 70,956 kB
  • sloc: xml: 968,269; php: 23,883; javascript: 671; sh: 177; makefile: 37
file content (348 lines) | stat: -rw-r--r-- 10,613 bytes parent folder | download
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
<?php
	/**
	 * +----------------------------------------------------------------------+
	 * | Copyright (c) 1997-2023 The PHP Group                                |
	 * +----------------------------------------------------------------------+
	 * | This source file is subject to version 3.01 of the PHP license,      |
	 * | that is bundled with this package in the file LICENSE, and is        |
	 * | available through the world-wide-web at the following url:           |
	 * | https://www.php.net/license/3_01.txt.                                |
	 * | If you did not receive a copy of the PHP license and are unable to   |
	 * | obtain it through the world-wide-web, please send a note to          |
	 * | license@php.net, so we can mail you a copy immediately.              |
	 * +----------------------------------------------------------------------+
	 * | Authors: Kalle Sommer Nielsen <kalle@php.net>                        |
	 * |          Hannes Magnusson <bjori@php.net>                            |
	 * +----------------------------------------------------------------------+
	 * 
	 * $Id$
	 */


	/**
	 * This script is based off the original build.chms.bat 
	 * in the root of doc-base
	 *
	 * This script does not work with Git, meaning enabling PHD_BETA 
	 * will fetch the old version from SVN (March, Anno 2012)
	 */

	/**
	 * Configuration
	 */
	include_once __DIR__ .'/build-chms-config.php';

	/**
	 * The languages to build are retrieved from github.
	 */
	if (file_exists(__DIR__ . '\\languages.inc'))
	{
		unlink(__DIR__ . '\\languages.inc');
	}

	execute_task('Get list of online languages', PATH_WGET, '--debug --verbose --no-check-certificate "https://raw.githubusercontent.com/php/web-php/master/include/languages.inc" --output-document=' . __DIR__ . '\\languages.inc', 'wget_langs');

	if (file_exists(__DIR__ . '\\languages.inc'))
	{
		include_once __DIR__ . '\\languages.inc';
	}

	/**
	 * Always build English first.
	 */
	unset($ACTIVE_ONLINE_LANGUAGES['en']);
	ksort($ACTIVE_ONLINE_LANGUAGES);

	$ACTIVE_ONLINE_LANGUAGES = array('en' => 'English') + $ACTIVE_ONLINE_LANGUAGES;

	/**
	 * Get the current working directory
	 */
	$cwd = getcwd();

	/**
	 * Upgrade PhD, if any updates are available
	 */
	if(PHD_BETA)
	{
		chdir(dirname(PATH_PHD));
		execute_task('Updating PhD from svn', PATH_GIT, 'pull', 'phd_git');
		chdir($cwd);
	}
	else
	{
		execute_task('Updating PhD', PATH_PEAR, 'upgrade --alldeps doc.php.net/phd', 'pear');
	}

	/**
	 * Update doc-base, to prevent build errors with 
	 * entities
	 */
	chdir(PATH_DOC . '\\doc-base\\');
	execute_task('Updating doc-base', PATH_GIT, 'pull', 'doc_base');
	chdir($cwd);

	/**
	 * We may want to try build a single language
	 */
	if($argc >= 2 && in_array($argv[1], array_keys($ACTIVE_ONLINE_LANGUAGES)))
	{
		$ACTIVE_ONLINE_LANGUAGES = Array($argv[1] => $ACTIVE_ONLINE_LANGUAGES[$argv[1]]);
	}

	/**
	 * Hold the results of this build
	 */
	$build_history = array();

	/**
	 * Start iterating over each translation
	 */
	foreach($ACTIVE_ONLINE_LANGUAGES as $lang_code => $language)
	{
		echo(date('r') . ' Processing language ' . $language . ' \'' . $lang_code . '\':' . PHP_EOL);

		/**
		 * Update that specific language folder in SVN
		 */
		chdir(PATH_DOC . '\\' . $lang_code . '\\');
		execute_task('- GIT', PATH_GIT, 'pull', 'git_' . $lang_code);
		chdir($cwd);

		/**
		 * Generate .manual-lang.xml
		 */
		execute_task('- Configure', PATH_PHP, PATH_DOC . '\doc-base\configure.php --disable-libxml-check --disable-segfault-speed --with-php="' . PATH_PHP . '" --with-lang=' . $lang_code . ' --enable-chm --output=' . PATH_DOC . '\\doc-base\\.manual-' . $lang_code . '.xml', 'configure_' . $lang_code);

		if(!is_file(PATH_DOC . '\\doc-base\\.manual-' . $lang_code . '.xml'))
		{
			echo(date('r') . ' - Build error: configure failed' . PHP_EOL);

			continue;
		}

		/**
		 * Run .manual.xml thru PhD, including the enhanced build if required
		 */
		$enhanced = (EXTENDED) ? '-f enhancedchm' : '';

		execute_task('- PhD', PATH_PHD, '-d "' . PATH_DOC . '\\doc-base\\.manual-' . $lang_code . '.xml' . '" -P PHP -f chm ' . $enhanced . ' -o "' . PATH_DOC . '\\tmp\\' . $lang_code . '" -L ' . $lang_code, 'phd_' . $lang_code);

		if(!is_file(PATH_DOC . '\\tmp\\' . $lang_code . '\\php-chm\\php_manual_' . $lang_code . '.hhp'))
		{
			echo(date('r') . ' - Build error: PhD failed' . PHP_EOL);

			continue;
		}


		/**
		 * Run DBCSFix.exe to convert Unicode to ASCII.
		 */
                $lcid = NULL;
                $multibyte_search_enabled = (MULTIBYTE_SEARCH && array_key_exists($lang_code, $CHM_FULLTEXT_SEARCH_LCID));
                if ($multibyte_search_enabled) {
                    $lcid = $CHM_FULLTEXT_SEARCH_LCID[$lang_code];
                    $dbcsfix_log = 'dbcsfix_' . $lang_code;
                    execute_task(
                        '- Convert to ASCII for Fulltext Search...',
                        PATH_DBCSFIX,
                        '/d:' . PATH_DOC . '\\tmp\\' . $lang_code
                      . ' ' . '/l:' . $lcid,
                        $dbcsfix_log);
                }

		/**
		 * Run the HTML Help Compiler to generate the actual CHM file
		 */
                if ($multibyte_search_enabled) {
                        execute_task('- HHC (with Fulltext Search)',
                            PATH_APPLOCALE,
                            $lcid . 
                            ' "' . PATH_HHC . '" "' .
                            PATH_DOC . '\\tmp\\' . $lang_code . '\\php-chm\\php_manual_' . $lang_code . '.hhp"',
                           'hhc_' . $lang_code);
                } else {
			execute_task('- HHC', PATH_HHC, '"' . PATH_DOC . '\\tmp\\' . $lang_code . '\\php-chm\\php_manual_' . $lang_code . '.hhp"', 'hhc_' . $lang_code);
                }

		if(!is_file(PATH_DOC . '\\tmp\\' . $lang_code . '\\php-chm\\php_manual_' . $lang_code . '.chm'))
		{
			echo(date('r') . ' - Build error: HHC failed' . PHP_EOL);

			continue;
		}
		
		/**
		 * Anything smaller than ~5MB is broken. Common broken sizes are 2MB and 15K. Common good size are 10-12MB.
		 */
		if(filesize(PATH_DOC . '\\tmp\\' . $lang_code . '\\php-chm\\php_manual_' . $lang_code . '.chm') < 5000000)
		{
			echo(date('r') . ' - Build error: CHM file too small, something went wrong' . PHP_EOL);
			
			continue;
		}

		/**
		 * Copy the CHM file into the archive
		 */
		if(!copy(PATH_DOC . '\\tmp\\' . $lang_code . '\\php-chm\\php_manual_' . $lang_code . '.chm', $chm_filename = PATH_CHM . '\\' . 'php_manual_' . $lang_code . '.chm'))
		{
			echo(date('r') . ' - Build error: Unable to copy CHM file into archive folder');

			continue;
		}
		else
		{
			/**
			 * Add to history
			 */
			$build_history[] = array('php_manual_' . $lang_code . '.chm', md5_file($chm_filename), filemtime($chm_filename));
		}

		/**
		 * Check if we are supposed to build the enhanced version
		 */
		if(EXTENDED)
		{
			/**
			 * Run the HTML Help Compiler to generate the actual CHM file
			 */
	                if ($multibyte_search_enabled) {
	                        execute_task('- [Enhanced] HHC (with Fulltext Search)',
	                            PATH_APPLOCALE,
	                            $lcid .
	                            ' "' . PATH_HHC . '" "' .
	                            PATH_DOC . '\\tmp\\' . $lang_code . '\\php-enhancedchm\\php_manual_' . $lang_code . '.hhp"',
	                           'hhc_enhanced_' . $lang_code);
			} else {
				execute_task('- [Enhanced] HHC', PATH_HHC, '"' . PATH_DOC . '\\tmp\\' . $lang_code . '\\php-enhancedchm\\php_manual_' . $lang_code . '.hhp"', 'hhc_enhanced_' . $lang_code);
			}

			if(!is_file(PATH_DOC . '\\tmp\\' . $lang_code . '\\php-enhancedchm\\php_manual_' . $lang_code . '.chm'))
			{
				echo(date('r') . ' - Build error: Enhanced: HHC failed' . PHP_EOL);

				goto cleanup;
			}
		
			/**
			 * Anything smaller than ~5MB is broken. Common broken sizes are 2MB and 15K. Common good size are 10-12MB.
			 */
			if(filesize(PATH_DOC . '\\tmp\\' . $lang_code . '\\php-enhancedchm\\php_manual_' . $lang_code . '.chm') < 5000000)
			{
				echo(date('r') . ' - Build error: Enhanced: CHM file too small, something went wrong' . PHP_EOL);
			
				goto cleanup;
			}

			/**
			 * Copy the CHM file into the archive
			 */
			if(!copy(PATH_DOC . '\\tmp\\' . $lang_code . '\\php-enhancedchm\\php_manual_' . $lang_code . '.chm', $e_chm_filename = PATH_CHM . '\\' . 'php_enhanced_' . $lang_code . '.chm'))
			{
				echo(date('r') . ' - Build error: Enhanced: Unable to copy CHM file into archive folder');

				goto cleanup;
			}
			else
			{
				/**
				 * Add to history
				 */
				$build_history[] = array('php_enhanced_' . $lang_code . '.chm', md5_file($e_chm_filename), filemtime($e_chm_filename));
			}
		}

		/**
		 * Cleanup
		 */
		cleanup:
		{
			echo(date('r') . ' - Clean up' . PHP_EOL);

			unlink(PATH_DOC . '\\doc-base\\.manual-' . $lang_code . '.xml');

			if(!DEBUG)
			{
				glob_recursive_apply('fsdelete', PATH_DOC . '\\tmp\\' . $lang_code . '\\php-chm\\');

				if(!EXTENDED)
				{
					glob_recursive_apply('fsdelete', PATH_DOC . '\\tmp\\' . $lang_code . '\\php-enhancedchm\\');
				}
			}
		}
	}


	/**
	 * Save build history
	 */
	file_put_contents(PATH_CHM . '\\build.log', implode(PHP_EOL, array_map(function($single_build)
	{
		return implode("\t", $single_build);
	}, $build_history)));

	echo(date('r') . ' Done!');


	/**
	 * Removes all files within a directory and then 
	 * deletes the directory
	 *
	 * @param	callable		Callback to the apply function
	 * @param	string			Glob directory to begin
	 * @param	string			(optional) The glob pattern to match on
	 * @return	void			No value is returned
	 */
	function glob_recursive_apply($callback, $glob_dir, $pattern = '*')
	{
		if(!is_dir($dir))
		{
			return;
		}

		$glob = glob($glob_dir . $pattern);

		if(sizeof($glob))
		{
			foreach($glob as $obj)
			{
				call_user_func($callback, $obj, $pattern);
			}
		}

		call_user_func($callback, $obj, $pattern);
	}

	/**
	 * Deletes a directory or file
	 *
	 * @param	string			Directory or filename to delete
	 * @param	string			The glob pattern to match on
	 * @return	void			No value is returned
	 */
	function fsdelete($obj, $pattern = '*')
	{
		if(is_dir($obj))
		{
			glob_recursive_apply('fsdelete', $obj . '\\', $pattern);
		}
		else
		{
			unlink($obj);
		}
	}

	/**
	 * Gets the contents of a specific log
	 *
	 * @param	string 			Name of the log (usually "program_lang")
	 * @return	string			Contents of the log
	 */
	function log_get_contents($logname)
	{
		return(@file_get_contents(PATH_LOG . '\\' . $log . '.log'));
	}
?>