Matt Oakes

New Design

July 1st, 2009

I’ve been working on replacing the new design for a while. The old one was looking fairly old, even thought it was less than a year old…

I also wanted to change the design so that it could be used for different projects that I’m doing. This means that the PHP Lastfm API website and the Lugaru Downloader website are now part of this site, rather than having there own designs. I’ve wanted to do this for a while to keep things for organised and make them all seem like they are linked together.

Another thing I wanted to do was to add a list of recent blog posts to the home page of my website. All I did for this was use Last RSS to get a list of recent blog posts and put them up onto the front page. It’s a really simple class to use. I highly recommend it. I also got rid of the loading of the latest updates list by doing the generation of the list automatically at regular intervals using cron and then just including the html on the home page.

Overall I’m happy with the new design. There’s still some bits I need to tidy up but it seems to be going well so far.

Lugaru Downloader

June 29th, 2009

It just occured to me that I haven’t blogged about this yet.

I’ve been messing around with Adobe AIR a bit recently and I decided to put some of the stuff I learnt to use and make a working application. As I was getting interested in a game called Overgrowth by an indie game company called Wolfire I decided to start making a content browser/downloader for it. This sort of hit a dead end though becasue the content that was downloaded didn’t actually appear in game (it will do eventually but the game isn’t complete yet so it doesn’t yet). So that sort of stopped for a while. If you’re interested in the code you can take a look at it on Google Code.

The code itself is fine, it’s just the game not being ready that stopped development. After talking to the guy at Wolfire it turned out they wanted something similar for their previous game Lugaru. As I didn’t actually own this game they were kind enough to give me the game for free; after this generosity it was only fair to pay them back with a nice downloader.

You can find the instructions on how to install and use the downloader on the Wolfire forums. I’m yet to release the code but I’ll most likely put it up onto Google Code when I get round to it.

Expect more information and code snippets from the project in the future.

Getting to grips with 7

June 29th, 2009

I’m a long time Windows user. I’ve been using it since Windows 3.1 and to be honest, I like it. That’s not something you’d hear a web designer say, but I’m gonna say it anyway because it is a really nice operating system.

For the past year and a bit I’ve been using Windows Vista. It’s an ok version of Windows but does has quite a few annoying little quirks. After a year and a bit my pc was starting to slow down, so I thought it was time to reinstall.

I quite like the process of reinstalling because I get to cut everything down to the bare essentials on my pc. It’s like cleaning your house (or room), it gives you a clearer state of mind and makes working that much easier.

As I was reinstalling anyway I decided to take advantage of Microsoft letting anyone use Windows 7 RC for free. All it took was a simple download and burn and I have a fully functioning operating system in just 15 minutes (this doesn’t include the time it took be to backup all my stuff. That took about 24 hours).

The first impressions of the operating system are good. The new task bar is really nice. I love how it mixes the opened programs with the ones you have pinned to it, but have not opened yet. It’s very Mac OS X like however it still works much like the older version of the taskbar. I also like how they’ve cleaned up the notification area to get rid of all the annoying icons. They now only show up when they have something to tell me (or if I pin them to the notification area so they always stay there, which I’ve done for Steam and my development server control panel).

Another really nice feature is the Aero Peak. It sounds a bit odd but it basically makes all the windows transparent when you mouse over the the show desktop button. This is useful because your desktop is now where gadgets are placed. I like the gadgets in Vista, but I couldn’t see the point of having the sidebar open just to see the weather occasionally. This solves the problem completely!

I’m also trying to use the features built in to 7 to replace some 3rd part stuff I used in Vista (and XP). I’m going to use the new taskbar as a replacment for RocketDock, and use the start menu search as a replacement for Launchy. I’ll have to see how that goes though. Might be tough to get out of old habbits.

I’m still getting used to the OS so I’m guessing I wont be as efficient at coding for a while; but once I’m up to speed I think it’s going to be really nice to use.

PHP Last.fm API Version 0.7.1 Released

June 25th, 2009

Version 0.7.1 of the PHP Last.fm API has been released. To download it you can go to the project website and press the download button.

It is another very small update. All it is is a (fairly major) bug fix which allows non-subscribers to make authenitcated API calls. Thanks to all the people who reported it to me, espesially ebotunes who wrote the code for the bugfix.

Using batch files to speed everything up

May 28th, 2009

Recently I’ve been using quite a few products the require me to use the command line. One of these tasks is using the Adobe AIR SDK to run a debug version of an application, to create a certificate or package the application up into a .air file ready to be sent out to the big wide world.

