Archive for January 2010

Sharing Files with Windows from Ubuntu Karmic

We got a desktop running Windows 7 over the Christmas holidays. I hadn’t been faced with the prospect of sharing files with it up until I wanted to play an MP3 file located on my laptop — which is running Ubuntu — from the desktop, which is connected to a better speaker system. As it turns out, sharing files with Windows from Ubuntu has gotten a lot easier with Ubuntu 9.10 Karmic Koala.

Open Nautilus and navigate to the parent directory of the one you’d like to share. Right-click on that directory and select the option “Sharing Options.” In the window that appears, check “Share this folder.” If Samba is not already installed on your system, this will prompt you to install it. The nice thing is that it handles all the interaction with APT and configuration of user accounts and shares for you versus the manual approach that’s been required in the past.

Once the service is installed, if you’d like to share the directory with users without requiring them to enter user credentials, check the box “Guest access.” Finally, click the “Create Share” button. You’ll receive a prompt to restart your session so the setting changes can take effect; go ahead and do this.

When I tried to access the directory from the Windows 7 desktop at this point, I still got prompted for credentials. I tried manually rebooting my laptop and at that point was able to access the files anonymously without further issue. I don’t know if a manual restart is always required, but it did the trick for me.

Hope this helps someone!

Splitting PHP Class Files

A recent work project required me to write a PHP script to interact with a remote SOAP service. Part of the service provider’s recommended practices entailed using a slightly dated software package called wsdl2php, which generates a single PHP file containing classes corresponding to all user-defined types from a specified WSDL file.

The issue I ran into was due to all the generated PHP classes being housed in a single file. I had to process two WSDL files that had several identical user-defined types in common. As a result, I couldn’t simply include the two PHP files generated from them because PHP doesn’t allow you to define two classes with the same name.

Looking at its source code, modifying wsdl2php to change this behavior was not a very appealing option. Attempting to consolidate the two WSDL files into one with no redundant user-defined type declarations seemed futile as well. Instead, I resolved to split the generated PHP files such that each class was contained in its own file. This would also allow me to use an autoloader to determine which of the classes I actually needed for the particular service call I was making.

Due to the number of classes, splitting the classes into separate files by hand would have been tedious and time-consuming. I decided to tap into my previous experience with the tokenizer extension to throw together a CLI script that would handle this for me. Once I got it working, it clocked in at just over 50 LOC with comments and whitespace. You simply call it from a shell and pass it the PHP file you want to split and the destination for the split class files.

I thought it might be useful for others needed to process similarly formatted source code, so I threw it into a github repository for anyone who might like to take a look. I’m open to suggestions for improvements to implement if enough people find it useful. Feel free to file an issue on the repository if you happen to find a bug.

So Long, Blue Parabola

I’ve decided to leave Blue Parabola. My last day there will be Tuesday February 16. I’d like to thank Keith Casey and Marco Tabini for choosing me to be part of the team. It’s been a privilege to work with them and I’ve learned a great deal.

As for what’s next, I’ll be starting at K-fx2. (Nope, no funemployment for me.) There, I’ll be developing Zend Framework applications and helping to streamline development processes and infrastructure.

Thanks to all my friends and family who’ve provided support during this transition. I’m looking forward to what the next year holds.

Speaking at a Conference

I can’t make any claim to the title of veteran conference speaker. Not yet, at least. However, I have done it once before at ZendCon in 2008 and I’ll be doing it again at php|tek this year. I thought I’d take a blog post to give out a few tips to any prospective first-time speakers based on my first speaking experience. I’m assuming there that you’ve already decided on a particular conference that you want to attend, you’ve submitted a session proposal, and you’ve been accepted.

First, in addition to the other things you should do before attending, be ready to give your presentation before you get on the plane. You should start on your slides as far in advance as possible. Don’t put it off or wait until the last minute, because it will likely be more work than you anticipate. This includes making sure that any live demos you intend to give will run as expected. Syntax errors and crashing web servers look very bad to the audience.

One of the reasons for this is that you’ll want to practice your talk out loud. It’s one thing to put the material onto slides, but it may sound different when it’s actually coming out of your mouth and going into the crowd. You may find stumbling points, places where you stutter or get caught off-guard when transitioning from one topic to another. Try to organize the presentation such that it matches your natural flow when talking about the topic without any slides at all.

Which reminds me, learn from the masters. People like Marco Tabini have spoken before and have a wealth of knowledge that they’ll share fairly freely most of the time, especially if alcohol (or, in Marco’s case, an espresso) is involved. Look at books like Presentation Zen by Garr Reynolds. Take the time to hone your presentation skills before you have to make your delivery.

If you’ve been to a conference before, you’ve probably already learned about my next point the hard way. Don’t depend on wifi internet access availability. Why not? Because the vast majority of the time, it will suck. There won’t be enough IP addresses, someone will do something to hog bandwidth and make latency skyrocket, it will find some way to refuse to work. Save local copies of files, write a minimal daemon to simulate a remote server, do whatever you need to do to avoid it.

