28 Apr

KFM 1.3 released

For those that don’t know, KFM (Kae’s File Manager) is an application which allows a user to edit/move/delete files online. With some plugins, you can even view videos, listen to your music files, etc.

There are a number of interesting improvements in 1.3.

The configuration methods have been improved. To set up your new KFM, simply copy configuration.dist.php to configuration.php, remove the bits you won’t be changing, and edit what’s left. As an example, here is my entire configuration.php:

<?php
$kfm_db_type = 'sqlitepdo';
$kfm_userfiles_address = '/home/kae/websites/kfm/1.3/files';
$kfm_userfiles_output = '/get.php';

Plugins!

The point of plugins is to enable actions for various file types. We have plugins for the default actions such as image resize, editing text with codepress, etc. You can download new plugins from here to supplement the default ones (I recommend jwplayer and malsup_media_player) – just unzip them in your /plugins/ directory and refresh your KFM.

The most important part about plugins is that they are easy to write. You don’t need to understand all of KFM to write your own plugin. Just copy an existing plugin that’s similar to what you want to do, and then edit it until it’s like what you wanted.

Another important part is that it helps to keep the size of KFM’s core down. We are aware that KFM is “bloating”, and are trying to keep an eye on that while still increasing its capabilities.

The list-view has been improved, along with a vast number of other things.

We have more new languages – Swedish (thanks to Aram Mäkivierikko), Persian (thanks to Ghassem Tofighi) and Hungarian (thanks to Ujj-Mészáros István).

There is a new strings translator on the way in 1.4, along with a new method of submitting translations. I’ve learned a lot in the last few months and feel I can do this better so I’m rewriting the translation methods. I’ll announce more as it becomes more complete.

A lot of work went into allowing KFM to be templated. This is not quite mature yet, so I won’t say too much about it, but it is now possible to design KFM the way you want it. If you have a design you’d like KFM to fit into, please contact me so I can help out. Documentation will come soon.

To download KFM 1.3, get the tar bzip (840k) or the zip (1M).

Please report any bugs to the usual place.

26 Apr

review: Mastering phpMyAdmin 2.11 for Effective MySQL Management

Packt publishing emailed me a week ago and asked me if I was interested in writing a review of a book on phpMyAdmin.

In short, my review is this: overall, the book detailed everything I can think of (and more) about phpMyAdmin – I learned about some features in phpMyAdmin that I was not already aware of. I would ask for less screenshots, more how-tos and less “we/our” speech for the next revision, but if there are any questions I have about phpMyAdmin, the answers are in this book.

I use phpMyAdmin only occasionally, when I can’t get direct access to the database in question (I’m a console jockey – there’s nothing like the thrill of tappity-tapping away, knowing that you’re doing Important Stuff and …well, yeah – geek).

I’ve used phpMyAdmin since forever, so was familiar with its common uses – importing/exporting databases, running simple queries, etc. I was interested to see if there was anything that I might have missed. And who better to show me than one of the developers, Marc Delisle.

The table of contents was large, spread over 12 pages. I feel it would have been easier to read if there was one page detailing just h1 listings, followed by 2-3 pages of h2 listings. The h3 and h4 listings could have been shown at the beginning of the concerned chapters.

Disregarding that, scanning the contents showed some features that I was not aware of. Did you know that phpMyAdmin has a built-in relations manager? Even if the engine used is MyISAM, which does not manage referential integrity, phpMyAdmin will protect you and manage it for you if queries are run through it.

Screenshots are provided for everything. This makes some things much easier to understand. Sometimes it made interesting features stand out even more. Did you know that phpMyAdmin can generate PDFs of the relational schema of a database? Some screenshots in chapter 15 show examples of this feature.

Marc obviously knows his stuff. As a major contributor to phpMyAdmin since the early days, he is an ideal person to talk us through configuration details and the various tools that phpMyAdmin offers.

There are, however, a number of things which detract from the readability of the book.

This is the first edition for this title, so the following are probably “teething” problems for the author. However, I think they should be taken care of for the second edition, to make the book easier to read. Although, the second edition would most likely have a different title (why is the version number in the title?)

The book is written from a 1st-person-plural point of view – now we do this, now we do that, our table, our server. This distracted and annoyed me while reading the book.

