Ticket #484: bug-wildcards.php

File bug-wildcards.php, 3.3 KB (added by Daniel Ménard, 14 years ago)

PHP script which reproduce the problem.

Line 
1<?php
2require('xapian.php'); // adjust the path to xapian.php
3
4echo 'PHP version ', PHP_VERSION, ', Xapian version ', Xapian::version_string(), "\n\n";
5
6// Create a dummy in-memory database
7$db=Xapian::inmemory_open();
8$doc = new XapianDocument();
9echo 'Creating a new db containing one document with terms : ';
10foreach(array('xapian', 'tester','testable','user','query') as $term)
11{
12 $doc->add_term(utf8_encode($term));
13 echo $term, ', ';
14}
15echo "\n\n";
16$db->add_document($doc);
17
18// Setup QueryParser
19$queryParser = new XapianQueryParser();
20$flags = XapianQueryParser::FLAG_WILDCARD;
21$queryParser->set_database($db);
22
23// Tests
24if (Xapian::version_string() >= '1.2.0') // take OP_SYNONYM into account
25{
26 $tests = array // 'query' => 'expected result'
27 (
28 'test* xapian' => 'Xapian::Query(((testable:(pos=1) SYNONYM tester:(pos=1)) OR xapian:(pos=2)))',
29 'xapian test*' => 'Xapian::Query((xapian:(pos=1) OR (testable:(pos=2) SYNONYM tester:(pos=2))))',
30
31 'test* xapian user' => 'Xapian::Query(((testable:(pos=1) SYNONYM tester:(pos=1)) OR xapian:(pos=2) OR user:(pos=3)))',
32 'xapian user test*' => 'Xapian::Query((xapian:(pos=1) OR user:(pos=2) OR (testable:(pos=3) SYNONYM tester:(pos=3))))',
33 'xapian test* user' => 'Xapian::Query((xapian:(pos=1) OR (testable:(pos=2) SYNONYM tester:(pos=2)) OR user:(pos=3)))',
34
35 'xapian query test* user' => 'Xapian::Query((xapian:(pos=1) OR query:(pos=2) OR (testable:(pos=3) SYNONYM tester:(pos=3)) OR user:(pos=4)))',
36 'xapian que* test* user' => 'Xapian::Query((xapian:(pos=1) OR query:(pos=2) OR (testable:(pos=3) SYNONYM tester:(pos=3)) OR user:(pos=4)))',
37
38 'xapian que* user test*' => 'Xapian::Query((xapian:(pos=1) OR query:(pos=2) OR user:(pos=3) OR (testable:(pos=4) SYNONYM tester:(pos=4))))',
39 );
40}
41else
42{
43 $tests = array // 'query' => 'expected result'
44 (
45 'test* xapian' => 'Xapian::Query((testable:(pos=1) OR tester:(pos=1) OR xapian:(pos=2)))',
46 'xapian test*' => 'Xapian::Query((xapian:(pos=1) OR testable:(pos=2) OR tester:(pos=2)))',
47
48 'test* xapian user' => 'Xapian::Query((testable:(pos=1) OR tester:(pos=1) OR xapian:(pos=2) OR user:(pos=3)))',
49 'xapian user test*' => 'Xapian::Query((xapian:(pos=1) OR user:(pos=2) OR testable:(pos=3) OR tester:(pos=3)))',
50 'xapian test* user' => 'Xapian::Query((xapian:(pos=1) OR testable:(pos=2) OR tester:(pos=2) OR user:(pos=3)))',
51
52 'xapian query test* user' => 'Xapian::Query((xapian:(pos=1) OR query:(pos=2) OR testable:(pos=3) OR tester:(pos=3) OR user:(pos=4)))',
53 'xapian que* test* user' => 'Xapian::Query((xapian:(pos=1) OR query:(pos=2) OR testable:(pos=3) OR tester:(pos=3) OR user:(pos=4)))',
54
55 'xapian que* user test*' => 'Xapian::Query((xapian:(pos=1) OR query:(pos=2) OR user:(pos=3) OR testable:(pos=4) OR tester:(pos=4)))',
56 );
57}
58
59foreach($tests as $test=>$expected)
60{
61 $query = $queryParser->parse_query(utf8_encode($test), $flags);
62 echo 'query: ', $test, "\n";
63 $result = $query->get_description();
64 echo 'result: ', $result, "\n";
65 if ($result === $expected)
66 {
67 echo "OK.\n";
68 }
69 else
70 {
71 echo 'expect: ', $expected, "\n";
72 echo "FAILS.\n";
73 }
74 echo "\n";
75}