Multiplay Labs

from the MPUK.HQ

WordPress v3.0 breaks pagination for sites with category in their permalinks

without comments

Unfortunately WordPress v3.0 breaks post pagination for sites that include category in their permalinks.

The issue is caused by a fix to wp-includes/canonical.php (r13781) that was added to fix bug #14201

The code in question is:

} elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false ) {
           $category = get_term_by('slug', get_query_var('category_name'), 'category');
           $post_terms = wp_get_object_terms($wp_query->get_queried_object_id(), 'category', array('fields' => 'tt_ids'));
           if ( (!$category || is_wp_error($category)) || ( !is_wp_error($post_terms) && !empty($post_terms) && !in_array($category->term_taxonomy_id, $post_terms) ) )
               $redirect_url = get_permalink($wp_query->get_queried_object_id());

The problem is that get_term_by doesn’t support hierarchies so when passed a second level category e.g. cars/sports it will fail and hence the rewrite is performed loosing the page information.

The following fixes this but I’m not 100% that the intention is to only check the last category in the hierarchy, although with our data anything more would appear to fail. This may indicate that additional fixes are required but anyway:-

$category = get_term_by('slug', end( explode( '/', get_query_var('category_name') ) ), 'category') ;

For more information see WordPress Bug #13471

Written by Steven Hartland

June 30th, 2010 at 6:53 pm

Posted in Hackery

Vbulletin Error Template issues

without comments

The Standard Error template in Vbulletin ( 3.8.4 Patch Level 2 ) is broken due to a missing global declaration of $spacer_open, $spacer_close.

This shouldn’t break the template with both being missing but during the processing of the template spacer_close gets defined, so the resulting html is broken.

The fix is to add:

global $spacer_open, $spacer_close;

to the head of function standard_error in includes/functions.php

Written by Steven Hartland

June 22nd, 2010 at 10:04 am

Posted in Hackery

A Great CSS Gradient Generator Tool

without comments

Found this the other day and its a great little tool for generating CSS gradients :)

Written by Steven Hartland

June 21st, 2010 at 9:20 pm

Posted in HTML & CSS

How to fix body background images not displaying correctly in Chrome

without comments

It’s been one of the annoyances which has eluded me for quite a while now. When a page with a background image e.g. background-image: url( myimage.jpg) on the document body loads, sometimes it won’t display, or will partially display in Chrome. If you resize the window or browse full screen then your fine so it seems really random.

Well today I discovered a simple little work around, all you need is following in your css:

html { height: 100% }

Written by Steven Hartland

June 21st, 2010 at 8:58 pm

Posted in HTML & CSS

Preventing XSL transforms self closing HTML tags

without comments

Every once in a while you will wan’t to do the something like the following an xsl stylesheet:

<div></div>

Unfortunately as XML is well formed and HTML isn’t the resulting HTML will be invalid e.g

<div/>

The solution to this is quite simple, all you need to do is to tell the transform engine you are generating HTML using the output tag e.g.

<xsl:output method="html" />

Also note that if your target html is actually xhtml then using html as the output method will have some side effect e.g.

<br />

will be incorrectly replaced with

<br>

So what do you do if you want xhtml and empty elements which require a closing tag and not short form?

Well I’ve found two methods:-
1. Use a text node with output escaping disabled

<xsl:text disable-output-escaping="yes">&lt;div&gt;&lt;/div&gt;</xsl:text>

2. Use a xsl comment

<div><xsl:comment>.</xsl:comment></div>

The first means you need to escape all of you < / >, which is cumbersome the second adds unneeded comments to your output, so its up to you which downside you prefer ;-)

Written by Steven Hartland

June 19th, 2010 at 11:37 pm

Posted in XSL

Tagged with , ,

Fun with Cookie Domains and Rails

without comments

If you are working on a site in rails that supports multiple subdomains and want to share a cookie between them, it makes sense to set your cookie domain in your environment.rb to something like:

	config.action_controller.session = {
		:key => 'my_app_key',
		:secret => 'my_super_secret',
		:domain => '.domain.tld'
	}

However, you may come across a weird problem when developing locally, where the CSRF protection no longer works, always throwing up an invalid authenticity token error.

The answer to this is simple – by setting the cookie domain you’ve essentially cut your local machine (or any machine not in that domain for that matter) out of the loop, as per the usual cookie security policies. The easy way to be able to continue to do local development is set up a local DNS alias with a matching domain scheme. For me, I did this to my /etc/hosts file:

127.0.0.1 local.domain.tld

And now doing local development via http://local.domain.tld works a charm.

Written by Andrew Montgomery-Hurrell

June 16th, 2010 at 8:55 am

Posted in Code,Hackery,Rails

FreeBSD partition edit with sysinstall results in “Invalid partition table”

without comments

