Add documentation on compile process so I can repeat it! :-)
[xhtml-compiler.git] / update.php
blob4b9395d33ca37aba50e328c79198f1c8f6e30841
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 = (php_sapi_name() === 'cli')) {
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 'hc-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 files';
30 exit;
33 // clean up the cache
34 if ($type == 'clean') {
35 $files = scan_dirs_for_pages('.html', $xc->getConf('allowed_dirs'));
36 foreach ($files as $file) {
37 if (is_created_by_us($file)) {
38 unlink($file);
41 exit('Removed all cache files.');
44 // regular processing
45 list($files, $src_files) = scan_dirs_for_pages(
46 array('.html', '.xhtml'), $xc->getConf('allowed_dirs')
49 // status arrays
50 $updated = array();
51 $created = array();
52 $orphans = array();
54 foreach ($files as $file) {
55 $page = new XHTMLCompiler_Page($file, true);
56 // we're only updating files, not creating new ones
57 if ($page->isSourceExistent()) {
58 if (
59 // don't do it for force, since it's handled below
60 $type != 'force' &&
61 $page->isCacheExistent() &&
62 $page->isCacheStale()
63 ) {
64 $page->generate();
65 $updated[] = $page->getCachePath();
67 } elseif (is_created_by_us($page->getCachePath())) {
68 unlink($page); // orphan
69 $orphans[] = $page->getCachePath();
73 foreach ($src_files as $file) {
74 $page = new XHTMLCompiler_Page($file);
75 if ($type == 'all' && !$page->isCacheExistent()) {
76 // generate a new page
77 $page->generate();
78 $created[] = $page->getCachePath();
79 } elseif ($type == 'force') {
80 $page->generate();
81 $updated[] = $page->getCachePath();
85 // nice error message
86 if (empty($updated)) $updated[] = '(none)';
87 echo wordwrap('Updated pages: ' . implode(', ', $updated)) . PHP_EOL;
89 if (empty($orphans)) $orphans[] = '(none)';
90 echo wordwrap('Removed orphans: ' . implode(', ', $orphans)) . PHP_EOL;
92 if ($cli && ($type != 'force')) {
93 if (empty($created)) $created[] = '(none)';
94 echo wordwrap('Created pages: ' . implode(', ', $created)) . PHP_EOL;