Comments for Tera-WURFL
31 Oct 2006 18:31
PHP5 + MySQL5 Version
I've been playing around with MySQL 5's Stored Procedures a bit, and I completely implemented the searching section of $obj->getDeviceCapabilitesFromAgent method in 1 stored procedure. This one is a mouthfull but could speed things up significantly!
CREATE PROCEDURE `findUserAgent`(IN minlen INT, IN ua VARCHAR(128), IN preferroot TINYINT)
findua:BEGIN
DECLARE curlen INT;
DECLARE prematch VARCHAR(45) DEFAULT NULL;
DECLARE matches INT DEFAULT 0;
DECLARE maxmatch INT DEFAULT 0;
/* Look for an exact match first */
SELECT deviceID INTO prematch FROM tera_wurfl_hybrid WHERE user_agent=ua LIMIT 1;
IF prematch IS NOT NULL
THEN
/* Found an exact match - done. */
SELECT "Matched Exactly" AS match_type;
SELECT * FROM tera_wurfl_hybrid WHERE deviceID=prematch;
LEAVE findua;
END IF;
/* See if there is ever going to be a match */
SELECT COUNT(user_agent) INTO maxmatch FROM tera_wurfl_hybrid WHERE user_agent LIKE (CONCAT(LEFT(ua,minlen),'%'));
IF maxmatch = 0
THEN
/* No matches were found, even down to the smallest char length - we don't need to continue */
SELECT "Will Never Match" AS match_type;
LEAVE findua;
END IF;
/* We've already tested the full length and the smallest length, so don't test them again */
SELECT LEFT(ua, curlen - 1 ) INTO ua;
SET minlen = minlen + 1;
SELECT CHAR_LENGTH(ua) INTO curlen;
WHILE ( curlen > minlen AND matches = 0) DO
SELECT COUNT(user_agent) INTO matches FROM tera_wurfl_hybrid WHERE user_agent LIKE CONCAT(ua,'%');
IF matches > 0 AND curlen > minlen
THEN
SELECT "Matched After Trimming" AS match_type;
IF preferroot = 1
THEN
/* We would prefer a device root if there are multiple matching user agents */
SELECT * FROM tera_wurfl_hybrid WHERE user_agent LIKE CONCAT(ua,'%') ORDER BY actual_device_root DESC LIMIT 1;
LEAVE findua;
ELSE
/* We could really through a 'ORDER BY RAND() ' in there if we wanted to get creative */
SELECT * FROM tera_wurfl_hybrid WHERE user_agent LIKE CONCAT(ua,'%') ORDER BY actual_device_root ASC LIMIT 1;
LEAVE findua;
END IF;
END IF;
SELECT LEFT(ua, curlen - 1 ) INTO ua;
SELECT CHAR_LENGTH(ua) INTO curlen;
END WHILE;
/*SELECT "done" AS `status`;*/
SELECT "Did Not Match" AS match_type;
END
I may branch off the main PHP4+MySQL4 version and make a new version using this method since it's so fast.
This is auto formatted very poorly so if you want a copy of it in a different form let me know.
A Java Web site development framework that is free from configuration and annotation.
Tera-WURFL 2.0 is out! With a faster and more accurate detection engine, Tera-WURFL 2.0 will empower your websites by differentiating between mobile and desktop browsers, small and large screens, compatible ringtones, video formats, streaming vs. download, and much more! Based on a prerelease of the Java WURFL Evolution, Tera-WURFL 2.0 uses UserAgentMatchers to apply specific detection methods to specific User Agents; for example, Nokia devices use the Nokia UserAgentMatcher.