In this post i would like to sum up my thought about using pure HTML files as fully functional templates. I’ve examined this pattern for about a year and i have to say it creates real order in application structure and lifecycle.
Read More »

When some project doesn’t use SVN or any other version-control system you have to download things manually. I don’t have to say that nobody wants to do this, so what can you do to not do it ? You can simulate yourself doing it…

Example below downloads latest release of madwifi branch with new HAL (which i need for my WiFi adapter).

#!/usr/bin/php
<?php
require('phpQuery.php');
$url = 'http://snapshots.madwifi-project.org/madwifi-hal-0.10.5.6/';
$browser = null;
$browserCallback = new CallbackReference($browser);
phpQuery::browserGet($url, $browserCallback);
if ($browser)
  $browser->find('table tr')->slice(-2, -1)
    ->downloadTo('/home/bob/Others/libs');
?>

Now, to get latest release all i need is to run script above within terminal. One missing thing is checking if anything has changed but i leave it to you to resolve.

For files which names doesn’t change you can just use wget, like so:

wget 'http://host.net/somefile.zip' -O new-name.zip

jQuery besides achieving such successes as being used by Google or Micro$soft also has ports to other major languages. Most of them are designed to be server-side what opens doors for new uses to the library.

jQuery ports to other languages:

Below some snippets showing each implementation in few lines.

PHP


foreach($doc['ul > li'] as $li) {
  pq($li)->addClass('my-new-class');
      ->filter(':last')
        ->addClass('last-li');
}
$doc['ul > li:last']
  ->addClass('last-li');

Ruby


# load the RedHanded home page
doc = Hpricot(open("http://redhanded.hobix.com/index.html"))
# change the CSS class on links
(doc/"span.entryPermalink").set("class", "newLinks")

Perl


pQuery("http://google.com/search?q=pquery")
  ->find("h2")
    ->each(sub {
      my $i = shift;
        print $i + 1, ") ", pQuery($_)->text, "\n";
      });

ActionScript


// add enterFrame event handler
$(stage).enterFrame(function(event:Event):void {
  $("RoundRect").attr("color", function(...args):uint {
    return Math.random() * 0xffffff;
  });
});

List comes from phpQuery wiki page. Do you know any other ports ? Share it in comments.

Since leaving windows behind (yey!) I couldn’t find decent music player (which won’t frustrate me). Foobar was my choice for a years. Tried many after that like Rhythmbox, Amarok (3 and 4), Listen, Sonata (MPD client), Banshee (<1.0 and 1.0) and others… Finally yesterday I found advertised-as-itunes-alternative Songbird - XUL/Mozilla powered music player! Main features are:

  • Addons
    Just like in Firefox. There is almost 300 of them right now.
  • Shoutcast client
    This is most important feature for me as I listen to radio most of the time (Secret Agent is my fav pick)
  • Webrowser with tabs
    Web is everywhere in this player.

I feel so foobar-like when using it. For those of you who like iTunes, there is (of course) iTunes theme.

When PDT 2.0 is still slow and DLTK 1.0 hasn’t reached final milestone, Zend guys are using unreleased PDT 1.0.5 in their Zend Studio 6.1. Most important thing is ZS is based on Eclipse 3.4 (Ganymade) and prior PDT 1.0.x builds worked only with 3.3 (Europa).

You can get it in 2 ways - first is to compile it from CVS according to these instructions and the second is just to download Zend Studio 6.1 and copy /features/org.eclipse.php* and /plugins/org.eclipse.php* to Eclipse 3.4 installation.

Update (17.11.2008):
PDT team have officially released 1.0.5 build for Eclipse 3.4. You can get it directly from download page.

It’s year old but found it today on benlog.org. I’m verry happy since i’ve been working on project providing similar chains and now i can use well-thought Rails equivalent. Yet i will still write my own implementation as it differs in many ways.

