[3.1.1] Update Munge docs.
[htmlpurifier.git] / maintenance / generate-standalone.php
blobd5a631a019e5cbfa93ad323c550bbbbaf89d13a6
1 #!/usr/bin/php
2 <?php
4 chdir(dirname(__FILE__));
5 require_once 'common.php';
6 assertCli();
8 /**
9 * @file
10 * Compiles all of HTML Purifier's library files into one big file
11 * named HTMLPurifier.standalone.php. This is usually called during the
12 * release process.
15 /**
16 * Global hash that tracks already loaded includes
18 $GLOBALS['loaded'] = array();
20 /**
21 * Custom FSTools for this script that overloads some behavior
22 * @warning The overloading of copy() is not necessarily global for
23 * this script. Watch out!
25 class MergeLibraryFSTools extends FSTools
27 function copyable($entry) {
28 // Skip hidden files
29 if ($entry[0] == '.') {
30 return false;
32 return true;
34 function copy($source, $dest) {
35 copy_and_remove_includes($source, $dest);
38 $FS = new MergeLibraryFSTools();
40 /**
41 * Replaces the includes inside PHP source code with the corresponding
42 * source.
43 * @param string $text PHP source code to replace includes from
45 function replace_includes($text) {
46 return preg_replace_callback(
47 "/require(?:_once)? ['\"]([^'\"]+)['\"];/",
48 'replace_includes_callback',
49 $text
53 /**
54 * Removes leading PHP tags from included files. Assumes that there is
55 * no trailing tag.
56 * @note This is safe for files that have internal <?php
57 * @param string $text Text to have leading PHP tag from
59 function remove_php_tags($text) {
60 return substr($text, 5);
63 /**
64 * Copies the contents of a directory to the standalone directory
65 * @param string $dir Directory to copy
67 function make_dir_standalone($dir) {
68 global $FS;
69 return $FS->copyr($dir, 'standalone/' . $dir);
72 /**
73 * Copies the contents of a file to the standalone directory
74 * @param string $file File to copy
76 function make_file_standalone($file) {
77 global $FS;
78 $FS->mkdirr('standalone/' . dirname($file));
79 copy_and_remove_includes($file, 'standalone/' . $file);
80 return true;
83 /**
84 * Copies a file to another location recursively, if it is a PHP file
85 * remove includes
86 * @param string $file Original file
87 * @param string $sfile New location of file
89 function copy_and_remove_includes($file, $sfile) {
90 $contents = file_get_contents($file);
91 if (strrchr($file, '.') === '.php') $contents = replace_includes($contents);
92 return file_put_contents($sfile, $contents);
95 /**
96 * @param $matches preg_replace_callback matches array, where index 1
97 * is the filename to include
99 function replace_includes_callback($matches) {
100 $file = $matches[1];
101 $preserve = array(
102 // PEAR (external)
103 'XML/HTMLSax3.php' => 1
105 if (isset($preserve[$file])) {
106 return $matches[0];
108 if (isset($GLOBALS['loaded'][$file])) return '';
109 $GLOBALS['loaded'][$file] = true;
110 return replace_includes(remove_php_tags(file_get_contents($file)));
113 echo 'Generating includes file... ';
114 shell_exec('php generate-includes.php');
115 echo "done!\n";
117 chdir(dirname(__FILE__) . '/../library/');
119 echo 'Creating full file...';
120 $contents = replace_includes(file_get_contents('HTMLPurifier.includes.php'));
121 $contents = str_replace(
122 // Note that bootstrap is now inside the standalone file
123 "define('HTMLPURIFIER_PREFIX', realpath(dirname(__FILE__) . '/..'));",
124 "define('HTMLPURIFIER_PREFIX', dirname(__FILE__) . '/standalone');
125 set_include_path(HTMLPURIFIER_PREFIX . PATH_SEPARATOR . get_include_path());",
126 $contents
128 file_put_contents('HTMLPurifier.standalone.php', $contents);
129 echo ' done!' . PHP_EOL;
131 echo 'Creating standalone directory...';
132 $FS->rmdirr('standalone'); // ensure a clean copy
134 // data files
135 $FS->mkdirr('standalone/HTMLPurifier/DefinitionCache/Serializer');
136 make_file_standalone('HTMLPurifier/EntityLookup/entities.ser');
137 make_file_standalone('HTMLPurifier/ConfigSchema/schema.ser');
139 // non-standard inclusion setup
140 make_dir_standalone('HTMLPurifier/ConfigSchema');
141 make_dir_standalone('HTMLPurifier/Language');
142 make_dir_standalone('HTMLPurifier/Filter');
143 make_dir_standalone('HTMLPurifier/Printer');
144 make_file_standalone('HTMLPurifier/Printer.php');
145 make_file_standalone('HTMLPurifier/Lexer/PH5P.php');
146 make_file_standalone('HTMLPurifier/Lexer/PEARSax3.php');
148 echo ' done!' . PHP_EOL;