Ignore php.ini
[xhtml-compiler.git] / update.php
blob5ca812ed7bf1c4bf5afd20268989104792054551
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');
77 $markup = $xc->getFilterManager()->getMarkup();
78 do {
79 $touched = false;
80 foreach ($allowed_dirs as $dir => $recursive) {
81 $dir = new XHTMLCompiler_Directory($dir);
82 // search for source files
83 foreach ($markup as $ext => $x) {
84 $files = $dir->scan('.' . $ext, $recursive);
85 foreach ($files as $file) {
86 try {
87 $page = new XHTMLCompiler_Page($file);
88 if ($type == 'all' && !$page->isCacheExistent()) {
89 // generate a new page
90 $touched = true;
91 $page->generate();
92 $created[] = $page->getCachePath();
93 } elseif ($type == 'force') {
94 $page->generate();
95 $updated[] = $page->getCachePath();
97 } catch (Exception $e) {
98 echo "Failed on $file\n";
99 echo $e . "\n";
103 // search for cache files
104 $files = $dir->scan('.html', $recursive);
105 foreach ($files as $file) {
106 try {
107 $page = new XHTMLCompiler_Page($file, true);
108 // we're only updating files, not creating new ones
109 if ($page->isSourceExistent()) {
110 if (
111 // don't do it for force, since it's already been done
112 $type != 'force' &&
113 $page->isCacheExistent() &&
114 $page->isCacheStale()
116 $page->generate();
117 $updated[] = $page->getCachePath();
118 $touched = true;
120 } elseif (is_created_by_us($orphan = $page->getCachePath())) {
121 unlink($orphan); // orphan
122 $orphans[] = $orphan;
124 } catch (Exception $e) {
125 echo "Failed on $file\n";
126 echo $e . "\n";
130 } while($touched && $type != 'force');
132 // nice error message
133 if (empty($updated)) $updated[] = '(none)';
134 echo wordwrap('Updated pages: ' . implode(', ', $updated)) . PHP_EOL;
136 if (empty($orphans)) $orphans[] = '(none)';
137 echo wordwrap('Removed orphans: ' . implode(', ', $orphans)) . PHP_EOL;
139 if ($cli && ($type != 'force')) {
140 if (empty($created)) $created[] = '(none)';
141 echo wordwrap('Created pages: ' . implode(', ', $created)) . PHP_EOL;