Tobiasz Cudnik

Archive for October, 2008

Songbird – Firefox for your music

In Software on 22.10.2008 at 10:27

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 SongbirdXUL/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.

Eclipse PDT 1.0.5 – Zend’s hidden release

In Software on 22.10.2008 at 10:04

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.

Rails’ ActiveSupport ported to PHP

In The Net on 05.10.2008 at 17:16

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 edits it’s own wiki

In phpQuery on 05.10.2008 at 3:05

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  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 for phpQuery

In phpQuery on 05.10.2008 at 3:05

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.

Web Scraping with cli version of phpquery piped with sed

In Web Scraping, phpQuery on 05.10.2008 at 3:05

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'

PHP in CLI using $argv

In Snippets on 05.10.2008 at 3:05

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"
}