It is too detailed in places, at the expense of readability. For example, chapter one goes through a blow-by-blow account of how phpMyAdmin came to be, with version numbers and even dates! I feel this should have been left to an appendix. The first chapter of any book should introduce a reader gently to the subject; not pummel the reader with historical facts.

The author sometimes forgets that the reader does not know the subject and its community’s habits. As an example, on page 28 the configuration value “PmaAbsoluteUri” is described, but afterwards, I was not sure a) what “Pma” is, or b) what the variable was supposed to do (the book described how it should be configured, but not what it was supposed to do). It isn’t until page 33 that the book pointed out that “pma” is an acronym for “phpMyAdmin” used by the community. I don’t feel stupid for not getting this immediately, as there is an awkward mixture of letter-cases going on there. By rights, “Pma” should stand for “Phpmyadmin”, not “phpMyAdmin”.

The sheer number of screenshots was a bit confusing. I feel that screenshots should only be used where the subject is not clear from the text. Two images on every page (almost) has the effect of breaking your concentration when you’re trying to read.

The book exclusively deals with how to use phpMyAdmin, but I think there should have been more about how to deal with MySQL itself, using phpMyAdmin. For example, chapter 17 details phpMyAdmin’s coverage of character sets and collations. As a person who knows the pain of upgrading a database from MySQL 4 to MySQL 5 while switching character sets, I was looking forward to seeing how this case is handled (which I am sure is pretty common in one form or another). However, the chapter describes the problems with character sets and does not offer any solutions to those problems.

Overall, the book was detailed, and I learned a lot of new stuff from it. I’d recommend it for administrators that are not comfortable with CLI MySQL and want a reference for the best web-based MySQL administration tool out there.

21 Apr

How to get your own way

… if you are the chairman of a committee.

This is incredible – it’s a first-hand account of how Norway’s OOXML vote was changed from No to Yes in the space of one meeting (No meaning “there are problems with the proposal, we should not support it”, and Yes meaning “there are no problems with the proposal, and it should be supported”).

The meeting started with the chairman, who is not a technical man and has little understanding of the problems of OOXML, stating that this meeting would not adjourn until there was consensus on either Yes or No.

What followed was a real life “Monty Hall” whittle – The meeting started with 30 people voting, most of whom did not vote yes. There was no consensus, so most of the voters were asked to leave the room. Then the vote was taken again. People still had different opinions, so more people were asked again. This repeated until there was only one vote left – the chairman’s. And so Norway’s vote was decided.

So what’s wrong with that?

Let’s say the vote was about slavery. The question was – should slavery be allowed. Let’s say that the chairman didn’t understand the need to treat all humans as sovereign, and was the only person voting “Yes”.

In the first round, the chairman would vote “Yes”, and everyone else votes No. No consensus, so most of the voters are asked to leave. Again, the vote is taken, and again, the tally has one Yes, and many Nos – no consensus. So more people are asked to leave. this process continues until all voters are in agreement. You guessed it – the chairman wins.

This voting process is hilarious because it has actually happened! I’m frankly astounded that there is less outcry.

This is an example of dictatorship – a question was asked, and everyone that gave an answer different to the chairman’s was removed from the room. Eventually, he was the only person left, and so the “vote” was tallied as 100% Yes.

19 Apr

NIN and Tripping The Rift

tripping the rift

I was watching the Tripping the Rift film and noticed the title of the newspaper that Gus was reading.

“N Eye N” – get it?

I have no idea what Tripping The Rift has to do with Nine Inch Nails, and haven’t found anything obvious online.

Which reminds me – my CDs copy of Ghosts is on the way.

15 Apr

Planet PHP (.ie)

Just waiting for the DNS to resolve, but the Irish PHP User Group now has its own planet.

planet.php.ie is an aggregation of PHP blogs in Ireland. At the moment, there are only a few. If you feel you should be in this group, please get in contact with either Ken Guest or myself.

12 Apr

HP G6000 in Linux

My old laptop, an Acer Travelmate 2420 (good machine) started developing vertical lines on its TFT over the last few weeks, so I thought it was time to upgrade, and eventually fix the old lappie up for my son.

I bought an HP G6000. Ken Guest recommended a Lenovo x300 (or something like that) – I was in a bit of a rush, though – I can’t stand being without a functional machine, so I headed into Gillanders in Monaghan and bought the HP.

