Xapian PHP5 Wrapper

As mentioned in an earlier post, I’ve been playing around with Xapian and PHP5, but the nice object oriented API of Xapian is flattened into 240 functions using the PHP bindings.

To make it easier to work with I’ve created a set of wrapper classes for PHP5 which allow you to use the API just as the documentation suggests, with the added bonus of using PHP iterators.

Wrapping 240 methods would be a bit painful, so I’ve create a base class which uses the __call override to actually dispatch method calls to Xapian functions, marshalling parameters and return values too!

You should find these wrappers “natural” to use if you read the Xapian C++ API documentation. except that all classes are prefixed with “Xapian” so the C++ Xapian::Document class becomes XapianDocument

The various iterator classes support the PHP5 Iterator interface, allowing you to use them in a foreach loop, for example

$termi= $db->allterms_begin();
foreach($termi as $term)
   echo "$term | ";

All of the classes will accept an Xapian resource in their constructor which will wrap that resource, for example, to open an in-memory database you could use

$db = new XapianWritableDatabase(inmemory_open());

Alternatively, pass the documented construction parameters to create a
new resource, e.g.

$database = new XapianDatabase($dbfile);

Inheritance is supported, for example you can call XapianDatabase methods on
a XapianWritableDatabase object


The included xapian_php5_smoketest.php is an adaptation of the original smoketest.php which comes with the xapian-bindings library. The PHP5 version of the smoketest runs around 2-3 times slower than the original, raw, smoketest.php.

The smoketest is almost entirely made up of Xapian calls, so if Xapian calls are
your bottleneck, then these classes will slow you down more

However, in most common uses, particularly querying, API calls are unlikely to
be the main bottleneck and using php5 iterators makes for some easy to read code.

Download the code

xapian_php5_wrapper_v0.1.tar.gz – 8Kb

If anyone finds this useful or has suggestions for improvement, do let me know.

3 thoughts on “Xapian PHP5 Wrapper

  1. Ryan Mahoney

    Nice work. I just finished a implementation using the binding under PHP5… wish I had found this two weeks ago! I’ve downloaded your code and will test it out.

  2. Pingback: David Levy (( Creativity Matters » Blog Archive » another Xapian PHP5 Wrapper

Comments are closed.