Christians Tagebuch PHP: Determine absolute link URLs (3.7.2013, 18:13)

When parsing HTML and following links, it is necessary to calculate absolute URLs from the href attribute values in and tags.

Link classes

Different types of link classes may occur in an HTML document:

Absolute URL
http://example.org/foo.html
An URL with scheme (protocol), host and path.
Absolute URL without scheme
//example.org/foo.html
The scheme is missing, but host and path are given. The document's protocol has to be used in this case, according to RFC 3986 section 4.2 and section 5.2.2.
Path-absolute URL without host
/path/to/file.html
Scheme, hostname and port are missing - only an absolute path is given.
Relative path
../foo/bar.html
A simple relative path.
Fragment only
#baz
An anchor with a hash sign in front. Links to another section in the same document.

To resolve those URLs, you need both the document URL and the link href value.

Code

Implementing the whole resolving algorithm is tedious, and you don't have to do it yourself. There are several implementations out there.

Net_URL2

PEAR offers the Net_URL2 package. Its resolve() method implements the procedure properly, is unit-tested and has no other dependencies. Example:

resolve('../baz.jpg');
// $abs is 'http://example.org/baz.jpg'
?>

Absolute URL deriver

absolute-url-deriver is a small composer-installable lib for resolving relative URLs.

While this library consists of one file only, it depends on another lib (much larger) that provides URL handling.

Empty URLs

HTML5 allows empty action attributes in

tags. Both libraries listed above cope with that; they return the source URL when the "target" URL is empty.

Base href

HTML documents may have a tag in their head section. When resolving links, you need to use this one instead of the document's URL itself. See my XPath article for more information about extracting attribute values from HTML.

Link
Christians Tagebuch PHP_CodeSniffer + emacs 24: Compilation error (6.5.2013, 08:25)

After ugprading to Ubuntu 13.04, emacs did not highlight compilation results from PHP_CodeSniffer anymore. Instead of a nicely colored log window, had the following in the *Messages* buffer:

Warning: defvar ignored because compilation-error-regexp-alist is let-bound
(No files need saving)
Error during redisplay: (void-variable compilation-error-regexp-alist) [4 times]
Compilation exited abnormally with code 1

A bug report gave me the hint about what to change:

To have the compilation mode variables globally available, I need to require the module in global scope, not in the scope of my phpcs-compilation definition.

