1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Search; |
4
|
|
|
|
5
|
|
|
use CultuurNet\Search\Parameter; |
6
|
|
|
use CultuurNet\UDB3\Iri\IriGeneratorInterface; |
7
|
|
|
use CultuurNet\UDB3\SearchAPI2; |
8
|
|
|
use Psr\Log\LoggerInterface; |
9
|
|
|
use Psr\Log\NullLogger; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* SearchServiceInterface implementation that uses Search API 2 and parses |
13
|
|
|
* results with a XML pull parser, decreasing the memory usage and improving |
14
|
|
|
* speed. |
15
|
|
|
*/ |
16
|
|
|
class PullParsingSearchService implements SearchServiceInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var SearchAPI2\ResultSetPullParser |
20
|
|
|
*/ |
21
|
|
|
protected $pullParser; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var SearchAPI2\SearchServiceInterface |
25
|
|
|
*/ |
26
|
|
|
protected $searchAPI2; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var IriGeneratorInterface |
30
|
|
|
*/ |
31
|
|
|
protected $eventIriGenerator; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var IriGeneratorInterface |
35
|
|
|
*/ |
36
|
|
|
protected $placeIriGenerator; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var LoggerInterface |
40
|
|
|
*/ |
41
|
|
|
protected $resultParserLogger; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var bool |
45
|
|
|
*/ |
46
|
|
|
protected $includePrivateItems; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param SearchAPI2\SearchServiceInterface $search |
50
|
|
|
* @param IriGeneratorInterface $eventIriGenerator |
51
|
|
|
* @param IriGeneratorInterface $placeIriGenerator |
52
|
|
|
* @param LoggerInterface|null $resultParserLogger |
53
|
|
|
*/ |
54
|
|
|
public function __construct( |
55
|
|
|
SearchAPI2\SearchServiceInterface $search, |
56
|
|
|
IriGeneratorInterface $eventIriGenerator, |
57
|
|
|
IriGeneratorInterface $placeIriGenerator, |
58
|
|
|
LoggerInterface $resultParserLogger = null |
59
|
|
|
) { |
60
|
|
|
$this->searchAPI2 = $search; |
61
|
|
|
$this->eventIriGenerator = $eventIriGenerator; |
62
|
|
|
$this->placeIriGenerator = $placeIriGenerator; |
63
|
|
|
$this->resultParserLogger = $resultParserLogger ? $resultParserLogger : new NullLogger(); |
64
|
|
|
|
65
|
|
|
$this->includePrivateItems = true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
public function search($query, $limit = 30, $start = 0, $sort = null) |
72
|
|
|
{ |
73
|
|
|
$response = $this->executeSearch($query, $limit, $start, $sort); |
74
|
|
|
|
75
|
|
|
$parser = $this->getPullParser(); |
76
|
|
|
|
77
|
|
|
return $parser->getResultSet($response->getBody(true)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return PullParsingSearchService |
82
|
|
|
*/ |
83
|
|
|
public function doNotIncludePrivateItems() |
84
|
|
|
{ |
85
|
|
|
$copy = clone $this; |
86
|
|
|
$copy->includePrivateItems = false; |
87
|
|
|
return $copy; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Finds items matching an arbitrary query. |
92
|
|
|
* |
93
|
|
|
* @param string $query |
94
|
|
|
* An arbitrary query. |
95
|
|
|
* @param int $limit |
96
|
|
|
* How many items to retrieve. |
97
|
|
|
* @param int $start |
98
|
|
|
* Offset to start from. |
99
|
|
|
* @param string $sort |
100
|
|
|
* Sorting to use. |
101
|
|
|
* |
102
|
|
|
* @return \Guzzle\Http\Message\Response |
103
|
|
|
*/ |
104
|
|
|
private function executeSearch($query, $limit, $start, $sort = null) |
105
|
|
|
{ |
106
|
|
|
$qParam = new Parameter\Query($query); |
107
|
|
|
$groupParam = new Parameter\Group(); |
108
|
|
|
$startParam = new Parameter\Start($start); |
109
|
|
|
$limitParam = new Parameter\Rows($limit); |
110
|
|
|
$typeParam = new Parameter\FilterQuery('type:event OR (type:actor AND category_id:8.15.0.0.0)'); |
111
|
|
|
|
112
|
|
|
$params = array( |
113
|
|
|
$qParam, |
114
|
|
|
$groupParam, |
115
|
|
|
$limitParam, |
116
|
|
|
$startParam, |
117
|
|
|
$typeParam, |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
if ($this->includePrivateItems) { |
121
|
|
|
$privateParam = new Parameter\FilterQuery('private:*'); |
122
|
|
|
|
123
|
|
|
$params[] = $privateParam; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if ($sort) { |
|
|
|
|
127
|
|
|
$params[] = new Parameter\Parameter('sort', $sort); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$response = $this->searchAPI2->search($params); |
|
|
|
|
131
|
|
|
|
132
|
|
|
return $response; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return SearchAPI2\ResultSetPullParser |
137
|
|
|
*/ |
138
|
|
|
protected function getPullParser() |
139
|
|
|
{ |
140
|
|
|
if (!$this->pullParser) { |
141
|
|
|
$this->pullParser = new SearchAPI2\ResultSetPullParser( |
142
|
|
|
new \XMLReader(), |
143
|
|
|
$this->eventIriGenerator, |
144
|
|
|
$this->placeIriGenerator |
145
|
|
|
); |
146
|
|
|
$this->pullParser->setLogger($this->resultParserLogger); |
147
|
|
|
} |
148
|
|
|
return $this->pullParser; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: