CategoryCriticism

Backup of a WordPress plugin / theme directory.

I often think: I’m just gonna do this little thing before and then I continue doing that.

The last little thing spanned a couple of days. I needed to make a patch for WordPress and cross fingers after pleading to apply it.

I could be wrong, but some years ago it was considered best practice to have users of your plugin to store their customizations into the same plugin directory. Well, maybe nobody advised me to but nobody advised me against either. The point is that it looks really neat to make users store all the stuff related to your plugin in the same place where your plugin leaves.

Years ago, however, there was no problem. Whenever you wanted to install a new plugin or update an old one, you always needed to first download it to your PC and then to upload it to your remote site, into the ´/wp-content/´ directory, possibly by means of an FTP client. The process was repetitive and cumbersome, but exactly by virtue of that you did things orderly.

Always make a backup of the stuff you are going to replace.

That was the mantra. Even on toilet walls you could read it.

Then, the update now link appeared and it all changed, for better. No more nothing, just a simple click and … magically everything happened remotely. Really really good feature. But there was a problem. A no-feature became a feature (bug?). The magic update now functionality never makes a backup of what is gonna replace. Why? I really don’t know. It is still a mantra for me!

So, the last couple of days, I made a patch for WordPress 4.1 and submitted it minutes ago. It’s quite simple: Always make a backup of the stuff you are going to replace. Then a plugin / theme developer can access a backup of their previous stuff and transparently migrate the previous custom files to the new version. For example, from an activation hooked function:

$plugin_dir = dirname(__FILE__);
$prefix = backup_prefix($plugin_dir, 'plugin');
$backups = get_transients_like("%$prefix%");
if (count($backups)) {
  $last = array_pop($backups);
  copy_dir($last['backup_dir'] . '/custom_files', $plugin_dir . '/custom_files');
}

WordPress and UTF-8

We know that WordPress cannot manage 4-bytes-long UTF-8 characters since the bug report Inserting a 4-byte UTF-8 character truncates data was filed on May 28, 2010. As of today (four years later) #13590 is still not fixed.

I contributed first a patch for fixing the bug on January 19, 2011 and then a plugin for fixing WP sites on May 25, 2011. They work in a very simple way:

  1. immediately before writing to the database, a 4-bytes-long UTF-8 character is converted to a safe code
  2. immediately after reading from the database, a safe code is converted to a 4-bytes-long UTF-8 character

Escaping / unescaping is very useful because PHP, which can manage 4-bytes-long UTF-8 characters, will manage them and MySQL, which cannot manage them (*), will manage a safe code instead.

There are only a couple of issues with this method, though. If a string contains some 4-bytes-long UTF-8 characters then

  1. the length of the string in PHP will appear lower than in MySQL
  2. the order of the string in PHP will be different from MySQL

For me, they are minor problems, when compared to the peace of mind of being able to paste or edit whatever UTF-8 text I might come up with.

