Fix iconv truncation bug.
[htmlpurifier.git] / maintenance / generate-includes.php
blob498f49b91e044977d79fa83b0b9d4c26eb900f62
1 #!/usr/bin/php
2 <?php
4 chdir(dirname(__FILE__));
5 require_once 'common.php';
6 require_once '../tests/path2class.func.php';
7 require_once '../library/HTMLPurifier/Bootstrap.php';
8 assertCli();
10 /**
11 * @file
12 * Generates an include stub for users who do not want to use the autoloader.
13 * When new files are added to HTML Purifier's main codebase, this file should
14 * be called.
17 chdir(dirname(__FILE__) . '/../library/');
18 $FS = new FSTools();
20 $exclude_dirs = array(
21 'HTMLPurifier/Language/',
22 'HTMLPurifier/ConfigSchema/',
23 'HTMLPurifier/Filter/',
24 'HTMLPurifier/Printer/',
25 /* These should be excluded, but need to have ConfigSchema support first
29 $exclude_files = array(
30 'HTMLPurifier/Lexer/PEARSax3.php',
31 'HTMLPurifier/Lexer/PH5P.php',
32 'HTMLPurifier/Printer.php',
35 // Determine what files need to be included:
36 echo 'Scanning for files... ';
37 $raw_files = $FS->globr('.', '*.php');
38 if (!$raw_files) throw new Exception('Did not find any PHP source files');
39 $files = array();
40 foreach ($raw_files as $file) {
41 $file = substr($file, 2); // rm leading './'
42 if (strncmp('standalone/', $file, 11) === 0) continue; // rm generated files
43 if (substr_count($file, '.') > 1) continue; // rm meta files
44 $ok = true;
45 foreach ($exclude_dirs as $dir) {
46 if (strncmp($dir, $file, strlen($dir)) === 0) {
47 $ok = false;
48 break;
51 if (!$ok) continue; // rm excluded directories
52 if (in_array($file, $exclude_files)) continue; // rm excluded files
53 $files[] = $file;
55 echo "done!\n";
57 // Reorder list so that dependencies are included first:
59 /**
60 * Returns a lookup array of dependencies for a file.
62 * @note This function expects that format $name extends $parent on one line
64 * @param $file
65 * File to check dependencies of.
66 * @return
67 * Lookup array of files the file is dependent on, sorted accordingly.
69 function get_dependency_lookup($file) {
70 static $cache = array();
71 if (isset($cache[$file])) return $cache[$file];
72 if (!file_exists($file)) {
73 echo "File doesn't exist: $file\n";
74 return array();
76 $fh = fopen($file, 'r');
77 $deps = array();
78 while (!feof($fh)) {
79 $line = fgets($fh);
80 if (strncmp('class', $line, 5) === 0) {
81 // The implementation here is fragile and will break if we attempt
82 // to use interfaces. Beware!
83 $arr = explode(' extends ', trim($line, ' {'."\n\r"), 2);
84 if (count($arr) < 2) break;
85 $parent = $arr[1];
86 $dep_file = HTMLPurifier_Bootstrap::getPath($parent);
87 if (!$dep_file) break;
88 $deps[$dep_file] = true;
89 break;
92 fclose($fh);
93 foreach (array_keys($deps) as $file) {
94 // Extra dependencies must come *before* base dependencies
95 $deps = get_dependency_lookup($file) + $deps;
97 $cache[$file] = $deps;
98 return $deps;
102 * Sorts files based on dependencies. This function is lazy and will not
103 * group files with dependencies together; it will merely ensure that a file
104 * is never included before its dependencies are.
106 * @param $files
107 * Files array to sort.
108 * @return
109 * Sorted array ($files is not modified by reference!)
111 function dep_sort($files) {
112 $ret = array();
113 $cache = array();
114 foreach ($files as $file) {
115 if (isset($cache[$file])) continue;
116 $deps = get_dependency_lookup($file);
117 foreach (array_keys($deps) as $dep) {
118 if (!isset($cache[$dep])) {
119 $ret[] = $dep;
120 $cache[$dep] = true;
123 $cache[$file] = true;
124 $ret[] = $file;
126 return $ret;
129 $files = dep_sort($files);
131 // Build the actual include stub:
133 $version = trim(file_get_contents('../VERSION'));
135 // stub
136 $php = "<?php
139 * @file
140 * This file was auto-generated by generate-includes.php and includes all of
141 * the core files required by HTML Purifier. Use this if performance is a
142 * primary concern and you are using an opcode cache. PLEASE DO NOT EDIT THIS
143 * FILE, changes will be overwritten the next time the script is run.
145 * @version $version
147 * @warning
148 * You must *not* include any other HTML Purifier files before this file,
149 * because 'require' not 'require_once' is used.
151 * @warning
152 * This file requires that the include path contains the HTML Purifier
153 * library directory; this is not auto-set.
158 foreach ($files as $file) {
159 $php .= "require '$file';" . PHP_EOL;
162 echo "Writing HTMLPurifier.includes.php... ";
163 file_put_contents('HTMLPurifier.includes.php', $php);
164 echo "done!\n";
166 $php = "<?php
169 * @file
170 * This file was auto-generated by generate-includes.php and includes all of
171 * the core files required by HTML Purifier. This is a convenience stub that
172 * includes all files using dirname(__FILE__) and require_once. PLEASE DO NOT
173 * EDIT THIS FILE, changes will be overwritten the next time the script is run.
175 * Changes to include_path are not necessary.
178 \$__dir = dirname(__FILE__);
182 foreach ($files as $file) {
183 $php .= "require_once \$__dir . '/$file';" . PHP_EOL;
186 echo "Writing HTMLPurifier.safe-includes.php... ";
187 file_put_contents('HTMLPurifier.safe-includes.php', $php);
188 echo "done!\n";
190 // vim: et sw=4 sts=4