generate-includes.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 $file
  57. * File to check dependencies of.
  58. * @return
  59. * Lookup array of files the file is dependent on, sorted accordingly.
  60. */
  61. function get_dependency_lookup($file) {
  62. static $cache = array();
  63. if (isset($cache[$file])) return $cache[$file];
  64. if (!file_exists($file)) {
  65. echo "File doesn't exist: $file\n";
  66. return array();
  67. }
  68. $fh = fopen($file, 'r');
  69. $deps = array();
  70. while (!feof($fh)) {
  71. $line = fgets($fh);
  72. if (strncmp('class', $line, 5) === 0) {
  73. // The implementation here is fragile and will break if we attempt
  74. // to use interfaces. Beware!
  75. $arr = explode(' extends ', trim($line, ' {'."\n\r"), 2);
  76. if (count($arr) < 2) break;
  77. $parent = $arr[1];
  78. $dep_file = HTMLPurifier_Bootstrap::getPath($parent);
  79. if (!$dep_file) break;
  80. $deps[$dep_file] = true;
  81. break;
  82. }
  83. }
  84. fclose($fh);
  85. foreach (array_keys($deps) as $file) {
  86. // Extra dependencies must come *before* base dependencies
  87. $deps = get_dependency_lookup($file) + $deps;
  88. }
  89. $cache[$file] = $deps;
  90. return $deps;
  91. }
  92. /**
  93. * Sorts files based on dependencies. This function is lazy and will not
  94. * group files with dependencies together; it will merely ensure that a file
  95. * is never included before its dependencies are.
  96. *
  97. * @param $files
  98. * Files array to sort.
  99. * @return
  100. * Sorted array ($files is not modified by reference!)
  101. */
  102. function dep_sort($files) {
  103. $ret = array();
  104. $cache = array();
  105. foreach ($files as $file) {
  106. if (isset($cache[$file])) continue;
  107. $deps = get_dependency_lookup($file);
  108. foreach (array_keys($deps) as $dep) {
  109. if (!isset($cache[$dep])) {
  110. $ret[] = $dep;
  111. $cache[$dep] = true;
  112. }
  113. }
  114. $cache[$file] = true;
  115. $ret[] = $file;
  116. }
  117. return $ret;
  118. }
  119. $files = dep_sort($files);
  120. // Build the actual include stub:
  121. $version = trim(file_get_contents('../VERSION'));
  122. // stub
  123. $php = "<?php
  124. /**
  125. * @file
  126. * This file was auto-generated by generate-includes.php and includes all of
  127. * the core files required by HTML Purifier. Use this if performance is a
  128. * primary concern and you are using an opcode cache. PLEASE DO NOT EDIT THIS
  129. * FILE, changes will be overwritten the next time the script is run.
  130. *
  131. * @version $version
  132. *
  133. * @warning
  134. * You must *not* include any other HTML Purifier files before this file,
  135. * because 'require' not 'require_once' is used.
  136. *
  137. * @warning
  138. * This file requires that the include path contains the HTML Purifier
  139. * library directory; this is not auto-set.
  140. */
  141. ";
  142. foreach ($files as $file) {
  143. $php .= "require '$file';" . PHP_EOL;
  144. }
  145. echo "Writing HTMLPurifier.includes.php... ";
  146. file_put_contents('HTMLPurifier.includes.php', $php);
  147. echo "done!\n";
  148. $php = "<?php
  149. /**
  150. * @file
  151. * This file was auto-generated by generate-includes.php and includes all of
  152. * the core files required by HTML Purifier. This is a convenience stub that
  153. * includes all files using dirname(__FILE__) and require_once. PLEASE DO NOT
  154. * EDIT THIS FILE, changes will be overwritten the next time the script is run.
  155. *
  156. * Changes to include_path are not necessary.
  157. */
  158. \$__dir = dirname(__FILE__);
  159. ";
  160. foreach ($files as $file) {
  161. $php .= "require_once \$__dir . '/$file';" . PHP_EOL;
  162. }
  163. echo "Writing HTMLPurifier.safe-includes.php... ";
  164. file_put_contents('HTMLPurifier.safe-includes.php', $php);
  165. echo "done!\n";
  166. // vim: et sw=4 sts=4