As I said, #13590 is still not fixed after 4 years. Meanwhile,

  1. 2010-05-28 06:53 – #13590 opened – sardisson filed Inserting a 4-byte UTF-8 character truncates data
  2. 2010-10-27 12:28 – #13590 – Component changed from General to Charset – Milestone changed from Awaiting Review to Future Release
  3. 2010-12-31 13:21 – #13590 closed – Resolution set to invalid – Status changed from new to closed
  4. 2011-01-13 02:55 – #13590 – Milestone Future Release deleted
  5. 2011-01-19 09:32 – #13590 reopened – Component changed from Charset to Database – Keywords utf8 added – Resolution invalid deleted – Status changed from closed to reopened – Summary changed from Inserting a tetragram (SMP/Plane 1) character truncates post fields to Inserting a 4-byte UTF-8 character truncates data – Version changed from 2.9.2 to 3.0.4
  6. 2011-01-19 09:57 – #13590 – Milestone set to Awaiting Review
  7. 2011-01-19 21:40 – #13590 – Attachment wp-db-utf8-patch.diff​ added (my patch)
  8. 2011-05-25 – I published my plugin
  9. 2011-05-25 21:43 – #13590 – Keywords has-patch added – Type changed from defect (bug) to enhancement
  10. 2012-07-11 04:26 – #21212 opened – Gary Pendergast (pento) filed MySQL tables should use utf8mb4 character set, basically suggesting to take advantage of utf8mb4, which is how MySQL developers fixed their bug. (*)
  11. 2012-08-08 06:49 – #13590 – Milestone changed from Awaiting Review to 3.5
  12. 2012-08-29 01:54 – #13590 closed – Keywords utf8 removed – Milestone 3.5 deleted – Resolution set to maybelater – Status changed from reopened to closed – Version changed from 3.0.4 to 2.9.2
  13. 2013-02-18 21:36 – #13590 – Duplicated by #23495
  14. 2012-07-11 04:28 – #21212 – Attachment 21212-utf8mb4.diff​ added
  15. 2012-07-11 04:29 – #21212 – Keywords has-patch added
  16. 2012-07-11 11:22 – #21212 – Related to #13590
  17. 2012-07-18 06:52 – #21212 – Attachment 21212-utf8mb4.2.diff​ added
  18. 2012-07-30 – Mathias Bynens published How to support full Unicode in MySQL databases
  19. 2012-08-07 04:13 – #21212 – Attachment 21212-utf8mb4.3.diff​ added
  20. 2012-08-07 05:06 – #21212 – Milestone changed from Awaiting Review to 3.5
  21. 2012-08-07 06:39 – #21212 – Keywords commit added
  22. 2012-08-29 01:54 – #21212 closed – Milestone 3.5 deleted – Resolution set to maybelater – Status changed from new to closed
  23. 2012-10-09 05:24 – #21212 reopened – Keywords has-patch commit removed – Resolution maybelater deleted – Status changed from closed to reopened
  24. 2012-10-09 06:56 – #21212 – Milestone set to Awaiting Review
  25. 2014-04-07 – Gary Pendergast (pento) published WordPress and UTF-8
  26. 2014-04-22 02:08 – #21212 – Duplicated by #27961
  27. 2014-06-11 – Andrew Nacin (nacin) and core WP developers started thinking about a fix.
  28. 2014-08-22 15:47 – #21212 – Duplicated by #29322
  29. 2014-09-28 13:20 – #21212 – Duplicated by #29773
  30. 2014-10-04 18:49 – #21212 – Duplicated by #29857

FWIW, since I’ve been using my plugin I forgot about WordPress and UTF-8.

English (and not just the Library of Congress)

I’m fed by those ridiculous requirements in job offers:

  • Javascript (and not just jQuery)
  • Ruby (and not just Rails)
  • PHP (and not just Zend Framework),

What do they want?

First of all, jQuery, Rails, and Zend Framework are libraries written in JavaScript, Ruby and PHP respectively. So, of course, knowledge of the latter implies that of the former, because you can’t really do anything with a library if you don’t know the language it’s written in.

Then, if you want me to program an application in any given language without using libraries available at my fingertips for that language, then you’re just a fool. Do you really want me to give up on years of worldwide engineering experience advising about code reuse? What is it open source to you?

 

Ruby on Rails Jobs

I entered the Ruby on Rails world soon after the Agile Web Development with Rails manual came out, in 2005. Both Rails and its manual are now (2013) at the 4th installment.

Agile Web Development with Rails

The Book

Up to then and for about half a year more, I had been programming in Lotus Notes. It was a wonderful environment for many different applications, based on few, simple and powerful concepts, but for web development it was always far behind what true web environments like LAMP allowed.

I got tired of it and I’d been willing to return to my passion, web development, since I left Italy. In 1995, together with my then best italian friend, Fulvio, we started our first web development business in Rome: Energymedia (webzines publisher). A little later I went to Barcelona as an Erasmus student and right after getting my IT Engineering Diploma, I found a Lotus Notes job in Spain.

At the beginning of 2006 Rails was getting more and more hype so I decided to study it. I was immediately fascinated by its right way of doing. Everything that I knew about web development, and was cumbersome and painful in PHP (I had studied the WordPress code), had been thought of and a nice and easy and coherent solution had been provided.

Unfortunately no one was doing Rails in Barcelona at that time, so there were no possibilities to find Rails jobs. I decided to improve my PHP education and bought the Advanced PHP Programming manual, which was exactly what I needed. It taught me how to do the right things in PHP. Then I got hired by a dynamic but very old-fashioned PHP intranets developing firm.

Advanced PHP Programming

The Other Book

There I mixed up what I had learnt from both books and built my own pseudo PHP framework, with a pretty useful ActiveRecord implementation. I had programmed it for a big office automation project I was leading. Codenamed SIGOV, it was an intranet for the Parliament of Catalonia, used to make laws, from inception to discussion, to approval, to publication. My first commercial PHP project (see my CV).

For five years more I continued working in PHP, but in the summer of 2012 I finally landed a Senior Ruby on Rails Backend Engineer role. It was something really great for me, to be able to work in my beloved environment, but the only way I was able to do it was as a contractor for a USA company, MusicXray.