All of these require me to run an application from the command line. To debug an application I need to run an application called adl with a path to the applications .xml file as a parameter. Doing this via the command line is simple enough but it’s something that is done fairly regularly and the commands never change; so I created a Windows batch file that contains the commands I need to run the application debugger.

The batch file is very simple and looks like this:

@echo off
cd src
adl app.xml

This is an extremely simple batch file which simply runs the two commands and opens a debugger.

I also have a slightly more complicated script which takes the certificate that is generated and uses it to package up and sign the .air file ready for distribution:

@echo off
set /p packagename= "Enter a package name (*.air):"
adt -package -storetype pkcs12 -keystore cert.pfx %packagename% src/app.xml -C src .

This is just slightly more advanced and uses a prompt to get a variable that is used to package the files.

One of my favorite script that I’ve made is one for PHP Last.fm API. It takes the latest version our of the SVN repository, extracts it into a folder I have setup on my server which holds each release and then creates three different compressed files that are then uploaded manually to SourceForge. I’d like to make the uploading automatic as well but I haven’t got round to that bit yet.

@echo off
set /p version= "Enter the new version number:"
cd release
mkdir %version%
cd %version%
svn export --username <em>******</em> --password <em>******</em> https://phplastfmapi.svn.sourceforge.net/svnroot/phplastfmapi/tags/%version% C:\webserver\htdocs\dev\lastfm_protocol\release\%version%\code
mkdir zip
cd zip
7za a phplastfmapi-%version%.zip ../code/* -r
7za a phplastfmapi-%version%.7z ../code/* -r
7za a phplastfmapi-%version%.tar ../code/* -r
7za a phplastfmapi-%version%.tar.gz phplastfmapi-%version%.tar
del phplastfmapi-%version%.tar
echo Version %version% has been exported and compressed
pause

All of this used to be done manually and would take about 10 minutes to complete. With this script I can just type the right version number and have it all done in under 20 seconds.

I won’t go into how you can create your own batch files but I though I’d try and give you some ideas of ways that you can use batch files to speed up your working.

If you’re on Windows and want to learn about creating your own files you could check out the Command Windows website. Similar systems exist for all other major operating systems but I don’t have any experience with them.

Hopefully this post has given you some ideas for the types of scripts you could create to speed up you working.

Amazingly Interesting Talks

May 10th, 2009

For a while now I’ve been a great fan of watching videos of talks done by some amazingly smart people. I like hearing about the things that people are working on around the world, either technological problems, science problems or social problems.

One of the best known sources for these types of videos is TED. TED is a conference where people are invited to talk about whatever they are working on at the minute. One of the great ones I’ve watched recently is a talk from Bill Gates where he tackles world heath issues.

Another great source that I’ve found in the past few weeks is Google Tech Talks. They are videos that we’re taken at the Googleplex in front of Google engineers, but feature speakers from many different companies, including Adobe and Yahoo. They range of really detailed (mostly too detailed) programming related talk to ones more about science as a whole. A great talk that I’ve just finished watching is How to Count ALL Human Carbon Emissions in the US by Dr Kevin Gurney. It talks about how little we really know about carbon dioxide and what they are doing to improve our knowledge. It’s really interesting and well worth taking an hour out to watch it.

On a more programming related note, I’ve been playing around with Adobe AIR recently. I’m using HTML and JavaScript to build all of the applications, so this talk about the good parts of JavaScript. It talks about the bits that JavaScript got right and how to avoid the bad bits that cause people all the problems.

Take a look at them and see what you think.

Quick Tip: JavaScript Sorting

May 4th, 2009

I’m currently writing an Adobe Air application in HTML and JavaScript. I needed to be able to sort a multi-dimentional array based on one of the elements inside the array.

For example I had an array which looked like:

Array(
	1 => Array(
		'name' => 'Adam',
		'content' => 'Stuff goes here'
	),
	2 => Array(
		'name' => 'James',
		'content' => 'Stuff goes here'
	),
	3 => Array(
		'name' => 'Bob',
		'content' => 'Stuff goes here'
	),
	4 => Array(
		'name' => 'Sam',
		'content' => 'Stuff goes here'
	)
)

I know that isn’t in a JavaScript like format, it’s just to show what I mean

I wanted the output of the sort again the name element; so the final array would look like this:

Array(
	1 => Array(
		'name' => 'Adam',
		'content' => 'Stuff goes here'
	),
	2 => Array(
		'name' => 'Bob',
		'content' => 'Stuff goes here'
	),
	3 => Array(
		'name' => 'James',
		'content' => 'Stuff goes here'
	),
	4 => Array(
		'name' => 'Sam',
		'content' => 'Stuff goes here'
	)
)

I found a really nice code snippet which emailed how to do this, along with a good explaination of why it works. There seems to be a lot of good code on that website.

http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256C8D00514FA4

Open Source Licences

May 3rd, 2009

I run quite a few open source projects. Some are still active (PHP Last.FM API and Overgrowth Downloader) and some are woefully incomplete or pretty much dead (MyWebFeed, DigPHP and Online Last.fm Player). A few weeks ago I got asked why I chose to use the MIT licence for my projects (all except DigPHP uses it). I thought this was a very interesting question to answer, however it’s really down to personal taste more than anything.

The most used open source licence is the GNU GPL Licence. This type of licence is what’s known as a copyleft licence. This means that anyone can use your source code freely, however if they make changes they are forced to release the code under exactly the same licence. This seems like a good idea on the surface as it forces people to release their changes under an open source licence, therefore making the use of that licence wider. For most of my projects however this is a problem as they are designed to be used within other projects. For example the PHP Last.fm API isn’t something that can be used on its own; it has to be integrated inside another project. If I’d have used the GPL licence, this would not be possible.

There is however another licence from the same group called the Lesser GPL (LGPL). This allows people to use your open source project freely within their project, even if they don’t use the same licence; however it still makes the restriction that if any changes are made to the LGPL licence project the changes must be released under the same licence.

There is however another major problem with the GPL licences; it’s huge! If you take a look at the text of the licence it is 674 lines long. Most people will never read this, and even if they do there’s no guarantee they will understand it.

Therefore I decided to go with a different licence. I chose the MIT licence.

The main reason for this was how permissive it is. It doesn’t have the strict restrictions that the GPL licence has; it allows anyone to use, copy, modify, merge, publish, distribute, sublicense, and sell copies of your project. This is extremely permissive and some of what is permitted may scare some people, most likely the right to sell part of the licence. For me this isn’t a problem as selling a piece of software that is free isn’t generally a good business model, so it is very unlikely to happen on any large sort of way.

There is another advantage to the MIT licence; it’s tiny. At only 19 lines long (with the warranty disclaimer, 11 lines without) it is very easy to read and understand. Also, because it’s only small more people are likely to read it in the first place and realise that they can take the code and do what they like with it.

Quickstart Guide to SQLite in PHP5

March 1st, 2009

PHP5 comes bundled with a great new feature: a built-in SQLite extension that allows you to store data in a database without any database server at all. The functions work in much the same way as the corresponding MySQL functions in PHP, except there is no need to have a separate database server running alongside PHP.

I will not cover how to setup SQLite in detail. For me, it was as simple as uncommenting the following three lines in my php.ini file:

extension=php_pdo.dll
extension=php_pdo_sqlite.dll
extension=php_sqlite.dll

If you need more help setting up SQLite, take a look at the PHP documentation.

Note: There are two different PHP extensions for SQLite. The one we will be focusing on uses procedural functions. The other extension is designed for SQLite3 and uses the class SQLite3. You can read more information about the SQLite3 extension in the PHP manual.

Creating a Database Connection

After you have installed and setup SQLite, you can begin using it right away. Those who have used PHP’s MySQL extension will find themselves right at home. Opening a connection to an SQLite database is quite simple:

<?php 
if ($db = sqlite_open('my_sqlite_db', 0666, $sqlite_error)) 
{ 
 
} 
else 
{ 
    die ($sqlite_error); 
} 
?>

Let us examine the code above bit by bit. The if statement is used to catch errors whilst opening a connection to SQLite, similar to MySQL’s mysql_error(). The important bit of code above is the sqlite_open() function.

The first parameter of sqlite_open() is the name of the database you want to open. You can think of this as the database name in MySQL, except that if the database does not exist, it will be created for you. The database will be created in the same directory as the PHP file that calls the function. If this file is in a path that is accessible to anyone, they will be able to access your database and to view the contents of it. Obviously, this is a major security risk. To remedy this, you need to enter a relative path out of the web root, such as:

../../../database_name

So, your code might change to something like this:

<?php 
if ($db = sqlite_open('../../../my_sqlite_db', 0666, $sqlite_error)) 
{ 
 
} 
else { 
    die ($sqlite_error); 
} 
?>

The second parameter is the mode. This is intended to be used to open databases in read-only mode. Most of the time you will want to use the value 0666, which allows full access to the database. Again, to draw a similarity to MySQL, this is like opening a database with a user that has full permissions (which is how most of us use MySQL).

The last parameter is an error-handling variable that stores any error messages that occur. If an error occurs, you can echo this variable to see what went wrong, as we did in the previous examples.

The sqlite_open() function returns a database handler that is used in the other SQLite functions. This database handler is used in SQLite functions in the same way as the optional second parameter of mysql_query() is used. By default, mysql_query() uses the last link opened by mysql_connect(), so passing a database handler to the function is optional. We save the return value in the $db variable for later use because the SQLite extension does not assume the last opened connection like the MySQL extension does.

Running Queries

After you have connected to the database, you will want to start running queries. To do this, you need to use another function called sqlite_query():

<?php 
if ($db = sqlite_open('../../../my_sqlite_db', 0666, $sqlite_error)) 
{ 
    sqlite_query($db, 'CREATE TABLE test ( name varchar(10) )'); 
    sqlite_query($db, "INSERT INTO test VALUES ('lowter')"); 
} 
else 
{ 
    die ($sqlite_error); 
} 
?>

The code above will create a new table called test and insert a new row with a name value of lowter. The first parameter is always the database handler, which was stored in the $db variable when we ran sqlite_open(). The second parameter is the actual SQL query you want to run. You can look at the SQLite documentation for a full list of SQL syntax.

Note: Some of the SQL syntax in SQLite is different than in MySQL, so if you run into any trouble you may want to take a look at the documentation linked to above.

After you have inserted some data into the database, you will want to retrieve it. To do this, we will run a SELECT query and use a few more functions to extract the data:

<?php 
if ($db = sqlite_open('../../../my_sqlite_db', 0666, $sqlite_error)) 
{ 
    $result = sqlite_query($db, 'SELECT name FROM test'); 
 
    while ($row = sqlite_fetch_array($result)) 
    { 
        echo 'Name: '.$row['name'].'<br />'; 
    }  
} 
else 
{ 
die ($sqlite_error); 
} 
?>

If you are familiar with MySQL, then this code should look familiar to you. The $result variable holds all of the results from the SQL query. The while loop goes through each retrieved row and extracts the data as an array using sqlite_fetch_array(). We then use this array to output the data.

Summary

There are a lot of other functions that are available in PHP’s SQLite extension. If you want to see a full list of all of them, take a look at the SQLite extension in the PHP manual.

Hopefully this article has given you a quick overview into the power that SQLite brings to PHP. I have been using it a lot recently as a caching mechanism because it does not require a separate database server or having to deal with a lot of cache files. For more information about SQLite in general, make sure to read Lowter’s article SQLite Overview.

Enhancing Opera 9.6’s RSS Feed Display

October 9th, 2008

This blog post was originally going to be about the new features of Opera 9.6 and how much I like them. However, I spotted a major flaw with one of the new features and luckily it’s a flaw that anyone can fix with a little bit of modification.

One of the great new features of Opera 9.6 is that it will show a styled page when it comes across an RSS or Atom feed, which makes it readable to humans. It then has a big button that you press to subscribe to the feed. It’s a great little feature… assuming you use the built-in feed reader. The major flaw is that it only has a subscribe button for the built-in reader and not for any other services, such as Google Reader (which I use daily). I decided to see if this was fixable and dove straight into the configuration section. (Head to opera:config to see this for yourself.)

In the configuration, there is a setting called “Webfeeds HTML Template File”, which is basically an HTML template that styles the entire feed display page. Upon looking in this file, I saw that it was simply an HTML file with some CSS and a lot of JavaScript. Therefore, I decided it was possible to add new buttons and other little goodies; so that’s what I’ve done!

Once you follow the instructions below, you will be given five different options of feed readers to use with only the click of a button. At the minute these are:

  • Google (Reader and iGoogle)
  • My Yahoo!
  • Bloglines
  • NewsGator
  • Built-in Opera reader

Installation

  1. Download the ZIP file.
  2. Copy the HTML file into a folder on your computer. It can be anywhere, but make sure you know how to browse to it. Opera’s user style folder is a good place to put it (C:/Program Files/Opera/styles/user on Windows and /Users/your_username/Library/Preferences/Opera Preferences/Styles/user on Mac OS X).
  3. Type
  4. opera:config
  5. into the address bar in Opera.

  6. Type feed into the search box at the top of the page.
  7. Press choose, browse to the HTML file, and select it.
  8. Navigate to a feed to check that it works. Try the Lowter blog feed and see if you get the five buttons!