mitcho Michael 芳貴 Erlewine

Linguist. Fifth year PhD student at MIT.

blog

Archive for the ‘how to’ Category

Checking mochitest test coverage

Tuesday, March 22nd, 2011

Firefox Download ButtonOne of the last bugs for Firefox Panorama was bug 625818: “Check Panorama mochitest test suite coverage”. Our automated tests ensure that we do not regress on existing functionality, but it’s only as good as its coverage: how much of the Panorama code base is actually being “hit” through the process of running the test suite.

Panorama went through a pretty rapid development cycle, making it into Firefox 4 which was released today (yay!). Moreover, for a while we were developing outside of mozilla-central, without the regular “patches require tests” requirement. This makes checking its test coverage particularly important.

Check out the final result, the Panorama test coverage report. The good news: our code coverage is 86%! (Some notes on what improvements can be made are in the bug.)

code coverage report

PhiliKON had previously worked on hooking into the JS Debugger service’s interruptHook to test xpcshell tests. I modified this code to run instead in the Mochitest browser chrome tests. This code can be found on the bug.

With this patch applied, I invoked the test suite with the following code: TEST_PATH=browser/base/content/tests/tabview COVERAGE_FILTER="*tabview*" COVERAGE=true make -C obj-ff-dbg mochitest-browser-chrome . That’s a regular mochitest-browser-chrome invocation with the COVERAGE=true flag which turns on code coverage checking, and COVERAGE_FILTER=*tabview* which filters out results from files which don’t have “tabview” in their paths. This creates a file called coverage.json in the working directory of the test suite, meaning, for me, obj-ff-dbg/_tests/testing/mochitest/.

This JSON file is a multidimensional array, with file paths and then line numbers as keys. The file paths here, as best as possible, have been converted into local filesystem paths. PhiliKON built a script which produces beautiful reports based on this output.

A word of warning: running with this JSD interruptHook is ridiculously slow. A number of tests for Panorama are timing-dependent (drag-drop tests, for example), making some of them fail, but that’s okay… as long as it completed not via a timeout, it actually did run through all the code. In order to get this to run through everything with some degree of control, I split up the mochitest tabview suite in to a few chunks. I then took the multiple resulting coverage.json files and passed them into another script, in tools/coverage/aggregate.py, which takes multiple JSON results like this and puts them together into a single JSON file. I then passed this aggregate JSON file to PhiliKON’s wonderful report script and—voila—the Panorama test coverage report! Easy as pie.

Beginning development with Jetpack SDK 0.2

Wednesday, April 14th, 2010

This article is a translation of a recent article in Japanese by fellow Jetpack Ambassador Gomita which was published on the Mozilla Labs Jetpack blog. I’m cross-posting it here for posterity.

Mozilla Labs recently released version 0.2 of the Jetpack SDK, which fixes some issues of the 0.1 release such as a glitch regarding development with Windows. SDK 0.2 doesn’t include the planned APIs for rapid development of new browser functionality, but you can still play with SDK 0.2 to get a flavor for development with the Jetpack SDK.

In this article we begin by setting up an SDK 0.2 development environment and explain the steps required to develop a simple, practical add-on using SDK 0.2. The instructions here are for Windows, but the basic steps are the same in every platform.

(more…)

Creating an image-sized iframe overlay with Shadowbox

Wednesday, January 13th, 2010

I recently have been working with the Shadowbox JavaScript library for an upcoming revision to the MIT Edgerton Digital Collections website. Shadowbox is a nice [[Lightbox (JavaScript)|lightbox]] library designed to work with various JavaScript libraries like jQuery, prototype, and mootools with a nice modular design.

Shadowbox is organized around different “players”—one for each kind of media that will be displayed. The library by default comes with players for Flash, HTML fragments, iframes, QuickTime, and Windows Media. Some of these players, like those for images and video, automatically recognize the media size and adjust the lightbox accordingly, while others such as the iframe player can use a set size or can fill the screen. For the Edgerton site, though, we had a need for displaying an iframe but in the dimensions of a set image, so that we could display the image with an overlay. Here are some notes on how to implement a custom player for Shadowbox.

(more…)

Fixing Geshi on line 2132

Saturday, June 13th, 2009

I recently noticed that some of my blog posts, most notably my Templates in YARPP 3 article, was producing a PHP error:

