<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>Derick Rethans - tag: opensource</title>
    <link>http://derickrethans.nl/feed-opensource.xml</link>
    <description>This feed shows the latest 15 items with the tag opensource</description>
    <language>en-us</language>
    <copyright>All rights reserved - Derick Rethans</copyright>
    <managingEditor>derick@derickrethans.nl (Derick Rethans)</managingEditor>
    <pubDate>Mon, 08 Aug 2011 10:51:02 +0000</pubDate>
    <lastBuildDate>Mon, 08 Aug 2011 10:51:02 +0000</lastBuildDate>
    <generator>eZ Components Feed dev (http://ezcomponents.org/docs/tutorials/Feed)</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>60</ttl>
    <item>
      <title>Valgrinding shared modules</title>
      <link>http://derickrethans.nl/valgrinding-shared-modules.html</link>
      <description>&lt;div class="article"&gt;
  &lt;div class="body"&gt;
    &lt;div class="articleListItem"&gt;
      &lt;h1&gt;&lt;a name="valgrinding_shared_modules"/&gt;Valgrinding shared modules&lt;/h1&gt;
      &lt;dl class="head"/&gt;
      &lt;div class="articleMetaData"&gt;
        &lt;div class="location"&gt; London, UK&lt;/div&gt;
        &lt;div class="date"&gt;Monday, August 8th 2011, 09:56 BST&lt;/div&gt;
      &lt;/div&gt;
      &lt;p&gt;Over the past year I've been writing various &lt;a href="http://derickrethans.nl/available-for-php-extension-writing.html"&gt;commercial&lt;/a&gt; (more about that later) and open source PHP extensions (such as &lt;a href="https://github.com/derickr/quickhash"&gt;QuickHash&lt;/a&gt;, more about that later too). Most of the time they are shared extensions that are not part of PHP. While testing whether I correctly free all memory with &lt;a href="http://valgrind.org/"&gt;Valgrind&lt;/a&gt;, I ran into the issue where I couldn't see the stack frames of where the memory leaks occurred in the extensions, and once I even ran into a &lt;a href="https://bugs.kde.org/show_bug.cgi?id=277045"&gt;Valgrind bug&lt;/a&gt;. The reason why Valgrind could not show the function names belonging to the stack frames is because PHP had already unloaded the shared extensions from memory.&lt;/p&gt;
      &lt;p&gt;I often found myself compiling the extensions into PHP statically so that there was nothing to unload for PHP, but that was becoming annoying. So instead I added a &lt;a href="http://news.php.net/php.cvs/65492"&gt;patch&lt;/a&gt; to PHP that prevents the shutdown sequence from actually unloading the modules. You can trigger this behaviour by setting the &lt;code&gt;ZEND_DONT_UNLOAD_MODULES&lt;/code&gt; environment variable before running your script:&lt;/p&gt;
      &lt;pre&gt;# export ZEND_DONT_UNLOAD_MODULES=1
# valgrind --leak-check=full --show-reachable=yes php -r 'echo "Hello World\n";'

&lt;/pre&gt;
      &lt;p&gt;Without &lt;code&gt;ZEND_DONT_UNLOAD_MODULES&lt;/code&gt; exported, my Valgrind output shows a block like:&lt;/p&gt;
      &lt;pre&gt;# unset ZEND_DONT_UNLOAD_MODULES
# valgrind --leak-check=full --show-reachable=yes php -r 'echo "Hello World\n";'
...
==25829== 8 bytes in 1 blocks are indirectly lost in loss record 2 of 21
==25829==    at 0x4C25E84: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==25829==    by 0xCE440DC: ???
==25829==    by 0xCE44316: ???
==25829==    by 0xCE44368: ???
==25829==    by 0xCBEE55F: ???
==25829==    by 0xCBD3F87: ???
==25829==    by 0x949A85: zend_activate_modules (zend_API.c:2285)
==25829==    by 0x8B5EBC: php_request_startup (main.c:1491)
==25829==    by 0xA83D60: do_cli (php_cli.c:954)
==25829==    by 0xA84F7B: main (php_cli.c:1356)
...

&lt;/pre&gt;
      &lt;p&gt;And with &lt;code&gt;ZEND_DONT_UNLOAD_MODULES&lt;/code&gt; exported, it shows instead:&lt;/p&gt;
      &lt;pre&gt;# export ZEND_DONT_UNLOAD_MODULES=1
# valgrind --leak-check=full --show-reachable=yes php -r 'echo "Hello World\n";'
...
==25824== 8 bytes in 1 blocks are still reachable in loss record 2 of 30
==25824==    at 0x4C25E84: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==25824==    by 0xCE440DC: event_base_priority_init (in /usr/lib/libevent-1.4.so.2.1.3)
==25824==    by 0xCE44316: event_base_new (in /usr/lib/libevent-1.4.so.2.1.3)
==25824==    by 0xCE44368: event_init (in /usr/lib/libevent-1.4.so.2.1.3)
==25824==    by 0xCBEE55F: zm_activate_http_request_pool (http_request_pool_api.c:58)
==25824==    by 0xCBD3F87: zm_activate_http (http.c:373)
==25824==    by 0x949A85: zend_activate_modules (zend_API.c:2285)
==25824==    by 0x8B5EBC: php_request_startup (main.c:1491)
==25824==    by 0xA83D60: do_cli (php_cli.c:954)
==25824==    by 0xA84F7B: main (php_cli.c:1356)
...

&lt;/pre&gt;
      &lt;p&gt;As you can see all the symbols are now nicely resolved. This patch is part of the upcoming PHP 5.4, but applies to &lt;a href="http://derickrethans.nl/files/php-zend-unload-modules-20110808.diff.txt"&gt;PHP 5.2 and PHP 5.3&lt;/a&gt; as well.&lt;/p&gt;
      
      
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
</description>
      <guid>201108080956</guid>
      <pubDate>Mon, 08 Aug 2011 08:56:00 +0000</pubDate>
    </item>
    <item>
      <title>Translating Twitter, part 2</title>
      <link>http://derickrethans.nl/translating-twitter-part2.html</link>
      <description>&lt;div class="article"&gt;
  &lt;div class="body"&gt;
    &lt;div class="articleListItem"&gt;
      &lt;h1&gt;&lt;a name="translating_twitter_part_2"/&gt;Translating Twitter, part 2&lt;/h1&gt;
      &lt;dl class="head"/&gt;
      &lt;div class="articleMetaData"&gt;
        &lt;div class="location"&gt; London, UK&lt;/div&gt;
        &lt;div class="date"&gt;Tuesday, May 31st 2011, 09:04 BST&lt;/div&gt;
      &lt;/div&gt;
      &lt;p&gt;A while ago I wrote in &lt;a href="http://derickrethans.nl/translating-twitter.html"&gt;an article&lt;/a&gt; about translating tweets in my client &lt;a href="http://derickrethans.nl/projects.html#haunt"&gt;Haunt&lt;/a&gt;. For the translating itself I was using the Google Translate API, which has sadly be &lt;a href="http://googlecode.blogspot.com/2011/05/spring-cleaning-for-some-of-our-apis.html"&gt;deprecated&lt;/a&gt;. Evil after all I suppose.&lt;/p&gt;
      &lt;p&gt;I've now rewritten my translation code to use the &lt;a href="http://www.microsoft.com/web/post/using-the-free-bing-translation-apis"&gt;Bing Translation APIs&lt;/a&gt; instead. You need to register an API key (see &lt;a href="http://www.bing.com/developers/appids.aspx%29"&gt;http://www.bing.com/developers/appids.aspx)&lt;/a&gt; to be able to use the APIs. The APIs that I am using are fairly simple though.&lt;/p&gt;
      &lt;p&gt;For a simple translation, requesting &lt;a href="http://api.microsofttranslator.com/V2/Http.svc/Translate?appId=%5Byourappid%5D&amp;to=en&amp;text=%5Byourtext%5D"&gt;http://api.microsofttranslator.com/V2/Http.svc/Translate?appId=[yourappid]&amp;to=en&amp;text=[yourtext]&lt;/a&gt; is all you need to do. It will auto-detect the language for you as well.&lt;/p&gt;
      &lt;p&gt;However, it does not return the detected language, so I had to resort to using two requests in order to reimplement the same functionality that I had before with the Google APIs. I also found that it was easier to use the Http and not the Ajax variant of the API. It requires using SimpleXML to get to the data, but at least you do not have to fight with the &lt;a href="http://en.wikipedia.org/wiki/Byte-order_mark"&gt;BOM&lt;/a&gt; (Byte-order mark) and quoting.&lt;/p&gt;
      &lt;p&gt;The full code looks like:&lt;/p&gt;
      &lt;pre&gt;&lt;?php
$apiBase = 'http://api.microsofttranslator.com/V2/Http.svc/';
$appId = 'yourappid';
$text = urlencode( 'Een hoge boom vangt veel wind' );

$language = (string) simplexml_load_string(
        file_get_contents(
                "{$apiBase}/Detect?appId={$appId}&amp;text={$text}"
        )
);

$inEnglish = (string) simplexml_load_string(
        file_get_contents(
                "{$apiBase}/Translate?appId={$appId}&amp;text={$text}&amp;to=en"
        )
);

var_dump( $language, $inEnglish );
?&gt;

&lt;/pre&gt;
      &lt;p&gt;with as output:&lt;/p&gt;
      &lt;pre&gt;string(2) "nl"
string(34) "A high tree catches a lot of wind."


&lt;/pre&gt;
      
      
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
</description>
      <guid>201105310904</guid>
      <pubDate>Tue, 31 May 2011 08:04:00 +0000</pubDate>
    </item>
    <item>
      <title>Short URLs</title>
      <link>http://derickrethans.nl/short-urls.html</link>
      <description>&lt;div class="article"&gt;
  &lt;div class="body"&gt;
    &lt;div class="articleListItem"&gt;
      &lt;h1&gt;&lt;a name="short_urls"/&gt;Short URLs&lt;/h1&gt;
      &lt;dl class="head"/&gt;
      &lt;div class="articleMetaData"&gt;
        &lt;div class="location"&gt; London, UK&lt;/div&gt;
        &lt;div class="date"&gt;Tuesday, February 22nd 2011, 09:43 GMT&lt;/div&gt;
      &lt;/div&gt;
      &lt;p&gt;When using twitter, or other mobile devices where space is a premium, it is quite common to use shortened URLs. Many people will not be keen on typing &lt;a href="http://www.openstreetmap.org/?lat=51.5073&amp;lon=-0.1278&amp;zoom=14&amp;layers=M"&gt;http://www.openstreetmap.org/?lat=51.5073&amp;lon=-0.1278&amp;zoom=14&amp;layers=M&lt;/a&gt;, and will prefer something like &lt;a href="http://osm.org/go/euu4gY@w-"&gt;http://osm.org/go/euu4gY@w-&lt;/a&gt; instead &lt;a href="http://derickrethans.nl#_footnote_0_1" class="footnote"&gt;1&lt;/a&gt;.&lt;/p&gt;
      &lt;p&gt;The URLs for my articles are automatically generated from the title. There is a little function that uses &lt;a href="http://derickrethans.nl/projects.html#translit"&gt;translit&lt;/a&gt;'s &lt;em&gt;transliterate()&lt;/em&gt; function to ASCII-ify the titles. It transforms for example "pfcongrez, P2P Конференция, php|tek, PHP Vikinger, DPC and eZ Conference and Awards" to "pfcongrez-p2p-konferenciya-phptek-php-vikinger-dpc-and-ez-conference-and-awards" with code similar to:&lt;/p&gt;
      &lt;pre&gt;$titleShort = transliterate( $fullTitle,
    array(
        'cyrillic_transliterate', 'lowercase_latin',
        'normalize_ligature', 'diacritical_remove',
        'normalize_punctuation', 'remove_punctuation',
        'spaces_to_underscore', 'compact_underscores'
    ),
    'utf8', 'us-ascii'
);

&lt;/pre&gt;
      &lt;p&gt;However, &lt;a href="http://derickrethans.nl/pfcongrez-p2p-konferenciya-phptek-php-vikinger-dpc-and-ez-conference-and-awards.html"&gt;http://derickrethans.nl/pfcongrez-p2p-konferenciya-phptek-php-vikinger-dpc-and-ez-conference-and-awards.html&lt;/a&gt; is hardly a short URL. What we want is something short and descriptive. First of all, I thought it would work to generate short URLs automatically, following the following algorithm:&lt;/p&gt;
      &lt;ol&gt;
        &lt;li&gt;
          &lt;p&gt;Strip out all chars that are not &lt;code&gt;A-Z&lt;/code&gt;, &lt;code&gt;a-z&lt;/code&gt;, &lt;code&gt;0-9&lt;/code&gt;, &lt;code&gt;.&lt;/code&gt;, space and &lt;code&gt;-&lt;/code&gt;.&lt;/p&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;p&gt;Split the title up in words, splitting on space.&lt;/p&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;p&gt;If the sanitized URL is shorter than 14 chars: join the words back together with a "-", otherwise:&lt;/p&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;p&gt;Loop over each word, replacing each word with the first letter of the word, unless it is one of the important words (such as "php" or "xdebug").&lt;/p&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;p&gt;Add a marker for the date, to ensure that short URLs are unique. The date marker is generated like:&lt;/p&gt;
          &lt;ol&gt;
            &lt;li&gt;
              &lt;p&gt;Format the date as &lt;code&gt;two digit year&lt;/code&gt;, &lt;code&gt;ISO week&lt;/code&gt; and &lt;code&gt;ISO weekday&lt;/code&gt;&lt;/p&gt;
            &lt;/li&gt;
            &lt;li&gt;
              &lt;p&gt;Convert the created number to base 36 with:&lt;/p&gt;
            &lt;/li&gt;
          &lt;/ol&gt;
          &lt;pre&gt;php -r "echo date_create( '20090410' )-&gt;format( 'yWN' );"
09155

php -r 'echo base_convert("09155", 10, 36), "\n";'
72b

&lt;/pre&gt;
        &lt;/li&gt;
      &lt;/ol&gt;
      &lt;p&gt;Our long title from above, then turns into &lt;code&gt;pp-p-php-vdaecaa-72b&lt;/code&gt;. Although relatively short, it is still not a descriptive URL. To be honest, it's a bit difficult to come up with a short, descriptive URL for this title, so let us examine another one, of a more recent post.&lt;/p&gt;
      &lt;p&gt;The title "64-bit integers in MongoDB" is converted into "6iim-7yp", which is again short, but not very informative; and "Xdebug 2.1 Released" is converted into "xdebug-2.1-7x2", which &lt;em&gt;does&lt;/em&gt; make sense.&lt;/p&gt;
      &lt;p&gt;Because I did not get very good results with automatically converting titles into sort URLs, I decided that for all new articles I will just define my own.&lt;/p&gt;
      &lt;p&gt;Now, once we have the short URLs, they need to be used. Instead of my long main domain &lt;a href="http://derickrethans.nl"&gt;http://derickrethans.nl&lt;/a&gt;, I registered a new one just for short URLs: &lt;a href="http://drck.me"&gt;http://drck.me&lt;/a&gt; to reduce the URL length even more. For the current article, the length of the URL shrinks from 39 to 26 characters. The short URL is also embedded in the HTML source with a &lt;code&gt;rev=canonical&lt;/code&gt; tag and a &lt;code&gt;rel=shortlink&lt;/code&gt; tag, such as:&lt;/p&gt;
      &lt;pre&gt;&lt;link rev="canonical" type="text/html" href="http://drck.me/shrturl-8ju"&gt;
&lt;link rel="shortlink" type="text/html" href="http://drck.me/shrturl-8ju"&gt;

&lt;/pre&gt;
      &lt;p&gt;This tag is meant to provide alternative URLs for the current one. For more information on &lt;code&gt;rev=canonical&lt;/code&gt; see &lt;a href="http://revcanonical.appspot.com/"&gt;http://revcanonical.appspot.com/&lt;/a&gt; or Chris Shiflett's &lt;a href="http://shiflett.org/blog/2009/apr/save-the-internet-with-rev-canonical"&gt;entry&lt;/a&gt;, and for &lt;code&gt;rel=shortlink&lt;/code&gt; see the &lt;a href="http://microformats.org/wiki/rel-shortlink"&gt;microformats&lt;/a&gt; site. The &lt;a href="http://code.google.com/p/shortlink/wiki/Specification"&gt;shortlink specification&lt;/a&gt; also recommends the use of the HTTP &lt;code&gt;Link:&lt;/code&gt; header:&lt;/p&gt;
      &lt;pre&gt;Link: &lt;http://drck.me/shrturl-8ju&gt;; rel=shortlink

&lt;/pre&gt;
      &lt;p&gt;The two tags and the HTTP header make it possible for services that prefer an as short as possible URL to automatically discover a shorter version. Sadly, it doesn't see that the twitter home page does this. I hope they will add support for that in the future. From my side, I am going to implement this in &lt;a href="http://derickrethans.nl/projects.html#haunt"&gt;Haunt&lt;/a&gt;, my twitter client build in PHP-GTK.&lt;/p&gt;
      
      
    &lt;/div&gt;
    &lt;ul class="footnotes"&gt;
      &lt;li&gt;
        &lt;a name="_footnote_0_1"&gt;1&lt;/a&gt;
        &lt;p&gt;This actually uses a very clever form of shortening geo-locations: &lt;a href="http://wiki.openstreetmap.org/wiki/Shortlink"&gt;http://wiki.openstreetmap.org/wiki/Shortlink&lt;/a&gt;&lt;/p&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
&lt;/div&gt;
</description>
      <guid>201102220943</guid>
      <pubDate>Tue, 22 Feb 2011 09:43:00 +0000</pubDate>
    </item>
    <item>
      <title>Debugging Variables</title>
      <link>http://derickrethans.nl/debugging-variables.html</link>
      <description>&lt;div class="article"&gt;
  &lt;div class="body"&gt;
    &lt;div class="articleListItem"&gt;
      &lt;h1&gt;&lt;a name="debugging_variables"/&gt;Debugging Variables&lt;/h1&gt;
      &lt;dl class="head"/&gt;
      &lt;div class="articleMetaData"&gt;
        &lt;div class="location"&gt; London, UK&lt;/div&gt;
        &lt;div class="date"&gt;Thursday, February 10th 2011, 09:12 GMT&lt;/div&gt;
      &lt;/div&gt;
      &lt;p&gt;Sometimes you want to inspect the contents of variables more closely, by looking at its internal properties. For this, PHP has the  &lt;a href="http://php.net/debug_zval_dump"&gt;debug_zval_dump&lt;/a&gt; function.&lt;/p&gt;
      &lt;p&gt;The internal representation of a PHP variable container (called zval), contains the type and value of a variable, but also whether it is a reference and what its refcount is. Due to PHP's copy-on-write policy, one specific zval container can be used by multiple variables at the same time as we will see in a bit.&lt;/p&gt;
      &lt;p&gt;The &lt;code&gt;refcount&lt;/code&gt; field contains how many symbols (variable names) point to a zval, regardless of whether a reference set is used or not. For example, both the following examples will have as end-result: two symbols and one zval:&lt;/p&gt;
      &lt;pre&gt;&lt;?php
$a = 42; // one symbol: 'a'; one zval: int(42)
$b = $a; // two symbols: 'a', 'b'; one zval: int(42)
?&gt;

&lt;?php
$a = 42; // one symbol: 'a'; one zval: int(42)
$b =&amp; $a; // two symbols: 'a', 'b'; one zval: int(42)
?&gt;

&lt;/pre&gt;
      &lt;p&gt;In order for PHP to know the difference, there is an extra flag on the zval: &lt;code&gt;is_ref&lt;/code&gt;, which is set if you assign a variable by reference with &lt;code&gt;=&amp;&lt;/code&gt;. It is important to know which of the situations actually occurred because PHP needs to be able to do the correct thing when a third symbol comes into play. In the first situation, a third symbol can be associated with the same zval &lt;strong&gt;only&lt;/strong&gt; if it is assigned by assignment. If the third symbol is assigned by reference with something like &lt;code&gt;$c =&amp; $b&lt;/code&gt;; then PHP needs to duplicate (split) the zval into two zvals because only symbol &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; are linked, whereas &lt;code&gt;a&lt;/code&gt; is distinct. Without the separation PHP will not be able to know which of the two symbols are referenced (&lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt;), and which one is not part of the reference set (&lt;code&gt;a&lt;/code&gt;).&lt;/p&gt;
      &lt;p&gt;The opposite happens in the second situation. If a third symbol is assigned by reference with something like &lt;code&gt;$c =&amp; $b&lt;/code&gt;; then PHP can just update the &lt;code&gt;refcount&lt;/code&gt; on the zval. If the third symbol is assigned by value with &lt;code&gt;$c =
$b&lt;/code&gt; then PHP needs to split the zval so that &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; still point to the original zval, and &lt;code&gt;c&lt;/code&gt; to the newly created one without having links to either &lt;code&gt;a&lt;/code&gt; or &lt;code&gt;b&lt;/code&gt;.&lt;/p&gt;
      &lt;p&gt;This brings us to the very simple rule for when PHP needs to split upon assignment:&lt;/p&gt;
      &lt;ul&gt;
        &lt;li&gt;
          &lt;p&gt;If the type of assigning (by-value with &lt;code&gt;=&lt;/code&gt; or by-ref with &lt;code&gt;=&amp;&lt;/code&gt;) matches the value of &lt;code&gt;is_ref&lt;/code&gt;: just update &lt;code&gt;refcount&lt;/code&gt; by adding 1.&lt;/p&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;p&gt;If the type of assigning does not match the value of &lt;code&gt;is_ref&lt;/code&gt;: split the zval.&lt;/p&gt;
        &lt;/li&gt;
      &lt;/ul&gt;
      &lt;p&gt;Now let us get back to the original issue: the inspection of the internal structure of variable contents with  &lt;a href="http://php.net/debug_zval_dump"&gt;debug_zval_dump&lt;/a&gt;. When a variable is passed to a function as an argument, an assignment to the function's local scope is made; just like an assignment in a function. Which means that the same splitting rules apply. The following are (mostly) equivalent regarding on what happens to the &lt;code&gt;is_ref&lt;/code&gt; and &lt;code&gt;refcount&lt;/code&gt; values:&lt;/p&gt;
      &lt;pre&gt;&lt;?php
$a = 42;
$b = $a; // assignment in local scope
?&gt;

&lt;/pre&gt;
      &lt;p&gt;and:&lt;/p&gt;
      &lt;pre&gt;&lt;?php
function foo($b)
{
}

$a = 42;
foo($a); // assignment to $b in the function's scope
?&gt;

&lt;/pre&gt;
      &lt;p&gt;PHP's  &lt;a href="http://php.net/debug_zval_dump"&gt;debug_zval_dump&lt;/a&gt; takes a variable as argument for analysis and is thus also bound to the same splitting rules as outlined earlier. This means that's not really well suited for dumping a zval's internal structure as it would always modify the zval. Besides adding 1 to the &lt;code&gt;refcount&lt;/code&gt; field, it could also force a split resulting in unexpected output:&lt;/p&gt;
      &lt;pre&gt;&lt;?php
$a = 42;
debug_zval_dump($a);
?&gt;

&lt;/pre&gt;
      &lt;p&gt;shows:&lt;/p&gt;
      &lt;pre&gt;long(42) refcount(2)

&lt;/pre&gt;
      &lt;p&gt;&lt;a href="http://xdebug.org"&gt;Xdebug&lt;/a&gt; has a similar function to display a zval's internal data: &lt;a href="http://xdebug.org/docs/all_functions#xdebug_debug_zval"&gt;xdebug_debug_zval&lt;/a&gt;. This function does requires not a variable to be passed as its argument, but instead requires a variable &lt;strong&gt;name&lt;/strong&gt; to be passed in. With this, the manipulation of the zval is avoided and the proper values are shown:&lt;/p&gt;
      &lt;pre&gt;&lt;?php
$a = 42;
xdebug_debug_zval('a');
?&gt;

&lt;/pre&gt;
      &lt;p&gt;which shows:&lt;/p&gt;
      &lt;pre&gt;a: (refcount=1, is_ref=0)=42

&lt;/pre&gt;
      &lt;p&gt;This is what is expected, and as bonus, it also shows the &lt;code&gt;is_ref&lt;/code&gt; flag. PHP's &lt;a href="http://bugs.php.net/bug.php?id=53895"&gt;bug #53895&lt;/a&gt; mentions a few issues with the  &lt;a href="http://php.net/debug_zval_dump"&gt;debug_zval_dump&lt;/a&gt; function. Gustavo is quite right to say that passing the variable name by-ref is not going to work, and any form of this function where the variable is passed in is not going to work. Johannes however is also right that passing just the variable name as a string might not be good enough; as you can't directly dump the zval information for for example array keys. There is an Xdebug &lt;a href="http://bugs.xdebug.org/view.php?id=310"&gt;feature request&lt;/a&gt; for this functionality to be implemented however. If implemented, you should be able to debug the &lt;code&gt;foo&lt;/code&gt; array key with something like:&lt;/p&gt;
      &lt;pre&gt;&lt;?php
$a = array('foo' =&gt; 3.1415);
xdebug_debug_zval('a["foo"]');
?&gt;

&lt;/pre&gt;
      &lt;p&gt;Now if I only had some spare time...&lt;/p&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
</description>
      <guid>201102100912</guid>
      <pubDate>Thu, 10 Feb 2011 09:12:00 +0000</pubDate>
    </item>
    <item>
      <title>Translating Twitter</title>
      <link>http://derickrethans.nl/translating-twitter.html</link>
      <description>&lt;div class="article"&gt;
  &lt;div class="body"&gt;
    &lt;div class="articleListItem"&gt;
      &lt;h1&gt;&lt;a name="translating_twitter"/&gt;Translating Twitter&lt;/h1&gt;
      &lt;dl class="head"/&gt;
      &lt;div class="articleMetaData"&gt;
        &lt;div class="location"&gt; London, UK&lt;/div&gt;
        &lt;div class="date"&gt;Wednesday, January 5th 2011, 09:04 GMT&lt;/div&gt;
      &lt;/div&gt;
      &lt;p&gt;
        &lt;em&gt;Note: Google &lt;a href="http://googlecode.blogspot.com/2011/05/spring-cleaning-for-some-of-our-apis.html"&gt;deprecated&lt;/a&gt; the API used in this article. See the &lt;a href="http://drck.me/bing-translate-8nq"&gt;new article&lt;/a&gt; that uses &lt;a href="http://www.microsofttranslator.com/dev/"&gt;Bing Translate&lt;/a&gt; instead.&lt;/em&gt;
      &lt;/p&gt;
      &lt;p&gt;
        &lt;em&gt;Note: I've made an &lt;a href="http://derickrethans.nl#update"&gt;update&lt;/a&gt; on January 6th to this article after a comment by a reader.&lt;/em&gt;
      &lt;/p&gt;
      &lt;p&gt;As the author of &lt;a href="http://twitter.com/xdebug"&gt;Xdebug&lt;/a&gt; I am interested in finding out what people think of it, and whether they have problems or compliments. I've set-up a &lt;a href="http://twitter.com"&gt;twitter&lt;/a&gt; account for Xdebug, &lt;a href="http://twitter.com/xdebug"&gt;@xdebug&lt;/a&gt;, and my twitter client &lt;a href="http://derickrethans.nl/projects.html#haunt"&gt;Haunt&lt;/a&gt; also shows me all tweets with the &lt;a href="http://twitter.com/#%21/search/xdebug"&gt;search term xdebug&lt;/a&gt;.&lt;/p&gt;
      &lt;p&gt;However, sometimes I get tweets in a language I can't read; for example Brazilian Portuguese:&lt;/p&gt;
      &lt;blockquote&gt;
        &lt;p&gt;Debugando aplicações PHP com Xdebug e Eclipse PDT: &lt;a href="http://bit.ly/ffJC4G"&gt;http://bit.ly/ffJC4G&lt;/a&gt;&lt;/p&gt;
        &lt;div class="attribution"&gt;
          &lt;cite&gt; junichi_y&lt;/cite&gt;
        &lt;/div&gt;
      &lt;/blockquote&gt;
      &lt;p&gt;or Japanese:&lt;/p&gt;
      &lt;blockquote&gt;
        &lt;p&gt;@pomu0325 ありがとうございます！このXdebugの書き方と各場所を調べてたんですよ！こんなふうに書くんですね。&lt;/p&gt;
        &lt;div class="attribution"&gt;
          &lt;cite&gt; Ken&lt;/cite&gt;
        &lt;/div&gt;
      &lt;/blockquote&gt;
      &lt;p&gt;Once in a while, I would send these tweets through &lt;a href="http://www.google.com/language_tools?hl=en"&gt;Google's language tools&lt;/a&gt;  but then my friend &lt;a href="http://www.naramore.net/blog"&gt;Elizabeth&lt;/a&gt; tweeted:&lt;/p&gt;
      &lt;blockquote&gt;
        &lt;p&gt;Hey Lazyweb, is there a twitter client that lets me filter tweets by language?&lt;/p&gt;
        &lt;div class="attribution"&gt;
          &lt;cite&gt;  Elizabeth Naramore&lt;/cite&gt;
        &lt;/div&gt;
      &lt;/blockquote&gt;
      &lt;p&gt;Instead of a manual copy and paste in into the language tools, I thought it'd be nice to embed it directly into the client when it is requested.&lt;/p&gt;
      &lt;p&gt;Sadly, tweets don't have a language associated with them, so the first step is to actually find out which language a tweet is in. Google provides a web service called "&lt;a href="http://code.google.com/apis/language/translate/v1/using_rest_langdetect.html"&gt;Language Detect&lt;/a&gt;". To use this service, you only have to query a specific URL containing the text you want to guess the language off. and parse the returned &lt;a href="http://json.org/"&gt;JSON&lt;/a&gt; structure. The Google website has an &lt;a href="http://code.google.com/apis/language/translate/v1/using_rest_langdetect.html#json_snippets_php"&gt;example&lt;/a&gt; which basically boils down to requesting the following URL: &lt;a href="https://ajax.googleapis.com/ajax/services/language/detect?v=1.0&amp;q=Hola%2C%2Bmi%2Bamigo"&gt;https://ajax.googleapis.com/ajax/services/language/detect?v=1.0&amp;q=Hola,+mi+amigo&lt;/a&gt;&lt;/p&gt;
      &lt;p&gt;It returns the following JSON struct:&lt;/p&gt;
      &lt;pre&gt;{
        "responseData":
        {
                "language":"es",
                "isReliable":false,
                "confidence":0.08829542
        },
        "responseDetails": null,
        "responseStatus": 200
}

&lt;/pre&gt;
      &lt;p&gt;If the &lt;code&gt;responseStatus&lt;/code&gt; is 200, then it worked. &lt;code&gt;responseData-&gt;language&lt;/code&gt; contains the found language, and &lt;code&gt;responseData-&gt;isReliable&lt;/code&gt;/&lt;code&gt;responseData/confidence&lt;/code&gt; describe how sure Google is that the language found is actually correct. The larger the text, the easier it is to find out of course. In this case, although the confidence is low, the language is guessed correctly: &lt;code&gt;es&lt;/code&gt;, for Spanish.&lt;/p&gt;
      &lt;p&gt;Now we have the language, we can use another web service from Google to translate the text from the guessed language to our target language which in my case is English. This &lt;a href="http://code.google.com/apis/language/translate/v1/using_rest_translate.html"&gt;Translate&lt;/a&gt; service wants the text and a language pair for translations. Google &lt;a href="http://code.google.com/apis/language/translate/v1/using_rest_translate.html#prereqs"&gt;suggests&lt;/a&gt; you add a &lt;code&gt;key&lt;/code&gt;, and an &lt;code&gt;userip&lt;/code&gt;, but this is not strictly necessary. The language pair has the format &lt;code&gt;source-language-code|destination-language-code&lt;/code&gt;; which is in our case &lt;code&gt;es|en&lt;/code&gt;. The service is again very simple to use as you can see in this &lt;a href="http://code.google.com/apis/language/translate/v1/using_rest_translate.html#json_snippets_php"&gt;example&lt;/a&gt;. It boils again down to requesting an URL, such as: &lt;a href="https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&amp;q=Hola%2C+mi+amigo%21&amp;langpair=es%7Cen"&gt;https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&amp;q=Hola, mi amigo!&amp;langpair=es|en&lt;/a&gt;&lt;/p&gt;
      &lt;p&gt;It returns the following JSON struct:&lt;/p&gt;
      &lt;pre&gt;{
        "responseData":
        {
                "translatedText":"Hello, my friend!"
        },
        "responseDetails": null,
        "responseStatus": 200
}

&lt;/pre&gt;
      &lt;p&gt;If the &lt;code&gt;responseStatus&lt;/code&gt; is 200, then it worked and &lt;code&gt;responseData-&gt;translatedText&lt;/code&gt; contains the translated text.&lt;/p&gt;
      &lt;p&gt;A screenshot shows the translation feature of &lt;a href="http://derickrethans.nl/projects.html#haunt"&gt;Haunt&lt;/a&gt; in action:&lt;/p&gt;
      &lt;img src="http://derickrethans.nl/images/content/haunt.gif" alt="haunt.gif"/&gt;
      &lt;p&gt;You can find the implementation &lt;a href="http://svn.xdebug.org/cgi-bin/viewvc.cgi/twitter/trunk/twitter.php?annotate=34&amp;root=openmoko#l209"&gt;here&lt;/a&gt;. Haunt also has a &lt;a href="http://derickrethans.nl/projects.html#haunt"&gt;project page&lt;/a&gt;.&lt;/p&gt;
      
      
      
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
</description>
      <guid>201101050904</guid>
      <pubDate>Wed, 05 Jan 2011 09:04:00 +0000</pubDate>
    </item>
    <item>
      <title>Analysing Colours in an Image</title>
      <link>http://derickrethans.nl/finding-out-colours.html</link>
      <description>&lt;div class="article"&gt;
  &lt;div class="body"&gt;
    &lt;div class="articleListItem"&gt;
      &lt;h1&gt;&lt;a name="analysing_colours_in_an_image"/&gt;Analysing Colours in an Image&lt;/h1&gt;
      &lt;dl class="head"/&gt;
      &lt;div class="articleMetaData"&gt;
        &lt;div class="location"&gt; London, UK&lt;/div&gt;
        &lt;div class="date"&gt;Thursday, November 25th 2010, 18:24 GMT&lt;/div&gt;
      &lt;/div&gt;
      &lt;p&gt;For a &lt;a href="http://plumwillow.com"&gt;project&lt;/a&gt; that I am working on, I had to figure out which colours are primarily used in an image so that it can be indexed for sorting. Obviously, there is only a small finite set of colours that we are interested in.  This article shows on how I went about analysing an image to see which of those colours were primarily used in the image.&lt;/p&gt;
      &lt;p&gt;First of all, I had to define a palette that I wanted to index on. Then I needed to check the image to see which colours existed, and map those to my finite set of colours. Then we could count them, and select the ones that were used most.&lt;/p&gt;
      &lt;p&gt;Picking the palette of colours wasn't very simple especially because pink and purple are difficult to differentiate, as well as orange and brown. It required a little bit of effort, but we settled on the following colours:&lt;/p&gt;
      &lt;img src="http://derickrethans.nl/images/content/pw-colours.png" alt="pw-colours.png"/&gt;
      &lt;p&gt;Some of the colour names have multiple colour values attached to them; this was so that we can tune the analysing algorithm a little bit to provide better results.&lt;/p&gt;
      &lt;p&gt;In order to match the colours from the image to the palette, I needed to create a mapping algorithm. It's difficult to do this with RGB values because they don't have the natural properties that we humans use for colours. However, the HSV model works a lot better. A HSV colour 'wheel' looks like:&lt;/p&gt;
      &lt;img src="http://derickrethans.nl/images/content/hsv-normal.png" alt="hsv-normal.png"/&gt;
      &lt;p&gt;On the X-axis we have the &lt;a href="http://en.wikipedia.org/wiki/Hue"&gt;hue&lt;/a&gt; — a value that describes &lt;em&gt;which&lt;/em&gt; colour it is on a scale from 0 to 360 (°). On The Y-axis we have both the &lt;a href="http://en.wikipedia.org/wiki/Saturation_%28color_theory%29"&gt;saturation&lt;/a&gt; — a value that describes the intensity of a colour and &lt;a href="http://en.wikipedia.org/wiki/Lightness_%28color%29"&gt;value&lt;/a&gt; — the value describing the lightness of a colour. The respective six blocks on the y-axis correspond with saturation values of 0.0, 0.2, 0.4, 0.6, 0.8 and 1.0, and each of the ten bands of each block with the value values of 0.0, 0.1, 0.2 ... 0.9 and 1.0.&lt;/p&gt;
      &lt;p&gt;Then I cheated and used &lt;a href="http://gimp.org"&gt;The Gimp&lt;/a&gt; to change this image of the colour wheel to an indexed image, using the palette from above. The option for that is: Image -&gt; Mode -&gt; Indexed -&gt; "Use Custom Palette" and then pick your palette. Don't forget to change it back to RGB before you save it though.  The resulting image then looks like:&lt;/p&gt;
      &lt;img src="http://derickrethans.nl/images/content/hsv-normal-palette.png" alt="hsv-normal-palette.png"/&gt;
      &lt;p&gt;The script to draw this palette is:&lt;/p&gt;
      &lt;pre&gt;&lt;?php
require 'colortools.php';

$img = imagecreatetruecolor( 720, 720 );

$hFactor = 2;
$hStep = 15;
$vStep = 0.10;
$vFactor = 120;

for ( $h = 0; $h &lt; 360; $h += $hStep )
{
    for ( $s = 0; $s &lt; 1.01; $s += 0.2 )
    {
        for ( $v = 0; $v &lt; 1.01; $v += $vStep )
        {
            colorComponent::hsvToRgb( $h, $s, $v, $r, $g, $b );
            $c = imagecolorallocate( $img, $r * 255, $g * 255, $b * 255 );
            imagefilledrectangle(
                $img,
                $h * $hFactor, $s * 600 + $v * $vFactor,
                ($h + $hStep) * $hFactor - 1, $s * 600 + ($v + $vFactor) - 1,
                $c
            );
        }
    }
}

imagepng( $img, $argv[1] );
?&gt;

&lt;/pre&gt;
      &lt;p&gt;Each square's coordinates represent a mapping between (rounded) H, S and V values. With a little script, we can generate a mapping table:&lt;/p&gt;
      &lt;pre&gt;&lt;?php
function colorToRgb($color, &amp;$r, &amp;$g, &amp;$b)
{
    $r = ($color &gt;&gt; 16) &amp; 0xFF;
    $g = ($color &gt;&gt; 8) &amp; 0xFF;
    $b = $color &amp; 0xFF;
}

$img = imagecreatefrompng( '/tmp/hsv-normal-palette.png' );

$hFactor = 2;
$vFactor = 12;

echo "&lt;" . "?php\nclass colorMap {\n\tpublic \$map;\n\n";
echo "\tfunction __construct() {\n\t\t\$this-&gt;map = array(\n";

for ( $h = 0; $h &lt; 360; $h += 15 )
{
    for ( $s = 0; $s &lt; 5; $s += 1 )
    {
        for ( $v = 0; $v &lt; 10; $v += 1 )
        {
            $c = imagecolorat( $img, $h * $hFactor, $s * 120 + $v * $vFactor );
            colorToRgb($c, $r, $g, $b);

            printf( "\t\t\t'x%03x%01x%01x' =&gt; '#%02x%02x%02x',\n",
                $h, $s, $v, $r, $g, $b
            );
        }
    }
}

echo "\t\t);\t}\n}\n";
?&gt;

&lt;/pre&gt;
      &lt;p&gt;It would be better if we had the algorithm to convert from a random HSV value to a HSV value matching the closest colour in the palette. That's something for a next stage though.&lt;/p&gt;
      &lt;p&gt;The mapping table is used to find out which colours of our palette are actually part of the image. To sample all pixels in an image would take too much time, so we restrict ourselves to 200x200 samples, making 40000 pixels.&lt;/p&gt;
      &lt;p&gt;For each sampled pixel we:&lt;/p&gt;
      &lt;ul&gt;
        &lt;li&gt;
          &lt;p&gt;find the RGB values&lt;/p&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;p&gt;convert the RGB values to HSV (unless it's transparent, then we skip it)&lt;/p&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;p&gt;normalize the HSV values by rounding the values&lt;/p&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;p&gt;map the rounded value with our mapping key to produce the RGB hex value from our palette&lt;/p&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;p&gt;count how much of each of the RGB hex values we've gotten&lt;/p&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;p&gt;create an array where the number of pixels normalised to each RBG hex value is larger than 5%&lt;/p&gt;
        &lt;/li&gt;
      &lt;/ul&gt;
      &lt;p&gt;The HSV-to-normalized-key algorithm looks like:&lt;/p&gt;
      &lt;pre&gt;$key = sprintf('x%03x%01x%01x',
    max(0, min(345, floor( $h / 15 ) * 15)),
    min(4, floor($s * 5)),
    min(9, floor($v * 10))
);

&lt;/pre&gt;
      &lt;p&gt;To visualise this process, I've created a before and after image:&lt;/p&gt;
      &lt;img src="http://derickrethans.nl/images/content/phonebooth-normal.png" alt="phonebooth-normal.png"/&gt;
      &lt;p&gt;Sadly, as you can see, our typical red phone box isn't quite detected as being red, but rather black and grey. For some reason the conversion from an arbitrary HSV value to a palette colour with The Gimp isn't quite as good as we've hoped. So instead of drawing a correct HSV colour wheel, I decided to boost the colours a little bit away from grey by modifying the S and V values. This caused the following change in the palette-drawing script:&lt;/p&gt;
      &lt;pre&gt;$sR = 1 - ( (1-$s) * (1-$s) );
$vR = 1 - ( (1-$v) * (1-$v) * (1-$v) * (1-$v) );
colorComponent::hsvToRgb( $h, $sR, $vR, $r, $g, $b );

&lt;/pre&gt;
      &lt;p&gt;In a diagram, this looks like:&lt;/p&gt;
      &lt;img src="http://derickrethans.nl/images/content/hsv-corrections.png" alt="hsv-corrections.png"/&gt;
      &lt;p&gt;We use this new algorithm, to redraw a new HSV wheel:&lt;/p&gt;
      &lt;img src="http://derickrethans.nl/images/content/hsv-boost.png" alt="hsv-boost.png"/&gt;
      &lt;p&gt;And with The Gimp create a new palette:&lt;/p&gt;
      &lt;img src="http://derickrethans.nl/images/content/hsv-boost-palette.png" alt="hsv-boost-palette.png"/&gt;
      &lt;p&gt;This new palette we then scan to create the color map out off. With this new color map we re-analyse the image to count our primary image colours. The result is then:&lt;/p&gt;
      &lt;img src="http://derickrethans.nl/images/content/phonebooth-boost.png" alt="phonebooth-boost.png"/&gt;
      &lt;p&gt;Which tells us that red was the most prevalent colour with over 50% of the sampled pixels (13916 + 7822 out of 40000), grey being second with 35% and black with 19%. Brown, orange and green also make up more than 5% of the total amount of sampled pixels.&lt;/p&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
</description>
      <guid>201011251824</guid>
      <pubDate>Thu, 25 Nov 2010 18:24:00 +0000</pubDate>
    </item>
  </channel>
</rss>