require_once('ActiveSupport.php');
// Outputs "14th"
_(14)->ordinalize();
// Returns number of bytes in 7.3 megabytes
_(7.3)->megabytes();
// Returns true
_("an example sentence")->endsWith("sentence");

I really liked the idea of ‘_‘ suffix to change returned type. See lots of great examples in Rails Rubyisms Advent.

phpQuery can connect to Google Code’s wiki editing form, authorize itself, replace page contents and finally submit the form.

// declare main variable
$pq = null;
// create dummy document as start point
phpQuery::newDocument('
<div/>')
// authorize your google account
	->script('google_login')
// redirect authorized XHR component to googlecode's wiki form
	->location('http://code.google.com/p/phpquery/w/edit/test')
// save result as $pq, althought we could continue the chain
// but it would break in case of error...
		->toReference($pq);
if ($pq) {
// read about CallbackReference later in this post...
	$pq->WebBrowser(new CallbackReference($pq))
		->find('textarea:first')
			->val('lorem ipsum')
			->parents('form')
// first submit is Preview, so fire up second
				->find(':submit:eq(1)')
// triggering submit event thought input[type=submit]:click
// is a way to choose which submit is send
					->click();
	if ($pq) {
// print without <script> tags
		print $pq->script('safe_print');
	}
}

You can notice hot new feature - new CallbackReference($pq). Such callback sets first callback parameter to passed variable, by reference. Such pattern works with all methods accepting callbacks. Thanks to that, we can use if statements instead of function callbacks. In above example, CallbackReference object is called when click event is triggered.

Presented code snippet makes use of new Script plugin, particularly google_login.

Now it can be combined with automated XML documentation to wiki script, but this maybe for jQuery 1.3

Scripts plugin is an easy file includer. Each script file have 4 variables in scope:

  • $self Represents $this
  • $params Represents parameters passed to script() method (without script name)
  • $return If not null, will be used as method result
  • $config Content of __config.php file

Possible use cases

  • Authorizations
  • Custom Chains
  • Simple phpQuery plugins
  • Sort functions
  • Website APIs
  • Event binding groups

Example

$return = $self->find($params[0]);

Actually there is only one script which is Google Login. It doesn’t support GMail yet, unfortunately.

I’ve done what i was thinking about for some time. Terminal-firendly phpQuery CLI interface. Took about 10 minutes of coding… Works like this:


phpquery http://code.google.com/p/phpquery/downloads/list --find '.vt.col_4 a:first' --contents

This will return number of downloads latest phpQuery release file. Notice there is no need to quote url in any way. I was very happy with this so i’ve added callback support in text() and htmlOuter() methods, like so:


phpquery http://code.google.com/p/phpquery/downloads/list --find '.vt.col_4 a:first' --text strip_tags trim

When i had all stuff working, i’ve used it straight away to scrap forums and categories lists from old IPB v1.x. I’ve piped phpQuery result with sed, filtering final output.

// Fetch categories
./phpquery http://forum.wiadomosc.info/ --find '.maintitle a' | sed -r 's/^.*?c=([0-9]+).+?>(.+?)<.*$/\1 - \2/g'
// Fetch forums
./phpquery http://forum.wiadomosc.info/ --find '.row4 b:has(a)' | sed -r 's/^.*?forum=([0-9]+)[^>]*>([^<]*)<.*$/\1: \/\/ \2/g'

Just an example showing how easy it is to implement CLI in PHP scripts. Sandbox.php file:

#!/usr/bin/php
<?php
var_dump($argv);
?>

./sandbox.php param1 –param2 param3 param4 –param5 -p 6 -fbi

array(9) {
[0]=>
string(13) "./sandbox.php"
[1]=>
string(6) "param1"
[2]=>
string(8) "--param2"
[3]=>
string(6) "param3"
[4]=>
string(6) "param4"
[5]=>
string(8) "--param5"
[6]=>
string(2) "-p"
[7]=>
string(1) "6"
[8]=>
string(4) "-fbi"
}