Ticket #406: test.php

File test.php, 2.5 KB (added by Daniel Ménard, 15 years ago)

PHP script which reproduce the problem

Line 
1<?php
2require('xapian.php'); // adjust the path to Xapian php library
3
4echo 'PHP version ', PHP_VERSION, ', Xapian version ', Xapian::version_string(), "\n\n";
5
6// Create a dummy database
7$db=Xapian::inmemory_open();
8$doc = new XapianDocument();
9echo 'Creating a new db containing a document with terms : ';
10foreach(array('test','tester','testable','user') as $term)
11{
12 $doc->add_term(utf8_encode($term));
13 echo $term, ', ';
14}
15echo "\n\n";
16$db->add_document($doc);
17
18// Stopper
19$stopper=new XapianSimpleStopper();
20$stopwords=explode(',', 'this,is,a');
21foreach($stopwords as $stopword)
22 $stopper->add($stopword);
23
24// QueryParser
25$queryParser = new XapianQueryParser();
26$queryParser->set_stopper($stopper);
27
28// Flags
29$flags = XapianQueryParser::FLAG_WILDCARD;
30
31// Pass db to the query parser
32$queryParser->set_database($db);
33
34// Tests. One time with defaulOp=OR, one time with defaultOp=AND
35foreach (array(XapianQuery::OP_OR=>'OR', XapianQuery::OP_AND=>'AND') as $defaultOp=>$name)
36{
37 echo '-------------- DefaultOp=', $name, "--------------\n";
38 $queryParser->set_default_op($defaultOp);
39
40 $tests = array // 'query' => 'expected result'
41 (
42 // no wildcards : OK
43 'this is a test' => 'Xapian::Query(test:(pos=4))',
44
45 // One wildcard : OK
46 'test*'=> 'Xapian::Query((test:(pos=1) OR testable:(pos=1) OR tester:(pos=1)))',
47
48 // One stopword + one wildcard : OK
49 'a test*' => 'Xapian::Query((test:(pos=2) OR testable:(pos=2) OR tester:(pos=2)))',
50
51 // Two stopwords + one wildcard : FAILS
52 'is a test*' => 'Xapian::Query((test:(pos=3) OR testable:(pos=3) OR tester:(pos=3)))',
53
54 // Three stopwords + one wildcard : FAILS
55 'this is a test*' => 'Xapian::Query((test:(pos=4) OR testable:(pos=4) OR tester:(pos=4)))',
56
57 // Three stopwords + two wildcard : FAILS
58 'this is a us* test*' => 'Xapian::Query((test:(pos=4) OR testable:(pos=4) OR tester:(pos=4)))',
59
60 // Three stopwords + one wildcard + another term : OK
61 'this is a user test*' => 'Xapian::Query((user:(pos=4) OR test:(pos=5) OR testable:(pos=5) OR tester:(pos=5)))',
62 );
63
64 foreach($tests as $test=>$expected)
65 {
66 $query = $queryParser->parse_query(utf8_encode($test), $flags);
67 echo 'query: ', $test, "\n";
68 $result = $query->get_description();
69 echo 'result: ', $result, "\n";
70 if ($result === 'Xapian::Query()') echo "FAILS.\n";
71 echo "\n";
72 }
73}