DEV Community

Cover image for Resurrecting a PHP app after 8+ years
Lance Wicks
Lance Wicks

Posted on • Originally published at lancewicks.com on

Resurrecting a PHP app after 8+ years

Recently I was reminded that a website I created as part of a 2 year academic research project was dead ( http://www.rwjl.net ) So I set out to bring it back to life.

Step one… where is the code?

So the last time I did any work on the site was 2012 or there about. So I did not have the original source code available… so I probably had it under some form of source control… but it was not in my github repos; or on my laptop.

So l started but pulling the files direct from the site via sftp. Then initialising a git repo and using this as the first commit (after having done a read through checking for any credentials etc that I did not want in the history).

Step two… what is broken?

To summarise, there were two main issues to address.

  1. PHP 5 -> 7
  2. SQLite 2 -> 3

So the original site was PHP5 and the hosting provider had auto upgraded the site to PHP7. So on initial investigation the old SQLite calls were failing as teh interface has changed, so I needed to address that. Here is an example:

OLD:

$dbo = new SQLiteDatabase("$dbfile");
$query = "SELECT * FROM players";
$result = $dbo->query($query) or die("Error in query");
while ($result->valid()) {
    $row = $result->current();
    ...
    $result->next();
}
Enter fullscreen mode Exit fullscreen mode

This is no longer valid, so needed to be re-written a bit like this:

$dbo = new SQLite3("$db");
$query = "SELECT * FROM players";
while ($row = $result->fetchArray()) {
    ...
}
Enter fullscreen mode Exit fullscreen mode

This was the first part of the problem solved, the second issue was that as you can see from the above code, PHP7 is SQLite3 and all my old data files were from 2010-2012 and were sqlite2; so time to migrate the data.

This is actually easy. All I had to do was install sqlite2 on my laptop so that I had sqlite3 and sqlite2 installed. Then to so a migration was a simple dump the sqlite2 version into sqlite3. E.g.

sqlite sqlite2/01_ijfgrand_prix2010_tunis.db | 01_ijfgrand_prix2010_tunis.db
Enter fullscreen mode Exit fullscreen mode

I had about 100 of these to do, so wrote a quick bash loop to dump all the contents to file, then another loop to import them into sqlite3… I won’t paste it here as it was ugly. 😉

Another fix I had to make was an array initialisation that was possible (though terrible) in PHP5 but sensibly not valid in PHP7, so I changed this:

$scores = '';
...
$scores[] = $change;
Enter fullscreen mode Exit fullscreen mode

Which became:

$scores = array();
...
$scores[] = $change;
Enter fullscreen mode Exit fullscreen mode

There was only one instance of this pattern, so I presume originally $scores was a string at some stage and then I started using it as an array. PHP5 was more lenient I guess; but PHP7 enforces a better initialisation.

Step 3… Clean ups and getting the tests fixed.

Confession time, I did not get this site working the “right” way. I got this working and then started thinking about the tests. This was in past because the way I originally wrote this site, I did not use dependency management, so my version of simpletest was out of date and the tests would not run etc.

I decided to be pragmatic and get the site working first (someone had emailed me about it, so the priority was get the data back online… not practice doing things the right way for this blog).

Now things were working, I started deleting things. All versions of the site that had been “backed up” to sub directories. Libraries that I had not brought in via a tool like composer got deleted.

As I needed them, I created a composer.json and used that to bring in the dependencies. These don’t end up in the repo; so there is less to confuse me next time.

I had used php-codesniffer originally on the code; so I added this to the composer.json too and ran that over the code and applied many of the improvements it suggested.

Summary

As a full-time Perl developer, it was nice to pick up a web application I wrote and maintained in PHP back in 2010 through to 2012. The code is so untidy to my 2019 eyes; this is a me thing not a PHP thing.

PHP and Perl are very close in terms syntax and “look” so I find jumping from Perl to PHP much easier than what I have been doing lately of jumping from Perl to Go where the syntax is quite different and of course types and structs to adjust to.

It was enjoyable to bring the site back to life and re-visit a project that meant a lot to me at the time and to see a full (although small) web application back on the internet.

I’m not planning to do much with the site or code, but have pushed it onto Github now to all the data and code is available to researchers or other developers… or me when I neglect it again and in another 8 years I have to bring the site back to life again.

Top comments (0)