It’s not only that Rails jobs in Barcelona are still scarce nowadays, but they are also underpaid. Well, all programming jobs and really all jobs at large are underpaid in Spain. That’s the reason why very good programmers have to work abroad. A best friend of mine, which is a very good PHP programmer with whom I worked, had to go to Tromsø, Norway, to raise its salary by about a third.

During my quest for the next Rails job, I had an interesting interview in Barcelona, a couple of months ago. 3scale is one of the very few web companies that at the same time work with Ruby and Rails, have international relevance, and are based in Barcelona. A big chunk of interest in the interview came from the salary they offered, fifty thousands euros gross a year.

In general it is quite hard to compare salary figures between different locations, so I do it with a simple method that gives me at least a rough approximation. The biggest unknown is the amount of taxes a state is gonna take, so I just compare gross salaries per year. This is not as bad an approximation as it may appear at first sight, because we really do (or should) work everywhere for ourselves and for the place we live in. Taxes are the means by which such a social collaboration is done, and to higher taxes usually correspond higher service levels.

Then all I do is a simple proportion like this:

X : there = Y : here

from which I get this formula:

Y = X here / there

Now, here / there is a factor that takes into account differences in cost of living between those locations, so I usually go to http://www.numbeo.com/cost-of-living/ and use the percentage on the line that reads “Consumer Prices Including Rent”. Sometimes that info is not available, and it gets complicated.

As an example of how this would work in practice, let’s see how much they will be really paying me in San Francisco, California, for a Senior Rails Developer position. According to indeed.com, offered salaries in San Francisco for such a job are about 124000 USD gross a year, and according to numbeo.com, Consumer Prices Including Rent in San Francisco, CA are 44.62% higher than in Barcelona. Thus

X = 124000 USD
there = 1.4462 here

Y = 124000 USD * here / (1.4462 here) = 92891 EUR / 1.4462 = 64231 EUR

3scale offered me a salary which is 22% lower than the average in San Francisco. Instead my last Rails job was eighty thousands dollars gross a year, working as a contractor. That converted to a salary of sixty thousands euros gross a year, and they were really that much because I worked remotely from Barcelona. Still 3scale’s offer was quite interesting, because in Barcelona only a fourth of all programmers seems to get a salary of more than thirty three thousands euros gross a year.

Kids playing Lego

Pair programming in Rails (once in a lifetime chance to do it with a girl)

Another interesting bit of that interview was that the recruiter (i.e. the boss) openly underrated Rails programmers. He compared them to kids playing Lego. I don’t share that point of view at all. In fact, not only I wished to play Lego as a child had I had any chance (they were so expensive), but also programming Rails is much more fun (no girls, though).

The sad thing is that Rails strives to make web building so easy that companies often hire not so good programmers and get some good results nonetheless. Of course they also unknowingly increase the technical debt of their applications. In fact, while in a Lego piece there is only its interface (plastic, color and size), both Rails magic and gems hide entire philosophies. And you should understand all of them before using them. Even if they work as expected, even if we only interface with them, in reality we should not only know what they do but also understand how they do it.

Any fool can know. The point is to understand.
― Albert Einstein

However, I must admit I have a similar prejudice with respect to Ruby. I think many Ruby programs work by chance, and that is almost inherent to the language. With PHP and JavaScript it’s not the same, because they are much more WYSIWYG than Ruby. Not only a single word in a Ruby snippet can be one of many different things (a variable, a call to a method of self, a call to a global method; a class, a module) but you also can never be really sure that the code you see into a class is all there will be into it at execution time. Don’t get me wrong: I love this powerful language, but I doubt many Ruby programmers are even aware of the blades, let alone how to shave without bleeding.

With great power comes great responsibility.
― Stan Lee

The fact that Rails programming is considered of lesser ability than other languages is maybe at the root of why Rails jobs are much less paid, at least in Europe. For example, I recently underwent a job interview for London, UK. All was fine until we got to the salary they offered: it was fifty thousands pounds.

Using the above formula, I get

X = 50000 GBP
there = 1.6276 here

Y = 50000 GBP * here / (1.6276 here) = 58519 EUR / 1.6276 = 35954 EUR

And that figure is almost as much as I earned in 2007, at the beginning of my PHP commercial experience!!

 

TDD mistake

TDD:

  1. first, write a failing test
  2. then, write just enough code to make the test pass
  3. repeat from 1.

What TDD does is to focus on the interface of any code you are going to write. IMO, only very basic solutions are good candidates for TDD. By basic I mean not simple but minimal, like a REST API.

© 2017 Notes Log

Theme by Anders NorenUp ↑