1 | <?php |
---|
2 | /** |
---|
3 | * Copyright 2011 Bas de Nooijer. All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions are met: |
---|
7 | * |
---|
8 | * 1. Redistributions of source code must retain the above copyright notice, |
---|
9 | * this list of conditions and the following disclaimer. |
---|
10 | * |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
12 | * this listof conditions and the following disclaimer in the documentation |
---|
13 | * and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" |
---|
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
26 | * |
---|
27 | * The views and conclusions contained in the software and documentation are |
---|
28 | * those of the authors and should not be interpreted as representing official |
---|
29 | * policies, either expressed or implied, of the copyright holder. |
---|
30 | * |
---|
31 | * @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl> |
---|
32 | * @license http://github.com/basdenooijer/solarium/raw/master/COPYING |
---|
33 | * @link http://www.solarium-project.org/ |
---|
34 | * |
---|
35 | * @package Solarium |
---|
36 | * @subpackage Client |
---|
37 | */ |
---|
38 | |
---|
39 | /** |
---|
40 | * Build an update request |
---|
41 | * |
---|
42 | * @package Solarium |
---|
43 | * @subpackage Client |
---|
44 | */ |
---|
45 | class Solarium_Client_RequestBuilder_Update extends Solarium_Client_RequestBuilder |
---|
46 | { |
---|
47 | |
---|
48 | /** |
---|
49 | * Build request for an update query |
---|
50 | * |
---|
51 | * @param Solarium_Query_Update $query |
---|
52 | * @return Solarium_Client_Request |
---|
53 | */ |
---|
54 | public function build($query) |
---|
55 | { |
---|
56 | $request = parent::build($query); |
---|
57 | $request->setMethod(Solarium_Client_Request::METHOD_POST); |
---|
58 | $request->setRawData($this->getRawData($query)); |
---|
59 | |
---|
60 | return $request; |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * Generates raw POST data |
---|
65 | * |
---|
66 | * Each commandtype is delegated to a separate builder method. |
---|
67 | * |
---|
68 | * @param Solarium_Query_Update $query |
---|
69 | * @throws Solarium_Exception |
---|
70 | * @return string |
---|
71 | */ |
---|
72 | public function getRawData($query) |
---|
73 | { |
---|
74 | $xml = '<update>'; |
---|
75 | foreach ($query->getCommands() AS $command) { |
---|
76 | switch ($command->getType()) { |
---|
77 | case Solarium_Query_Update::COMMAND_ADD: |
---|
78 | $xml .= $this->buildAddXml($command); |
---|
79 | break; |
---|
80 | case Solarium_Query_Update::COMMAND_DELETE: |
---|
81 | $xml .= $this->buildDeleteXml($command); |
---|
82 | break; |
---|
83 | case Solarium_Query_Update::COMMAND_OPTIMIZE: |
---|
84 | $xml .= $this->buildOptimizeXml($command); |
---|
85 | break; |
---|
86 | case Solarium_Query_Update::COMMAND_COMMIT: |
---|
87 | $xml .= $this->buildCommitXml($command); |
---|
88 | break; |
---|
89 | case Solarium_Query_Update::COMMAND_ROLLBACK: |
---|
90 | $xml .= $this->buildRollbackXml(); |
---|
91 | break; |
---|
92 | default: |
---|
93 | throw new Solarium_Exception('Unsupported command type'); |
---|
94 | break; |
---|
95 | } |
---|
96 | } |
---|
97 | $xml .= '</update>'; |
---|
98 | |
---|
99 | return $xml; |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * Build XML for an add command |
---|
104 | * |
---|
105 | * @param Solarium_Query_Update_Command_Add $command |
---|
106 | * @return string |
---|
107 | */ |
---|
108 | public function buildAddXml($command) |
---|
109 | { |
---|
110 | $xml = '<add'; |
---|
111 | $xml .= $this->boolAttrib('overwrite', $command->getOverwrite()); |
---|
112 | $xml .= $this->attrib('commitWithin', $command->getCommitWithin()); |
---|
113 | $xml .= '>'; |
---|
114 | |
---|
115 | foreach ($command->getDocuments() AS $doc) { |
---|
116 | $xml .= '<doc'; |
---|
117 | $xml .= $this->attrib('boost', $doc->getBoost()); |
---|
118 | $xml .= '>'; |
---|
119 | |
---|
120 | foreach ($doc->getFields() AS $name => $value) { |
---|
121 | $boost = $doc->getFieldBoost($name); |
---|
122 | if (is_array($value)) { |
---|
123 | foreach ($value AS $multival) { |
---|
124 | $xml .= $this->_buildFieldXml($name, $boost, $multival); |
---|
125 | } |
---|
126 | } else { |
---|
127 | $xml .= $this->_buildFieldXml($name, $boost, $value); |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | $xml .= '</doc>'; |
---|
132 | } |
---|
133 | |
---|
134 | $xml .= '</add>'; |
---|
135 | |
---|
136 | return $xml; |
---|
137 | } |
---|
138 | |
---|
139 | /** |
---|
140 | * Build XML for a field |
---|
141 | * |
---|
142 | * Used in the add command |
---|
143 | * |
---|
144 | * @param string $name |
---|
145 | * @param float $boost |
---|
146 | * @param mixed $value |
---|
147 | * @return string |
---|
148 | */ |
---|
149 | protected function _buildFieldXml($name, $boost, $value) |
---|
150 | { |
---|
151 | $xml = '<field name="' . $name . '"'; |
---|
152 | $xml .= $this->attrib('boost', $boost); |
---|
153 | $xml .= '>' . htmlspecialchars($value, ENT_NOQUOTES, 'UTF-8'); |
---|
154 | $xml .= '</field>'; |
---|
155 | |
---|
156 | return $xml; |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | * Build XML for a delete command |
---|
161 | * |
---|
162 | * @param Solarium_Query_Update_Command_Delete $command |
---|
163 | * @return string |
---|
164 | */ |
---|
165 | public function buildDeleteXml($command) |
---|
166 | { |
---|
167 | $xml = '<delete>'; |
---|
168 | foreach ($command->getIds() AS $id) { |
---|
169 | $xml .= '<id>' . htmlspecialchars($id, ENT_NOQUOTES, 'UTF-8') |
---|
170 | . '</id>'; |
---|
171 | } |
---|
172 | foreach ($command->getQueries() AS $query) { |
---|
173 | $xml .= '<query>' . htmlspecialchars($query, ENT_NOQUOTES, 'UTF-8') |
---|
174 | . '</query>'; |
---|
175 | } |
---|
176 | $xml .= '</delete>'; |
---|
177 | |
---|
178 | return $xml; |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * Build XML for an update command |
---|
183 | * |
---|
184 | * @param Solarium_Query_Update_Command_Optimize $command |
---|
185 | * @return string |
---|
186 | */ |
---|
187 | public function buildOptimizeXml($command) |
---|
188 | { |
---|
189 | $xml = '<optimize'; |
---|
190 | $xml .= $this->boolAttrib('waitFlush', $command->getWaitFlush()); |
---|
191 | $xml .= $this->boolAttrib('waitSearcher', $command->getWaitSearcher()); |
---|
192 | $xml .= $this->attrib('maxSegments', $command->getMaxSegments()); |
---|
193 | $xml .= '/>'; |
---|
194 | |
---|
195 | return $xml; |
---|
196 | } |
---|
197 | |
---|
198 | /** |
---|
199 | * Build XML for a commit command |
---|
200 | * |
---|
201 | * @param Solarium_Query_Update_Command_Commit $command |
---|
202 | * @return string |
---|
203 | */ |
---|
204 | public function buildCommitXml($command) |
---|
205 | { |
---|
206 | $xml = '<commit'; |
---|
207 | $xml .= $this->boolAttrib('waitFlush', $command->getWaitFlush()); |
---|
208 | $xml .= $this->boolAttrib('waitSearcher', $command->getWaitSearcher()); |
---|
209 | $xml .= $this->boolAttrib( |
---|
210 | 'expungeDeletes', |
---|
211 | $command->getExpungeDeletes() |
---|
212 | ); |
---|
213 | $xml .= '/>'; |
---|
214 | |
---|
215 | return $xml; |
---|
216 | } |
---|
217 | |
---|
218 | /** |
---|
219 | * Build XMl for a rollback command |
---|
220 | * |
---|
221 | * @return string |
---|
222 | */ |
---|
223 | public function buildRollbackXml() |
---|
224 | { |
---|
225 | return '<rollback/>'; |
---|
226 | } |
---|
227 | |
---|
228 | } |
---|