This is an experimental plugin for the upcoming caching plugin. When it’s hooked with OnWebPageInit, it saves the content of the generated page to /htcache/path/alias.html (so create /htcache and set right rights).
function mkdir_r($dirName, $rights=0777){
$dirs = explode('/', $dirName);
$dir='';
foreach ($dirs as $part) {
$dir.=$part.'/';
if (!is_dir($dir) && strlen($dir)>0)
mkdir($dir, $rights);
}
}
function repairpath($url) {
return str_replace("//","/",$url);
}
function htcache_out($output) {
$htcache_ignore = "file favicon.ico, file robots.txt";
global $modx;
$htcache_root="c:/root/htcache";
$dirname="";
if (count($_GET)<=1) {
if (!isset($_GET["q"])) $filename=repairpath($htcache_root."/index.html"); else {
if (strpos(" ".$htcache_ignore,"file ".$_GET["q"])) {
$msg="Error: this file '".$_GET["q"]."' should exist and can't be renderred by the CMS.";
$output=$msg;
return $msg;
}
$filename=repairpath($htcache_root."/".$_GET["q"].".html");
}
if (!file_exists($filename)) {
$dirname=preg_replace("|^(.*)/[^/]*$|","\\1",$filename);
mkdir_r($dirname);
}
$fcached=fopen($filename,"w");
fwrite($fcached,$output);
fclose($fcached);
}
//$output=$filename."<br/><br/>".$output;
return $output;
}
$e = &$modx->Event;
switch ($e->name) {
case "OnWebPageInit":
ob_end_clean();
$actual_ob_content=ob_get_contents();
ob_end_clean();
ob_start("htcache_out");
echo $actual_ob_content;
ob_start();
/*
$doc = &$modx->documentOutput;
$modx->documentOutput=$modx->rewriteUrls($doc);
*/
break;
default: // stop here
return;
break;
}
return 1;
You then add to .htaccess these lines:
RewriteCond %{REQUEST_URI} ^/index\..{3,4}$
RewriteCond %{QUERY_STRING} ^$
RewriteCond htcache/index.html -F
RewriteRule index.php htcache/index.html [L,QSA]
RewriteCond htcache%{REQUEST_URI}.html -f
RewriteRule ^(.*)$ htcache%{REQUEST_URI}.html [L,QSA]
...and all documents will be taken from the static .html files, not even running PHP.
The only problem is, that no refreshing is implemented yet =)
Please change the $htcache_root.
Please appreciate my nice ob_stuff tweaking to rewrite the default handling ;-) :
[tt] ob_end_clean();
$actual_ob_content=ob_get_contents();
ob_end_clean();
ob_start("htcache_out");
echo $actual_ob_content;
ob_start();[/tt]
It works =)