That point goes hand in hand with this one: test your equipment early and have a Plan B. In particular, hook your laptop up to the projector in the room in which you’ll be speaking (or to a test projector, if the conference hosts provide one and prefer you use that) to make sure it can display your slides. Ben Ramsey was gracious enough to loan me his Macbook at ZendCon because my Sony Vaio refused to work with the projector and the time-sensitive situation did nothing but add to my speaking nerves. Make sure you don’t end up in the same spot.

Lastly, don’t let critical reception deter you from speaking again. I got pretty negative feedback the first time around, but I took it in stride. While I know I have plenty of room for improvement, I’m still going to give it another shot. Do your very best, then strive to be better.

Hope you enjoyed this blog post and gleaned something useful from it. If you’ve got any of your own speaking tips, please feel free to add a comment on this post. If you’ll be attending php|tek, I look forward to seeing you there!

Speaking at tek-X

As the recently released schedule shows, I will be speaking at the php|tek 2010 conference. The session I’ll be presenting is entitled “New SPL Features in PHP 5.3″ and will be an extended version of the webcast I presented as part of the CodeWorks webcast series.

While there, I also plan on participating in the Hack Track and may try to recruit a few new contributors (like you!) for the Phergie project. I am very much looking forward to the event and hope to see you there!

Database Testing with PHPUnit and MySQL

I recently made a contribution to the PHPUnit project that I thought I’d take a blog post to discuss. One of the extensions bundled with PHPUnit adds support for database testing. This extension was contributed by Mike Lively and is a port of the DbUnit extension for the JUnit Java unit testing framework. If you’re interested in learning more about database unit testing, check out this presentation by Sebastian Bergmann on the subject.

One of the major components of both extensions is the data set. Database unit tests involve loading a seed data set into a database, executing code that performs an operation on that data set such as deleting a record, and then checking the state of the data set to confirm that the operation had the desired effect. DbUnit supports multiple formats for seed data sets. The PHPUnit Database extension includes support for DbUnit’s XML and flat XML formats plus CSV format as well.

If you’re using MySQL as your database, CSV has been the only format supported by both the mysqldump utility and the PHPUnit Database extension up to this point. My contribution adds support for its XML format to the extension. While this support was developed to work in the PHPUnit 3.4.x branch, it won’t be available in a stable release until 3.5.0. In the meantime, this is how you can use it now.

  1. Go to the commit on Github and apply the additions and modifications included in it to your PHPUnit installation.
  2. From a shell, get your XML seed data set and store it in a location accessible to your unit test cases.
    mysqldump --xml -t -u username -p database > seed.xml
  3. Creat a test case class that extends PHPUnit_Extensions_Database_TestCase. Implement getConnection() and getDataSet() as per the documentation where the latter will include a method call to create the data set from the XML file as shown below.
    $dataSet = $this->createMySQLXMLDataSet('/path/to/seed.xml');
  4. At this point, you can execute operations on the database to get it to its expected state following a test, produce an XML dump of the database in that state, and then compare that dump to the actual database contents in a test method to confirm that the two are equal.
    $expected = $this->createMySQLXMLDataSet('/path/to/expected.xml');
    $actual = new PHPUnit_Extension_Database_DataSet_QueryDataSet($this->getConnection());
    // Specify a SELECT query as the 2nd parameter here to limit the data set, else the entire table is used
    $actual->addTable('tablename');
    $this->assertDataSetsEqual($expected, $actual);

That’s it! Hopefully this proves useful to someone else.

PHPUnit and Xdebug on Ubuntu Karmic

This is just a quick post to advise anyone who may be using PHPUnit and Xdebug together on Ubuntu Karmic. If you try to upgrade to PHPUnit 3.4.6 and you’re using the php5-xdebug Ubuntu package (which is Xdebug 2.0.4), you may get output that looks like this:

$ sudo pear upgrade phpunit/PHPUnit
Did not download optional dependencies: pear/Image_GraphViz, pear/Log, use --alldeps to download automatically
phpunit/PHPUnit can optionally use package "pear/Image_GraphViz" (version >= 1.2.1)
phpunit/PHPUnit can optionally use package "pear/Log"
phpunit/PHPUnit can optionally use PHP extension "pdo_sqlite"
phpunit/PHPUnit requires PHP extension "xdebug" (version >= 2.0.5), installed version is 2.0.4
No valid packages found
upgrade failed

There are two ways to deal with this situation. First off, note that the newer Xdebug 2.0.5 version includes several bugfixes including one related to code coverage reporting. That said, if you still want to continue using the php5-xdebug package anyway, you can force the upgrade by having the PEAR installer ignore dependencies like so:

sudo pear upgrade -n phpunit/PHPUnit

The other method involves installing Xdebug 2.0.5. First, if you have the php5-xdebug package, remove it.

sudo apt-get remove php5-xdebug

Next, use the PECL installer to install Xdebug. This requires that you have the php5-dev package installed so that the extension can be compiled locally.

