generate-includes.php 5.2 KB

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