A good programming language should have features that make the kind of people who use the phrase “software engineering” shake their heads disapprovingly.

Paul Graham

svn patches from git

A while ago I started using Mark Jaquith’s gitified WordPress for contributing to Core.

The trouble is that the patches generated by git diff aren’t exactly the same as the ones generated by svn. I’ve tried several cooky solutions until I found this one.

So here’s the easies way to create a patch from a git repository, to be aplied to an svn repository:

git diff --no-prefix > ~/some-feature.diff

To avoid typing --no-prefix each time, you can enable it by default:

git config --global diff.noprefix true

And then, to apply it:

patch -p0 < ~/some-feature.diff

The important thing is that you use the exact same command you would use to apply a patch created from svn.

OS Mess

I just realised that every major desktop operating system is scrambling to reinvent itself.

Microsoft is working on Windows 8, which looks like will come with a HTML5 component.

Apple recently launched OS X Lion, which started integrating aspects of iOS.

On the Linux front, Ubuntu’s last release switched to Unity, while all the rest of the distros adopted Gnome 3.

All of these transitions have received mixed responses, since none of them are fully backed yet.

Interesting times.

Plugin Dependencies: Version 1.1

The main feature added in this version is the “Provides:” header. This allows virtual packages to be defined:

/*
Plugin Name: Lib X
Provides: lib-x
*/

Now, dependant plugins can specify ‘lib-x’ as a dependency:

/*
Plugin Name: Cool Plugin
Depends: lib-x
*/

The first advantage is that dependencies are no longer tied to plugin file paths.

More importantly, you can now have dependency alternatives: there can be more than one plugin that provides the same functionality:

/*
Plugin Name: Lib Alt
Provides: lib-x
*/

When you activate Lib Alt, Lib X will automatically be deactivated.

Disciples vs. Peers

I don’t want to help random people do great work, I want to TRY to do great work myself, and CELEBRATE other people who are ALREADY doing it.

Hugh MacLeod

And that is why I stopped spending time on wp-hackers and on WPSE.

svn tagging is a joke

I keep reading the phrase “Git isn’t better than Subversion, it’s just different”.

I’m sure a few years ago people said “SVN isn’t better than CVS, it’s just different”.

Anyway, here’s one key aspect that shows how primitive svn is:

Subversion doesn’t actually have tags or branches. It only has folders.

The usual branches and tags directories you see in most svn repositories are not special. They’re just a naming convention.

After you “tag” a release (basically copy the trunk folder into a new folder in tags), you can modify it at will and Subversion won’t complain.

Compare with Git or Mercurial, where a tag is a read-only pointer to a specific changeset.

The same with branches: in svn you create an entire copy of the trunk folder, while in git you just create a pointer which advances as you make revisions.

If you’re interested in learning more about git, I recommend reading the Pro Git book. Also, Hg Init is an awesome introduction to Mercurial and to distributed version control systems in general.

Posts 2 Posts: Version 0.8

UI Changes

With the WP 3.2 admin refresh, the connection box UI also received an overhaul:

A significant enhancement in this version is the ability to create draft posts from the connection box. The idea is to be able to make the connections first and then write the content. Hat tip to Oren Kolker.

Additionally, when a post is not published, you will see it’s status. Props Michael Fields for the suggestion.

API Changes

p2p_each_connected() has been revamped to, hopefully, make it easier to use. There’s a detailed tutorial on the wiki:

http://github.com/scribu/wp-posts-to-posts/wiki/Looping-The-Loop

Also, there’s a new function called p2p_list_posts() which takes a WP_Query object or an array of posts as the first argument and outputs a simple unordered list of links to those posts.

Finally, there are 3 new query vars that can be used to get connected posts ordered by a particular connection field. More details on the wiki:

http://github.com/scribu/wp-posts-to-posts/wiki/Connection-ordering

Value, Identity, State

As web developers, one thing that we don’t have to worry about is concurrency. We don’t have threads, we don’t care about multiple cores etc.

We do have to worry about asynchronicity, because of JavaScript, but that’s a fun challenge, in my experience.

I came across a fascinating talk from the creator of the Clojure language, Rich Hickey, which is called Persistent Data Structures and Managed References.

In the first part of the talk, he defines and clarifies the concept of state, by first defining the concepts of identity and value.

Then he goes on to describe how Clojure represents composite objects such as hash maps and how it’s able to keep them immutable in an efficient manner, by using persistent data structures.

It’s well worth the watch, even if you might not end up ever writing a single line of code in Clojure.

Reflection on filters

Filters are the most awesome thing about WordPress, code wise. 1

The Problem

Passing a class method to a filter, however, is not so awesome.

Say you have this class for manipulating WP_Query:

class My_Plugin {
 
	function parse_query( $wp_query ) {
		...
	}
 
	function posts_clauses( $clauses, $wp_query ) {
		...
	}
 
	function the_posts( $posts, $wp_query ) {
		...
	}
}

To hook it up to the appropriate filters, you have to write this code:

add_action( 'parse_query', array( 'My_Plugin', 'parse_query' ) );
add_filter( 'posts_clauses', array( 'My_Plugin', 'posts_clauses' ), 10, 2 );
add_filter( 'the_posts', array( 'My_Plugin', 'the_posts' ), 10, 2 );

Ugh… so much duplication! What a wonderful world it would be if we didn’t have to write all that boilerplate code each time.

The Solution

PHP has this neat feature called reflection. With it, you can reverse-engineer the properties and methods of a class or object.

So, this is exactly what the scbHooks class does. Given a class or object, it hooks all the public methods to filters of the same name. It also passes the appropriate number of arguments, by looking at the method signature.

So, instead of the boilerplate code above, you would just write:

scbHooks::add( 'My_Plugin' );

Ah… DRYness restored.

If you change your mind, you can remove the filters, just as easily:

scbHooks::remove( 'My_Plugin' );

Limitations

That’s pretty swell, but there are some drawbacks:

  • priority is always set to 10
  • impossible method names. For example: load-edit.php
  • can’t have two methods hooked to the same filter

I use scbHooks more like a starter kit, with which I can rapidly prototype a solution. Once I hit one of the above limitations, I abandon ship:

scbHooks::debug( 'My_Plugin' );

This method will actually generate the boilerplate source code, which you can paste into the plugin and completely forget about scbHooks.

The Extended Solution

Yitzi, a commenter, had the fabulous idea to set the priority via doc comment. I just modified the syntax a little and also added the ability to change the filter name:

class My_Plugin {
 
	/**
	 * @hook: wp_ajax_my-action
	 * @priority: 11
	 */
	function ajax_handler() {
		// awesome code
	}
}
 
scbHooks::debug( 'My_Plugin' );
 
// Result: add_filter( 'wp_ajax_my-action',  array( 'My_Plugin', 'ajax_handler' ), 11, 0 );

This effectively solves all the drawbacks I mentioned earlier. As such, I don’t think I’ll be abandoning ship so soon after all.

The Code

By the way, scbHooks is now a part of scbFramework.

Notes:

  1. Actions are just filters in disguise.