Archive for the 'Open Source' Category

Smaler PHP-generated HTML

Friday, December 9th, 2011

By Andrew Smith

I will have something like this on one of the pages in the website:

Each of those dropdown lists has the 190 language codes I mentioned in the previous post. It may not sound like a lot but 1900 <option> values really is a lot of HTML. It’s very easy to generate it all in PHP (one loop basically) but the result still has to be pulled over the internet, and suck up my bandwidth.

I had to think about this one for a bit, but I came up with a decent solution.

In my PHP loop I create two javascript arrays: one for visible name and one for value. I also have ten sets of dropdowns printed as HTML, but with the dropdown lists empty.

Then I write some javascript that will onload populate all the dropdown lists with all the names and values from the arrays.

This made the HTML sent to the browser probably 90% smaller, which made me very happy.

Just because it’s funny, here’s what some the php code looks like, I’m glad noone else will be working with me on this so I won’t have to explain it :)

  $languages = array();
  for ( $rowNum = 0; ($row = mysql_fetch_row($result)) != FALSE; $rowNum++)
  {
    $languages[$row[0]] = $row[1];
  }
?>
    <script type="text/javascript">
    function contentOnLoad()
    {
      var languageCodes = new Array(<?php
  # make the PHP array a Javascript array
  $firstElement = TRUE;
  foreach ($languages as $code => $name)
  {
    if ($firstElement)
      $firstElement = FALSE;
    else
      echo ",";

    echo "'" . $code . "'";
  }

That’s PHP, HTML, and JS all in one, still cracks me up :)

Scraping data from a reliable source

Friday, December 9th, 2011

By Andrew Smith

One of the things I will need in my database is a table with all the language codes used in Linux locales. Things like en, fr, es, etc. There are lots, but where do I get a reliable list?

I’ve done some searching and found the IANA language subtag repository. It’s a 45000 line text file with contents in this format:

%%
Type: language
Subtag: ab
Description: Abkhazian
Added: 2005-10-16
Suppress-Script: Cyrl

Of all those records only 1155 lines are 2-letter codes, which is what I was interested in. How do I get the language code and english name from there into a database? Piece of cake if you know some basic shell scripting:

#!/bin/bash

cat languagelist.txt | while read LINE;
do
  if echo $LINE | grep Subtag > /dev/null;
  then
    echo -n "`echo $LINE | cut -f 2 -d' '` ";
    HAVECODE=1
  elif echo $LINE | grep Description > /dev/null;
  then
    if [ $HAVECODE -eq 1 ]
    then
      echo `echo $LINE | cut -f 2 -d' '`;
    fi
    HAVECODE=0
  fi;
done

And insert it all into the database:

#!/bin/bash

./parselanguagelist.sh | while read LINE;
do
  CODE=`echo $LINE | cut -f 1 -d ' '`
  NAME=`echo $LINE | cut -f 2 -d ' '`
  mysql -u user -ppassword -e "INSERT INTO Language (LanguageCode,LanguageEnglishName) VALUES('$CODE','$NAME');" ostd
  if [ $? -eq 0 ]
  then
    echo "Inserted $CODE ($NAME)"
  else
    echo "Failed to insert $CODE ($NAME)"
  fi
done

Done, 190 records. And next time I want to update the list (who knows, it might happen) I’ll just need to get a new list and use the MySql feature that will let me either create or update a row depending on whether it already exists.

I think it would have taken me quite a while to generate this list of sql commands by hand :)

GUI for editing PHP live on the server

Thursday, December 8th, 2011

By Andrew Smith

I have an aversion for vi. It’s present on every linux box so I had to learn how to use it but I very much dislike it, it’s a pain the the ass to use. (Emacs is no better by the way)

I found a brilliant solution for that problem though. In Linux there is a thing called FUSE and another thing called sshfs.

That lets you mount a directory from the server onto your workstation filesystem, without any changes to the server (assuming you have sshd running, which of course you are). Then you can use your favourite GUI editor (mine is Scite) to edit your php and css. Brilliant stuff.

It also helps that my server is on the same lan as my workstation, so I get LAN transfer speeds, so the mounted network filesystem acts as fast as a harddrive.

Now I can use a nice editor to make my changes, a nice file manager to work with files and folders, and after making a change I just go to the browser and hit reload. This is so much more efficient than the command line!

Potentially I can even run svn commands from the workstations but I don’t think I will try that because the svn on my workstation is much newer than that on my server, so I expect that would cause problems.

I do keep a terminal to the server running in case there’s a critical error in my php. Running php from the command line is sometimes an easier way to find the bug.

Version control for websites

Thursday, December 8th, 2011

By Andrew Smith

I was always a fan of version control, even for binaries like word file for homework. This helps keep a history of stuff that may get deleted or changed, and helps a lot for working using multiple computers.

But I’ve never version controlled a website. Mostly because I never had one I was doing serious development on. In fact several of my websites were made using the Seamonkey editor once, uploaded to the server, and never touched again.

This project is different though, there will be a lot of serious code in it. Why not version control it? I decided to do it.

