Archive for September, 2010

Са́нкт-Петербу́рг

Thursday, September 30th, 2010

Last year, in September of 2009, I traveled to Saint Petersburg, Russia to work with the fine folks at the State Hermitage Museum. I had the good fortune to spend 8 days in Petersburg. While not cooped up in a conference room in the museum, I spent my free time exploring the city. Here are some of my favorite pictures from the trip. You can see the whole set over at Flickr.

Handling Nested CDATA With Builder

Tuesday, September 21st, 2010

As noted by our associates at Atomic Object, XML doesn’t allow for nested<![CDATA[…]]> elements. In the course of rewriting some pieces of code, I developed the following Builder workaround to allow our application to export valid XML by breaking the nested CDATA elements into distinct chunks. When read back in via our Nokogiri-based parser, it concatenates the values automagically, and the end result is clean, valid XML.

Fix code:

module Builder
  class XmlMarkup < XmlBase
 
    def cdata_with_escaping!(text)
      if text =~ /(\]\]>)/
        text.gsub!(/(\]\]>)/, "]]]]><![CDATA[>")
      end
      cdata_without_escaping!(text)
    end
    alias_method_chain 'cdata!', 'escaping'
 
  end
end

Sample output:

>> xml = Builder::XmlMarkup.new(str)
>> xml.cdata!("<![CDATA[Foo bar sna]]>")
>> xml.target!
=> "<![CDATA[<![CDATA[Foo bar sna]]]]><![CDATA[>]]>"  # valid XML!
>> xml.cdata_without_escaping!("<![CDATA[Foo bar sna]]>")
>> xml.target!
=> "<![CDATA[<![CDATA[Foo bar sna]]>]]>" # invalid XML!

Sample parsing with Nokogiri:

>> doc = Nokogiri::XML("<baz><![CDATA[<![CDATA[Foo bar sna]]]]><![CDATA[>]]></baz>")
=> #<Nokogiri::XML::Document:0x825aff3c name="document" children=[#<Nokogiri::XML::Element:0x825afc1c name="baz" children=[#<Nokogiri::XML::CDATA:0x825af99c "<![CDATA[Foo bar sna]]>">]>]>
>> doc.css('baz').first.content
=> "<![CDATA[Foo bar sna]]>"

Tracking Drupal User Registrations by Date

Friday, September 10th, 2010

Today I wanted to graph the number of registrations recorded by a Drupal site grouped by date. Drupal stores all account data in the users table. To identify accounts that are registered and verified, I queried with “login != 0″ in my WHERE clause, e.g.

SELECT COUNT(*) FROM user WHERE login != 0;

Since Drupal stores all dates as PHP-style Unix timestamps, e.g. 1284157128, I needed to convert those into a form that MySQL understands. I used from_unixtime() to convert the date to a MySQL date type. By casting, I was then able to use the column in a GROUP BY clause, yielding my result:

SELECT COUNT(*), DATE(FROM_UNIXTIME(created)) as created_date
FROM users WHERE login != 0 GROUP BY created_date;

Which produced results just as I wanted them:

Blue State

Wednesday, September 1st, 2010

Exchange while taking Rufus for a walk:

Man on street: That’s a great dog there, a boxer, right?

Me: Yep!

Man: Aw, good dogs aren’t they?

Me: Yeah, he’s pretty stupid but he does love life.

Man: Must be a Republican!

Zing!