After making what should have been a simple change to the partition layout on one of our FreeBSD 7.0 machines, where we split one partition into two. Upon writing changes sysinstall complained about missing directory for the second partition. It seemed it had created both partitions but only the first mount point directory. In addition the second partition label da0s3h wasn’t present so after creating the missing directory we rebooted the machine.

On reboot we where greeted by the error “Invalid partition table”. This was very worrying as the machine contained a large amount of data so was going to take ages to rebuild.

Our first thought was to reboot the machine from the livefs CDROM and attempt a repair but this failed as the CDROM emulation presented by the Supermicro IPMI, that we where using, wouldn’t play nice refusing to load with error = 5 from geom.

After much playing with various combinations we tried 8.1-BETA1 as a last resort, which to our surprise simply just worked :)

So now we could attempt a repair. sysinstall still showed all our partitions so it looked like all our data was intact. After a detailed inspection of the partition table using fdisk, it became apparent that for some unknown reason when we commited the changes to split the partition in two, sysinstall had marked all slices as active, which was confusing the boot loader.

To repair all we need to do was a simpple fdisk -a da0, once this was done we had just the one active partition and the machine booted as normal.

Once booted we performed a fsck which refused to acknowledge the fs on the two new partitions. Running newfs and them mounting them fixed this.

So the moral of this story is don’t use sysinstall to split partitions or if you do ensure you correct the active state of the slices before rebooting ;-)

Written by Steven Hartland

June 9th, 2010 at 11:49 am

Posted in FreeBSD

Invalid SQL Generated by Mixed Relative / Qualified Conditions in ActiveRecord

without comments

If you mix relative and fully qualified conditions in ActiveRecord, your in for random results with the potential for invalid SQL.

The following is an example of code which may or may not work.

User.find( :all, { :name => 'wibble', 'email.valid' => true }, :join => [ :email ] )

The problem lies in the base method sanitize_sql_hash_for_conditions that generates the fully qualified SQL statements. It takes a “default” table name that’s used for unqualified conditions, however it overwrites this variable if it comes across an fully qualified condition. This means that the generation of the SQL depends on the internal order of the hash resulting in unpredictable output.

In our case code which had been working for months suddenly stopped generating valid SQL without any changes, we even had a dev instance and a live instance running identical code but generating different SQL its that unpredictable. It seems something totally unrelated had effected the internal hash order and hence the validity of the generated SQL.

The fix for this is simply to ensure that fully qualified conditions don’t overwrite the default for relative conditions, the following patch achieves this.

Index: vendor/rails/activerecord/lib/active_record/base.rb
===================================================================
--- vendor/rails/activerecord/lib/active_record/base.rb (revision 1332)
+++ vendor/rails/activerecord/lib/active_record/base.rb (working copy)
@@ -2308,11 +2308,13 @@
 
               # Extract table name from qualified attribute names.
               if attr.include?('.')
-                table_name, attr = attr.split('.', 2)
-                table_name = connection.quote_table_name(table_name)
+                tn, attr = attr.split('.', 2)
+                tn = connection.quote_table_name(tn)
+                attribute_condition("#{tn}.#{connection.quote_column_name(attr)}", value)
+              else
+                attribute_condition("#{table_name}.#{connection.quote_column_name(attr)}", value)
               end
 
-              attribute_condition("#{table_name}.#{connection.quote_column_name(attr)}", value)
             else
               sanitize_sql_hash_for_conditions(value, connection.quote_table_name(attr.to_s))
             end

Written by Steven Hartland

October 9th, 2009 at 10:33 am

Posted in Code,Hackery,Rails

Invalid Foreign Key Silently Breaks Rails ActiveRecord belongs_to

without comments

If for whatever reason the foreign key is invalid for a belongs_to relationship in ActiveRecord then the relationship is silently ignored. This results in object.relationship.method calls failing with nil errors and is quite frustrating to debug as no attempt to load the related object is logged.
Given this if a related object which is listed as a belongs_to in the model yet doesn’t work check that either the manual :foreign_key or the automatically determined version i.e. name + ‘_id’ exists in the dependencies table.

Written by Steven Hartland

September 21st, 2009 at 12:18 am

Posted in Code,Rails

Logging php errors when using php-fpm

without comments

When moving to using php-fpm to serve pages via nginx from apache, its not very obvious how to see errors logged via the php function error_log, so I thought I’d post how to do it here so others don’t have to do quite a much searching as I did.

Its really simple just ensure both the following php variables are set:

  • error_log
  • log_errors

This can be done either in the php.ini but the method I prefer is setting it up in php-fpm.conf e.g.

<?xml version="1.0" ?>
<configuration>
  <workers>
    <section name="pool">
      <value name="php_defines">
        <value name="error_log">/var/log/php-error.log</value>
        <value name="log_errors">true</value>
      </value>
    </section>
  </workers>
</configuration>

Written by Steven Hartland

September 13th, 2009 at 8:22 pm

Posted in Hackery,Performance