Don't add vimline to auto-generated files.
[htmlpurifier/darkodev.git] / maintenance / generate-includes.php
blob8a30c4adfd0814328201888ad111cf5f07b79b7e
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 list(, $parent) = explode(' extends ', trim($line, ' {'."\n\r"), 2);
84 if (empty($parent)) break;
85 $dep_file = HTMLPurifier_Bootstrap::getPath($parent);
86 if (!$dep_file) break;
87 $deps[$dep_file] = true;
88 break;
91 fclose($fh);
92 foreach (array_keys($deps) as $file) {
93 // Extra dependencies must come *before* base dependencies
94 $deps = get_dependency_lookup($file) + $deps;
96 $cache[$file] = $deps;
97 return $deps;
101 * Sorts files based on dependencies. This function is lazy and will not
102 * group files with dependencies together; it will merely ensure that a file
103 * is never included before its dependencies are.
105 * @param $files
106 * Files array to sort.
107 * @return
108 * Sorted array ($files is not modified by reference!)
110 function dep_sort($files) {
111 $ret = array();
112 $cache = array();
113 foreach ($files as $file) {
114 if (isset($cache[$file])) continue;
115 $deps = get_dependency_lookup($file);
116 foreach (array_keys($deps) as $dep) {
117 if (!isset($cache[$dep])) {
118 $ret[] = $dep;
119 $cache[$dep] = true;
122 $cache[$file] = true;
123 $ret[] = $file;
125 return $ret;
128 $files = dep_sort($files);
130 // Build the actual include stub:
132 $version = trim(file_get_contents('../VERSION'));
134 // stub
135 $php = "<?php
138 * @file
139 * This file was auto-generated by generate-includes.php and includes all of
140 * the core files required by HTML Purifier. Use this if performance is a
141 * primary concern and you are using an opcode cache. PLEASE DO NOT EDIT THIS
142 * FILE, changes will be overwritten the next time the script is run.
144 * @version $version
146 * @warning
147 * You must *not* include any other HTML Purifier files before this file,
148 * because 'require' not 'require_once' is used.
150 * @warning
151 * This file requires that the include path contains the HTML Purifier
152 * library directory; this is not auto-set.
157 foreach ($files as $file) {
158 $php .= "require '$file';" . PHP_EOL;
161 echo "Writing HTMLPurifier.includes.php... ";
162 file_put_contents('HTMLPurifier.includes.php', $php);
163 echo "done!\n";
165 $php = "<?php
168 * @file
169 * This file was auto-generated by generate-includes.php and includes all of
170 * the core files required by HTML Purifier. This is a convenience stub that
171 * includes all files using dirname(__FILE__) and require_once. PLEASE DO NOT
172 * EDIT THIS FILE, changes will be overwritten the next time the script is run.
174 * Changes to include_path are not necessary.
177 \$__dir = dirname(__FILE__);
181 foreach ($files as $file) {
182 $php .= "require_once \$__dir . '/$file';" . PHP_EOL;
185 echo "Writing HTMLPurifier.safe-includes.php... ";
186 file_put_contents('HTMLPurifier.safe-includes.php', $php);
187 echo "done!\n";
189 // vim: et sw=4 sts=4