4 * Updates all files within our jurisdiction.
5 * @note This wants to be in a post-commit hook!
10 $xc = XHTMLCompiler
::getInstance();
12 // if in command line mode, allow more options
14 if ($cli = (!ini_get('register_globals') && !empty($argv))) {
16 $type = isset($argv[1]) ?
$argv[1] : 'normal'; // see below
18 header('Content-type: text/plain');
21 // display a help file
22 if ($type == 'help') {
24 'php update.php [type] -- Updates compiled HTML files.
26 . normal : update existing files, remove orphans (default)
27 . all : normal + create files without cached output
28 . force : regenerate all files
29 . clean : remove all cache and dependency files
35 if ($type == 'clean') {
37 $allowed_dirs = $xc->getConf('allowed_dirs');
38 foreach ($allowed_dirs as $dir => $recursive) {
39 $dir = new XHTMLCompiler_Directory($dir);
41 // cleanup HTML cache files
42 $files = $dir->scan('.html', $recursive);
43 foreach ($files as $file) {
44 if (!is_created_by_us($file)) continue;
48 // cleanup dependency files
49 $files = $dir->scan('.xc-deps', $recursive);
50 foreach ($files as $file) unlink($file);
52 // cleanup fragment files
53 $files = $dir->scan('.frag', $recursive);
54 foreach ($files as $file) unlink($file);
56 // cleanup RSS files (probably should be somewhere else)
57 $files = $dir->scan('.rss', $recursive);
58 foreach ($files as $file) {
59 $contents = file_get_contents($file);
60 if (strpos($contents, '<generator>XHTML Compiler</generator>') !== false) {
67 echo 'Removed all cache and dependency files.';
76 $allowed_dirs = $xc->getConf('allowed_dirs');
79 foreach ($allowed_dirs as $dir => $recursive) {
80 $dir = new XHTMLCompiler_Directory($dir);
81 // search for source files
82 $files = $dir->scan('.xhtml', $recursive);
83 foreach ($files as $file) {
84 $page = new XHTMLCompiler_Page($file);
85 if ($type == 'all' && !$page->isCacheExistent()) {
86 // generate a new page
89 $created[] = $page->getCachePath();
90 } elseif ($type == 'force') {
92 $updated[] = $page->getCachePath();
95 // search for cache files
96 $files = $dir->scan('.html', $recursive);
97 foreach ($files as $file) {
98 $page = new XHTMLCompiler_Page($file, true);
99 // we're only updating files, not creating new ones
100 if ($page->isSourceExistent()) {
102 // don't do it for force, since it's already been done
104 $page->isCacheExistent() &&
105 $page->isCacheStale()
108 $updated[] = $page->getCachePath();
111 } elseif (is_created_by_us($orphan = $page->getCachePath())) {
112 unlink($orphan); // orphan
113 $orphans[] = $orphan;
117 } while($touched && $type != 'force');
119 // nice error message
120 if (empty($updated)) $updated[] = '(none)';
121 echo wordwrap('Updated pages: ' . implode(', ', $updated)) . PHP_EOL
;
123 if (empty($orphans)) $orphans[] = '(none)';
124 echo wordwrap('Removed orphans: ' . implode(', ', $orphans)) . PHP_EOL
;
126 if ($cli && ($type != 'force')) {
127 if (empty($created)) $created[] = '(none)';
128 echo wordwrap('Created pages: ' . implode(', ', $created)) . PHP_EOL
;