Results tagged “elgg”

Elgg provides an API for building custom web services. You expose functionality through the web services API by building a plugin and then either publish your API for other developers to build clients or provide your own. Here is a simple example of exposing a method so that a client app can post a status update to the wire in Elgg:

1. Write your function that creates the wire post

function rest_wire_post($username, $text) {
    $user = get_user_by_username($username);
    if (!$user) {
        throw new InvalidParameterException('Bad username');
    }

    $obj = new ElggObject();
    $obj->subtype = 'thewire';
    $obj->owner_guid = $user->guid;
    $obj->access_id = ACCESS_PUBLIC;
    $obj->method = 'api';
    $obj->description = elgg_substr(strip_tags($text), 0, 140);

    $guid = $obj->save();

    add_to_river('river/object/thewire/create',
                 'create',
                 $user->guid,
                 $obj->guid
                );

    return 'success';
}


2. Expose it

expose_function('wire.post',
                'rest_wire_post',
                array( 'username' => array ('type' => 'string'),
                       'text' => array ('type' => 'string'),
                     ),
                'Post a status update to the wire',
                'GET',
                false,
                false);


3. Use it

Type this into your browser's address bar making the obvious replacements for your site:

http://mysite.com/services/api/rest/xml/?method=wire.post&username=cash&text=hello

Now, in a real application you would make this a POST request rather than a GET and protect it with user and API authentication. For more information on that see the Elgg web services wiki page.

Piwik and Elgg

While Google Analytics may be the most popular analytics service, there are times when you want to use your own hosted solution (intranet, control over data). Piwik is a great open source package that provides an impressive set of tools and a very nice look and feel. Using it with Elgg is extremely easy. You can write a plugin to do this in a few minutes as Elgg provides an analytics view in the footer that can be extended.

This is all it takes:


  1. Install Piwik

  2. Create your Elgg plugin skeleton

  3. Create a view to hold your Piwik javascript code - in this case piwik/analytics

  4. Register a callback for the init:system event

  5. In the callback extend the footer/analytics view

  6. Install and activate the plugin

The structure of this very simple plugin looks like this:

Plugin Structure

You can download it here.

Elgg Unit Tests

A new component in Elgg 1.7 will be a unit test framework (SimpleTest). I've written a skeleton example of how plugin authors can use the framework. It can be downloaded here.

A little trick that I've learned for debugging a call to the Elgg API that involves a database call is to wrap it with $CONFIG->debug. For example:


$CONFIG->debug = true;
$images = get_entities('object', 'blog', $guid, '', 25);
$CONFIG->debug = false;


This will write the query to the error log giving you a chance to see what you are doing wrong (or if there is a bug in the Elgg framework). Make sure that the global $CONFIG is available within the context. This works fine with versions up to 1.6. Unknown how this will work on 1.7 and beyond.


Update: For Elgg 1.7 and above you need to do this:

$CONFIG->debug = 'NOTICE';
$images = get_entities('object', 'blog', $guid, '', 25);
unset($CONFIG->debug);

This assumes that you have debugging turned off. If you have it set to something else (like 'ERROR'), you should set it back to that setting.

1
Close