Cache file not expiring in Bens Translator
Updated 07-05-2010: Changed the code to fix rather than remove the original check code as it seems the cache flush bypasses or doesn’t read the original check properly, so it needs the extra one.
I use Bens Translator on my site and it has been working great, however at some point I noticed that none of my old cache was expiring, so I did a bit of digging and this i what I came up with:
I know from when I had the old version 1.41 before the filemtime fix that I had an issue with getting warnings regarding filemtime, but gives no real info on what was the issue with the file, well it actually does, but it’s easily overlooked, more on that later, here’s the error it gave:
Warning: filemtime() [function.filemtime]: stat failed for _it in /my-path-here/wp-content/plugins/bens-translator/core/functions.php on line 1703
I have figured out this is because the function bentr_move_cached_file_to_stale in /core/functions.php is doing a time check on the $filename variable, as shown below:
/**
* Checks if Cache file is older and moves to stale folder
* @since 0.9
*/
function bentr_move_cached_file_to_stale($filename,$lang){
bentr_debug("bentr_move_cached_file_to_stale:: Run");
$bentr_error_expire_time = EXPIRE_TIME;
if (file_exists($filename)){
$filetime = filemtime($filename);
}
else {
return;
}
$filetime_days = (time() - $filetime) / 86400;
bentr_debug("EXPIRE_TIME::$bentr_error_expire_time::$filetime_days");
if (EXPIRE_TIME > 0 && $filetime_days >= EXPIRE_TIME ){
global $bentr_cache_dir;
global $bentr_stale_dir;
$cachedir = $bentr_cache_dir."/$lang";
$staledir = $bentr_stale_dir."/$lang";
$src = $cachedir . '/' . $filename;
$dst = $staledir . '/' . $filename;
if (!@rename($src,$dst)){
bentr_debug("Unable to move cached file $src to stale $dst");
} else {
bentr_debug("Moving cached file $src to stale $dst");
}
}
ELSE {
$expire_time_error = EXPIRE_TIME;
bentr_debug("Created less than $expire_time_error day(s) ago");
}
}
However $filename is just the name of the file as it is pulling $hash at line 1115, not the full path, here is the code earlier in the file that sends the $hash variable’s contents to the function as $filename:
//check if needs to be scheduled for a new translation
$filetime_days = (time() - filemtime($filename)) / 86400;
if (EXPIRE_TIME > 0 && $filetime_days >= EXPIRE_TIME ){
bentr_debug("bentr_get_page_content :: The file $filename has been created more than " . EXPIRE_TIME . " days ago. Scheduling for a new translation");
bentr_move_cached_file_to_stale($hash,$lang);
}
So that instance of filemtime works fine because it is dealing with the full path, but the instance of filemtime in the bentr_move_cached_file_to_stale function fails because it is dealing with just the $hash variable which appears to be just the name of the file, but it doesn’t even have to run because it has already run in the above code before it is called, so the error message above was telling me what was wrong as it was giving me just the filename, not the full path.
The reason why I checked version 1.41 is because that is the last version of the code where the script even runs, so it seems the fix for the filemtime bug:
if (file_exists($filename)){
$filetime = filemtime($filename);
}
else {
return;
}
…is the reason why it won’t expire the pages, it’s ending the function early if the file doesn’t exist, but of course the file doesn’t exist because it is given just a file name, not a full path. So… this version of the function bentr_move_cached_file_to_stale should work:
Updated due to findings about the cache flush:
/**
* Checks if Cache file is older and moves to stale folder
* @since 0.9
*/
function bentr_move_cached_file_to_stale($filename,$lang){
bentr_debug("bentr_move_cached_file_to_stale:: Run");
$bentr_error_expire_time = EXPIRE_TIME;
global $bentr_cache_dir;
global $bentr_stale_dir;
$cachedir = $bentr_cache_dir."/$lang";
$staledir = $bentr_stale_dir."/$lang";
$src = $cachedir . '/' . $filename;
$dst = $staledir . '/' . $filename;
if (file_exists($src)){
$filetime = filemtime($src);
}
else {
return;
}
$filetime_days = (time() - $filetime) / 86400;
bentr_debug("EXPIRE_TIME::$bentr_error_expire_time::$filetime_days");
if (EXPIRE_TIME > 0 && $filetime_days >= EXPIRE_TIME ){
if (!@rename($src,$dst)){
bentr_debug("Unable to move cached file $src to stale $dst");
} else {
bentr_debug("Moving cached file $src to stale $dst");
}
}
ELSE {
$expire_time_error = EXPIRE_TIME;
bentr_debug("Created less than $expire_time_error day(s) ago");
}
}
I have tested it and it works fine for me every time I have run it, also I can’t figure out how the code could have worked for anyone, if $hash gave a full path which is the only way it could have worked then by the time the function is at the end the path would be doubled and would error out at the end anyway, ie /path/to/file/path/to/file rather than just /path/to/file.
Another little thing that I noticed was that the backup function doesn’t work in the admin area, because on line 5 of /core/bentr-cacheman.php there’s an incorrect path causing it to create an empty tar, the line should be:
exec( "tar cfvz $path/benstranslatorbackup.tar.gz ".WP_CONTENT_DIR."/ben-translate-cache");
Still not sure why the translation warning bar won’t show though.
Edit: Translation warning bar wasn’t showing because my body tag has no spaces in it and functions.php line 1082 has a space after the body tag that causes it to skip it on my site, removing the space worked for me and should work for all websites due to the regular expression:
$page = preg_replace("/<body[^>]*>/i", "$replace" ,$page);


