-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate.php
147 lines (131 loc) · 5.09 KB
/
generate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
// Generates static web site pages.
require_once('config.php');
define('kPhpExtension', '.php');
define('kNewDirPermissions', 0755);
define('kIndex', 'index.php');
define('k404', '404.php');
function HtmlFromPhp($phpFile) {
// TODO: Handle errors.
return shell_exec(PHP_BINARY." ".$phpFile);
}
function IsPhp($fileName) {
$pos = strrpos($fileName, kPhpExtension);
return ($pos === false) ? false : strlen($fileName) - $pos == strlen(kPhpExtension);
}
// Does not delete $dir itself, only everything (except .git directory) inside. Does not stop execution on errors.
// .git directory is used in deployment scripts. On Windows it can have read only attributes set on some files. As a result
// not all files will be deleted and deployment scripts go crazy.
function RemoveFilesAndSubdirs($dir, $excludeDirs = array(".git")) {
if (file_exists($dir) === false)
return;
// Simple sanity check.
$dir = realpath($dir);
if ($dir == "/" or substr($dir, -2) == ":\\") {
echo "Do you really want to delete " . $dir . "?";
return;
}
$filter = function ($file, $key, $iterator) use ($excludeDirs) {
if ($iterator->hasChildren() && !in_array($file->getFilename(), $excludeDirs))
return true;
return $file->isFile();
};
$innerIterator = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS);
$iterator = new RecursiveIteratorIterator(new RecursiveCallbackFilterIterator($innerIterator, $filter), RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $fileInfo) {
$realPath = $fileInfo->getRealPath();
if ($fileInfo->isDir()) {
if (rmdir($realPath) === false)
echo "Error while rmdir " . $realPath . "\n";
} else {
if (unlink($realPath) === false)
echo "Error while unlink " . $realPath . "\n";
}
}
}
// $phpFiles is an array of filename => full/file/path/filename.
function BuildSiteMapXml($phpFiles) {
$siteMap = '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
foreach ($phpFiles as $file => $fullPath) {
// Ignore special 404.php page.
if (EndsWith($file, k404))
continue;
$url = URL($file);
// TODO: Lastmod should take into an account the modified date of page's included content.
$lastmod = date(DATE_W3C, filemtime($fullPath));
$siteMap = "$siteMap\n<url>\n <loc>$url</loc>\n <lastmod>$lastmod</lastmod>\n</url>";
}
return "$siteMap\n</urlset>";
}
function Generate($inDir, $outDir) {
$staticFilesCopied = 0;
if (file_exists($outDir))
RemoveFilesAndSubdirs($outDir);
else
mkdir($outDir, kNewDirPermissions, true);
print("Generating static html pages from php files:\n");
$iter = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($inDir),
RecursiveIteratorIterator::SELF_FIRST);
foreach($iter as $fileName => $fileInfo) {
$fileName = $iter->getFilename();
// Skip hidden files and directories, '.' and '..' directories.
if ($fileName[0] === '.')
continue;
// Generate html from .php files and simply copy everything else into the $outDir.
$outPath = FullPathTo($outDir, $iter->getSubPathName());
if ($fileInfo->isDir()) {
mkdir($outPath, kNewDirPermissions);
continue;
}
if (IsPhp($fileName)) {
$phpFileWithoutExtension = substr($outPath, 0, -strlen(kPhpExtension));
// Create folders with index.html files to redirect visitors to the same url without a slash.
if ($fileName != kIndex and $fileName != k404) {
mkdir($phpFileWithoutExtension, kNewDirPermissions);
$pageUrl = URL($fileName);
$redirectHtmlTemplate = <<<RHTML
<!DOCTYPE html>
<html>
<head>
<title>$pageUrl</title>
<link rel="canonical" href="$pageUrl"/>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<meta http-equiv="refresh" content="0; url=$pageUrl"/>
</head>
</html>
RHTML;
file_put_contents(FullPathTo($phpFileWithoutExtension, 'index.html'), $redirectHtmlTemplate);
}
$outPath = $phpFileWithoutExtension . '.html';
// TODO: Handle errors.
file_put_contents($outPath, HtmlFromPhp($fileInfo));
print("+ ".$outPath."\n");
$processedPhpFiles[$fileName] = $fileInfo;
}
else {
copy($fileInfo, $outPath);
$staticFilesCopied++;
}
}
$count = count($processedPhpFiles);
print("Processed $count php files.\n");
if ($staticFilesCopied)
print("Also copied ${staticFilesCopied} static resources.\n\n");
$sitemapPath = FullPathTo($outDir, 'sitemap.xml');
if (file_put_contents($sitemapPath, BuildSiteMapXml($processedPhpFiles)))
print("Generated sitemap $sitemapPath.\n");
else
print("ERROR creating sitemap $sitemapPath.\n");
}
function Usage($self) {
echo "Usage: php ".$self." <input www dir> <output www dir>\n";
echo "WARNING: All files in <output www dir> will be deleted!\n";
}
/////////////////////////////////////////////////////////////////////////////////////////
// Let's go!
/////////////////////////////////////////////////////////////////////////////////////////
if ($argc < 3) {
Usage($argv[0]);
exit;
}
Generate($argv[1], $argv[2]);