Moriarty
From n² wiki
Moriarty is the name of my PHP library for working with the platform. Its goals are to provide a useful and comprehensive facade to Platform services, with some extra niceties for manipulating RDF built in. It is based on an earlier (unnamed) client library I wrote that we use in many of our applications in Talis. Moriarty has the benefit of some rethinking about its style and architecture and is better at keeping pace with platform developments. Iand 09:00, 2 October 2007 (BST)
Contents |
[edit] Getting Started
Moriarty is currently at version 1.0
You can download the latest version of Moriarty from the n2 subversion repository:
http://n2.talis.com/svn/playground/iand/moriarty/releases/moriarty-1.0.tgz
Version 1.0 is tagged in the repository so you can easily use it in svn:externals
svn co http://n2.talis.com/svn/playground/iand/moriarty/tags/1.0/
If you're interested in the bleeding edge of development then you can check out the trunk:
svn co http://n2.talis.com/svn/playground/iand/moriarty/trunk/
I'm currently working in the trunk because Moriarty is still alpha quality and hasn't had a formal release yet.
Moriarty requires Benjamin Novack's excellent ARC libary for RDF processing (version 2). If you want to run the unit tests (you probably do) then you will also need PHPUnit version 3.2
I usually modify PHP's include path so that it can find these libraries like this:
define('MORIARTY_DIR', LIB_DIR . DIRECTORY_SEPARATOR . "moriarty"); define('PHPUNIT_DIR', LIB_DIR . DIRECTORY_SEPARATOR . "phpunit"); define('ARC_DIR', LIB_DIR . DIRECTORY_SEPARATOR . "arc"); ini_set('include_path', ini_get('include_path') .PATH_SEPARATOR.MORIARTY_DIR .PATH_SEPARATOR.PHPUNIT_DIR .PATH_SEPARATOR.ARC_DIR );
[edit] Hello, World!
The Hello World for any Platform app is performing a simple search on a store. Here's how you could do that using Moriarty:
require_once 'store.class.php'; require_once 'constants.inc.php'; $store = new Store('http://api.talis.com/stores/ukbib'); $contentbox = $store->get_contentbox(); $results = $contentbox->search_to_resource_list("feynman", 10, 0); echo '<h1>' . $results->title . '</h1>'; foreach ($results->items as $item) { echo '<p><a href="' . $item[RSS_LINK][0] . '">' . $item[RSS_TITLE][0] . '</a></p>'; }
[edit] Detailed Tutorial
The Kniblet Tutorial uses Moriarty extensively and contains many examples of using Moriarty for building applications. I recommend you check that tutorial out first.
[edit] Finding Out More
I blog about Moriarty on the n² Blog. See http://blogs.talis.com/n2/tags/moriarty
[edit] Submitting Some RDF
Now, how can we use Moriarty to add RDF to a store? The Store object provides simple access to the metabox. You can use the submit_rdfxml method to POST some RDF into a store:
require_once 'store.class.php'; $store = new Store('http://api.talis.com/stores/mystore'); $mb = $store->get_metabox(); $response = $mb->submit_rdfxml( $my_rdf ); echo $response->status_code; if ( $response->status_code >299) { echo ', server said: ' . htmlspecialchars( $response->body ); }
[edit] Authenticating
Most stores require authentication before you can submit content or rdf to them. The Platform uses a capability model (see Capabilities) to determine what each user can do. Each service operation requires a specific capability. Moriarty handles all this with a Credentials class. Just pass a Credentials object to the Store's constructor and Moriarty will use it for subsequent interactions with that store.
require_once 'store.class.php'; require_once 'credentials.class.php'; $credentials = new Credentials('user', 'mypassword'); $store = new Store('http://api.talis.com/stores/mystore', $credentials ); $mb = $store->get_metabox(); $response = $mb->submit_rdfxml( $my_rdf ); echo $response->status_code; if ( $response->status_code >299) { echo ', server said: ' . htmlspecialchars( $response->body ); }
[edit] Clearing A Store
Now you've put some random RDF into the store, you probably want to clear it out. This is pretty simple with Moriarty, just get the store's Scheduled Job Collection and schedule a Reset Data Job:
require_once 'store.class.php'; require_once 'credentials.class.php'; $store = new Store('http://api.talis.com/stores/mystore', new Credentials('user', 'mypassword') ); $queue = $store->get_job_queue(); $queue->schedule_reset_data();
If you want it to occur at a specific time then just pass in the time to the schedule_reset_data method:
$queue->schedule_reset_data( gmmktime(10, 11, 0, 12, 6, 2007) );

