Archive for the ‘Uncategorized’ Category.

Modifying the Facebook Live Feed with Greasemonkey and jQuery

My wife recently expressed to me that she didn’t care for some of the events appearing in the Facebook Live Feed, namely the ones relating to her friends adding other people as friends or becoming fans of pages, because they tended to flood her feed and crowd out other events she was actually interested in.

Unfortunately, Facebook doesn’t provide any filtering for the feed outside of either entire applications or entire people. Upon inspecting the markup for the feed, I found that content was styled such that it was easy to refer to entire entries using CSS selectors. I then recalled that the Firefox web browser, which she uses, has available for it an extension called Greasemonkey that enables you to execute custom JavaScript for a specified set of web pages. I poked around for existing Greasemonkey user scripts to handle the task, but most seemed to try to do too much and I had trouble finding any that supported custom filtering of the Live Feed.

It had been a while since I looked at Greasemonkey, but apparently it’s gained some nice features since. One of these is the @require tag, which allows you to pull in a remote JavaScript file that Greasemonkey will smartly cache locally to be reused. I used this to add the jQuery library to my script using the copy hosted on Google AJAX Library APIs, at which point removing the feed entries my wife didn’t want only required a few lines of code.

I’ve pushed the user script to a github repository if you’d like to download it. I’ve made it extensible such that removing other entries by phrase only requires adding entries to an array at the top. If you have any improvements to make, please feel free to fork it. Enjoy!

Building PHP 5.3.0 with Tidy Support

I dug around a bit, but most resources I came across on Google were about using the tidy extension for PHP rather than doing a custom build of PHP that included the tidy extension. Once I figured the details out, I thought I’d share. They admittedly seemed somewhat obvious after the fact, though also were not communicated as explicitly as I would have liked anywhere that I could see.

You’ll need a system with a CVS client installed to do this. The system I intended to build on didn’t have one, so I used my laptop, did a CVS checkout, made a gzipped tarball out of the CVS checkout directory, used scp to push it up to the remote server, and decompressed and extracted the archive from there.

  1. Download a PHP 5.3.0 tarball and decompress it.
    $ wget -c http://us2.php.net/get/php-5.3.0.tar.gz/from/a/mirror
    $ tar -zxf php-5.3.0.tar.gz
  2. Follow the anonymous checkout instructions to download tidy. Enter the first command shown here, press Enter without entering anything when prompted for a password, then enter the second command shown here.
    $ cvs -d:pserver:anonymous@tidy.cvs.sourceforge.net:/cvsroot/tidy login
    Logging in to :pserver:anonymous@tidy.cvs.sourceforge.net:2401/cvsroot/tidy
    CVS password:
    $ cvs -z3 -d:pserver:anonymous@tidy.cvs.sourceforge.net:/cvsroot/tidy co -P tidy
  3. When configuring the PHP build and specifying the path for tidy, just point to the root CVS checkout directory.
    $ ls tidy
    CVS  CVSROOT  bin  build  console  experimental  htmldoc  include  lib  src  test
    $ cd php-5.3.0
    $ mkdir build/php_build
    $ ./configure --prefix=`pwd`/build/php_build --with-tidy=../tidy
    $ make
    $ make install

I didn’t run into any problems when following this process. php -m shows the tidy module and it appears to run without issue. Hope this helps someone else.

New SPL Features in PHP 5.3 Webcast Slides

Hope you were able to make it to the CodeWorks 2009 webcast at which I presented on New SPL Features in PHP 5.3! I’ve posted the slides and source code. I hope you enjoyed the webcast and that you’ll register for the excellent selection of upcoming webcasts leading up to CodeWorks 2009.

php|tek 2009 Slides

If you made it out to php|tek and saw my uncon session on web scraping, the slides have been posted. If you’d like to discuss the topic further, feel free to contact me. I love chatting about the subject with others.

CDC Update

While using cdc recently after having written about it, I ran into an odd issue. While doing a lint check on a code block, a parse error was occurring on a line that contained a comment in the original source file.

The original code block to do this lint check had the line below to do the heavy lifting.

$response = shell_exec('echo ' . escapeshellarg($code) . ' | php -l');

A var_dump() on $code revealed this, where the comment line was the line on which the parse error was occurring.

string(474) "<?php
...
// Matches any of the standard escape sequences \r, \n, or \t
$matches = (preg_match('/\\r|\\n|\\t/', $string) == 1);
...
?>"

Presumably what was happening was, even though the var_dump() call showed that actual newlines were being interpreted correctly, the \r was also being interpreted rather than taken literally. This caused the comma following it to generate the error I was receiving, namely "Parse error: syntax error, unexpected ‘,’." (If you know why this is, I’m very curious to find out.)

Luckily I was able to tag Derick and he pointed me in the direction of an alternative: proc_open, which even has an example for executing a PHP file that works just as well when modified to perform a lint check on a PHP file. You can see the code changes in the related git commit.

