generate-includes.php 5.3KB

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