|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PSolr\Request; |
|
4
|
|
|
|
|
5
|
|
|
use Guzzle\Http\QueryString; |
|
6
|
|
|
use PSolr\Client\SolrClient; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Base class for PSolr request objects. |
|
10
|
|
|
*/ |
|
11
|
|
|
class SolrRequest extends QueryString |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var string|null |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $handlerName = null; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* $var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $responseClass = '\PSolr\Response\Response'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param array $params |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(array $params = array()) |
|
27
|
|
|
{ |
|
28
|
|
|
parent::__construct($params); |
|
29
|
|
|
$this->init(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param array $params |
|
34
|
|
|
* |
|
35
|
|
|
* @return \PSolr\Request\SolrRequest |
|
36
|
|
|
*/ |
|
37
|
|
|
static public function factory(array $params = array()) |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
|
|
return new static($params); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Initialization hook. Useful for setting default params. |
|
44
|
|
|
*/ |
|
45
|
|
|
public function init() {} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritDoc} |
|
49
|
|
|
* |
|
50
|
|
|
* Converts booleans to strings. |
|
51
|
|
|
*/ |
|
52
|
|
|
public function set($key, $value) |
|
53
|
|
|
{ |
|
54
|
|
|
if (is_bool($value)) { |
|
55
|
|
|
$value = $value ? 'true' : 'false'; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return parent::set($key, $value); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $handlerName |
|
63
|
|
|
* |
|
64
|
|
|
* @return \PSolr\Request\SolrRequest |
|
65
|
|
|
*/ |
|
66
|
|
|
public function setHandlerName($handlerName) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->handlerName = $handlerName; |
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getHandlerName() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->handlerName; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Renders the body, most often overridden by request objects that generate |
|
82
|
|
|
* JSON or XML, e.g. update requests. |
|
83
|
|
|
* |
|
84
|
|
|
* @return string|null |
|
85
|
|
|
*/ |
|
86
|
|
|
public function renderBody() |
|
87
|
|
|
{ |
|
88
|
|
|
return null; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Escape a value for use in XML. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $value |
|
95
|
|
|
* |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
|
|
public static function escapeXml($value) |
|
99
|
|
|
{ |
|
100
|
|
|
return htmlspecialchars($value, ENT_NOQUOTES, 'UTF-8'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Replace control (non-printable) characters from string that are invalid |
|
105
|
|
|
* to Solr's XML parser with a space. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $rawXml |
|
108
|
|
|
* |
|
109
|
|
|
* @return string |
|
110
|
|
|
* |
|
111
|
|
|
* @see http://drupalcode.org/project/apachesolr.git/blob/1dc510227f5077ccbc047be13dcf0de3120b100c:/Apache_Solr_Document.php#l399 |
|
112
|
|
|
*/ |
|
113
|
|
|
public static function stripCtrlChars($rawXml) |
|
114
|
|
|
{ |
|
115
|
|
|
// @see http://w3.org/International/questions/qa-forms-utf-8.html |
|
116
|
|
|
// Printable utf-8 does not include any of these chars below x7F |
|
117
|
|
|
return preg_replace('@[\x00-\x08\x0B\x0C\x0E-\x1F]@', ' ', $rawXml); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param \Psolr\Component\ComponentInterface $component |
|
122
|
|
|
* |
|
123
|
|
|
* @return \PSolr\Request\SolrRequest |
|
124
|
|
|
*/ |
|
125
|
|
|
public function addComponent(ComponentInterface $component) |
|
126
|
|
|
{ |
|
127
|
|
|
$component->preMergeParams($this); |
|
128
|
|
|
return $this->overwriteWith($component->toArray()); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param \PSolr\Request\SolrClient $solr |
|
133
|
|
|
* @param array|null $headers |
|
134
|
|
|
* @param array $options |
|
135
|
|
|
* |
|
136
|
|
|
* @return \PSolr\Response\Response|\SimpleXMLElement |
|
137
|
|
|
* |
|
138
|
|
|
* @throws \UnexpectedValueException |
|
139
|
|
|
*/ |
|
140
|
|
|
public function sendRequest(SolrClient $solr, $headers = null, array $options = array()) |
|
141
|
|
|
{ |
|
142
|
|
|
// If we don't have a request handler, then the passed request is only a |
|
143
|
|
|
// component and we cannot execute the search. |
|
144
|
|
|
if (null === $this->handlerName) { |
|
145
|
|
|
throw new \UnexpectedValueException('Unable to send request: handler name missing'); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
// @todo Add a method in \PSolr\Response\Response to normalize the XML |
|
149
|
|
|
// in to an array. Just use JSON and all is right in the world. |
|
150
|
|
|
$params = $this->toArray(); |
|
151
|
|
|
$data = $solr->sendRequest($this->handlerName, $params, $this->renderBody(), $headers, $options); |
|
152
|
|
|
return is_array($data) ? new $this->responseClass($data, $params) : $data; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|