Skip to content

How to write triggers for ARC

semsol edited this page Mar 14, 2011 · 1 revision

The latest versions of ARC can be extended with Trigger classes if those files follow certain conventions, as described on this page:

Name your extension ARC2_MyTriggerTrigger, save it as ARC2_MyTriggerTrigger.php, and place the file in the recently added triggers directory. This will allow ARC to later locate and include your class file.

Triggers have to be explicitly registered/activated via the Store or Endpoint configuration:

...
$config = array(
  'store_triggers' => array(
    /* register a trigger for INSERTs and LOADs */
    'insert' => array('countTriples'),
    'load' => array('countTriples'),
  ),
);
...
$store = ARC2::getStore($config);
...

The class file itself can be an arbitrary PHP class, but you have to define some parent Class (e.g. Class to be able to access ARC functionality).

Below, you see a basic example for an ARC Trigger that counts the triples in a store (e.g. after a LOAD operation).

<?php
/*
class:    ARC2 Trigger
version:  2008-02-01
*/

ARC2::inc('Class');

class ARC2_CountTriplesTrigger extends ARC2_Class {

  function __construct($a = '', &$caller) {/* caller is a store */
    parent::__construct($a, $caller);
  }
  
  function ARC2_CountTriplesTrigger($a = '', &$caller) {
    $this->__construct($a, $caller);
  }

  function __init() {
    parent::__init();
    $this->store = $this->caller;
  }

  /*  */

  function go() { /* automatically called by store or endpoint */
    $q = 'SELECT COUNT(*) AS ?t_count WHERE { ?s ?p ?o }';
    $row = $this->store->query($q, 'row');
    /* do something */
    ...
  }

}