cvs: pear /Services_Technorati package.xml /Services_Technorati/Services Technorati.php /Services_Technorati/Services/Technorati Exception.php
/Services_Technorati/test Services_Technorati2_Test.php Services_Technorati_Test.php run-tests.php

From: Date: Fri, 23 Feb 2007 15:44:26 +0000
Subject: cvs: pear /Services_Technorati package.xml /Services_Technorati/Services Technorati.php /Services_Technorati/Services/Technorati Exception.php
/Services_Technorati/test Services_Technorati2_Test.php Services_Technorati_Test.php run-tests.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-44845@lists.php.net to get a copy of this message
jystewart Fri Feb 23 15:44:26 2007 UTC Added files: /pear/Services_Technorati/Services/Technorati Exception.php Removed files: /pear/Services_Technorati/test Services_Technorati2_Test.php Modified files: /pear/Services_Technorati package.xml /pear/Services_Technorati/Services Technorati.php /pear/Services_Technorati/test Services_Technorati_Test.php run-tests.php Log: Renaming all files back to Services_Technorati (without the 2)

http://cvs.php.net/viewvc.cgi/pear/Services_Technorati/package.xml?r1=1.12&r2=1.13&diff_format=u Index: pear/Services_Technorati/package.xml diff -u pear/Services_Technorati/package.xml:1.12 pear/Services_Technorati/package.xml:1.13 --- pear/Services_Technorati/package.xml:1.12 Tue Feb 20 18:25:50 2007 +++ pear/Services_Technorati/package.xml Fri Feb 23 15:44:26 2007 @@ -1,16 +1,13 @@ <?xml version="1.0" encoding="UTF-8"?> -<package packagerversion="1.5.0" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 -http://pear.php.net/dtd/tasks-1.0.xsd -http://pear.php.net/dtd/package-2.0 -http://pear.php.net/dtd/package-2.0.xsd"> - <name>Services_Technorati2</name> +<package packagerversion="1.5.0" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd"> + <name>Services_Technorati</name> <channel>pear.php.net</channel> <summary>A class for interacting with the Technorati API</summary> <description>Services_Technorati is a wrapper for the REST-based Technorati webservices API. Technorati is a blog search engine that provides a number of interfaces for interacting with recent blog entries, such as searching for entries that link to a certain URL, are linked from a certain URL, or have been given certain tags. Services_Technorati provides an interface to all of the query types in Technorati API version 1.0, and supports filesystem caching of query data using Cache_Lite compatible cache objects. - </description> +As of version 2.0.0alpha1 this package is PHP5 only, and requires simplexml.</description> <lead> <name>James Stewart</name> <user>jystewart</user> @@ -23,44 +20,35 @@ <email>ryansking@php.net</email> <active>yes</active> </developer> - <!-- <date>2007-02-20</date> --> - <!-- <time>09:22:52</time> --> - <!-- <version> --> - <!-- <release>0.6.7beta</release> --> - <!-- <api>0.6.7beta</api> --> - <!-- </version> --> - <!-- <stability> --> - <!-- <release>beta</release> --> - <!-- <api>beta</api> --> - <!-- </stability> --> + <date>2007-02-23</date> + <time>10:42:13</time> + <version> + <release>2.0.0alpha1</release> + <api>2.0.0</api> + </version> + <stability> + <release>alpha</release> + <api>alpha</api> + </stability> <license uri="http://www.php.net/license">PHP License</license> - <!-- <notes>Added run-tests.php test harness</notes> --> + <notes>Initial release of the PHP5 only package</notes> <contents> <dir name="/"> - <dir name="Services"> - <file baseinstalldir="" name="Technorati2.php" role="php"> - <tasks:replace from="@package_version@" to="version" type="package-info" /> - </file> - <dir name="Technorati2"> - <file baseinstalldir="" name="Exception.php" role="php"> - <tasks:replace from="@package_version@" to="version" type="package-info" /> - </file> - </dir> - </dir> <!-- /Services --> - <dir name="test"> - <file baseinstalldir="" name="run-tests.php" role="test" /> - <file baseinstalldir="" name="Services_Technorati2_Test.php" role="test" /> - </dir> <!-- /test --> - </dir> <!-- / --> + <file baseinstalldir="" md5sum="6ff4808e223722a8f4a836ee129c5e6a" name="Services/Technorati/Exception.php" role="php"> + <tasks:replace from="@package_version@" to="version" type="package-info" /> + </file> + <file baseinstalldir="" md5sum="58254f9d26bcf5f7ba8e032e50df90d9" name="Services/Technorati.php" role="php"> + <tasks:replace from="@package_version@" to="version" type="package-info" /> + </file> + <file baseinstalldir="" md5sum="eec685143ce1ea2c22f4a611741a5de3" name="test/run-tests.php" role="test" /> + <file baseinstalldir="" md5sum="e7854f18d44ed06e285f44d665ef15d4" name="test/Services_Technorati_Test.php" role="test" /> + </dir> </contents> <dependencies> <required> <php> <min>5.0.0</min> </php> - <extension> - <name>xml</name> - </extension> <pearinstaller> <min>1.4.0b1</min> </pearinstaller> @@ -68,6 +56,9 @@ <name>HTTP_Request</name> <channel>pear.php.net</channel> </package> + <extension> + <name>simplexml</name> + </extension> </required> <optional> <package> @@ -77,5 +68,4 @@ </optional> </dependencies> <phprelease /> - <changelog /> </package> http://cvs.php.net/viewvc.cgi/pear/Services_Technorati/Services/Technorati.php?r1=1.21&r2=1.22&diff_format=u Index: pear/Services_Technorati/Services/Technorati.php diff -u /dev/null pear/Services_Technorati/Services/Technorati.php:1.22 --- /dev/null Fri Feb 23 15:44:26 2007 +++ pear/Services_Technorati/Services/Technorati.php Fri Feb 23 15:44:26 2007 @@ -0,0 +1,438 @@ +<?php +/* vim: set expandtab tabstop=4 shiftwidth=4: */ + +/** + * Client library for Technorati's REST-based webservices + * + * PHP version 5 + * + * @category Services + * @package Services_Technorati + * @author James Stewart <james@jystewart.net> + * @copyright 2006 James Stewart + * @license http://www.gnu.org/copyleft/lesser.html GNU LGPL + * @version CVS: $Id: Technorati.php,v 1.22 2007/02/23 15:44:26 jystewart Exp $ + * @link http://pear.php.net/package/Services_Technorati + */ + +/** + * using PEAR error management + */ +require_once 'PEAR.php'; + +/** + * uses HTTP to send requests + */ +require_once 'HTTP/Request.php'; + +/** + * We have our own Exception class + */ +require_once 'Technorati/Exception.php'; + +/** + * Client for Technorati's REST-based webservices + * + * Technorati is a blog search engine with a number of tools to help + * explore and utilise the blogosphere. The API provides enhanced + * access to all the site's features, performing API calls and + * returning SimpleXML objects of results. + * + * @todo update once attention.xml query is stabilised + * @category Webservices + * @package Services_Technorati + * @author James Stewart <james@jystewart.net> + * @license http://www.gnu.org/copyleft/lesser.html GNU LGPL + * @version Release: @package_version@ + * @link http://pear.php.net/package/Services_Technorati + */ +class Services_Technorati +{ + /** + * URI of the REST API + * + * @access protected + * @var string + */ + protected $apiUrl = 'http://api.technorati.com'; + + /** + * User agent to send with requests + * @access public + * @var string + */ + public $userAgent = 'Services_Technorati v2'; + + /** + * API Key + * + * @access protected + * @var string + */ + protected $apiKey = null; + + /** + * Create our client + * + * @access public + * @param string apiKey + * @param object cache + */ + function __construct($apiKey, $cache = null) + { + $this->apiKey = $apiKey; + $this->cache = $cache; + if ($this->cache) { + $this->cache->setOption('automaticSerialization', true); + } + } + + /** + * Factory methods to create client. This is in place to prepare for + * forwards compatibility with future versions of the API. + * + * @access public + * @param string apiKey + * @param object cache + * @param float apiVersion + * @return Services_Technorati object + */ + function factory($apiKey, $cache = null, $apiVersion = 1.0) + { + return new Services_Technorati($apiKey, $cache); + } + + /** + * KeyInfo provides information on daily usage of an API key. + * A Technorati API key is typically limited to 500 requests + * per day, where a day is measured as 00:00-23:59 PST + * This function does not cache, and this call does not use any + * of the daily request allocation for the given key. + * + * @access public + * @return array + */ + function keyInfo() { + return $this->sendRequest('keyinfo'); + } + + /** + * This method handles the majority of the work for most of our queries. + * + * @access private + * @param string the query type we're performing + * @param array an array for our main query parameter (key and value) + * @param array the valid options for this query + * @param array the user's chosen options + * @return array + */ + protected function general($query, $chief_param, $valid_options, $options = null) + { + /* Check for invalid options */ + if (is_array($options)) { + $options = $this->checkOptions($options, $valid_options); + if (PEAR::isError($options)) { + return $options; + } + $options = array_merge($options, $chief_param); + } else { + $options = $chief_param; + } + + /* Build cache URI */ + $filename = $query . '.' . str_replace(' ', '_', $query); + if (is_array($options)) { + $filename = $filename . implode('_', $options); + } + + /* Check if cached */ + if (isset($this->cache) and + $cache = $this->cache->get($filename, 'services_technorati')) { + return $cache; + } + + /* Not cached */ + $value = $this->sendRequest($query, $options); + + /* Save the data in the cache if appropriate. We use the filename as the ID */ + if (! PEAR::isError($value) and !empty($this->cache)) { + $this->cache->save($value, $filename, 'services_technorati'); + } + + return $value; + } + + /** + * The BlogPostTags query returns the top tags for a given blog URL. + * + * @access public + * @param string the url to query for + * @param array options + * @return array + */ + function blogPostTags($url, $options = array()) + { + return $this->general('blogposttags', array('url' => $url), + array('limit'), $options); + } + + /** + * Cosmos lets you see what blogs are linking to a given URL + * + * @access public + * @param string url + * @param array options + * @return array + */ + function cosmos($url, $options = null) + { + $valid_options = array('type', 'limit', 'start', 'current', 'claim', + 'highlight'); + return $this->general('cosmos', array('url' => $url), + $valid_options, $options); + } + + /** + * The search lets you see what blogs contain a given search string + * + * @access public + * @param string query + * @param array options + * @return array + */ + function search($query, $options = null) + { + $valid_options = array('start','limit','claim'); + return $this->general('search', array('query' => $query), + $valid_options, $options); + } + + /** + * The outbound query let you see what blogs are linked to from a given + * blog, including their associated info. It has been deprecated. + * + * @access public + * @param string url + * @param array options + * @return array + * @deprecated + */ + function outbound($url, $options = null) + { + throw new Services_Technorati_Exception('The \'outbound\' query has been deprecated'); + /* Check for invalid options */ + + // $valid_options = array('start'); + // return $this->general('outbound', array('url' => $url), + // $valid_options, $options); + } + + /** + * The tag query allows you to get a list of posts with the given tag + * associated with it. This API query is currently experimental. + * + * @access public + * @param string url + * @param array options + * @return array + */ + function tag($tag, $options = null) + { + /* Check for invalid options */ + + $valid_options = array('limit', 'start', 'format', + 'excerptsize', 'topexcerptsize'); + return $this->general('tag', array('tag' => $tag), + $valid_options, $options); + } + + /** + * TopTags lets you retrieve a list of the most popular post tags + * tracked by Technorati + * + * @access public + * @param array options + * @return array + */ + function topTags($options = null) + { + $valid_options = array('limit', 'start'); + return $this->general('toptags', array(), + $valid_options, $options); + } + + /** + * The getinfo query tells you things that Technorati knows about a user + * + * @access public + * @param string username + * @return array + */ + function getInfo($username) + { + $options = array('username' => urlencode($username)); + return $this->general('getinfo', $options, + array(), false); + } + + /** + * The bloginfo query provides info on what blog, if any, is + * associated with a given URL + * + * @access public + * @param string url + * @return array + */ + function blogInfo($url) + { + $options = array('url' => $url); + return $this->general('bloginfo', $options, + array(), false); + } + + /** + * This lets users retrieve their Attention.XML + * This API query is currently experimental. + * + * @access public + * @param string username + * @param string password + * @return array + */ + function attention($user, $password) + { + $key_options = array('username' => $user); + $options = array('password' => md5($password)); + return $this->general('attention', $key_options, array('password'), $options); + } + + /** + * This posts a new Attention.XML file to the Technorati system. + * This API query is currently experimental. This is the one call that + * doesn't use sendRequest, because it needs to POST a file. + * + * @access public + * @param string username + * @param string password + * @param string filename + * @return boolean + */ + function attentionPost($user, $password, $file) + { + $options = array('username' => $user, 'password' => md5($password)); + + /* Build cache URI */ + + $filename = 'attention.{$user}'; + + /* We don't cache this query */ + $request =& new HTTP_Request($this->apiUrl . 'attention'); + $request->addHeader('User-Agent', $this->userAgent); + $request->setMethod(HTTP_REQUEST_METHOD_POST); + $addfile = $request->addFile('attention.xml', $file); + + if (PEAR::isError($addfile)) { + return $addfile; + } + + $request->addPostData('username', $user); + $request->addPostData('password', md5($password)); + + $request->sendRequest(); + + $value = $this->processResponse($request); + + if (PEAR::isError($value)) { + return $value; + } + + /* Store in cache */ + + $this->cache->save($value, 'services_technorati'); + + return $value; + } + + /** + * Send everything out + * + * @access protected + * @param string type of query + * @param array parameters + * @return SimpleXML|PEAR_ERROR + */ + protected function sendRequest($query, $options = null) + { + /* Do all the nitty gritty HTTP stuff. Except attentionPost */ + $url = sprintf('%s/%s?key=%s', $this->apiUrl, $query, $this->apiKey); + + $request =& new HTTP_Request($this->apiUrl . 'attention'); + $request->addHeader('User-Agent', $this->userAgent); + $request->setURL($url); + + if (is_array($options)) { + foreach ($options as $key => $value) { + $request->addQueryString($key, $value); + } + } + + $request->sendRequest(); + + return $this->processResponse($request); + } + + /** + * This function takes the request sent by either attentionPost or + * sendRequest and processes it, returning an unserialized version + * + * @access protected + * @param HTTP_Request $request the request object + * @return array|PEAR_Error + */ + protected function processResponse($request) + { + if ($request->getResponseCode() != 200) { + print $request->getResponseCode(); + throw new Services_Technorati_Exception('Invalid Response Code', + $request->getResponseCode()); + } + + $result = $request->getResponseBody(); + + if (PEAR::isError($result)) { + throw new Services_Technorati_Exception('Technorati Response Error', + $result->getError()); + } + + $xml = simplexml_load_string($result); + if ($xml->document && $xml->document->result && $xml->document->result->error) { + throw new Services_Technorati_Exception('Technorati Response Error', + (string)$xml->document->result->error); + } + + return $xml; + } + + /** + * Filter options for those acceptable for this query. Raise + * warnings if others have been passed. + * + * @access protected + * @param array specified options + * @param array acceptable options + */ + protected function checkOptions($current, $accepted) + { + $accepted_options = array(); + foreach ($current as $option => $value) { + if (in_array($option, $accepted)) { + $accepted_options[$option] = $value; + } else { + throw new Services_Technorati_Exception($option .' is not an option for this query'); + } + } + return $accepted_options; + } +} + +?> http://cvs.php.net/viewvc.cgi/pear/Services_Technorati/test/Services_Technorati_Test.php?r1=1.6&r2=1.7&diff_format=u Index: pear/Services_Technorati/test/Services_Technorati_Test.php diff -u /dev/null pear/Services_Technorati/test/Services_Technorati_Test.php:1.7 --- /dev/null Fri Feb 23 15:44:26 2007 +++ pear/Services_Technorati/test/Services_Technorati_Test.php Fri Feb 23 15:44:26 2007 @@ -0,0 +1,119 @@ +<?php + +require_once 'Services/Technorati.php'; +require_once 'PHPUnit.php'; + +class Services_Technorati_TestCase extends PHPUnit_TestCase +{ + // contains the object handle of our Technorati class. + public $tapi; + + // contains our API key + public static $key; + + // /* constructor for test suite */ + // function __construct($name) { + // $this->PHPUnit_TestCase($name); + // } + + function setUp() { + $this->tapi =& Services_Technorati::factory(self::$key, null, 1.0); + } + + function tearDown() { + unset($this->tapi); + } + + /* + * Test that the factory brings the same result as going directly to the + * constructor. This will only work while we're going with the one API + * version + */ + function testFactory() { + $testTapi = new Services_Technorati(self::$key, null, 1.0); + $this->assertTrue($this->tapi == $testTapi); + } + + /* Test that the keyinfo query returns the two numeric values we need */ + function testKeyInfo() { + $result = $this->tapi->keyInfo(); + $this->assertTrue(is_numeric((int)$result->document->result->apiqueries)); + $this->assertTrue(is_numeric((int)$result->document->result->maxqueries)); + } + + /* Test that the result of a cosmos query is either a relevant PEAR error or an + * array populated with at least the query metadata. The only parameter + * passed is a URL. + */ + function testCosmos() { + $result = $this->tapi->cosmos('http://www.example.com'); + $this->assertTrue(is_object($result)); + $this->assertEquals('http://www.example.com', (string)$result->document->result->url); + } + + /* Test that the result of a search query is either a relevant PEAR error or an + * array populated with at least the query metadata. The only parameter + * passed is a single search term. + */ + function testSearch() { + $result = $this->tapi->search('example'); + $this->assertTrue(is_object($result)); + $this->assertEquals('example', (string)$result->document->result->query); + } + + /* Test that the result of a getInfo query is either a relevant PEAR error or an + * array populated with at least the query metadata. The only parameter + * passed is a single username. + */ + function testGetInfo() { + $result = $this->tapi->getInfo('jystewart'); + $this->assertTrue($result instanceof SimpleXMLElement); + $this->assertEquals('jystewart', (string)$result->document->result->username); + } + + /* Test that the result of an outbound query is either a relevant PEAR error or an + * array populated with at least the query metadata. The only parameter + * passed is a single URL. + + function testOutbound() { + $result = $this->tapi->outbound('http://www.example.com'); + $this->assertTrue(($result instanceof SimpleXMLElement && + (string)$result->document->result->url == 'http://www.example.com') or + PEAR::isError($result)); + } + + /* Test that the result of a blogInfo query is either a relevant PEAR error or an + * array populated with at least the query metadata. The only parameter + * passed is a single URL. + */ + function testBlogInfo() { + $result = $this->tapi->blogInfo('http://www.example.com'); + $this->assertTrue($result instanceof SimpleXMLElement); + $this->assertEquals('http://www.example.com', (string)$result->document->result->url); + } + + /* Test that the result of a tag query is either a relevant PEAR error or an + * array populated with at least the query metadata. The only parameter + * passed is a single search term. + */ + function testTag() { + $result = $this->tapi->tag('example'); + $this->assertTrue($result instanceof SimpleXMLElement); + $this->assertEquals('example', (string)$result->document->result->query); + } + + /* Test that the result of a TopTags query is either a relevant PEAR error or an + * array populated with at least a numeric value for the limit on number of results. + */ + function testTopTags() { + $result = $this->tapi->topTags(); + $this->assertTrue($result instanceof SimpleXMLElement); + $this->assertTrue(is_numeric((int)$result->document->result->limit)); + } +} + +$suite = new PHPUnit_TestSuite('Services_Technorati_TestCase'); +$result = PHPUnit::run($suite, '123'); +echo $result->toString(); + +?> \ No newline at end of file http://cvs.php.net/viewvc.cgi/pear/Services_Technorati/test/run-tests.php?r1=1.3&r2=1.4&diff_format=u Index: pear/Services_Technorati/test/run-tests.php diff -u pear/Services_Technorati/test/run-tests.php:1.3 pear/Services_Technorati/test/run-tests.php:1.4 --- pear/Services_Technorati/test/run-tests.php:1.3 Tue Feb 20 23:36:44 2007 +++ pear/Services_Technorati/test/run-tests.php Fri Feb 23 15:44:26 2007 @@ -9,9 +9,9 @@ */ require_once 'PHPUnit.php'; -require_once 'Services/Technorati2.php'; +require_once 'Services/Technorati.php'; -$testcases = array('Services_Technorati2_Test'); +$testcases = array('Services_Technorati_Test'); $suite =& new PHPUnit_TestSuite(); @@ -25,7 +25,7 @@ # You can get a key at http://www.technorati.com/developers/apikey.html $api_key = "<enter here>"; -Services_Technorati2_TestCase::$key = $api_key; +Services_Technorati_TestCase::$key = $api_key; $result = PHPUnit::run($suite); http://cvs.php.net/viewvc.cgi/pear/Services_Technorati/Services/Technorati/Exception.php?view=markup&rev=1.1 Index: pear/Services_Technorati/Services/Technorati/Exception.php +++ pear/Services_Technorati/Services/Technorati/Exception.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ /** * Client library for Technorati's REST-based webservices * * PHP version 5 * * @category Services * @package Services_Technorati * @author James Stewart <james@jystewart.net> * @copyright 2006 James Stewart * @license http://www.gnu.org/copyleft/lesser.html GNU LGPL * @version CVS: $Id: Exception.php,v 1.1 2007/02/23 15:44:26 jystewart Exp $ * @link http://pear.php.net/package/Services_Technorati */ /** * We are extending PEAR_Exception */ require_once 'PEAR/Exception.php'; /** * Services_Technorati_Exception is a simple extension of PEAR_Exception, existing * to help with identification of the source of exceptions. * * @author James Stewart <james@jystewart.net> * @version Release: @package_version@ * @package Services_Technorati */ class Services_Technorati_Exception extends PEAR_Exception { } ?>
« previous php.pear.cvs (#44845) next »