#!/usr/bin/perl -w
use strict;

# Update a PHP file which uses Xapian's PHP bindings from pre-0.9.7 syntax
# to work with 0.9.7 and later.  Note: This script isn't perfect - e.g. it will
# get confused if the "this" argument to any flat function contains a comma.

my @classnames = qw(BM25Weight BoolWeight Database Document ESet
	ESetIterator Enquire ExpandDecider MSet MSetIterator
	MatchDecider PositionIterator PostingIterator Quartz Query
	QueryParser RSet SimpleStopper Stem Stopper TermIterator TradWeight
	ValueIterator Weight WritableDatabase);

my $classes = join("|", @classnames);

my $tot = 0;
my $line = 0;
my @lines;
while (<>) {
    my $c = 0;
    $c += s/\bnew_($classes)\b/new Xapian$1/gio;
    $c += s/\b($classes)_([a-z0-9_]+)\s*\(\s*([^,)]*?)\s*(?:,\s*(.*?))?(\))/$3->$2($4$5/gio;
    push @lines, $line if $c;
    $tot += $c;
    print;
    ++$line;
}
if ($tot) {
    print STDERR "*** $tot change(s) in ".scalar(@lines)." line(s) : ".join(",", @lines)."\n";
}
