Harry Bailey

Harry Bailey specialise's in turning project chaos into clarity. With two decades of hands-on agency experience and agile-certified expertise, he offers practical, immediately actionable strategies, not just theory, to dramatically improve agency project delivery.

Fix apache after upgrading to Mountain Lion

Every time I upgrade OS X something breaks with apache or php or both.

The steps for me to fix it this time was…

* Open your httpd.conf file
* uncomment the Load php5 module
* uncomment the load vhosts module

in etc/apache2/users

* duplicate Guest.conf to harrybailey.conf – replace harrybailey with your username obviously
* edit harrybailey.conf to read:

<Directory "/Users/harrybailey/Sites/">
    Options Indexes MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

Note: as Michal mentions below you may want to add +FollowSymLinks to your options too. This allows the system to follow any symbolic links you have set up.

AGAIN – replace harrybailey with your username.

* save the file

You may also get a php error which includes…

“It is not safe to rely on the system’s timezone settings”

The fix for that is
* navigate to /etc/ and rename php.ini.default to php.ini
* edit your new php.ini file – find “;date.timezone = ” (no brackets)
* update it to read “date.timezone = Europe/London” (no brackets) – if you aren’t on GMT then find your relevant timezone string and use that
* save the file

* open terminal
* submit the line:
sudo apachectl restart

That did it for me. Hopefully it will do it for you too.

I was first seeing the default html file in the /Library/Webserver/Documents folder
I was then seeing 403 errors for all vhosts that I has setup
Personally I saw these problems after upgrading from Lion (10.7) to Mountain Lion (10.8)

Export structure and data from Navicat Premium as SQL

I was trying to find the quickest way to export both structure and data from all tables of a database from Navicat Premium and came up with these steps:

* Use the backup tool to create a backup of your database
* Right click the backup and select to Extract SQL…
* Save to wherever you like

You can now use that .sql file to create a new database wherever you like.

Yii (and Yii2) wildcard / catch all url rules

Both Yii and Yii2 have url management and routing built in. They will compare the url of the request to a list of rules you’ve defined.

Sometimes you might want to do various checks of the url in yii config main and then send every other request to a particular controller.

The rule you need to add last to the urlManager is…

'(.*)' => 'controller/action',

… and now any rule that isn’t matched previously will end up being sent to your defined controller and action.

In Yii2 you can also do the same:

'<url:(.*)>' => 'controller/action'

Extending jQuery selectors and understanding the options

There are tons of posts around which bang on about how to extend jQuery to include your own selectors, but to find one that actually explains your options is a pain, hence this post.

jQuery has an extend function baked in which looks something like $.extend(target, object1, object2); and will extend target (which should be an object) with the properties and methods of object1 and object2. You can add as many additional objects (think object3, object4 and object5) to the end as you like.

What we want to do it extend a very specific part of jQuery itself. The $.expr[‘:’] object which stores the built in selectors and any we add.

Indeed when you console.log($.expre[‘:’]) you see a full list of the selectors available in the version of jQuery you are using:

animated
button
checkbox
checked
data
disabled
empty
enabled
file
focus
focusable
has
header
hidden
image
input
onbranch
parent
password
radio
reset
selected
submit
tabbable
text
visible

NB: I’ve removed the ui- selectors to avoid confusion.

So now you know what not to call your selector (unless you want to replace a built in one), we can define our own…

$.extend($.expr[':'],{
	block: function(e,i,m,s) {
		return $(e).css('display') === 'block';
	}
});

The first thing you will notice about the above is that the selector function takes up to (although may need fewer) four argument; e, i, m and s.

e is the element currently being checked.
i is the index of that elements within all elements being checked (starts at zero)
m stands for match and is additional information that we can use in our function.
s stands for stack and is all elements in the current selection or stack

so $(e) makes the current element e into a jQuery object
i allows us to check for the 3rd, 6th, 23rd thing
m contains the following:

[0] is the actual selector called. For example :anyof(“div,4,hr,10″)
[1] is just the selector name. For example: anyof
[2] is the type of quotes used (if any) in the selector. So ” would be returned for :anyof(“div,4,hr,10”)
[3] is any parameters used in the form of a string so “div,4,hr,10” from above

and s contains an array of one or more html elements.

So now we know what is actually returned we can write a cleverer selector…

$.extend($.expr[':'],{
	anyof: function(e,i,m,s) {
		if(m[3].length)
		{
			$(m[3].split(',')).each(function(i2,e2)
			{
				if(parseInt(e2))
				{
					// correct index?
					if(parseInt(e2) == i)
					{
						return true;
					}
				}
				else
				{
					// match selector
					if($(e).is(e2))
					{
						return true;
					}
				}
			});
			return false;
		}
		else
		{
			return false;
		}
	}
});

or if we wanted to check for the unique items in the stack:

$.extend($.expr[':'],{
	isunique: function(e,i,m,s) {
		
		$(s).each(function(i2,e2)
		{
			if($(e2).prop('tagName') == $(e).prop('tagName'))
			{
				return false;
			}
		});

		return true;
	}
});

NB: Since jQuery 1.6 you should use .prop() for tagName not attr()

The above selector will check all other elements in the stack and return only those with a unique tag. We could expand it to check other things about the element to see if it is truly unique such as it’s classes or styles.

NB: I haven’t checked all the code above and certainly haven’t optimised it, but it is likely to work as is or with a small number of fixes.

Grouping by time periods or timeslices in Mysql

Sometimes when you pull back a whole load of results from a database table and order them by their timestamps you might want to either sum any result which happened at a time close to each other or you might want to only bring back one type of something within a period of time to stop that item flooding your results.

I’ve used this technique a couple of time now. The first time was to make lots and lots or data fit onto a relatively small graph, so I needed some form of average. The second time was because if the same event happened in a set period of time I only wanted to know about it once.

The query looks something like this once we include our timeslices / time periods in our select and then follow it up by grouping by them.

SELECT name, 
type, 
value,
ROUND((CEILING(UNIX_TIMESTAMP(`timestamp`) / 600) * 600)) AS timeslice
FROM reading
GROUP BY timeslice

Here I have selected timeslices of 10 minutes (600 seconds) but you can group larger or shorter periods of time if you require them.

So there you have it, grouping by timeslices for summing readings or limiting duplicates in results.