More svn:eol-style=native for XHTML files.
[xhtml-compiler.git] / update.php
blob852d07f3349804a8c80b67e5bdfb572b59150abc
1 <?php
3 /**
4 * Updates all files within our jurisdiction.
5 * @note This wants to be in a post-commit hook!
6 */
8 require 'common.php';
10 $xc = XHTMLCompiler::getInstance();
12 // if in command line mode, allow more options
13 $type = 'normal';
14 if ($cli = (!ini_get('register_globals') && !empty($argv))) {
15 set_time_limit(0);
16 $type = isset($argv[1]) ? $argv[1] : 'normal'; // see below
17 } else {
18 header('Content-type: text/plain');
21 // display a help file
22 if ($type == 'help') {
23 echo
24 'php update.php [type] -- Updates compiled HTML files.
25 [type] is:
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
31 exit;
34 // clean up the cache
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;
45 unlink($file);
48 // cleanup dependency files
49 $files = $dir->scan('.xc-deps', $recursive);
50 foreach ($files as $file) unlink($file);
52 // cleanup RSS files (probably should be somewhere else)
53 $files = $dir->scan('.rss', $recursive);
54 foreach ($files as $file) {
55 $contents = file_get_contents($file);
56 if (strpos($contents, '<generator>XHTML Compiler</generator>') !== false) {
57 unlink($file);
63 echo 'Removed all cache and dependency files.';
64 exit;
67 // status arrays
68 $updated = array();
69 $created = array();
70 $orphans = array();
72 $allowed_dirs = $xc->getConf('allowed_dirs');
74 foreach ($allowed_dirs as $dir => $recursive) {
75 $dir = new XHTMLCompiler_Directory($dir);
76 // search for source files
77 $files = $dir->scan('.xhtml', $recursive);
78 foreach ($files as $file) {
79 $page = new XHTMLCompiler_Page($file);
80 if ($type == 'all' && !$page->isCacheExistent()) {
81 // generate a new page
82 $page->generate();
83 $created[] = $page->getCachePath();
84 } elseif ($type == 'force') {
85 $page->generate();
86 $updated[] = $page->getCachePath();
89 // search for cache files
90 $files = $dir->scan('.html', $recursive);
91 foreach ($files as $file) {
92 $page = new XHTMLCompiler_Page($file, true);
93 // we're only updating files, not creating new ones
94 if ($page->isSourceExistent()) {
95 if (
96 // don't do it for force, since it's already been done
97 $type != 'force' &&
98 $page->isCacheExistent() &&
99 $page->isCacheStale()
101 $page->generate();
102 $updated[] = $page->getCachePath();
104 } elseif (is_created_by_us($page->getCachePath())) {
105 unlink($page); // orphan
106 $orphans[] = $page->getCachePath();
111 // nice error message
112 if (empty($updated)) $updated[] = '(none)';
113 echo wordwrap('Updated pages: ' . implode(', ', $updated)) . PHP_EOL;
115 if (empty($orphans)) $orphans[] = '(none)';
116 echo wordwrap('Removed orphans: ' . implode(', ', $orphans)) . PHP_EOL;
118 if ($cli && ($type != 'force')) {
119 if (empty($created)) $created[] = '(none)';
120 echo wordwrap('Created pages: ' . implode(', ', $created)) . PHP_EOL;