sudo apt-get install php5-dev
sudo pecl install xdebug

At this point, create the file /etc/php5/conf.d/xdebug.ini if it doesn’t already exist and populate it with these contents:

zend_extension=/usr/lib/php5/20060613/xdebug.so

Then bounce Apache so that the new extension will be loaded.

sudo apache2ctl restart

That’s it. Hope someone finds this helpful.

A New Chapter

This will be my last post on this blog [ishouldbecoding.com]. I’ve decided to both move my blog domain to matthewturland.com and switch to using WordPress.

For now, I’m not migrating the existing content on this blog over. I’m still debating that, and I’ll likely end up doing it, but for now I’d like to focus on getting new content out there.

This blog has served me well for nearly two and a half years, but it’s time to make a change and move on to something new. Thanks to all my faithful readers; I hope my writings prove useful to you following this transition.

I’m a Honey Pot

Side note: Yes, the title of this post is a throwback to the 418 status code in the HTTP protocol. My sense of humor is just odd that way.

I thought I’d kick things off on my new blog with a quick post on something I did while getting it set up.

Before switching to this new blog, I’d moved to using the spamhoneypot plugin on my old Habari blog to capture spam. I had a great amount of success in that switch, but in deciding to move to using WordPress on this new blog, I noticed that it had no equivalent plugins. There were several anti-spam plugins, but they all required use of a third-party service. I hadn’t seen consistent success with plugins that used this approach in the past, so I wanted to avoid repeating those experiences.

So, I decided to try my hand at writing a WordPress plugin. After wading through the filter and action documentation and googling around for a bit, I came up with a fairly simple plugin that seems to do the job.

The plugin works by adding a textarea field to the comment form that’s hidden using a CSS style. Since bots don’t generally detect CSS like this, they proceed to fill out the field like any other field. This implies that they aren’t a human being using a browser, in which case the plugin marks the comment as spam. I’ve found this catches the vast majority of spam comments with very few false results.

I’ve submitted to have the plugin hosted on the WordPress site, but until then, you can grab a copy off of a Github repository I’ve set up for it. Hope you find it useful!

Update 1/2/10 8:41 AM CST: The plugin is now available for download from the WordPress site.

A Summary to End a Decade

Tonight we reach the end of the 2000s or, as some people like to call it, the "ones." As is customary among most of my peers, I’ve reviewed last year’s summary to reflect on what I accomplished this year. So, here are the high points in no particular order.

  • Draw: The writing and technical editing for my book was done by May. The publisher was able to get it proofread, but it’s still in layout due to delays from CodeWorks and the holidays. I’m really hoping things will come together next month, but as it stands now, I’m not able to hold a copy of my book in my hands just yet. In terms of writing books, there may be new developments come next year, but I’m keeping those under wraps for now until I have more definitive news to pass along.
  • Win: A number of things changed with the Acadiana Open Source Group this year including our regular meeting format and where our web site is hosted. Additionally, we’ll be moving the meeting location to the LITE Center beginning next month, which is a very exciting development. I’m looking forward to continued success with the group as it approaches its third year.
  • Win: I published not one, not two, but three articles between the January and June issues of php|architect Magazine. I’ve also continued to serve as a Technical Editor for several issues.
  • Win: Even though I wasn’t accepted to speak at any conferences that were held this year, I did get confirmation that I will be speaking at php|tek in 2010. Given how I originally worded this goal, I’m going to call it a win.
  • Draw: I did actually purchase the MySQL Certification Study Guide, read it, schedule the test, and show up to take it. Through an error on the part of the testing provider, however, the exam was pulled prematurely and I wasn’t allowed to take it that day. In hindsight, this goal doesn’t seem as important to me now as it did when I originally set out to do it.
  • Fail: I didn’t find time to learn more about the C programming language and PHP internals to make any sort of contribution. Hopefully I can make amends on that in the coming year.

So, more successes than failures. Not too shabby, all in all. Again, there were also some accomplishments that weren’t on the list, but that I feel are significant.

  • I’ve marked the end of my first year with Blue Parabola. It’s been an interesting year and I think the coming year holds a lot of new and exciting developments. (Dun dun dun!)
  • I gave webcasts for both php|tek and CodeWorks. I’ll be giving an extended version of the latter presentation as a session at php|tek 2010.
  • I helped to organize the Hackathon at php|tek, an idea which has gone on to inspire a Hack Track at php|tek 2010. This is definitely an event to look forward to!
  • While Phergie isn’t ready for stable release just yet, I think the core is mostly done and what remains is porting plugins and fixing any issues or filling in any gaps that present themselves. In addition to wrapping that up, I also plan on getting some sort of proper web site (maybe a MediaWiki installation) up for the project this year.
  • I just barely got this in today, but I made my first contribution to the PHPUnit project in the form of a commit that adds support for the mysqldump XML format to PHPUnit’s Database extension.

Happy New Year (and Decade) everyone and I hope you have many more!