Hi,
first of all I want to say that I really like the changes in WB 2.8 RC1, and I hope WB continues being awesome!
--
Anyways:
I wrote a little php script that caches wb pages automatically and ouputs them.
Here's how it works:
1. It checks if there exists a cachefile for the requested page (via the modified_date and the page_id)
2. If there exists one it will include it and its done
3. If there doesn't exist a cachefile it will display the page normally (wb will generate the page as usual), and it will create a new cachefile.
3. If there exists a cachefile but the cachefile is outdated, wb will display the page as usual and the script will generate a new cachefile and delete the outdated one.
Notes:
- All cachefiles are stored in WB_PATH."/cache/" (folder must already exist)
- The cachefiles look like this:
creationdate-pid-
page_id.html
for example: 1248370168-pid-7.html
- The script also uses the ob_start("ob_gzhandler"); php-function to compress the page
- It appends an html-comment with the time it took to create the page
HowTo INSTALL:
- rename the index.php file in the wb directory to index-1.php and comment out
echo $output; in the last line ( //echo $output )
- copy the cachescript in the wb folder and name it index.php (it replaces the old index.php)
Here's the script (with german comments):
index.php<?php
// seite komprimieren
ob_start("ob_gzhandler");
// function to measure speed of page
function getmicrotime($t)
{
list($usec, $sec) = explode(" ",$t);
return ((float)$usec + (float)$sec);
}
$starttime = microtime();
ob_start();
//$starttime = array_sum(explode(" ",microtime()));
// Include config file
require_once(dirname(__FILE__).'/config.php');
// Check if the config file has been set-up
if(!defined('WB_PATH')) {
header("Location: install/index.php");
exit(0);
}
// modified date aus der db holen
$sql_result = $database->query("SELECT * FROM `".TABLE_PREFIX."pages` WHERE `page_id` = '$page_id'");
$sql_row = $sql_result->fetchRow();
$last_modified = $sql_row['modified_when'];
$pid = $page_id;
// modified date aufräumen (wird eig. nicht mehr benötigt)
$badwords = array("/", "am", ":", " ", ",");
$ddate = str_replace($badwords, "", $last_modified);
// name des cache files
$ddate = $ddate."-pid-".$pid;
// pfad des cache files
$cachefile = WB_PATH."/cache/".$ddate.".html";
// überprüfen ob ein cachefile existiert das zum modified date passt
if (file_exists($cachefile) && (filemtime("includes/".$ddate)
< filemtime($cachefile))) {
// cache file einbinden
include($cachefile);
// informationen übers caching anhängen
echo "<!-- ".$ddate.".html";
echo " cached @ ".date('H:i', filemtime($cachefile))." was served -->";
}
else {
// falls kein passendes cachefile existiert
include(WB_PATH.'/index-2.php');
$sitecontent = $output;
// veraltetes cachefile suchfunktion
function boost_array_find($needle, $haystack, $a_not = array()) {
$out = array();
foreach($haystack as $key=>$value) {
if (strpos($value, $needle) !== FALSE) {
$good = TRUE;
foreach($a_not as $not) {
if (strpos($key, $not) !== FALSE) {
$good = FALSE;
}
}
if ($good) {
$out[$key] = $value;
}
}
}
return $out;
}
// cachefiles auflisten
$dir = WB_PATH."/cache/";
$files = scandir($dir);
$forthis = "-pid-".$pid;
// cachefile finden
$out = boost_array_find($forthis, $files);
// cachefile löschen
foreach ($out as $value) {
$oldfile = $dir.$value;
if (file_exists($oldfile)) {
// alte datei löschen
unlink($oldfile);
} else {
//print "The file $filename does not exist";
}
}
// neues cachefile anlegen
if (!$handle = fopen($cachefile, "w+")) {
print "Kann die Datei $cachefile nicht oeffnen";
exit;
}
// Schreibe seiteninhalt ins cachefile
if (!fwrite($handle, $sitecontent)) {
print "Kann in die Datei $cachefile nicht schreiben";
exit;
}
fclose($handle);
// content ausgeben damit die seite beim ersten aufruf nicht leer ist
echo $output;
}
ob_end_flush();
// function to output speed of page
$endtime = microtime();
$diff = getmicrotime($endtime) - getmicrotime($starttime);
echo "<!--".$diff." -->";
// komprimierte seite ausgeben
ob_end_flush();
?>
WARNING:
I'm not a developer and therefore the script is probably not optimal and it probably contains design errors and so on...
Please improve it if you can.
It's of course gpl.