Remove $a = array($a) which is miscompiled by Zend OpCache.
[htmlpurifier/darkodev.git] / maintenance / generate-standalone.php
blob254d4d83bcc57c5732244e94c7aecdb1f6391b52
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 public function copyable($entry)
29 // Skip hidden files
30 if ($entry[0] == '.') {
31 return false;
33 return true;
35 public function copy($source, $dest)
37 copy_and_remove_includes($source, $dest);
40 $FS = new MergeLibraryFSTools();
42 /**
43 * Replaces the includes inside PHP source code with the corresponding
44 * source.
45 * @param string $text PHP source code to replace includes from
47 function replace_includes($text)
49 // also remove vim modelines
50 return preg_replace_callback(
51 "/require(?:_once)? ['\"]([^'\"]+)['\"];/",
52 'replace_includes_callback',
53 $text
57 /**
58 * Removes leading PHP tags from included files. Assumes that there is
59 * no trailing tag. Also removes vim modelines.
60 * @note This is safe for files that have internal <?php
61 * @param string $text Text to have leading PHP tag from
63 function remove_php_tags($text)
65 $text = preg_replace('#// vim:.+#', '', $text);
66 return substr($text, 5);
69 /**
70 * Copies the contents of a directory to the standalone directory
71 * @param string $dir Directory to copy
73 function make_dir_standalone($dir)
75 global $FS;
76 return $FS->copyr($dir, 'standalone/' . $dir);
79 /**
80 * Copies the contents of a file to the standalone directory
81 * @param string $file File to copy
83 function make_file_standalone($file)
85 global $FS;
86 $FS->mkdirr('standalone/' . dirname($file));
87 copy_and_remove_includes($file, 'standalone/' . $file);
88 return true;
91 /**
92 * Copies a file to another location recursively, if it is a PHP file
93 * remove includes
94 * @param string $file Original file
95 * @param string $sfile New location of file
97 function copy_and_remove_includes($file, $sfile)
99 $contents = file_get_contents($file);
100 if (strrchr($file, '.') === '.php') $contents = replace_includes($contents);
101 return file_put_contents($sfile, $contents);
105 * @param $matches preg_replace_callback matches array, where index 1
106 * is the filename to include
108 function replace_includes_callback($matches)
110 $file = $matches[1];
111 $preserve = array(
112 // PEAR (external)
113 'XML/HTMLSax3.php' => 1
115 if (isset($preserve[$file])) {
116 return $matches[0];
118 if (isset($GLOBALS['loaded'][$file])) return '';
119 $GLOBALS['loaded'][$file] = true;
120 return replace_includes(remove_php_tags(file_get_contents($file)));
123 echo 'Generating includes file... ';
124 shell_exec('php generate-includes.php');
125 echo "done!\n";
127 chdir(dirname(__FILE__) . '/../library/');
129 echo 'Creating full file...';
130 $contents = replace_includes(file_get_contents('HTMLPurifier.includes.php'));
131 $contents = str_replace(
132 // Note that bootstrap is now inside the standalone file
133 "define('HTMLPURIFIER_PREFIX', realpath(dirname(__FILE__) . '/..'));",
134 "define('HTMLPURIFIER_PREFIX', dirname(__FILE__) . '/standalone');
135 set_include_path(HTMLPURIFIER_PREFIX . PATH_SEPARATOR . get_include_path());",
136 $contents
138 file_put_contents('HTMLPurifier.standalone.php', $contents);
139 echo ' done!' . PHP_EOL;
141 echo 'Creating standalone directory...';
142 $FS->rmdirr('standalone'); // ensure a clean copy
144 // data files
145 $FS->mkdirr('standalone/HTMLPurifier/DefinitionCache/Serializer');
146 make_file_standalone('HTMLPurifier/EntityLookup/entities.ser');
147 make_file_standalone('HTMLPurifier/ConfigSchema/schema.ser');
149 // non-standard inclusion setup
150 make_dir_standalone('HTMLPurifier/ConfigSchema');
151 make_dir_standalone('HTMLPurifier/Language');
152 make_dir_standalone('HTMLPurifier/Filter');
153 make_dir_standalone('HTMLPurifier/Printer');
154 make_file_standalone('HTMLPurifier/Printer.php');
155 make_file_standalone('HTMLPurifier/Lexer/PH5P.php');
157 echo ' done!' . PHP_EOL;
159 // vim: et sw=4 sts=4