document.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <?php
  2. /**
  3. * Copyright (c) 2007-2009, Conduit Internet Technologies, Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * - Neither the name of Conduit Internet Technologies, Inc. nor the names of
  15. * its contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * @copyright Copyright 2007-2009 Conduit Internet Technologies, Inc. (http://conduit-it.com)
  31. * @license New BSD (http://solr-php-client.googlecode.com/svn/trunk/COPYING)
  32. * @version $Id: Document.php 15 2009-08-04 17:53:08Z donovan.jimenez $
  33. *
  34. * @package Apache
  35. * @subpackage Solr
  36. * @author Donovan Jimenez <djimenez@conduit-it.com>
  37. */
  38. /**
  39. * Additional code Copyright (c) 2011 by Peter Wolanin, and
  40. * additional contributors.
  41. *
  42. * This program is free software; you can redistribute it and/or modify
  43. * it under the terms of the GNU General Public License as published by
  44. * the Free Software Foundation; either version 2 of the License, or (at
  45. * your option) any later version.
  46. *
  47. * This program is distributed in the hope that it will be useful, but
  48. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  49. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  50. * for more details.
  51. *
  52. * You should have received a copy of the GNU General Public License
  53. * along with this program as the file LICENSE.txt; if not, please see
  54. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  55. */
  56. /**
  57. * Holds Key / Value pairs that represent a Solr Document along with any
  58. * associated boost values. Field values can be accessed by direct dereferencing
  59. * such as:
  60. *
  61. * @code
  62. * $document->title = 'Something';
  63. * echo $document->title;
  64. * @endcode
  65. *
  66. * Additionally, the field values can be iterated with foreach:
  67. *
  68. * @code
  69. * foreach ($document as $fieldName => $fieldValue) {
  70. * // ...
  71. * }
  72. * @endcode
  73. */
  74. class SearchApiSolrDocument implements IteratorAggregate {
  75. /**
  76. * Document boost value.
  77. *
  78. * @var float|false
  79. */
  80. protected $documentBoost = FALSE;
  81. /**
  82. * Document field values, indexed by name.
  83. *
  84. * @var array
  85. */
  86. protected $fields = array();
  87. /**
  88. * Document field boost values, indexed by name.
  89. *
  90. * @var array
  91. */
  92. protected $fieldBoosts = array();
  93. /**
  94. * Clears all boosts and fields from this document.
  95. */
  96. public function clear() {
  97. $this->documentBoost = FALSE;
  98. $this->fields = array();
  99. $this->fieldBoosts = array();
  100. }
  101. /**
  102. * Gets the current document boost.
  103. *
  104. * @return float|false
  105. * The current document boost, or FALSE if none is set.
  106. */
  107. public function getBoost() {
  108. return $this->documentBoost;
  109. }
  110. /**
  111. * Sets the document boost factor.
  112. *
  113. * @param float|false $boost
  114. * FALSE for default boost, or a positive number for setting a document
  115. * boost.
  116. */
  117. public function setBoost($boost) {
  118. $boost = (float) $boost;
  119. if ($boost > 0.0) {
  120. $this->documentBoost = $boost;
  121. }
  122. else {
  123. $this->documentBoost = FALSE;
  124. }
  125. }
  126. /**
  127. * Adds a value to a multi-valued field
  128. *
  129. * NOTE: the solr XML format allows you to specify boosts PER value even
  130. * though the underlying Lucene implementation only allows a boost per field.
  131. * To remedy this, the final field boost value will be the product of all
  132. * specified boosts on field values - this is similar to SolrJ's
  133. * functionality.
  134. *
  135. * @code
  136. * $doc = new ApacheSolrDocument();
  137. * $doc->addField('foo', 'bar', 2.0);
  138. * $doc->addField('foo', 'baz', 3.0);
  139. * // Resultant field boost will be 6!
  140. * echo $doc->getFieldBoost('foo');
  141. * @endcode
  142. *
  143. * @param string $key
  144. * The name of the field.
  145. * @param $value
  146. * The value to add for the field.
  147. * @param float|false $boost
  148. * FALSE for default boost, or a positive number for setting a field boost.
  149. */
  150. public function addField($key, $value, $boost = FALSE) {
  151. if (!isset($this->fields[$key])) {
  152. // create holding array if this is the first value
  153. $this->fields[$key] = array();
  154. }
  155. else if (!is_array($this->fields[$key])) {
  156. // move existing value into array if it is not already an array
  157. $this->fields[$key] = array($this->fields[$key]);
  158. }
  159. if ($this->getFieldBoost($key) === FALSE) {
  160. // boost not already set, set it now
  161. $this->setFieldBoost($key, $boost);
  162. }
  163. else if ((float) $boost > 0.0) {
  164. // multiply passed boost with current field boost - similar to SolrJ implementation
  165. $this->fieldBoosts[$key] *= (float) $boost;
  166. }
  167. // add value to array
  168. $this->fields[$key][] = $value;
  169. }
  170. /**
  171. * Gets information about a field stored in Solr.
  172. *
  173. * @param string $key
  174. * The name of the field.
  175. *
  176. * @return array|false
  177. * An associative array of info if the field exists, FALSE otherwise.
  178. */
  179. public function getField($key) {
  180. if (isset($this->fields[$key])) {
  181. return array(
  182. 'name' => $key,
  183. 'value' => $this->fields[$key],
  184. 'boost' => $this->getFieldBoost($key)
  185. );
  186. }
  187. return FALSE;
  188. }
  189. /**
  190. * Sets a field value.
  191. *
  192. * Multi-valued fields should be set as arrays or via the addField()
  193. * function which will automatically make sure the field is an array.
  194. *
  195. * @param string $key
  196. * The name of the field.
  197. * @param string|array $value
  198. * The value to set for the field.
  199. * @param float|false $boost
  200. * FALSE for default boost, or a positive number for setting a field boost.
  201. */
  202. public function setField($key, $value, $boost = FALSE) {
  203. $this->fields[$key] = $value;
  204. $this->setFieldBoost($key, $boost);
  205. }
  206. /**
  207. * Gets the currently set field boost for a document field.
  208. *
  209. * @param string $key
  210. * The name of the field.
  211. *
  212. * @return float|false
  213. * The currently set field boost, or FALSE if none was set.
  214. */
  215. public function getFieldBoost($key) {
  216. return isset($this->fieldBoosts[$key]) ? $this->fieldBoosts[$key] : FALSE;
  217. }
  218. /**
  219. * Sets the field boost for a document field.
  220. *
  221. * @param string $key
  222. * The name of the field.
  223. * @param float|false $boost
  224. * FALSE for default boost, or a positive number for setting a field boost.
  225. */
  226. public function setFieldBoost($key, $boost) {
  227. $boost = (float) $boost;
  228. if ($boost > 0.0) {
  229. $this->fieldBoosts[$key] = $boost;
  230. }
  231. else {
  232. $this->fieldBoosts[$key] = FALSE;
  233. }
  234. }
  235. /**
  236. * Returns all current field boosts, indexed by field name.
  237. *
  238. * @return array
  239. * An associative array in the format $field_name => $field_boost.
  240. */
  241. public function getFieldBoosts() {
  242. return $this->fieldBoosts;
  243. }
  244. /**
  245. * Gets the names of all fields in this document.
  246. *
  247. * @return array
  248. * The names of all fields in this document.
  249. */
  250. public function getFieldNames() {
  251. return array_keys($this->fields);
  252. }
  253. /**
  254. * Gets the values of all fields in this document.
  255. *
  256. * @return array
  257. * The values of all fields in this document.
  258. */
  259. public function getFieldValues() {
  260. return array_values($this->fields);
  261. }
  262. /**
  263. * Implements IteratorAggregate::getIterator().
  264. *
  265. * Implementing the IteratorAggregate interface allows the following usage:
  266. * @code
  267. * foreach ($document as $key => $value) {
  268. * // ...
  269. * }
  270. * @endcode
  271. *
  272. * @return Traversable
  273. * An iterator over this document's fields.
  274. */
  275. public function getIterator() {
  276. $arrayObject = new ArrayObject($this->fields);
  277. return $arrayObject->getIterator();
  278. }
  279. /**
  280. * Magic getter for field values.
  281. *
  282. * @param string $key
  283. * The name of the field.
  284. *
  285. * @return string|array|null
  286. * The value that was set for the field.
  287. */
  288. public function __get($key) {
  289. return $this->fields[$key];
  290. }
  291. /**
  292. * Magic setter for field values.
  293. *
  294. * Multi-valued fields should be set as arrays or via the addField() function
  295. * which will automatically make sure the field is an array.
  296. *
  297. * @param string $key
  298. * The name of the field.
  299. * @param string|array $value
  300. * The value to set for the field.
  301. */
  302. public function __set($key, $value) {
  303. $this->setField($key, $value);
  304. }
  305. /**
  306. * Magic isset for fields values.
  307. *
  308. * Do not call directly. Allows the following usage:
  309. * @code
  310. * isset($document->some_field);
  311. * @endcode
  312. *
  313. * @param string $key
  314. * The name of the field.
  315. *
  316. * @return bool
  317. * Whether the given key is set in this document.
  318. */
  319. public function __isset($key) {
  320. return isset($this->fields[$key]);
  321. }
  322. /**
  323. * Magic unset for field values.
  324. *
  325. * Do not call directly. Allows the following usage:
  326. * @code
  327. * unset($document->some_field);
  328. * @endcode
  329. *
  330. * @param string $key
  331. * The name of the field.
  332. */
  333. public function __unset($key) {
  334. unset($this->fields[$key]);
  335. unset($this->fieldBoosts[$key]);
  336. }
  337. /**
  338. * Create an XML fragment from this document.
  339. *
  340. * This string can then be used inside a Solr add call.
  341. *
  342. * @return string
  343. * An XML formatted string for this document.
  344. */
  345. public function toXml() {
  346. $xml = '<doc';
  347. if ($this->documentBoost !== FALSE) {
  348. $xml .= ' boost="' . $this->documentBoost . '"';
  349. }
  350. $xml .= '>';
  351. foreach ($this->fields as $key => $value) {
  352. $fieldBoost = $this->getFieldBoost($key);
  353. $key = htmlspecialchars($key, ENT_COMPAT, 'UTF-8');
  354. if (is_array($value)) {
  355. foreach ($value as $multivalue) {
  356. $xml .= '<field name="' . $key . '"';
  357. if ($fieldBoost !== FALSE) {
  358. $xml .= ' boost="' . $fieldBoost . '"';
  359. // Only set the boost for the first field in the set.
  360. $fieldBoost = FALSE;
  361. }
  362. $xml .= '>' . htmlspecialchars($multivalue, ENT_NOQUOTES, 'UTF-8') . '</field>';
  363. }
  364. }
  365. else {
  366. $xml .= '<field name="' . $key . '"';
  367. if ($fieldBoost !== FALSE) {
  368. $xml .= ' boost="' . $fieldBoost . '"';
  369. }
  370. $xml .= '>' . htmlspecialchars($value, ENT_NOQUOTES, 'UTF-8') . '</field>';
  371. }
  372. }
  373. $xml .= '</doc>';
  374. // Remove any control characters to avoid Solr XML parser exception.
  375. return self::stripCtrlChars($xml);
  376. }
  377. /**
  378. * Sanitizes XML for sending to Solr.
  379. *
  380. * Replaces control (non-printable) characters that are invalid to Solr's XML
  381. * parser with a space.
  382. *
  383. * @param string $string
  384. * The string to sanitize.
  385. *
  386. * @return string
  387. * A string safe for including in a Solr request.
  388. */
  389. public static function stripCtrlChars($string) {
  390. // See: http://w3.org/International/questions/qa-forms-utf-8.html
  391. // Printable utf-8 does not include any of these chars below x7F
  392. return preg_replace('@[\x00-\x08\x0B\x0C\x0E-\x1F]@', ' ', $string);
  393. }
  394. }