Warning: preg_match() [function.preg-match]: Compilation failed: unrecognized character after (?< at offset 3 in /…/html/blog/wp-content/plugins/wp-syntax/geshi/geshi.php on line 2132

This seemed to be coming from the version 1.0.8.4 version of Geshi I had installed. A quick google search for “geshi line 2132” gives you over a thousand errors, so this seems to be common issue. Geshi is a fabulous and popular syntax highlighter and is the core component of the WP-Syntax plugin for WordPress.

I did some digging around and realized that the issue was with the compilation of this monstrosity of a regular expression, used (as far as I can tell) to identify PHP code snippets, for example the <?php … ?> keywords:

/(?<start><\\?(?>php\b)?)(?:
(?>[^\"'?\\/<]+)|
\\?(?!>)|
(?>'(?>[^'\\\\]|\\\\'|\\\\\\\|\\\\)*')|
(?>\"(?>[^\"\\\\]|\\\\\"|\\\\\\\\|\\\\)*\")|
(?>\\/\\*(?>[^\\*]|(?!\\*\\/)\\*)*\\*\\/)|
\\/\\/(?>.*?$)|
\\/(?=[^*\\/])|
<(?!<<)|
<<<(?<phpdoc>\w+)\s.*?\s\k<phpdoc>
)*(?<end>\\?>|\Z)/sm

Not knowing exactly where to start in diagnosing this crazy expression, I simply disabled those “script delimiters” in the geshi/php.php file. The sections I commented out are lines 1080-1101. Now the script delimiters like &lt;?php don’t get highlighted nicely, but I feel that’s a small price to pay for eliminating these errors. Another solution for the WP-Syntax users seems to be to downgrade to 0.9.4. Hopefully in the near future an update to Geshi will come out which fixes this issue once and for all.

Adding Your Language to Ubiquity Parser 2

Wednesday, April 29th, 2009

NOTE: This blog post has now been added to the Ubiquity wiki and is updated there. Please disregard this article and instead follow these instructions.

You’ve seen the video. You speak another language. And you’re wondering, “how hard is it to add my language to Ubiquity with Parser 2?” The answer: not that hard. With a little bit of JavaScript and knowledge of and interest in your own language, you’ll be able to get at least rudimentary Ubiquity functionality in your language. Follow along in this step by step guide and please submit your (even incomplete) language files!

As Ubiquity Parser 2 evolves, there is a chance that this specification will change in the future. Keep abreast of such changes on the Ubiquity Planet and/or this blog (RSS).

(more…)

External orders in WordPress queries

Saturday, November 29th, 2008

The advanced WordPress user is intimately familiar with query_posts, the function which controls which posts are displayed in “The Loop.” query_posts gives plugin and theme writers the ability to display only posts written in Janary (query_posts("monthnum=1")) or disallow posts from a certain category (query_posts("cat=-529")1). One of the parameters you can set here is orderby which affects the ordering of the posts returned, with allowed values such as author, date, or title. But what if you want to order your posts in some other order, defined outside of your wp_posts table? Here I’m going to lay out some thoughts on rolling your own external ordering source for WordPress queries.

In order to introduce an external ordering source, we need to do four things: 1. create the external ordering source, 2. hook up (read “join”) the external ordering source 3. make sure we use that order, and 4. make it play nice. ^^

By the way, I’m going to assume you, dear reader, are PHP-savvy, proficient in MySQL, and already know a little about WordPress. This how-to is not for the PHPhobic.

(more…)


  1. This, incidentally, is precisely what I do to hide, by default, my tweets in my index.php and archives.php

Keep up with Yet Another Related Posts Plugin with RSS!

Saturday, October 4th, 2008

As more and more people have been using my Yet Another Related Posts Plugin for [[WordPress]], I thought it would be nice to have an RSS feed for users to stay on top of the latest releases.

Clicking on a version’s permalink will let you download the plugin. Subscribe now and be the first to find out when the upcoming version 2.1 is released!

I decided to semi-automate this RSS-producing process as well. As a plugin developer using wordpress.org’s plugin hosting, I sync a local copy of the plugin to their server using [[SVN]]. I wrote a [[PHP]] script to get the modification date information directly from the local files, parse the version log in the read me, and produce the RSS feed. If there’s an interest, perhaps I’ll release this code in the future.

Doing your Taiwan Fulbright taxes

Monday, July 14th, 2008

So you received a Fulbright fellowship to teach English in Taiwan. Congratulations! And they’re even going to pay you! Great! But if they’re paying you, you’ll have to pay taxes… so here’s my guide to doing your Taiwan Fulbright English Teaching Assistant taxes.

Note: Many of the considerations here are specific to Fulbright English Teaching Assistants (ETA’s) in Taiwan. If this exact grant doesn’t apply to you, you may be better off simply taking a look at the IRS’s guide for Fulbrighters and also the Tax Guide for U.S. Citizens and Resident Aliens Abroad. I am not a [[Certified Public Accountant]], tax advisor, nor [[Enrolled Agent]]1 so your mileage may vary. Consider yourself warned.

(more…)


  1. Highest (coolness of title)/(actual coolness of the job) ratio ever. 

My new scale

Tuesday, June 24th, 2008

[[ANA]] has draconian baggage restrictions (checked: 20kg total, US$10/kg thereafter) and I don’t own a scale. Problem solved!

Verdict: suitcase 18kg, guitar 6kg. I think I’ll make one more box to send at the post office tomorrow morning.

I feel like a caveman. THAT IS ALL.

St. Patrick’s Day Pilaf, brought to you by Sufjan Stevens

Saturday, March 15th, 2008

St. Patrick’s Day for many means a wholesale celebration of faux-Irishness through [[Guinness]] and everything green. While I’m not a fan of beer, I decided to put something green together to eat today. One of my favorite food writers [[Mark Bittman]] of the New York Times made a chicken with salsa verde but since I can’t find half of those ingredients in this country, I made a simpler non-Irish dish: a green pilaf, based on Bittman’s own recipe. Why not try a simple green vegetarian dish for St. Patrick’s Day?

As an added bonus, I set this recipe to Sufjan Stevens’ Illinoise. I was just in a Chicago-missing mood and listening to it while cooking, and it seemed to work so well.

Time: 45 minutes (mostly waiting, though)
2 tablespoons extra virgin olive oil
1 medium onion, chopped
2 cloves garlic, chopped
Salt and pepper
1 1/2 cups (400cc) chicken stock (I used a [[bouillon cube]]—雞湯塊)
1 medium head of broccoli, just the flowers, in small chunks (maybe 3/4-1 cup)
1 cup (250cc) short grain white rice
1/2 cup (130cc) chopped parsley, optional

  1. Track 1: “Concerning the UFO Sighting Near Highland, Illinois.” Put oil in a pan on medium heat. Add the garlic, onion, and a pinch of salt and stir occasionally until the onion is translucent or until you hear the piano riff on track 3, “Come On! Feel the Illinoise!” In the down time, you can make sure your chicken broth is heated up in a pot.
  2. Track 3: “Come On! Feel the Illinoise!” Add rice to the onion pan and stir occasionally until they get clear and start to brown, sometime during the second half of track 3, “Come On! Feel the Illinoise! Pt. II: Carl Sandburg Visits Me In a Dream.” Throw the broccolli in the broth to let cook for the last minute of “Carl Sandburg.”
  3. Track 4: “John Wayne Gacy, Jr.” Add the stock/broccolli to the rice/onion pan. Heat to a boil and then let cook for the rest of the track. Stir occasionally. Compare yourself to a serial killer as you watch the bubbles.
  4. Track 5: “Jacksonville.” Cover and cook until the end of track 9, “Chicago.”
  5. Track 10: “Casimir Pulaski Day.” Enjoy my favorite song on the CD. Turn heat off and let sit, uncover, stir gently, cover again, and let sit on the burner until track 15, “The Predatory Wasp of the Palisades Is Out to Get Us.” Optionally mix in chopped parsley for additional green. Serve.

Happy St. Patrick’s Day!

Display your Last.fm rankings using PHP 4’s XSLT support

Friday, February 1st, 2008

With all the exciting recent news about Last.fm, I thought I would document a simple bit of code I added to my site the other day.

Last.fm offers a number of [[Flash]]-based widgets you can add to your website. Unfortunately, this doesn’t give you much flexibility and, of course, requires Flash. But you, dear friend, have a site written in [[PHP]], and the rankings are just XML files. There is a better way.

Looking around on the web, there are some good instructions and recommendations for using PHP 5’s object-oriented XML support. But, as we know, not everyone is using PHP 5. Here’s what I did on my PHP install, which includes the DOM/XML and DOM/XSLT extensions.1

Write your XSL Transformation

The first step is to write your [[XSL Transformation]], or XSLT, a special XML “program” which takes an XML file and reformats it into another XML file. Remember when you learned in Algebra class what a function was? An XSLT defines a function from XML to XML. In our case, we need to take a special proprietary XML file like the this one for my weekly top artists and return some solid XHTML.

Let’s take a look at the XSLT I used: (it helps to take a look at the original XML file at the same time.)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="weeklyartistchart">
<ol>
<xsl:apply-templates select="artist[position() &lt; 6]"/>
</ol>
</xsl:template>
<xsl:template match="artist">
<li><a><xsl:attribute name="href"><xsl:value-of select="./url"/></xsl:attribute><xsl:value-of select="./name"/></a><span><xsl:value-of select="./artist"/></span>: <xsl:value-of select="./playcount"/></li>
</xsl:template>
</xsl:stylesheet>

The full spec description will give you all the juicy details, but you really only need a few basic details (or you can just steal my code). First, we note the <xsl:template match="weeklyartistchart"> and <xsl:template match="artist"> items. Each of these code blocks describe what to do to each <weeklyartistchart> and <artist> nodes, respectively, in the input XML. In the code above, when a <weeklyartistchart> is found, an ordered list is opened and the artist template is applied to the first five (position() &lt; 6) <artist> nodes. In the artist template (<xsl:template match="artist">), the script takes each <artist> and prints a list item with the name of the artist, with a link to the url given in the original <artist>’s <url> subnode.

Process the XML with your XSLT

Once your XSLT is written, save it to a file, like last.fm.xml. Now we’ll use the DOM/XSLT extension and apply this XSLT file to the live weekly artist chart XML file from last.fm. Here’s the code I used:

$chartxml = domxml_open_file("/weeklyartistchart.xml");
$xslt = domxml_xslt_stylesheet_file("last.fm.xsl");
$charthtml = $xslt->process($chartxml);
echo $charthtml->dump_mem();

The code is pretty self explanatory—just four lines: 1. open the remote XML file using domxml_open_file, 2. open the stylesheet (XSL transformation), 3. apply the stylesheet to the remote XML file, and 4. echo the output.

That’s it! You can see the results here on my music page.


  1. To see if these instructions will work for you, check your phpinfo for the lines “DOM/XML enabled” and “DOM/XSLT enabled”. If the items aren’t even showing up, you’re out of luck. :( There are, however, other comparable methods to process XML and XSLT in PHP 4.
    dom/xml check 

Survival Tips for Visiting Taiwan

Friday, November 16th, 2007

As my family and Bailey are coming to visit me soon, I decided to write up some basic survival tips for visiting Taiwan. While most aspects of living in Taiwan are very similar to the US or Japan, here are some things to keep in mind:

  1. Don’t flush the toilet paper in the toilets. For this reason, almost every bathroom here has a trash basket. Also, keep some tissue paper on you as most public restrooms do not provide paper.

  2. Don’t drink the tap water. I don’t know what happens if you do, as I haven’t tried, but I’m not planning on it. The good news is that most public buildings and many other establishments have drinking water machines. Bring a good water bottle.1

  3. If you’re visiting northern Taiwan in the winter, bring some raingear. Sure, you’ll probably buy a few umbrellas (I’m on number three), but rain coats are pretty useful too. You can also be really Taiwanese by wearing your raincoat backwards.2

  4. Taiwan is big on recycling. Luckily, though, in most places there’s just a trash can (垃圾桶, pronounced lèsètǒng as opposed to the Mainland lājītǒng) and a recycling bin… in general, most paper and plastic containers that don’t have food waste can be recycled. Plastic bags and wrapping cannot be.

  5. Keep all your receipts. Taiwan has a receipt lottery (formally the Uniform-Invoice Prize). While you can claim it as a foreign visitor with a valid visa, as the winning numbers are released about a couple months after each time period ends, you’ll probably just want to give the receipts to me. ^^
    IMG_9844

  6. Oh, and cold tea is sweetened by default. This freaked me out when I first tried some. Just a warning.

Of course, if you’re not planning to visit me yet but have time between late-January and mid-February (my Chinese New Year break), let me know. Let’s talk.


  1. I love my Nalgene

  2. This most likely comes from everyone riding scooters. 

Updating your zenphoto theme for zenphoto 1.1

Sunday, November 4th, 2007

I use zenphoto as the backend to my photos section with a custom theme to hook into my site’s navigation and such. I chose zenphoto for my website a year ago based on it’s main strength: simplicity. It does much less than the competition, but it does what I need it to do—for the most part. It’s a fantastic bare-bones mysql/php photo gallery option.

Since then, though, I (along with many others) have been slightly disappointed by the lack of development in the promising project, without having the time or energy to pitch in myself. Such is life. But now the wait is over: Zenphoto 1.1 is out.

Zenphoto 1.1, I believe, does a good job balancing this tradition of simplicity with some popular new features. Highlights include (there are many) tagging, subalbums, chronological archives, RSS feeds, EXIF support, Google Maps, search, and preliminary video support. Exciting stuff.

As I maintain my own theme, though, some of these new features of course require me to update my theme. Below is my rough guide to editing your theme to take maximum advantage of zenphoto 1.1.

(more…)