PostCache file not expiring in Bens Translator

May 1, 2010 – 8:13 am

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);

PostWhispy Blue Theme Release

August 16, 2009 – 3:14 pm
Post Image

I’ve just released the latest theme for WordPress, “Whispy Blue”, you can find out more information and download it here.

Whispy Blue

Whispy Blue

Leave a comment below if you experience any issues with using the theme.

Version 1.05 has been released, get it here.

PostNew Theme! – Light Curves

April 2, 2009 – 2:05 am
Post Image

This theme has taken me some time to finish, at one point I had given up on it due to it’s complexity so I removed some unneeded features and was able to get it done.

More information can be found here

If you need any help on this theme, send a comment below.

Update: Version 1.01 now up

PostWordPress 2.7 RC1 update and a few tips

December 5, 2008 – 11:40 am
Post Image

Today I upgraded Tech Gremlin to WordPress 2.7, it was pain free and I love what they have done with the admin section. I was expecting to have a bit of a learning curve with finding where everything is now however I am fairly surprised that everything is where I expect it to be.

A few issues of note, the Blogging Tips plugin is now situated in a spot that doesn’t look right, needs to be about 20 pixels lower.

Tip #1
If you are using the Google Sitemaps plugin and you experience php errors uploading the new sitemaps files after upgrading your WordPress version, this is a permissions issue and is fixed by deleting sitemaps.xml and the gzipped version of the file if you also use that and refreshing the page, posting the data you attempted to post before.

Tip #2
Try to just use the html version of the post add/editor as graphical post editors make a general mess of the html and make editing it later a slow and tortuous experience.

Tip #3
I know this seems pretty obvious to some however it is important to try to get a host situated physically closer to you, due to the latency it may take as much as an hour more to upgrade your blog via ftp than it would with a host that is situated in your country as each file is small, yet needs to be negotiated for on the connection each time, this ends up taking it much longer to upload than just one file of the same size, it is highly affected by latency. If however you cannot get around having a host a fair way away, uploading the wordpress zip file and using their admin panel (or the shell if you are familiar with it) to extract it into place is much faster even than the previous example, however not all hosts support this.

PostWordPress 2.7 very soon

December 3, 2008 – 5:36 pm
Post Image

It appears WordPress 2.7 will be coming out in 2 days time, 9am Western Australia Daylight Savings time on Friday. I’m quite interested as I haven’t been keeping up to date with it’s development as much as I would have liked, I will be able to finish my new theme once I know there will be no major changes in the foreseeable future after this release.

PostSite Issues

December 3, 2008 – 4:37 pm

Starting this afternoon I’ve been experiencing a few issues between some WordPress code and using PHP5. It seems when I use PHP5, my default version for some time, I experience a rather large error that according to WordPress.org seems to be possibly related to Magic Quotes or a rogue plugin, however it is occurring on my theme test site as well so I’m not so sure about plugins being the cause. So in an effort to get some stability I have reverted to PHP4 and I’m about to upgrade WordPress and see if I can sort out this problem.

Problem Solvered
As I have been noticing it appears to be related to magic quotes, I attempted to turn it off in .htaccess but this appeared to just make it angry, however adding

ini_set('magic_quotes_runtime','off');

to my wp-config.php file fixed the problem.

By the way if you have this problem do not copy my code listed above as the quotes will turn into invalid characters, you’ll have to type it out yourself.

PostFirst WordPress Theme

September 28, 2008 – 5:41 pm
Post Image

I decided that I would give a go at writing an ambitious WordPress theme that hopefully will be much different from most WordPress themes, the general layout is familiar, but with little twists so I hope to bring something different to the WordPress community.

I have hit many snags along the way but so far it is W3C compliant and works in IE 7 and 8, FF and Safari/Chrome, however it looks shocking in IE6 using IE NetRenderer

I started writing this post 3 days ago and forgot what else to write about.

The site design is a bit over the place still, it’ll be cleaned up over the next few days after it has been finished.

PostServage Referral Widget 1.21 released

September 18, 2008 – 5:29 pm
Post Image

This is not a necessary upgrade, all I have done is updated the URL in certain parts of the widget and readme to reference the new domain. Download Here.

PostChanging Domains

September 18, 2008 – 4:24 pm

Tuesday night I set about purchasing a new domain name for the blog, firstly it was tough simply finding one that could serve the purpose. .com domains that actually mean anything are starting to be in very short supply and even though many interesting companies have come up with words that at the time didn’t mean anything, a blog is suited to words that make sense from day 1.

After choosing my domain and delegating the name servers (surprisingly fast nowadays) I was able to start the difficult task of moving an establishing blog to a new domain while the previous one was still running.

To move a WordPress blog, you must have the contents of the site’s folder (ie public_html) and saved sql file, once you’ve done that, upload the wordpress files to the new site, upload the sql files to the new database and change the details in wp-config.php file, once you’ve done that you must change the domain listed in the sql table wp_options from your old one to your new one. Then run this sql command:

UPDATE wp_posts SET guid = REPLACE (guid, 'http://exampleoldsiteurl.com', 'http://examplenewsiteurl.com');

This is assuming you have the default names for wp_posts and guid, change the first url to your old domain, change the second url to your new domain.

This was fairly straight forward, however it seems that every link in every post and page’s links are hard coded with the url of the old domain, so I went through using a similar command to the previous one to change every instance in every post to the new address

There were also some references to the absolute address for the site, which changed completely, they didn’t seem to be causing problems, but I changed them just to be sure.

Once I had done that the site was active, I just needed to change a few minor things and it was almost ready.

Firstly I redirected beejay.perthmatrix.com/* to www.techgremlin.com/*, this is to ensure that anywhere that I haven’t updated my address and anywhere that has a link to beejay.perthmatrix.com is still able to access the area requested as the server automatically redirects, for example if you go to http://beejay.perthmatrix.com/2008/09/17/the-digital-downloads-delusion/ you’ll find that you have just arrived at http://www.techgremlin.com/2008/09/17/the-digital-downloads-delusion/ without having to update the link, it also works for image links.

If you are running cpanel this is quite easy, on the redirection page add your old address to the first field and add your new domain to the field underneath titled ‘redirect to’ and check the box labelled wild card redirect, then click add.

This also helps me as my site works well for it’s SEO abilities and considering I don’t know what I’m doing in that area half the time. Allowing the old domain to stay open, while redirecting people who have found the old site through a search engine allows the new domain time to be indexed by the same search engines.

WordPress.com accounts are tricky, it seems that if you create a username for use with the WordPress Stats Plugin for a site, it is linked to it forever, further more you can’t add any more, so I instead created a new account for this domain.

PostWordPress 2.7 Navigation Survey

September 16, 2008 – 12:31 pm
Post Image

If your blog is running WordPress make sure you take the WordPress 2.7 Navigation Survey, there will be some navigation changes in the next major release (2.7) in November and WordPress would like your input.

Take the Survey Here. It takes less than 5 minutes.