[3.0.0] Upgraded test scripts and other goodies. Also removed some PHP4 cruft.
[htmlpurifier/darkodev.git] / maintenance / merge-library.php
blobd8112e6747b60a5ed4650371cfee8eb24cf51b61
1 #!/usr/bin/php
2 <?php
4 chdir(dirname(__FILE__));
5 require_once 'common.php';
6 assertCli();
8 /**
9 * Compiles all of HTML Purifier's library files into one big file
10 * named HTMLPurifier.standalone.php.
13 /**
14 * Global hash that tracks already loaded includes
16 $GLOBALS['loaded'] = array('HTMLPurifier.php' => true);
18 /**
19 * Custom FSTools for this script that overloads some behavior
20 * @warning The overloading of copy() is not necessarily global for
21 * this script. Watch out!
23 class MergeLibraryFSTools extends FSTools
25 function copyable($entry) {
26 // Skip hidden files
27 if ($entry[0] == '.') {
28 return false;
30 return true;
32 function copy($source, $dest) {
33 copy_and_remove_includes($source, $dest);
36 $FS = new MergeLibraryFSTools();
38 /**
39 * Replaces the includes inside PHP source code with the corresponding
40 * source.
41 * @param string $text PHP source code to replace includes from
43 function replace_includes($text) {
44 return preg_replace_callback(
45 "/require_once ['\"]([^'\"]+)['\"];/",
46 'replace_includes_callback',
47 $text
51 /**
52 * Removes leading PHP tags from included files. Assumes that there is
53 * no trailing tag.
54 * @note This is safe for files that have internal <?php
55 * @param string $text Text to have leading PHP tag from
57 function remove_php_tags($text) {
58 return substr($text, 5);
61 /**
62 * Creates an appropriate blank file, recursively generating directories
63 * if necessary
64 * @param string $file Filename to create blank for
66 function create_blank($file) {
67 global $FS;
68 $dir = dirname($file);
69 $base = realpath('../tests/blanks/') . DIRECTORY_SEPARATOR ;
70 if ($dir != '.') {
71 $FS->mkdir($base . $dir);
73 file_put_contents($base . $file, '');
76 /**
77 * Copies the contents of a directory to the standalone directory
78 * @param string $dir Directory to copy
80 function make_dir_standalone($dir) {
81 global $FS;
82 return $FS->copyr($dir, 'standalone/' . $dir);
85 /**
86 * Copies the contents of a file to the standalone directory
87 * @param string $file File to copy
89 function make_file_standalone($file) {
90 global $FS;
91 $FS->mkdir('standalone/' . dirname($file));
92 copy_and_remove_includes($file, 'standalone/' . $file);
93 return true;
96 /**
97 * Copies a file to another location recursively, if it is a PHP file
98 * remove includes
99 * @param string $file Original file
100 * @param string $sfile New location of file
102 function copy_and_remove_includes($file, $sfile) {
103 $contents = file_get_contents($file);
104 if (strrchr($file, '.') === '.php') $contents = replace_includes($contents);
105 return file_put_contents($sfile, $contents);
109 * @param $matches preg_replace_callback matches array, where index 1
110 * is the filename to include
112 function replace_includes_callback($matches) {
113 $file = $matches[1];
114 $preserve = array(
115 // PHP 5 only
116 'HTMLPurifier/Lexer/DOMLex.php' => 1,
117 'HTMLPurifier/Printer.php' => 1,
118 // PEAR (external)
119 'XML/HTMLSax3.php' => 1
121 if (isset($preserve[$file])) {
122 return $matches[0];
124 if (isset($GLOBALS['loaded'][$file])) return '';
125 $GLOBALS['loaded'][$file] = true;
126 create_blank($file);
127 return replace_includes(remove_php_tags(file_get_contents($file)));
130 chdir(dirname(__FILE__) . '/../library/');
131 create_blank('HTMLPurifier.php');
133 echo 'Creating full file...';
134 $contents = replace_includes(file_get_contents('HTMLPurifier.php'));
135 $contents = str_replace(
136 "define('HTMLPURIFIER_PREFIX', dirname(__FILE__));",
137 "define('HTMLPURIFIER_PREFIX', dirname(__FILE__) . '/standalone');
138 set_include_path(HTMLPURIFIER_PREFIX . PATH_SEPARATOR . get_include_path());",
139 $contents
141 file_put_contents('HTMLPurifier.standalone.php', $contents);
142 echo ' done!' . PHP_EOL;
144 echo 'Creating standalone directory...';
145 $FS->rmdirr('standalone'); // ensure a clean copy
147 // data files
148 $FS->mkdir('standalone/HTMLPurifier/DefinitionCache/Serializer');
149 make_dir_standalone('HTMLPurifier/EntityLookup');
151 // non-standard inclusion setup
152 make_dir_standalone('HTMLPurifier/Language');
154 // optional components
155 make_file_standalone('HTMLPurifier/Printer.php');
156 make_dir_standalone('HTMLPurifier/Printer');
157 make_dir_standalone('HTMLPurifier/Filter');
158 make_file_standalone('HTMLPurifier/Lexer/PEARSax3.php');
160 // PHP 5 only files
161 make_file_standalone('HTMLPurifier/Lexer/DOMLex.php');
162 make_file_standalone('HTMLPurifier/Lexer/PH5P.php');
163 echo ' done!' . PHP_EOL;