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');
78 foreach ($allowed_dirs as $dir => $recursive) {
79 $dir = new XHTMLCompiler_Directory($dir);
80 // search for source files
81 $files = $dir->scan('.xhtml', $recursive);
82 foreach ($files as $file) {
83 $page = new XHTMLCompiler_Page($file);
84 if ($type == 'all' && !$page->isCacheExistent()) {
85 // generate a new page
87 $created[] = $page->getCachePath();
88 } elseif ($type == 'force') {
90 $updated[] = $page->getCachePath();
93 // search for cache files
94 $files = $dir->scan('.html', $recursive);
95 foreach ($files as $file) {
96 $page = new XHTMLCompiler_Page($file, true);
97 // we're only updating files, not creating new ones
98 if ($page->isSourceExistent()) {
100 // don't do it for force, since it's already been done
102 $page->isCacheExistent() &&
103 $page->isCacheStale()
106 $updated[] = $page->getCachePath();
108 } elseif (is_created_by_us($orphan = $page->getCachePath())) {
109 unlink($orphan); // orphan
110 $orphans[] = $orphan;
115 // nice error message
116 if (empty($updated)) $updated[] = '(none)';
117 echo wordwrap('Updated pages: ' . implode(', ', $updated)) . PHP_EOL
;
119 if (empty($orphans)) $orphans[] = '(none)';
120 echo wordwrap('Removed orphans: ' . implode(', ', $orphans)) . PHP_EOL
;
122 if ($cli && ($type != 'force')) {
123 if (empty($created)) $created[] = '(none)';
124 echo wordwrap('Created pages: ' . implode(', ', $created)) . PHP_EOL
;