I spent a few hours on Friday dual-booting Linux onto the machine. FC7 initially, as that’s all I had with me, then upgraded to FC8 as soon as possible.

It wasn’t until I got home until I realised that the WIFI was broken (my office network is mostly wire-networked).

A few hours of study showed there was no simple solution to this. lspci claimed that the card is an AR5006EG from Atheros. the card is actually an AR5007EG. Either way, there is no “official” Linux support for this, and the unoffical support is not perfect either.

After reviewing the available information, I thought the easiest solution was to replace the WIFI car.d. I had an old Sony Vaio upstairs, so I pulled out the WIFI card from that, an Intel 3945abg, and placed that in the machine (if you unscrew the RAM plate at the back, you will see the WIFI card in its Mini-PCI-Express slot – lever the aerials off it and replace the card – simple, innit). unfortunately, that was far from perfect – the Intel 3945abg is not even seen by lspci – some hints I found online suggested that the 3945 doesn’t adhere to the standards.

Long story (asked Trevor in Gillanders for permission to dig through their laptop graveyard for a WIFI replacement – found nothing. tried some machines I had in the office – nothing found) – eventually, I figured out the solution.

Here’s the trick. You will not get the AR5007EG working in 64-bit Linux. Instead, you need to compile a custom kernel using the 386 mode, and download the patched version of MadWifi to compile the kernel module.

After that, everything’s simple. To fix the xorg – download the official NVidia driver module installer and let it compile a kernel module for you. That’s it – done.

Sorry for the length of this – I tried to throw in all the keywords that I searched for while trying to solve this.

09 Apr

javascript cache problem, solved

Almost every time someone says to me that something I was working on is broken, the answer is “clear your cache”, and that automagically fixes everything. However, that’s not an ideal solution – ideally, the problem would never happen in the first place.

So, why does the problem happen?

Let’s say that there is a HTML file which calls a JS function like this: showImage(); – the JS function is included from the external file /j/images.js.

Browsers are usually set to cache .js files, and that’s the correct behaviour for the most part. Unfortunately, when a file needs to be fixed, it can cause problems.

For example, let’s say that I’ve corrected the function name to match my usual naming scheme – images_show();. I change the reference in both places. The browser reads the new HTML file from the net, but loads the JavaScript from the cache – suddenly there’s a mismatch which causes a problem.

So, how to get around this?

The solution I’m using at the moment involves a little bit of mod_rewrite and PHP.

Sticking with the contrived example, let’s rewrite /j/images.js so it is accessible from /j/images (using /.htaccess):

ExpiresActive On
ExpiresDefault A259200
RewriteEngine on
RewriteRule ^j/images$ /j/images.js [L]

Now, we add a little magic. We want to change the URL if the file has changed. The only way to know this is to look at the modified date of the file.

In your PHP, you could do it like this:

<script type="text/javascript" src="/j/images/<php? echo md5(`ls -l j/images.js`); ?>"></script>

and then change the .htaccess file to allow that:

ExpiresActive On
ExpiresDefault A259200
RewriteEngine on
RewriteRule ^j/images/(.*)$ /j/images.js [L]

Now, if no file changes happen, then the MD5 hash (and therefore the URL) will be cacheable, and if the file changes, then the URL will automatically change as well.

…and that’s not all!

I like to aggregate my JavaScript files to reduce the network pain felt by the browser. In my CMS, it’s done with a /j/js.php file. Here’s a short excerpt:

<?php
$js=file_get_contents('jquery-1.2.3.min.js');
$js.=file_get_contents('js.js');
$js.=file_get_contents('tabs.js');
$js.=file_get_contents('addrow.js');
$js.=file_get_contents('formhide.js');
/* more files */

header('Cache-Control: max-age=2592000');
header('Expires-Active: On');
header('Expires: Fri, 1 Jan 2500 01:01:01 GMT');
header('Pragma:');
header('Content-type: text/javascript; charset=utf-8');

echo $js;

That’s then pointed to with this line in my .htaccess:

RewriteRule ^js/(.*)$ /j/js.php [L]

And it’s referenced in the browser like this:

echo '<script type="text/javascript" src="/js/'.md5(`ls -l j`).'"></script>';