I had a Subversion repository for the project already, and in there I created a subdirectory for the stuff that will go on the server. On the server I checked out that subdirectory into the root of the webserver, as ‘ostd’. Then I added and committed all the php and css and images. Done!

Now I just have to remember to do commits when I’m done working on a feature or will be away from the project for a few days.

Making a good looking website

Thursday, December 8th, 2011

By Andrew Smith

I am the first to admit that I’m almost completely incapable of making something look good. I’m reasonably good at making things functional and easy to use, but when it comes to looks.. not me.

At first I thought I would start with just plain black and white PHP output, but I felt that was going to be a significant problem when trying to attract interest, even early in the project. Also I wouldn’t want to find out two months in that I should have written a Wordpess skin rather than my own PHP.

So then what to do?

I quickly discounted WordPress. I have several Wodpress installations and the maintenance is a nightmare. New versions come out all the time, most of the time there have been security bugs fixed so an upgrade is required, but constantly upgrading a WordPress installation and making sure that the new version still works was too much.

Then I looked around for website templates. There are actually quite a few websites with free templates on. I chose one from http://www.webtemplateocean.com/ – it’s licenced Creative Commons Attribution, that worked for me.

Then the template (one html file, one CSS, and a bunch of images) needed to PHPified. Basically that means to split the body into three parts – top, body, and bottom. The top and bottom are the same for every page, so they can be printed from PHP functions. The body can be different for every page.

I was quite happy with the result. My index.php has basically nothing in it other than the content of the main page, and the same is true for all the other pages.

I will add a link to the website once it’s a little more cleaned up, in a later post.

Starting a Project

Thursday, December 8th, 2011

By Andrew Smith

I have a long list of ideas. Why have I chosen OSTD? Well – it had to be something I was passionate about, and I am always proud to tell people that my software is translated into 40 languages. I would love to be able to say that with my help hundreds of other software was also translated into many languages.

Then I needed to make a balance between time/patience/skills required and those available. I know what I can do and I know what I can learn but how much time and patience I will have is a more complicated question.

To help me with that I made a list of use cases, which I’ll call ‘features’. With my understanding of the technology – given the list of use cases I could figure out whether this is doable in reasonable time. The feature list also helps when choosing what to do next and how to structure a database.

An overarching concern is – will people be interested in using this service? That question is just as relevant whether it’s an open source project or a commercial venture. I coulnd’t see the answer, so I had to take a leap of faith before finally deciding to go ahead. After all, if it were obvious that it would be a success, then someone would have probably done it already. I’m hoping that a leap of faith is necessary when taking on something new and exciting :)

Project Name

Thursday, December 8th, 2011

By Andrew Smith

OSTD, the Open Source Translation Database – that’s the best I could come up with. The name and the acronym has to be unique enough that I can get to the top of search engine results relatively easily. But there’s so much more to the name.

At first I wanted to get the website a proper domain name, but then I counted how much it would cost and that discouraged me. Between the registration, the dynamic DNS, and the SSL keys I would have to spend over 100 dollars a year on it, which I’m not willing to shell out.

The name has to be memorable and guessable. What would people search for if they wanted a service like mine? If they found it once, would they remember what it was called?

It can’t sound geeky. I have a personal problem with 1337.

It shouldn’t sound too stupid either.

There shouldn’t be conflicts with things like sexually transmitted diseases. Frankly I have no idea why ostd.org exists, my best guess is that std.org was not available. But ostd is different enough from std, so I let that fly for now.

But hey – if you have a better name idea – please share, I’m quite open to changing it at this early stage.

OSTD: Open Source Translation Database

Thursday, December 8th, 2011

By Andrew Smith

I have an ideas list, a sort of todo list for when I have time to burn. One of the ideas on my list is a website that can help authors of open source software to translate their software into languages other than english. I have recently agreed to teach part time at Seneca and also I decided to take a break from looking for a full-time job for a couple of months. Which means I have time to burn :)

There are similar strings in many pieces of software, so theoretically if it’s translated in SoftwareA it should be easily translatable in SoftwareB. In practice however this is not the case. It is impractical for a project maintainer to find strings in other software that will look like his own. In fact in most cases it is difficult to even find the pot/po files in existing software because there are so many version control and release systems.

So the core of my idea is a database of existing translations that can be used to automatically translate some strings in new software. The open source maintainer would submit a .pot (template) file and get back partially translated .po files in a bunch of languages.

Over time this website can be expanded to provide general internationalization (i18n) advice, advice for which english strings to pick which the service will translate more easily, a network of translators who may be interested in volunteering to help a particular project, etc.

I’m going to try and record in this blog the issues that come up with designing, building, and marketing this website. That shouldn’t be much of a problem since I don’t have a hope in hell of ever making money from this project, it’s just community service :)

How I almost switched to Ubuntu

Friday, June 24th, 2011

By Andrew Smith

Lat week I got a fancy new printer. I was trying to find something that will work for years and years so I shopped around for a couple of days.

The shopping experience was pretty painful. It’s very difficult to find whether a printer is actually supported in Linux before buying it and trying it out. Still, at the end of the second day I picked an MFC-7860DW – a solid all-in-one with a good feature set and apparently (not clearly though) supported on Linux being a Postscript printer.

