Add entity files to xc-deps.
[xhtml-compiler.git] / update.php
blob9a432236e4bf3cdfccedf5ac9e37fbfd5c716518
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 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) {
61 unlink($file);
67 echo 'Removed all cache and dependency files.';
68 exit;
71 // status arrays
72 $updated = array();
73 $created = array();
74 $orphans = array();
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
86 $page->generate();
87 $created[] = $page->getCachePath();
88 } elseif ($type == 'force') {
89 $page->generate();
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()) {
99 if (
100 // don't do it for force, since it's already been done
101 $type != 'force' &&
102 $page->isCacheExistent() &&
103 $page->isCacheStale()
105 $page->generate();
106 $updated[] = $page->getCachePath();
108 } elseif (is_created_by_us($page->getCachePath())) {
109 unlink($page); // orphan
110 $orphans[] = $page->getCachePath();
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;