Simple, innit! That simple trick now keeps track of a number of files, and the browser knows immediately if there are any changes.

BTW: The same trick can be used with images, css, and any number of other “static” objects.

04 Apr

points in an area in mysql

3am, daughter wide awake, not allowed to sleep – what’s a guy to do? Let’s do an experiment.

Let’s say we want to efficiently select all points in an area from a database. This has real-world applications – I’ll be using it in a geographical project very soon.

First, create a simple table in MySQL. I’ve created mine in a database called ‘geodb’.

CREATE TABLE points(x INT, y INT, INDEX(x), INDEX(y));

Note that x and y are both indexed.

Then, seed that table using some PHP (I had to up the max_execution_time on my laptop to 300 for this).

<?php
mysql_connect('localhost','username','password');
mysql_select_db('geodb');
for($i=0;$i<10000000;++$i){
  $x=rand(-1000000,1000000);
  $y=rand(-1000000,1000000);
  mysql_query("INSERT INTO points (x,y) VALUES ($x,$y)");
}

Ok. For the rest of the experiment, we’ll be trying various ways to extract the number of points within a radius of 100000 from (0,0). The goal is to have the lowest working time. Each method should return the exact same result.

First, from the console, do a straight select statement.

time echo "SELECT COUNT(x) FROM points WHERE SQRT(x*x+y*y)<100000" | mysql -uusername -ppassword geodb

Returned result 7993 in .414 seconds. Wow – pretty quick already… but, my daughter is still awake, so let’s continue.

We can improve this by avoiding the math on points that we are certain can not be in the area. For example, in a radius of 100000 from (0,0), and points with x<-100000, x>100000, y<-100000, y>100000 can definitely not be in the circle.

time echo "SELECT COUNT(x) FROM (SELECT x,y FROM points WHERE x>-100000 AND x<100000 AND y>-100000 AND y<100000) AS sub1 WHERE SQRT(x*x+y*y)<100000" | mysql -uusername -ppassword geodb

.326 seconds. Better. However, there are two calculations being performed on each value. Let’s reduce that.

time echo "SELECT COUNT(x) FROM (SELECT x,y FROM points WHERE ABS(x)<=100000 AND ABS(y)<=100000) AS sub1 WHERE SQRT(x*x+y*y)<100000" | mysql -uusername -ppassword geodb

.297 – more than 25% fster than the original.

It should be possible to reduce that further. For example, we can be certain that x,y values which are both less than (100000*Cos(Pi/4)) are contained inside the circle, so that’s another < comparison, reducing the number of maths operations. I’d test that one as well, but my daughter is finally asleep in my left arm as type.

03 Apr

Translation in PHP

As some of you know, my KFM project is available in 17 different languages. I did that using a home-brewed translation method.

Recently, I’ve been working on translating our CMS, WebME. I took a more “official” approach this time, and looked through the PHP documentation.

This article is not about localisation, in that I don’t care at the moment about the difference between en_GB and en_US (other than to point out that Americans spell everything wrong). It’s about translation itself.

The “official” way to do translations is to use the compiled-in gettext support. An un-official, but very popular alternative is to use the PHP-gettext project, which is used by WordPress.

I chose to use the compiled-in gettext support. As I control the server that our CMS is run on, there is no support problem, so I can guarantee that required software will be there.

Another reason for using the compiled-in version vs the PHP library is that, in the words of PHP-gettext’s developer, “I’m not very fond of PHP the language, there’ll be a lot to fix”. In other words, there is a chance that the PHP-gettext library may blow up, and the developer may just shrug his shoulders.

So, how does localisation work? To start off, you need at least one language other than the main language of your site (presumably it’s English). For testing purposes, let’s assume it’s Irish

Translation is managed in “domains” (think “namespaces”). That’s not extremely important if you are only translating a few hundred strings, as you can just use one called “default”.

Language strings are recorded in .mo files. You don’t need a file for your main language.

The files are saved in a directory on your server, using this structure:

/path/to/locales/
        ga/
            LC_MESSAGES/
                default.mo
        de/
            LC_MESSAGES/
                default.mo

The “locales” directory can be named anything you want, and the “default” bits are named after your domain (namespace).

The ga/de directories here should properly be ga_IE and de_DE, but we’re not interested in the locale part – we’re only interested in the language part.