On Windows the thing worked pretty much out of the box – pop in CD, have it detect the printer on the network, click install, print. Not on Slackware :)

On Linux I quickly got confused by all the terminology. There on the Brother website for this printer is an LPR driver, a cupswrapper driver (both in rpm/deb formats only), and generic install instructions. Also here and there I found references to brscript, which made me suspect this isn’t actually a real postscript printer.

I extracted the files from the .debs, copied them manually to the destination, and looked at the post-install scripts and ran the commands manually. Printer driver was added successfully to CUPS and I could add the detected network printer, and I could even print a CUPS test page successfully, but nothing else would print. Everything else would come out as pages and pages of garbage.

After many hours of beating my head against the keyboard I installed Ubuntu in a VM and installed the brother deb packages in there and after a minute of fiddling with cups – printing worked, including two-sided (duplex) printing.

This was a particularly busy week for me and I snapped, I could no longer justify spending so much time installing a printer and I figured maybe it’s time to switch to Ubuntu, and I did.

Thankfully though it wasn’t long (less than a week) until I remembered why I was a Slackware user in the first place – it’s conservative, stable, and doesn’t modify upstream packages. Every day I found something in Ubuntu that would drive me crazy. Parts of the screen would be blacked out after resizing certain windows (crazy driver bug maybe, but it worked in Slackware), my USB mass storage phone (with worked in Slackware) would sort of mount when I clicked on it in Thunar but the contents would not be displayed, and the final straw was grub2.

Don’t get me wrong, I’m all for usability and also I appreciate that sometimes something old needs to be dropped so something new and better can take its place. But it seems to me that grub2 doesn’t fit either of those parameters. It is absolutely a monster to configure (just try to change something simple like the boot timeout if you don’t already know how to do it) and doesn’t give me any benefits as far as I can see. On the web someone suggested a GUI program to configure GRUB2 and that’s when I decided to hell with it. If you’re going to require a GUI program to configure a boot loader – you better bloody make sure it’s installed and accessible by default.

So I switched back to Slackware (Ubutnu was on a separate partition of course) and having a little more patience and a fresh mind I just used the generic postscript driver, which worked, which is why I bought a postscript printer in the first place :)

I wrote it up in case someone else will look for info about this printer on Linux, I will extend that guide when I find more about using this printer’s features on Slackware.

The end of an era

Thursday, April 14th, 2011

By Andrew Smith

Today I wanted to make some yummy apple puff pastry, so I turned my laptop on to put up a recipe and.. the screen blipped and went off. This machine (an IBM x40) was so old that I wasn’t at all surprised. The laptop went with me through school (when I rode bicycles and buses and subways), several jobs (more subways and driving), came with me on planes and Sudbury trips, has been used by my mother for about a year, and for the last year or two has been sitting on my desk, with a monster work laptop squashing it (which was probably what killed the screen).

With little hope and mostly our of boredom I took the keyboard off. To do this I used the skills I acquired in my last career as a computer technician, when for some time I worked for IBM fixing up used laptops. Like many IBM laptops this was a wonderful machine. Not only is it solid like a rock but it’s easy to take apart, and inside, after years of beatings.. it looked like new!

I am telling you I have never seen a used laptop this clean – both literally and in terms of design and construction. I was going to look for loose or damaged cables or mechanical pieces – yeah right! Everything was shiny, orderly, and in its place. Unfortunately I saw the image was still being displayed, just not backlit. Yep, laptops like this we sent straight to the scrap pile because the screen cost more than the rest of the parts put together. I felt a little bad at this point (what a nice computer) but whatever, it lived a good long life.

As a sign of respect for the machine and the engineers who built it I put it back together in preparation for the scrap yard, then I thought of giving it a burial (which obviously wouldn’t work), then I thought of getting it some flowers (I’m not kidding), and then I realised why I had such trouble letting go and I almost cried..

The problem was not letting go of that laptop! The problem was that I wasn’t going to get a replacement laptop, the problem was that those days are over. I have an Android phone which does almost all that the laptop used to do. There is this concept of a laptop dock (check out the Atrix) which would serve for the rest. I’m not going to go buy a new laptop, hell I don’t even want one for free.

This reminded me of the end of my last career. About eight years ago I decided that a computer technician is not a career with a future and I took a leap of faith, going back to school to become a software engineer. It was a good decision and I think it paid off but it has not been long enough, I am not yet ready to switch careers again!

Yes, computers are as common as ever and there is a new world of mobile devices that promises a few more years of prosperity for software engineers like me, but I wonder if I’m already seeing signs that software is becoming a commodity by default.. and then what? Will I end up like one of those guys I was laughing at in school, “maintaining” (pretending to do work on) a Cobol program on an IBM mainframe, but with slightly newer technologies? How ghastly..

And even if there are still going to be new and exciting things to do for software engineers – what about me? Will I be able to learn the new tools, or is my brain over full with things that no longer matter?

Life is sad.. but I guess that’s the cost of progress, older farts like me have to get off their asses and relearn everything or let the next generation take their place. Well, I’m not the type to give up without a fight so bring it on kids :)