Don't add vimline to auto-generated files.
[htmlpurifier/darkodev.git] / maintenance / generate-standalone.php
blob9fe5354a202f388cb69912a9b7210455f583dec4
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 // also remove vim modelines
47 return preg_replace_callback(
48 "/require(?:_once)? ['\"]([^'\"]+)['\"];/",
49 'replace_includes_callback',
50 $text
54 /**
55 * Removes leading PHP tags from included files. Assumes that there is
56 * no trailing tag. Also removes vim modelines.
57 * @note This is safe for files that have internal <?php
58 * @param string $text Text to have leading PHP tag from
60 function remove_php_tags($text) {
61 $text = preg_replace('#// vim:.+#', '', $text);
62 return substr($text, 5);
65 /**
66 * Copies the contents of a directory to the standalone directory
67 * @param string $dir Directory to copy
69 function make_dir_standalone($dir) {
70 global $FS;
71 return $FS->copyr($dir, 'standalone/' . $dir);
74 /**
75 * Copies the contents of a file to the standalone directory
76 * @param string $file File to copy
78 function make_file_standalone($file) {
79 global $FS;
80 $FS->mkdirr('standalone/' . dirname($file));
81 copy_and_remove_includes($file, 'standalone/' . $file);
82 return true;
85 /**
86 * Copies a file to another location recursively, if it is a PHP file
87 * remove includes
88 * @param string $file Original file
89 * @param string $sfile New location of file
91 function copy_and_remove_includes($file, $sfile) {
92 $contents = file_get_contents($file);
93 if (strrchr($file, '.') === '.php') $contents = replace_includes($contents);
94 return file_put_contents($sfile, $contents);
97 /**
98 * @param $matches preg_replace_callback matches array, where index 1
99 * is the filename to include
101 function replace_includes_callback($matches) {
102 $file = $matches[1];
103 $preserve = array(
104 // PEAR (external)
105 'XML/HTMLSax3.php' => 1
107 if (isset($preserve[$file])) {
108 return $matches[0];
110 if (isset($GLOBALS['loaded'][$file])) return '';
111 $GLOBALS['loaded'][$file] = true;
112 return replace_includes(remove_php_tags(file_get_contents($file)));
115 echo 'Generating includes file... ';
116 shell_exec('php generate-includes.php');
117 echo "done!\n";
119 chdir(dirname(__FILE__) . '/../library/');
121 echo 'Creating full file...';
122 $contents = replace_includes(file_get_contents('HTMLPurifier.includes.php'));
123 $contents = str_replace(
124 // Note that bootstrap is now inside the standalone file
125 "define('HTMLPURIFIER_PREFIX', realpath(dirname(__FILE__) . '/..'));",
126 "define('HTMLPURIFIER_PREFIX', dirname(__FILE__) . '/standalone');
127 set_include_path(HTMLPURIFIER_PREFIX . PATH_SEPARATOR . get_include_path());",
128 $contents
130 file_put_contents('HTMLPurifier.standalone.php', $contents);
131 echo ' done!' . PHP_EOL;
133 echo 'Creating standalone directory...';
134 $FS->rmdirr('standalone'); // ensure a clean copy
136 // data files
137 $FS->mkdirr('standalone/HTMLPurifier/DefinitionCache/Serializer');
138 make_file_standalone('HTMLPurifier/EntityLookup/entities.ser');
139 make_file_standalone('HTMLPurifier/ConfigSchema/schema.ser');
141 // non-standard inclusion setup
142 make_dir_standalone('HTMLPurifier/ConfigSchema');
143 make_dir_standalone('HTMLPurifier/Language');
144 make_dir_standalone('HTMLPurifier/Filter');
145 make_dir_standalone('HTMLPurifier/Printer');
146 make_file_standalone('HTMLPurifier/Printer.php');
147 make_file_standalone('HTMLPurifier/Lexer/PH5P.php');
148 make_file_standalone('HTMLPurifier/Lexer/PEARSax3.php');
150 echo ' done!' . PHP_EOL;
152 // vim: et sw=4 sts=4