Getting Google Calendars into Thunderbird on Ubuntu

As work-related events recently started piling up, we gained a need to keep them organized. So we got a Google Calendar, shared it between us, and all was dandy. I wanted to be able to pull that into my local e-mail client, though. I’m running Ubuntu Ibex and its Thunderbird package.

My friend and colleague Keith Casey pointed out to me that Thunderbird has a Provider for Google Calendar add-on that works with Lightning, which I already had installed. So I install the GCal add-on and restarted Thunderbird. No visible change. I checked the Add-ons area only to find that the entry for the GCal add-on was displaying the message "Requires additional items" with no way to see what said items were. A search prompted me to check the version of the Lightning add-on, which I found to be 0.8 because I’d already installed the Ubuntu package for it and that was the latest version available.

As it turns out, the Ubuntu wiki has instructions for what I figured out on my own the first time around: version 0.9 of the Lightning extension has to be downloaded and installed manually for 64bit builds. After that, the GCal add-on was right as rain. Adding a calendar was as simply as accessing Google Calendar via the web, going to Calendar Settings for an individual calendar, and using the XML link for that calendar when adding the calendar to Lightning using new new Google Calendar option added by the GCal add-on.

Zend_Form and Zend_Loader_PluginLoader SNAFU

I’ve spoken on this blog before about scaling Zend_Form for larger forms by having it use a central set of plugin loaders for all elements contained within a form, rather than allowing each element to implicitly create its own instances. Another way to accomplish the same goal is to have form elements use the same loaders that the form has already created for itself.

<php

class FastloadingForm extends Zend_Form
{
    public function addElement($element, $name = null, $options = null)
    {
        if (!is_array($options)) {
            $options = array();
        }

        // A plugin loader is implicitly created if default decorators are loaded
        $options['disableLoadDefaultDecorators'] = true;

        // Add the element to the form
        parent::addElement($element, $name, $options);

        // Configure the element to use the form's plugin loaders
        $element = $this->getElement($name);
        foreach ($this->_loaders as $type => $loader) {
            if ($type != 'ELEMENT') {
                $element->setPluginLoader($loader, $type);
            }
        }

        // Now load default decorators for the element
        $element->loadDefaultDecorators();

        return $this;
    }
}

However, this approach can cause an issue if subforms are being used because of a bug in Zend_Loader_PluginLoader.

The bug entails Zend_Loader_PluginLoader allowing multiple instances of the same path to be added per prefix. This becomes an issue because of how Zend_Form handles adding subforms: it automatically calls addPrefixPath() on the subform for each path contained in each of the form’s own plugin loaders. If both the form and subform are using the same plugin loaders, the bug causes the same paths to be added multiple times. When plugins are subsequently loaded, the duplicate paths are all searched individually, creating a bottleneck as more subforms are added.

The issue in the JIRA bug tracker for ZF has patches for both the issue itself and the unit test suite for the Zend_Loader_PluginLoader component. Please view the bug report and vote to ensure that the issue receives attention and is fixed in a release in the near future.

DomQuery Update

I think it’s mostly flown under the radar, but one of my smaller projects is a class called DomQuery that is built on top of DOM and the SPL ArrayObject. The functionality is provides is somewhat similar to jQuery, but it’s different in that it does so programmatically through the API rather than using an expression parser.

This post is mainly to inform anyone who might be interested that I’ve moved the project from its old home at Assembla to a new repository on github. I’ve been enjoying my use of git for version control of other projects and it seems an appropriate place to house DomQuery to allow other people to play with it. I haven’t had time recently to make many updates, but hope that will change in the short term. If you haven’t used DomQuery, why not try it today?

Updating PHP Syntax Highlighting for vim on Ubuntu

This is just a quick post, mostly a "note to self" so I don’t forget how to do this. If you were curious, it was the result of indirect inspiration from these Become a Bash Ninja slides.

If you’re running Ubuntu 8.10 as your desktop OS, have vim installed, and use it for PHP development, you may not be aware that the PHP syntax file that comes bundled with vim is a bit outdated. There’s a fairly simple way to update it, though. In fact, it’s doable with two simple commands from Terminal.

cd /usr/share/vim71;
wget -O - http://www.vim.org/scripts/download_script.php?src_id=8651 | sudo tar -zxv

Webcast Slides

Hard to believe it’s been that long, but two months ago I mentioned the free webcast series sponsored by Adobe and leading up to php|tek 2009.

I’ve posted the slides from my webcast on February 27. If you weren’t able to make it, I gave an introduction to what web scraping is, basic details of the HTTP protocol, available resources for developing web scraping applications, and best practices. I know there are plans to make the audio from the webcast and I will update this post with a link once it becomes available.

If the slides and audio aren’t enough for you, I will in all likelihood be giving an extended version of the presentation that includes both retrieval and analysis as part of the Unconference event at php|tek. Look forward to seeing you there!