Resourcesync: Making things happen with callbacks

resync_logoIn a previous blog post I introduced the ResourceSync PHP API library.  This is a code library written PHP that makes it easy to interact with web sites that support the new ResourceSync standard.  The default behavior for the code when scynchronising with a server either during a baseline sync (complete sync) or a incremental sync (of only changed files since the last baseline sync) is to simply download the files and store them on disk in the same directories as they exist on the server.

However, unless you want to just store the files for backup purposes, the chances are that you’ll want to process them in some way.  There are two ways to do this, either perform the synchronisation, and then process the files, or process them as they are downloaded.

From the last post, you’ll know that by using the ResourceSync PHP library, performing a sync can be as simple as:

include 'ResyncResourcelist.php';
$resourcelist = new ResyncResourcelist('');

This will process the resourcelist file by file, and download them to the /resync/ directory.

In order to process these, you need to register a ‘callback’ function with the library.  Each time an item is synchronised, the code in the callback function will be executed.

The following code snippet shows a very simple example of a callback.  This example displays the filename of the resource that has been downloaded, and prints the XML that described the file in the ResourceSync resourcelist.  The XML can be useful as it provides contextual information about the file, such as its size, checksum, last modified date, and links to related items.  Of course some of these will have already been checked by the library (such as last modified date when using the date range option, and the checksum to make sure the file has been retried successfully).

$resourcelist->registerCallback(function($file, $resyncurl) {
    echo '  - Callback given value of ' .$file . "\n";
    echo '   - XML:' . "\n" . $resyncurl->getXML()->asXML() . "\n";

When performing a baseline sync using the ResyncResourcelist class it is only possible to register a single callback.  This is called whenever any file is downloaded.

However the ResyncChangelist class allows three different callbacks to be registered, depending on the action: CREATED, UPDATED, or DELETED.

$changelist->registerCreateCallback(function($file, $resyncurl) {
    echo '  - CREATE Callback given value of ' .$file . "\n";
    echo '   - XML:' . "\n" . $resyncurl->getXML()->asXML() . "\n";

$changelist->registerUpdateCallback(function($file, $resyncurl) {
    echo '  - UPDATE Callback given value of ' .$file . "\n";
    echo '   - XML:' . "\n" . $resyncurl->getXML()->asXML() . "\n";

$changelist->registerDeleteCallback(function($file, $resyncurl) {
    echo '  - DELETE Callback given value of ' .$file . "\n";
    echo '   - XML:' . "\n" . $resyncurl->getXML()->asXML() . "\n";

Depending on the purpose of your code, it is likely that you would want to handle these three types of events in different ways, hence the three callback options.

In the next blog post, I’ll show an example of this code in action, as it uses the callback to look at each resource’s XML to discover whether it is a metadata file or a related resource.  It then uses this information to deposit the item into a repository using SWORD.разработка и поддержка web сайтов

3 thoughts on “Resourcesync: Making things happen with callbacks

  1. Pingback: The ResourceSync PHP Library | Stuart Lewis' Blog

  2. Pingback: ResourceSync and SWORD | Stuart Lewis' Blog

  3. Dean Farrell

    Thanks for the series on ResourceSync and the PHP implementation. It seems like ResourceSync wants to Sync entire directories. Is it possible to sync specific files? It seems this might be possible if each file were in the resourcelist?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>