[scriptindex] Add new "split" action
[xapian.git] / xapian-bindings / php7 / smoketest.php
blob101f79da07de47e4fddaca9bd8d9e51de0756a45
1 <?php
2 // Run this PHP script using 'make check' in the build tree.
4 /* Simple test to ensure that we can load the xapian module and exercise basic
5 * functionality successfully.
7 * Copyright (C) 2004,2005,2006,2007,2009,2011,2012,2013,2014,2015,2016,2017 Olly Betts
8 * Copyright (C) 2010 Richard Boulton
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 * USA
26 # Die on any error, warning, notice, etc.
27 function die_on_error($errno, $errstr, $file, $line) {
28 if ($file !== Null) {
29 print $file;
30 if ($line !== Null) print ":$line";
31 print ": ";
33 print "$errstr\n";
34 exit(1);
36 set_error_handler("die_on_error", -1);
38 include "xapian.php";
40 # Test the version number reporting functions give plausible results.
41 $v = Xapian::major_version().'.'.Xapian::minor_version().'.'.Xapian::revision();
42 $v2 = Xapian::version_string();
43 if ($v != $v2) {
44 print "Unexpected version output ($v != $v2)\n";
45 exit(1);
48 $db = new XapianWritableDatabase('', Xapian::DB_BACKEND_INMEMORY);
49 $db2 = new XapianWritableDatabase('', Xapian::DB_BACKEND_INMEMORY);
51 # Check PHP5 handling of Xapian::DocNotFoundError
52 try {
53 $doc2 = $db->get_document(2);
54 print "Retrieved non-existent document\n";
55 exit(1);
56 } catch (Exception $e) {
57 if ($e->getMessage() !== "DocNotFoundError: Docid 2 not found") {
58 print "DocNotFoundError Exception string not as expected, got: '{$e->getMessage()}'\n";
59 exit(1);
63 # Check QueryParser parsing error.
64 try {
65 $qp = new XapianQueryParser;
66 $qp->set_stemmer(new XapianStem("en"));
67 $qp->parse_query("test AND");
68 print "Successfully parsed bad query\n";
69 exit(1);
70 } catch (Exception $e) {
71 if ($e->getMessage() !== "QueryParserError: Syntax: <expression> AND <expression>") {
72 print "QueryParserError Exception string not as expected, got: '$e->getMessage()'\n";
73 exit(1);
77 # Check that open_stub() is wrapped as expected.
78 try {
79 $db = Xapian::auto_open_stub("nosuchdir/nosuchdb");
80 print "Opened non-existent stub database\n";
81 exit(1);
82 } catch (Exception $e) {
83 if ($e->getMessage() !== "DatabaseOpeningError: Couldn't open stub database file: nosuchdir/nosuchdb (No such file or directory)") {
84 print "DatabaseOpeningError Exception string not as expected, got: '{$e->getMessage()}'\n";
85 exit(1);
89 # Check that DB_BACKEND_STUB works as expected.
90 try {
91 $db = new XapianDatabase("nosuchdir/nosuchdb", Xapian::DB_BACKEND_STUB);
92 print "Opened non-existent stub database\n";
93 exit(1);
94 } catch (Exception $e) {
95 if ($e->getMessage() !== "DatabaseOpeningError: Couldn't open stub database file: nosuchdir/nosuchdb (No such file or directory)") {
96 print "DatabaseOpeningError Exception string not as expected, got: '{$e->getMessage()}'\n";
97 exit(1);
101 # Check that open_stub() writable form is wrapped as expected.
102 try {
103 $db = Xapian::auto_open_stub("nosuchdir/nosuchdb", Xapian::DB_OPEN);
104 print "Opened non-existent stub database\n";
105 exit(1);
106 } catch (Exception $e) {
107 if ($e->getMessage() !== "DatabaseOpeningError: Couldn't open stub database file: nosuchdir/nosuchdb (No such file or directory)") {
108 print "DatabaseOpeningError Exception string not as expected, got: '{$e->getMessage()}'\n";
109 exit(1);
113 # Check that DB_BACKEND_STUB works as expected.
114 try {
115 $db = new XapianWritableDatabase("nosuchdir/nosuchdb",
116 Xapian::DB_OPEN|Xapian::DB_BACKEND_STUB);
117 print "Opened non-existent stub database\n";
118 exit(1);
119 } catch (Exception $e) {
120 if ($e->getMessage() !== "DatabaseOpeningError: Couldn't open stub database file: nosuchdir/nosuchdb (No such file or directory)") {
121 print "DatabaseOpeningError Exception string not as expected, got: '{$e->getMessage()}'\n";
122 exit(1);
126 # Regression test for bug#193, fixed in 1.0.3.
127 $vrp = new XapianNumberValueRangeProcessor(0, '$', true);
128 $a = '$10';
129 $b = '20';
130 $vrp->apply($a, $b);
131 if (Xapian::sortable_unserialise($a) != 10) {
132 print Xapian::sortable_unserialise($a)." != 10\n";
133 exit(1);
135 if (Xapian::sortable_unserialise($b) != 20) {
136 print Xapian::sortable_unserialise($b)." != 20\n";
137 exit(1);
140 $stem = new XapianStem("english");
141 if ($stem->get_description() != "Xapian::Stem(english)") {
142 print "Unexpected \$stem->get_description()\n";
143 exit(1);
146 $doc = new XapianDocument();
147 $doc->set_data("a\x00b");
148 if ($doc->get_data() === "a") {
149 print "get_data+set_data truncates at a zero byte\n";
150 exit(1);
152 if ($doc->get_data() !== "a\x00b") {
153 print "get_data+set_data doesn't transparently handle a zero byte\n";
154 exit(1);
156 $doc->set_data("is there anybody out there?");
157 $doc->add_term("XYzzy");
158 $doc->add_posting($stem->apply("is"), 1);
159 $doc->add_posting($stem->apply("there"), 2);
160 $doc->add_posting($stem->apply("anybody"), 3);
161 $doc->add_posting($stem->apply("out"), 4);
162 $doc->add_posting($stem->apply("there"), 5);
164 // Check virtual function dispatch.
165 if (substr($db->get_description(), 0, 17) !== "WritableDatabase(") {
166 print "Unexpected \$db->get_description()\n";
167 exit(1);
169 $db->add_document($doc);
170 if ($db->get_doccount() != 1) {
171 print "Unexpected \$db->get_doccount()\n";
172 exit(1);
175 $terms = array("smoke", "test", "terms");
176 $query = new XapianQuery(XapianQuery::OP_OR, $terms);
177 if ($query->get_description() != "Query((smoke OR test OR terms))") {
178 print "Unexpected \$query->get_description()\n";
179 exit(1);
181 $query1 = new XapianQuery(XapianQuery::OP_PHRASE, array("smoke", "test", "tuple"));
182 if ($query1->get_description() != "Query((smoke PHRASE 3 test PHRASE 3 tuple))") {
183 print "Unexpected \$query1->get_description()\n";
184 exit(1);
186 $query1b = new XapianQuery(XapianQuery::OP_NEAR, array("smoke", "test", "tuple"), 4);
187 if ($query1b->get_description() != "Query((smoke NEAR 4 test NEAR 4 tuple))") {
188 print "Unexpected \$query1b->get_description()\n";
189 exit(1);
191 $query2 = new XapianQuery(XapianQuery::OP_XOR, array(new XapianQuery("smoke"), $query1, "string"));
192 if ($query2->get_description() != "Query((smoke XOR (smoke PHRASE 3 test PHRASE 3 tuple) XOR string))") {
193 print "Unexpected \$query2->get_description()\n";
194 exit(1);
196 $subqs = array("a", "b");
197 $query3 = new XapianQuery(XapianQuery::OP_OR, $subqs);
198 if ($query3->get_description() != "Query((a OR b))") {
199 print "Unexpected \$query3->get_description()\n";
200 exit(1);
202 $enq = new XapianEnquire($db);
203 $enq->set_query(new XapianQuery(XapianQuery::OP_OR, "there", "is"));
204 $mset = $enq->get_mset(0, 10);
205 if ($mset->size() != 1) {
206 print "Unexpected \$mset->size()\n";
207 exit(1);
209 $terms = join(" ", $enq->get_matching_terms($mset->get_hit(0)));
210 if ($terms != "is there") {
211 print "Unexpected matching terms: $terms\n";
212 exit(1);
215 # Feature test for MatchDecider
216 $doc = new XapianDocument();
217 $doc->set_data("Two");
218 $doc->add_posting($stem->apply("out"), 1);
219 $doc->add_posting($stem->apply("outside"), 1);
220 $doc->add_posting($stem->apply("source"), 2);
221 $doc->add_value(0, "yes");
222 $db->add_document($doc);
224 class testmatchdecider extends XapianMatchDecider {
225 function apply($doc) {
226 return ($doc->get_value(0) == "yes");
230 $query = new XapianQuery($stem->apply("out"));
231 $enquire = new XapianEnquire($db);
232 $enquire->set_query($query);
233 $mdecider = new testmatchdecider();
234 $mset = $enquire->get_mset(0, 10, null, $mdecider);
235 if ($mset->size() != 1) {
236 print "Unexpected number of documents returned by match decider (".$mset->size().")\n";
237 exit(1);
239 if ($mset->get_docid(0) != 2) {
240 print "MatchDecider mset has wrong docid in\n";
241 exit(1);
244 class testexpanddecider extends XapianExpandDecider {
245 function apply($term) {
246 return ($term[0] !== 'a');
250 $enquire = new XapianEnquire($db);
251 $rset = new XapianRSet();
252 $rset->add_document(1);
253 $eset = $enquire->get_eset(10, $rset, XapianEnquire::USE_EXACT_TERMFREQ, 1.0, new testexpanddecider());
254 foreach ($eset->begin() as $t) {
255 if ($t[0] === 'a') {
256 print "XapianExpandDecider was not used\n";
257 exit(1);
261 # Check min_wt argument to get_eset() works (new in 1.2.5).
262 $eset = $enquire->get_eset(100, $rset, XapianEnquire::USE_EXACT_TERMFREQ);
263 $min_wt = 0;
264 foreach ($eset->begin() as $i => $dummy) {
265 $min_wt = $i->get_weight();
267 if ($min_wt >= 1.9) {
268 print "ESet min weight too high for testcase\n";
269 exit(1);
271 $eset = $enquire->get_eset(100, $rset, XapianEnquire::USE_EXACT_TERMFREQ, 1.0, NULL, 1.9);
272 $min_wt = 0;
273 foreach ($eset->begin() as $i => $dummy) {
274 $min_wt = $i->get_weight();
276 if ($min_wt < 1.9) {
277 print "ESet min_wt threshold not applied\n";
278 exit(1);
281 if (XapianQuery::OP_ELITE_SET != 10) {
282 print "OP_ELITE_SET is XapianQuery::OP_ELITE_SET not 10\n";
283 exit(1);
286 # Regression test - overload resolution involving boolean types failed.
287 $enq->set_sort_by_value(1, TRUE);
289 # Regression test - fixed in 0.9.10.1.
290 $oqparser = new XapianQueryParser();
291 $oquery = $oqparser->parse_query("I like tea");
293 # Regression test for bug#192 - fixed in 1.0.3.
294 $enq->set_cutoff(100);
296 # Check DateRangeProcessor works.
297 function add_rp_date(&$qp) {
298 $rpdate = new XapianDateRangeProcessor(1, Xapian::RP_DATE_PREFER_MDY, 1960);
299 $qp->add_rangeprocessor($rpdate);
301 $qp = new XapianQueryParser();
302 add_rp_date($qp);
303 $query = $qp->parse_query('12/03/99..12/04/01');
304 if ($query->get_description() !== 'Query(VALUE_RANGE 1 19991203 20011204)') {
305 print "XapianDateRangeProcessor didn't work - result was ".$query->get_description()."\n";
306 exit(1);
309 # Check DateValueRangeProcessor works.
310 function add_vrp_date(&$qp) {
311 $vrpdate = new XapianDateValueRangeProcessor(1, 1, 1960);
312 $qp->add_valuerangeprocessor($vrpdate);
314 $qp = new XapianQueryParser();
315 add_vrp_date($qp);
316 $query = $qp->parse_query('12/03/99..12/04/01');
317 if ($query->get_description() !== 'Query(VALUE_RANGE 1 19991203 20011204)') {
318 print "XapianDateValueRangeProcessor didn't work - result was ".$query->get_description()."\n";
319 exit(1);
322 # Feature test for XapianFieldProcessor
323 class testfieldprocessor extends XapianFieldProcessor {
324 function apply($str) {
325 if ($str === 'spam') throw new Exception('already spam');
326 return new XapianQuery("spam");
330 $tfp = new testfieldprocessor();
331 $qp->add_prefix('spam', $tfp);
332 $query = $qp->parse_query('spam:ignored');
333 if ($query->get_description() !== 'Query(spam)') {
334 print "testfieldprocessor didn't work - result was ".$query->get_description()."\n";
335 exit(1);
338 try {
339 $query = $qp->parse_query('spam:spam');
340 print "testfieldprocessor exception not rethrown\n";
341 exit(1);
342 } catch (Exception $e) {
343 if ($e->getMessage() !== 'already spam') {
344 print "Exception has wrong message\n";
345 exit(1);
349 # Test setting and getting metadata
350 if ($db->get_metadata('Foo') !== '') {
351 print "Unexpected value for metadata associated with 'Foo' (expected ''): '".$db->get_metadata('Foo')."'\n";
352 exit(1);
354 $db->set_metadata('Foo', 'Foo');
355 if ($db->get_metadata('Foo') !== 'Foo') {
356 print "Unexpected value for metadata associated with 'Foo' (expected 'Foo'): '".$db->get_metadata('Foo')."'\n";
357 exit(1);
360 # Test OP_SCALE_WEIGHT and corresponding constructor
361 $query4 = new XapianQuery(XapianQuery::OP_SCALE_WEIGHT, new XapianQuery('foo'), 5.0);
362 if ($query4->get_description() != "Query(5 * foo)") {
363 print "Unexpected \$query4->get_description()\n";
364 exit(1);
367 # Test MultiValueKeyMaker.
369 $doc = new XapianDocument();
370 $doc->add_term("foo");
371 $doc->add_value(0, "ABB");
372 $db2->add_document($doc);
373 $doc->add_value(0, "ABC");
374 $db2->add_document($doc);
375 $doc->add_value(0, "ABC\0");
376 $db2->add_document($doc);
377 $doc->add_value(0, "ABCD");
378 $db2->add_document($doc);
379 $doc->add_value(0, "ABC\xff");
380 $db2->add_document($doc);
382 $enquire = new XapianEnquire($db2);
383 $enquire->set_query(new XapianQuery("foo"));
386 $sorter = new XapianMultiValueKeyMaker();
387 $sorter->add_value(0);
388 $enquire->set_sort_by_key($sorter, true);
389 $mset = $enquire->get_mset(0, 10);
390 mset_expect_order($mset, array(5, 4, 3, 2, 1));
394 $sorter = new XapianMultiValueKeyMaker();
395 $sorter->add_value(0, true);
396 $enquire->set_sort_by_key($sorter, true);
397 $mset = $enquire->get_mset(0, 10);
398 mset_expect_order($mset, array(1, 2, 3, 4, 5));
402 $sorter = new XapianMultiValueKeyMaker();
403 $sorter->add_value(0);
404 $sorter->add_value(1);
405 $enquire->set_sort_by_key($sorter, true);
406 $mset = $enquire->get_mset(0, 10);
407 mset_expect_order($mset, array(5, 4, 3, 2, 1));
411 $sorter = new XapianMultiValueKeyMaker();
412 $sorter->add_value(0, true);
413 $sorter->add_value(1);
414 $enquire->set_sort_by_key($sorter, true);
415 $mset = $enquire->get_mset(0, 10);
416 mset_expect_order($mset, array(1, 2, 3, 4, 5));
420 $sorter = new XapianMultiValueKeyMaker();
421 $sorter->add_value(0);
422 $sorter->add_value(1, true);
423 $enquire->set_sort_by_key($sorter, true);
424 $mset = $enquire->get_mset(0, 10);
425 mset_expect_order($mset, array(5, 4, 3, 2, 1));
429 $sorter = new XapianMultiValueKeyMaker();
430 $sorter->add_value(0, true);
431 $sorter->add_value(1, true);
432 $enquire->set_sort_by_key($sorter, true);
433 $mset = $enquire->get_mset(0, 10);
434 mset_expect_order($mset, array(1, 2, 3, 4, 5));
437 # Feature test for ValueSetMatchDecider:
439 $md = new XapianValueSetMatchDecider(0, true);
440 $md->add_value("ABC");
441 $doc = new XapianDocument();
442 $doc->add_value(0, "ABCD");
443 if ($md->apply($doc)) {
444 print "Unexpected result from ValueSetMatchDecider->apply(); expected false\n";
445 exit(1);
448 $doc = new XapianDocument();
449 $doc->add_value(0, "ABC");
450 if (!$md->apply($doc)) {
451 print "Unexpected result from ValueSetMatchDecider->apply(); expected true\n";
452 exit(1);
455 $mset = $enquire->get_mset(0, 10, 0, null, $md, null);
456 mset_expect_order($mset, array(2));
458 $md = new XapianValueSetMatchDecider(0, false);
459 $md->add_value("ABC");
460 $mset = $enquire->get_mset(0, 10, 0, null, $md, null);
461 mset_expect_order($mset, array(1, 3, 4, 5));
464 function mset_expect_order($mset, $a) {
465 if ($mset->size() != sizeof($a)) {
466 print "MSet has ".$mset->size()." entries, expected ".sizeof($a)."\n";
467 exit(1);
469 for ($j = 0; $j < sizeof($a); ++$j) {
470 $docid = $mset->get_hit($j)->get_docid();
471 if ($docid != $a[$j]) {
472 print "Expected MSet[$j] to be $a[$j], got ".$docid()."\n";
473 exit(1);
478 # Feature tests for Query "term" constructor optional arguments:
479 $query_wqf = new XapianQuery('wqf', 3);
480 if ($query_wqf->get_description() != 'Query(wqf#3)') {
481 print "Unexpected \$query_wqf->get_description():\n";
482 print $query_wqf->get_description() . "\n";
483 exit(1);
486 $query = new XapianQuery(XapianQuery::OP_VALUE_GE, 0, "100");
487 if ($query->get_description() != 'Query(VALUE_GE 0 100)') {
488 print "Unexpected \$query->get_description():\n";
489 print $query->get_description() . "\n";
490 exit(1);
493 $query = XapianQuery::MatchAll();
494 if ($query->get_description() != 'Query(<alldocuments>)') {
495 print "Unexpected \$query->get_description():\n";
496 print $query->get_description() . "\n";
497 exit(1);
500 $query = XapianQuery::MatchNothing();
501 if ($query->get_description() != 'Query()') {
502 print "Unexpected \$query->get_description():\n";
503 print $query->get_description() . "\n";
504 exit(1);
507 # Test access to matchspy values:
509 $matchspy = new XapianValueCountMatchSpy(0);
510 $enquire->add_matchspy($matchspy);
511 $enquire->get_mset(0, 10);
512 $values = array();
513 foreach ($matchspy->values_begin() as $k => $term) {
514 $values[$term] = $k->get_termfreq();
516 $expected = array(
517 "ABB" => 1,
518 "ABC" => 1,
519 "ABC\0" => 1,
520 "ABCD" => 1,
521 "ABC\xff" => 1,
523 if ($values != $expected) {
524 print "Unexpected matchspy values():\n";
525 var_dump($values);
526 var_dump($expected);
527 print "\n";
528 exit(1);
533 class testspy extends XapianMatchSpy {
534 public $matchspy_count = 0;
536 function apply($doc, $wt) {
537 if (substr($doc->get_value(0), 0, 3) == "ABC") ++$this->matchspy_count;
541 $matchspy = new testspy();
542 $enquire->clear_matchspies();
543 $enquire->add_matchspy($matchspy);
544 $enquire->get_mset(0, 10);
545 if ($matchspy->matchspy_count != 4) {
546 print "Unexpected matchspy count of {$matchspy->matchspy_count}\n";
547 exit(1);
551 # Regression test for SWIG bug - it was generating "return $r;" in wrapper
552 # functions which didn't set $r.
553 $indexer = new XapianTermGenerator();
554 $doc = new XapianDocument();
556 $indexer->set_document($doc);
557 $indexer->index_text("I ask nothing in return");
558 $indexer->index_text_without_positions("Tea time");
559 $indexer->index_text("Return in time");
561 $s = '';
562 foreach ($doc->termlist_begin() as $term) {
563 $s .= $term . ' ';
565 if ($s !== 'ask i in nothing return tea time ') {
566 print "PHP Iterator wrapping of TermIterator doesn't work ($s)\n";
567 exit(1);
570 $s = '';
571 foreach ($doc->termlist_begin() as $k => $term) {
572 $s .= $term . ':' . $k->get_wdf() . ' ';
574 if ($s !== 'ask:1 i:1 in:2 nothing:1 return:2 tea:1 time:2 ') {
575 print "PHP Iterator wrapping of TermIterator keys doesn't work ($s)\n";
576 exit(1);
579 # Test GeoSpatial API
580 $coord = new XapianLatLongCoord();
581 $coord = new XapianLatLongCoord(-41.288889, 174.777222);
583 define('COORD_SLOT', 2);
584 $metric = new XapianGreatCircleMetric();
585 $range = 42.0;
587 $centre = new XapianLatLongCoords($coord);
588 $query = new XapianQuery(new XapianLatLongDistancePostingSource(COORD_SLOT, $centre, $metric, $range));
590 $db = new XapianWritableDatabase('', Xapian::DB_BACKEND_INMEMORY);
591 $coords = new XapianLatLongCoords();
592 $coords->append(new XapianLatLongCoord(40.6048, -74.4427));
593 $doc = new XapianDocument();
594 $doc->add_term("coffee");
595 $doc->add_value(COORD_SLOT, $coords->serialise());
596 $db->add_document($doc);
598 $centre = new XapianLatLongCoords();
599 $centre->append(new XapianLatLongCoord(40.6048, -74.4427));
601 $ps = new XapianLatLongDistancePostingSource(COORD_SLOT, $centre, $metric, $range);
602 $q = new XapianQuery("coffee");
603 $q = new XapianQuery(XapianQuery::OP_AND, $q, new XapianQuery($ps));
605 $enq = new XapianEnquire($db);
606 $enq->set_query($q);
607 $mset = $enq->get_mset(0, 10);
608 if ($mset->size() != 1) {
609 print "Expected one result with XapianLatLongDistancePostingSource, got ";
610 print $mset->size() . "\n";
611 exit(1);
614 $s = '';
615 foreach ($db->allterms_begin() as $k => $term) {
616 $s .= "($term:{$k->get_termfreq()})";
618 if ($s !== '(coffee:1)') {
619 print "PHP Iterator iteration of allterms doesn't work ($s)\n";
620 exit(1);
623 # Test reference tracking and regression test for #659.
624 $qp = new XapianQueryParser();
626 $stop = new XapianSimpleStopper();
627 $stop->add('a');
628 $qp->set_stopper($stop);
630 $query = $qp->parse_query('a b');
631 if ($query->get_description() !== 'Query(b@2)') {
632 print "XapianQueryParser::set_stopper() didn't work as expected - result was ".$query->get_description()."\n";
633 exit(1);