A device database is the most accurate method for identifying mobile devices and determining their characteristics. The developer tools used in device awareness are a device database and its accompanying API. The device database might be a physical file in XML, JSON, or other format, or it might be a set of database tables intended for import into an existing relational database management system (DBMS). Device database vendors provide APIs for many popular web runtime frameworks.
The Wireless Universal Resource File (WURFL, http://wurfl.sourceforge.net) is an open-data mobile device database and open-source API that provides access to a community-driven database of mobile device and browser characteristics. The WURFL project is about ten years old. You can learn more about WURFL from the interview with its co-creator, Luca Passani, which you will find in a sidebar later in this section.
WURFL recognizes almost all known mobile devices and tracks many device
characteristics. WURFL rarely misidentifies a mobile device, but when that happens, it is easy to communicate with the database maintainers and contribute device information that corrects the problem.
WURFL stores device data in an XML format. The device database is one large XML file, wurfl.xml, which at the time of writing contains about 13MB of device information.
Device characteristics are textual and categorized into groups. (The WURFL API allows lookup by characteristic and group names.) The WURFL database is hierarchical, which means that mobile device models in the database inherit capabilities from ancestor devices. An ancestor device can be an earlier device model or a virtual device that represents a version of a mobile OS or browser. At every step in the hierarchy, device capabilities can be updated to match the actual functionality of mobile device families or an individual device.
INTERVIEW WITH LUCA PASSANI, CO-CREATOR OF WURFL
Luca Passani is the co-creator of the WURFL device database and API. Formerly of Openwave and AdMob, Passani now leads WURFL development and advocates for the rights of content owners and developers on the Mobile Web.
FREDERICK: Describe your motivation for creating WURFL?
PASSANI: While working for developer marketing within Openwave since 2001, the rest of the good Developer Marketing team and I were doing a great job at evangelizing about WAP and the Openwave WAP SDK. Developers liked the tool, but one point that was reiterated was, “Guys, it’s not an Openwave world alone. You have Nokia, you have Ericsson, you have PDAs, and we would like to support them all.” For this reason, I was asking Openwave if they would sponsor a free tool for developers. In the middle of the .com bubble burst, money was tight, so the answer was no. But the company was OK with me leveraging the open source model and the community on WMLProgramming to create an open-source developer tool.
After all, they were also asking me to help developers migrate from WML to XHTML-MP, so timing was great. That was how WURFL was born in January 2002.
After that, the only way was up. WURFL was free, open, and it worked. In 2007, I left Openwave and, after a [period] with AdMob, I created my own company around the idea of providing professional support around WURFL.
FREDERICK: How does WURFL differentiate itself from [other device database products] in the marketplace?
PASSANI: At the cost of sounding arrogant, I think the question should be phrased more fairly, “How do others differentiate from WURFL?”There is really no open-source competition to WURFL, but there are multiple commercial initiatives. Their story is typically about “better data, better documentation, better tools, and/or better support than WURFL.”
Honestly, I have seen first-hand that the “better data” part only refers to a subset of devices; for the rest, even commercial solutions copy from WURFL. As far as the API goes, I honestly believe the latest releases of Java and PHP API are better than commercial solutions in terms of speed, openness, and flexibility.
As far as tools and support are concerned, now that I have a company, [you can] expect improvement in that area, too.
FREDERICK: How is device data sourced and verified? What is the role of the community here?
PASSANI: The community is everything. Not only is the community adding the data, but [it is] also placing existing data under scrutiny. There is a process in place. In order to become a contributor, you need to be approved. After you are approved, your submissions are monitored, and, if you don’t abide by the WURFL conventions, your contributor credentials will be revoked. I have a small staff of people doing the check on submissions and also adding device information.
As far as data quality is concerned, it varies. Popular devices will typically get a lot of attention, so
information is most probably correct. On the other hand, a weird Motorola [mobile device] from Kazakhstan spotted in the logs will find its way into WURFL, some basic info will be set through UAProf, but thorough validation of the device is more unlikely to take place.
One important aspect to mention, though, is that, because of its openness, a lot of companies will adopt WURFL and set up their own business intelligence to correct information that does not work for them. A lot of times this information is fed back into WURFL. Other times it is not, which is a shame [because] sharing device information is key to fixing the problem for everyone and being able to use our time on our business model. In my opinion, using time on rediscovering the wheel is not a wise use of resources.
The WURFL database can be customized and extended using patch files, XML files in the same format as the main database that are applied sequentially to add new devices and override device capabilities. For example, the WURFL maintainers provide a patch file that identifies desktop Web browsers.
You can browse and search the master WURFL device data repository at
http://wurflpro.com/. Browsing the repository is a human-readable way to discover the capabilities for a mobile device. Figures 4-1 and 4-2 display screenshots of
wurflpro.com taken while searching for the Palm Pre device. Figure 4-1 shows all search results for the Pre model name. Figure 4-2 shows the WURFL database entry for the Palm Pre with webOS version 1.2.
Figure 4-1. Search results for Pre showing Palm Pre Device versions in the WURFL public repository at wurflpro.com
Figure 4-2. Device entry for Palm Pre with webOS version 1.2 in the WURFL public repository at wurflpro.com Figure 4-2 shows off several features of the WURFL device database. The WURFL device ID of the Palm Pre with webOS version 1.2 is palm_pre_ver_1_2. Its ancestor
entry in the device hierarchy is the “fall-back” device, palm_pre_ver_1_1. The screenshot shows all the device characteristics in the product_info group. This group provides basic mobile device product information. Each characteristic in the group is shaded with a background color that indicates where in the device hierarchy the value is defined.
Dark and light green characteristics are defined in the current device or its immediate parent. These values are considered the most reliable because they are defined close in the hierarchy to the actual device. Characteristic values defined more distantly up the device hierarchy, at the level of a virtual device family or OS version or browser version, are shaded yellow. Capabilities defined at the root of the device database hierarchy are shaded red to indicate that they are most distantly defined and often, the least reliable.
The WURFL project provides object-oriented device database APIs for Java, PHP, and .NET Web runtime frameworks licensed under the GPL. The API provides methods for recognizing a mobile device from the HTTP request or User-Agent header value, as well as for obtaining device characteristics by group or characteristic name.
You can download the WURFL database, patch files, and API implementations from the WURFL SourceForge site at http://wurfl.sourceforge.net/.
Follow these steps to install and use the WURFL database and PHP API:
1. Download the WURFL PHP API into an accessible location on your Web server.
2. Download the main WURFL database and patch file(s) into an accessible location on your Web server.
3. Configure the PHP API by customizing a wurfl-config.xml file.
4. Write PHP code that initializes the WURFL API using wurfl-config.xml.
5. Write PHP code that recognizes a mobile device using the HTTP request headers or User-Agent.
6. Write PHP code that uses the API to obtain device characteristics.
The first step required to use the WURFL API is that you download the latest version of the PHP API from the WURFL SourceForge software download site at
http://sourceforge.net/projects/wurfl/files/. This section describes installation for version 1.1 of the PHP API. Next, you install the software distribution into an accessible location on your Web server. This section assumes that the PHP API is installed into a directory named wurfl-php-1.r1.
The second step required to use the WURFL API in PHP is that you download and install the WURFL database and patch file(s) to an accessible location on your Web server.
Create a writable directory for caching WURFL device data to disk for fast lookups.
The third step required to use the WURFL API in PHP is that you configure the API installation. In PHP, you initialize the WURFL API by customizing a wurfl-config.xml file that provides the file locations of the main WURFL database, patch files. Listing 4-2 is an example wurfl-config.xml file that indicates that the main WURFL database, patch
<patch>bots_and_spider.xml</patch>
In Listing 4-2, the <main-file> element specifies the location of the main WURFL device database. The <patch> elements specify the location of any WURFL patch files used to modify the database contents. All file locations are absolute paths or relative paths from the wurfl-config.xml file location. The <persistence> element can specify one of several caching mechanisms for WURFL device data. The listing opts for a simple, file-based cache provider, and the <params> element specifies the cache directory (again, relative to the location of the configuration file itself) that should contain the cache files.
The fourth step required to use the WURFL API in PHP is that you write PHP code that initializes the WURFL API using wurfl-config.xml. Begin by finding the path to the PHP installation directory from the first step. After creating the configuration file in the third step, find its absolute path. This line shows an example absolute path to a configuration file:
/home/webuser/learnto.mobi/html/books/bmwd/04/wurfl/wurfl-config.xml
Listing 4-3 uses the WURFL API installation directory and configuration file path in a PHP code snippet that initializes the API. Note that the WURFL API installation directory in the require_once statement above is relative to the location of the file containing the PHP code (although you could also provide an absolute path; they are equally valid in PHP). The configuration file location is an absolute path.
Listing 4-3. Example WURFL API Initialization in PHP
require_once('wurfl-php-1.r1/WURFL/WURFLManagerProvider.php');
$wurflConfigFile =
"/home/webuser/learnto.mobi/html/books/bmwd/04/wurfl/wurfl-config.xml";
$wurflManager =
WURFL_WURFLManagerProvider::getWURFLManager($wurflConfigFile);
The fifth step required to use the WURFL API in PHP is that you write PHP code that recognizes a mobile device using Web request headers. Here, the WURFL API provides
three options for device recognition. The first (and preferred) API variant uses the entire set of HTTP request headers to identify the device, as in the following PHP code:
$device = $wurflManager->getDeviceForHttpRequest($_SERVER);
The variable $device is a WURFL_Device object that you can use in the sixth step to obtain device characteristics. The second API variant for device recognition uses only a User-Agent string (or value of the User-Agent HTTP request header) to identify the device, as in this PHP example:
$device = $wurflManager->getDeviceForUserAgent($_SERVER['HTTP_USER_AGENT']);
You use the third API variant for device recognition only when the WURFL ID for a device is already known. Here, you use the ID as the identifying parameter for the device in the database. In the code sample that follows, the Palm Pre device ID is used from Figure 4-2.
$device = $wurflManager->getDevice(“palm_pre_ver_1_2”);
The sixth and final step required to use the WURFL API in PHP is that you write PHP code that uses the API to obtain characteristics for the newly identified device. You can use the methods and properties of the $device instance of the WURFL_Device class (obtained in the fifth step) to obtain characteristic values.
The WURFL_Device object provides id and fallback properties that identify the current device ID and the ID of the immediate ancestor in the device hierarchy, respectively. Its getCapability and getAllCapabilities methods find device capability values.
The getCapability method returns the value of one device characteristic:
$bestMarkup = $device->getCapability('preferred_markup');
The getAllCapabilities method returns an Array of all device capabilities and values.
The array can then be dereferenced to find individual capability values, as in the following code sample:
$props = $device->getAllCapabilities();
$bestMarkup = $props['preferred_markup'];
$modelName = $props[‘model_name'];
$isMobileDevice = $props[‘is_wireless_device'];
All characteristic values are PHP strings (even Boolean values).