To create your .mo files, first create a file in your server called test.php:

<php
header('Content-type: text/html; Charset=utf-8');
setLocale(LC_ALL,'ga_IE.utf8');
binddomain('default','/path/to/locales'); // change this to your locales directory
textdomain('default');

echo _('Pages');

Note that I’ve used ‘ga_IE.utf8’ here instead of ‘ga’. I’ll explain that later – it’s important for now.

When you run that, it should output “Pages”. The next step is to write the translation for it.

To create a .mo file, I recommend poedit – it’s cross-platform and works well. I won’t get into too much detail – here is an excellent tutorial – just replace ‘__’ with ‘_’. btw, the Irish for “Pages” is “Leathanaí”

When the file is created, save it as “/path/to/locales/ga/LC_MESSAGES/default.mo”, and restart your webserver (Apache caches gettext strings, so when you change them, you may need to restart the webserver to clear the cache – YMMV).

Now, when you try your script, it should output “Leathanaí”. Simple, innit?

Actually, no. You see, that’s a very contrived version – we deliberately chose a working locale. However, you will want to grab the locale from the browser’s Accept header, and that is not guaranteed to work.

Try it yourself – replace ‘ga_IE.utf8’ with ‘ga’ – the output is now “Pages”. btw, that’s why you don’t need a translation for your main language – gettext will output it’s input if there is no existing translation.

So, how do we get a working locale from the browser?

First, you need a list of your existing languages:

  if ($handle = opendir('/path/to/locales')) {
      $files = array();
      while(false!==($file = readdir($handle)))if (is_dir('/path/to/locales/'.$file))$files[] = $file;
      closedir($handle);
      sort($files);
      $available_languages = array();
      foreach($files as $f)$available_languages[] = $f;
  } else {
      echo 'error: missing language files';
      exit;
  }

Next, parse the browser’s Accept header for a locale which matches what we have.

  $ls=array();
  if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))$_SERVER['HTTP_ACCEPT_LANGUAGE'] = '';
  $langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
  foreach($langs as $lang)if (in_array(preg_replace('/;.*/','',trim($lang)), $available_languages)) {
    $selected_language= preg_replace('/;.*/','',trim($lang));
    break;
  }
  if(!isset($selected_language))$selected_language='en';

Note that we default to ‘en’.

And now, we properly set up the locale.

  if(!setLocale(LC_ALL,$selected_language)){
    preg_match_all("/[^|\w]".$selected_language.'.*/',`locale -a`,$matches);
    if(!count($matches[0]))die('no locale info for "'.$selected_language.'"');
    $selected_language=trim($matches[0][0]);
    foreach($matches[0] as $m)if(preg_match('/utf8/',$m)){
      $selected_language=trim($m);
      break;
    }
    setLocale(LC_ALL,$selected_language);
  }

What’s happening here is that we are scanning the webserver’s list of compiled locales (not your list), as gettext will not work unless it has a properly defined locale which is already compiled on the server. The above code will, when given ‘ga’, find ‘ga_IE.utf8’ in the server’s locales list and use that.

After that, it’s just the normal domain bind, as described in the simple example.

header('Content-type: text/html; Charset=utf-8');
binddomain('default','/path/to/locales'); // change this to your locales directory
textdomain('default');

echo _('Pages');

So now you can translate in just about any language, whether the browser supplies a proper locale string or not.

But wait – there’s more! What if you want to translate the string “Welcome to the ‘$1’ page”, where $1 is a variable?

Unfortunately, PHP’s built-in gettext function can’t do that. But we can hack it together easily. Add this function definition to your script:

function __($string)
{
    $str = gettext($string);
    for($i = func_num_args()-1 ; $i ; --$i){
        $s=func_get_arg($i);
        $str=str_replace('%'.$i,$s,$str);
    }
    return $str;
}

Then replace your _(‘Page’) with __(“Welcome to the ‘$1’ page”,”Kaetastic”). You will need to change the “keywords” section of poedit to use ‘__’ as well as ‘_’, then rescan the PHP files, update your .mo and restart the webserver.

Brilliant! Now you have a proper multi-lingual site. I expect throngs of readers to browse your site in Klingon and Leet.

Note that for proper optimisation you should use __() instead of _() only when there are multiple parameters – otherwise it’s quicker just to use _().