Yaffs site version 1.1
[yaffs-website] / vendor / ezyang / htmlpurifier / maintenance / generate-standalone.php
1 #!/usr/bin/php
2 <?php
3
4 chdir(dirname(__FILE__));
5 require_once 'common.php';
6 assertCli();
7
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.
13  */
14
15 /**
16  * Global hash that tracks already loaded includes
17  */
18 $GLOBALS['loaded'] = array();
19
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!
24  */
25 class MergeLibraryFSTools extends FSTools
26 {
27     public function copyable($entry)
28     {
29         // Skip hidden files
30         if ($entry[0] == '.') {
31             return false;
32         }
33         return true;
34     }
35     public function copy($source, $dest)
36     {
37         copy_and_remove_includes($source, $dest);
38     }
39 }
40 $FS = new MergeLibraryFSTools();
41
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
46  */
47 function replace_includes($text)
48 {
49     // also remove vim modelines
50     return preg_replace_callback(
51         "/require(?:_once)? ['\"]([^'\"]+)['\"];/",
52         'replace_includes_callback',
53         $text
54     );
55 }
56
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
62  */
63 function remove_php_tags($text)
64 {
65     $text = preg_replace('#// vim:.+#', '', $text);
66     return substr($text, 5);
67 }
68
69 /**
70  * Copies the contents of a directory to the standalone directory
71  * @param string $dir Directory to copy
72  */
73 function make_dir_standalone($dir)
74 {
75     global $FS;
76     return $FS->copyr($dir, 'standalone/' . $dir);
77 }
78
79 /**
80  * Copies the contents of a file to the standalone directory
81  * @param string $file File to copy
82  */
83 function make_file_standalone($file)
84 {
85     global $FS;
86     $FS->mkdirr('standalone/' . dirname($file));
87     copy_and_remove_includes($file, 'standalone/' . $file);
88     return true;
89 }
90
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
96  */
97 function copy_and_remove_includes($file, $sfile)
98 {
99     $contents = file_get_contents($file);
100     if (strrchr($file, '.') === '.php') $contents = replace_includes($contents);
101     return file_put_contents($sfile, $contents);
102 }
103
104 /**
105  * @param $matches preg_replace_callback matches array, where index 1
106  *        is the filename to include
107  */
108 function replace_includes_callback($matches)
109 {
110     $file = $matches[1];
111     $preserve = array(
112       // PEAR (external)
113       'XML/HTMLSax3.php' => 1
114     );
115     if (isset($preserve[$file])) {
116         return $matches[0];
117     }
118     if (isset($GLOBALS['loaded'][$file])) return '';
119     $GLOBALS['loaded'][$file] = true;
120     return replace_includes(remove_php_tags(file_get_contents($file)));
121 }
122
123 echo 'Generating includes file... ';
124 shell_exec('php generate-includes.php');
125 echo "done!\n";
126
127 chdir(dirname(__FILE__) . '/../library/');
128
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
137 );
138 file_put_contents('HTMLPurifier.standalone.php', $contents);
139 echo ' done!' . PHP_EOL;
140
141 echo 'Creating standalone directory...';
142 $FS->rmdirr('standalone'); // ensure a clean copy
143
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');
148
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');
156
157 echo ' done!' . PHP_EOL;
158
159 // vim: et sw=4 sts=4