So after adding (require 'compile) before (defun phpcs() .. in my .emacs file, compilation works properly again.

Link
PHP_CodeSniffer 1.4.5 and 1.5.0RC2 released (3.4.2013, 22:55)

PHP_CodeSniffer versions 1.4.5 and 1.5.0RC2 have just been uploaded to PEAR and are now available to install. Version 1.4.5 is primarily a bug fix release although there are a few new sniffs and sniff settings that some developers may find useful. In addition to these changes 1.5.0RC2 contains big...

Link
Christians Tagebuch PEAR: Search packages in all channels using Pyrus (8.2.2013, 17:02)

Our old and trusted PEAR Installer supports multiple package sources, called "channels", since version 1.4.0 - which was released in 2005. You can host a channel yourself using one of the channel server applications. The most popular one is probably Pirum.

One of the problems with such a distributed network is finding the channel that provides the package you are looking for.

Pyrus, the PHP 5.3+ PEAR installer, has a solution for that:

$ pyrus search mockery
Pyrus version 2.0.0a4 SHA-1: 72271D92C3AA1FA96DF9606CD538868544609A52
Using PEAR installation found at /opt/phpfarm/inst/php-5.3.16/pear
Searching for mockery
1 packages found:
pear.survivethedeepend.com/Mockery

$ pyrus install pear.survivethedeepend.com/Mockery
Pyrus version 2.0.0a4 SHA-1: 72271D92C3AA1FA96DF9606CD538868544609A52
Using PEAR installation found at /opt/phpfarm/inst/php-5.3.16/pear
Sorry, the channel "pear.survivethedeepend.com" is unknown.
Do you want to add this channel and continue?

Please choose: 
  yes
  no
[yes] : 
Discovery of channel pear.survivethedeepend.com successful
Downloading pear.survivethedeepend.com/Mockery
...

Pyrus is able to search all existing channel servers and automatically discover then when they are not known yet.

Technical background

When running pyrus search, it asks the online service pyr.us. This service checks for package releases on the channel servers every night and indexes them.

The list of channel servers that pyr.us uses is fetched from pear.php.net/channels/xbel.php , which is an export of our PEAR channel server list.

So if you want Pyrus to find the packages of your PEAR channel, register your channel on this list.

Link
PHP_CodeSniffer 1.4.4 released (6.2.2013, 23:28)

PHP_CodeSniffer version 1.4.4 has just been uploaded to PEAR and is now available to install. This is primarily a bug fix release although there are a couple of nice new sniff features that some developers may find useful including a new sniff to run CSS Lint on your CSS...

Link
PHP_CodeSniffer 1.4.3 released (4.12.2012, 03:27)

PHP_CodeSniffer version 1.4.3 has just been uploaded to PEAR and is now available to install. This is primarily a bug fix release although support for the upcoming PHP 5.5 T_FINALLY token has been added a few PSR-2 issues have been fixed and a change has been made to improve...

Link
PHP_CodeSniffer general memory improvements (15.11.2012, 04:38)

PHP_CodeSniffer version 1.3.6 introduced memory improvements when using the summary report. You can take a look at the improvements that were reported at the time when running over the Symfony2 codebase. Obviously the summary report is only useful when running PHP_CodeSniffer from the command line and viewing the output...

Link
PHP_CodeSniffer 1.4.1 released (1.11.2012, 23:43)

PHP_CodeSniffer version 1.4.1 has just been uploaded to PEAR and is now available to install. This release includes a few important changes for developers who maintain their own standards and sniffs. Ignore Patterns In version 1.3.6 ignore patterns were changed so that they are checked against the relative path...

Link
PHP_CodeSniffer 1.4.0 released (26.9.2012, 04:42)

PHP_CodeSniffer version 1.4.0 has just been uploaded to PEAR and is now available to install. This release includes two new coding standards; PSR-1 and PSR-2.A lot of effort has gone into compiling these standards from existing sniffs writing new sniffs and making core changes to PHP_CodeSniffer to support new...

Link
Christians Tagebuch Surrogator - free avatar image server (17.8.2012, 22:02)

Avatar for cweiske I hereby announce the release of Surrogator version 0.1.0, a simple Libravatar compatible avatar image server written in PHP.

Libravatar?

You probably know Gravatar, the (proprietary) service by the Wordpress.com guys. You give it the md5 hash of an email address and get an image for it - given that the person owning the email address registered there and uploaded a picture.

Many many blog and web applications use those images to give comments a personal note and make it easy to recognize the commenters.

Libravatar (libre avatar) does basically the same, but has some huge benefits:

  • It is federated, which means that you can host your own avatar server for your domains. In case Libravatar.org or Gravatar are down, images for your email addresses will still be delivered.
  • You can use it as a service like Gravatar if you don't want to host your own server. But unlike Gravatar - which you cannot leave - you have full control over your avatars and your account. Full account export and removal are supported.
  • You can assign images to OpenIDs.
  • It is open source. The Libravatar sources are open for anyone to inspect, improve and install on your own server.
  • It falls back to Gravatar. The userbase of Gravatar is not to be neglected, so Libravatar falls back to the Gravatar image if there is no Libravatar one.
  • You do not have to register another account but may login with your OpenID.

Surrogator?

The original Libravatar software is written in Python and contains the whole package: Profiles, login, image upload, email and OpenID management.

I do not need all that; all I want is to put an image file foo@example.org.png somewhere, and that's it. This is Surrogator.

Just download (or git clone) Surrogator, follow the 4 setup steps, put the image files in the raw/ directory and that's it. It just works and takes little server resources.

Services_Libravatar

So Libravatar is cool, how can you use it? Luckily, Melissa Draper wrote the Services_Libravatar library which is documented and easily installable via PEAR or Pyrus:

$ pear install services_libravatar-alpha

Using it is as easy as writing four lines of code:

getUrl('foo@example.org');
echo 'Avatar';

That's it. Now start using free avatar images in your software, and use one of the available plugins.

Link
Links RSS 0.92   RDF 1.
Atom Feed  
PHP5 powered   PEAR
Link the Planet <a href="http://www.planet-pear.org/">Planet PEAR</a>