source: sandbox/expresso-solr/expressoMail1_2/inc/solrclient/library/Solarium/Query/Helper.php @ 7576

Revision 7576, 12.1 KB checked in by adir, 11 years ago (diff)

Ticket #000 - Adicionando a integracao de buscas com Solr na base a ser isnerida na comunidade

Line 
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 Query
37 */
38
39/**
40 * Query helper
41 *
42 * Generates small snippets for use in queries, filterqueries and sorting
43 *
44 * @package Solarium
45 * @subpackage Query
46 */
47class Solarium_Query_Helper
48{
49
50    /**
51     * Placeholder pattern for use in the assemble method
52     *
53     * @var string
54     */
55    protected $_placeHolderPattern = '/%(L|P|T|)([0-9]+)%/i';
56
57    /**
58     * Array of parts to use for assembling a query string
59     *
60     * @var array
61     */
62    protected $_assembleParts;
63
64    /**
65     * Counter to keep dereferenced params unique (within a single query instance)
66     *
67     * @var int
68     */
69    protected $_derefencedParamsLastKey = 0;
70
71    /**
72     * Solarium_Query instance, optional.
73     * Used for dereferenced params.
74     *
75     * @var Solarium_Query
76     */
77    protected $_query;
78
79    /**
80     * Constructor
81     *
82     * @param Solarium_Query $query
83     */
84    public function __construct($query = null)
85    {
86        $this->_query = $query;
87    }
88
89    /**
90     * Escape a term
91     *
92     * A term is a single word.
93     * All characters that have a special meaning in a Solr query are escaped.
94     *
95     * If you want to use the input as a phrase please use the {@link phrase()}
96     * method, because a phrase requires much less escaping.\
97     *
98     * @link http://lucene.apache.org/java/docs/queryparsersyntax.html#Escaping%20Special%20Characters
99     *
100     * @param string $input
101     * @return string
102     */
103    public function escapeTerm($input)
104    {
105        $pattern = '/(\+|-|&&|\|\||!|\(|\)|\{|}|\[|]|\^|"|~|\*|\?|:|\\\)/';
106
107        return preg_replace($pattern, '\\\$1', $input);
108    }
109
110    /**
111     * Escape a phrase
112     *
113     * A phrase is a group of words.
114     * Special characters will be escaped and the phrase will be surrounded by
115     * double quotes to group the input into a single phrase. So don't put
116     * quotes around the input.
117     *
118     * Do mind that you cannot build a complete query first and then pass it to
119     * this method, the whole query will be escaped. You need to escape only the
120     * 'content' of your query.
121     *
122     * @param string $input
123     * @return string
124     */
125    public function escapePhrase($input)
126    {
127        return '"' . preg_replace('/("|\\\)/', '\\\$1', $input) . '"';
128    }
129
130    /**
131     * Format a date to the expected formatting used in SOLR
132     *
133     * This format was derived to be standards compliant (ISO 8601)
134     * A date field shall be of the form 1995-12-31T23:59:59Z The trailing "Z" designates UTC time and is mandatory
135     *
136     * @see http://lucene.apache.org/solr/api/org/apache/solr/schema/DateField.html
137     *
138     * @param int|string|DateTime $input accepted formats: timestamp, date string or DateTime
139     * @return string|false false is returned in case of invalid input
140     */
141    public function formatDate($input)
142    {
143        switch(true){
144
145
146            // input of datetime object
147            case $input instanceof DateTime:
148                // no work needed
149                break;
150
151
152            // input of timestamp or date/time string
153            case is_string($input) || is_numeric($input):
154
155                // if date/time string: convert to timestamp first
156                if (is_string($input)) $input = strtotime($input);
157
158                // now try converting the timestamp to a datetime instance, on failure return false
159                try {
160                    $input = new DateTime('@' . $input);
161                } catch (Exception $e) {
162                    $input = false;
163                }
164                break;
165
166
167            // any other input formats can be added in additional cases here...
168            // case $input instanceof Zend_Date:
169
170
171            // unsupported input format
172            default:
173                $input = false;
174                break;
175        }
176
177
178        // handle the filtered input
179        if ($input) {
180            // when we get here the input is always a datetime object
181            $input->setTimezone(new DateTimeZone('UTC'));
182            $iso8601 = $input->format(DateTime::ISO8601);
183            $iso8601 = strstr($iso8601, '+', true); //strip timezone
184            $iso8601 .= 'Z';
185            return $iso8601;
186        } else {
187            // unsupported input
188            return false;
189        }
190    }
191
192    /**
193     * Render a range query
194     *
195     * From and to can be any type of data. For instance int, string or point.
196     *
197     * Example: rangeQuery('store', '45,-94', '46,-93')
198     * Returns: store:[45,-94 TO 46,-93]
199     *
200     * @param string $field
201     * @param string $from
202     * @param string $to
203     * @param boolean $inclusive
204     * @return string
205     */
206    public function rangeQuery($field, $from, $to, $inclusive = true)
207    {
208        if ($inclusive) {
209            return $field . ':[' . $from . ' TO ' . $to . ']';
210        } else {
211            return $field . ':{' . $from . ' TO ' . $to . '}';
212        }
213    }
214
215    /**
216     * Render a geofilt (distance) filter
217     *
218     * Find all entries within the distance of a certain point.
219     *
220     * @param  $pointX
221     * @param  $pointY
222     * @param  $field
223     * @param  $distance
224     * @return string
225     */
226    public function geofilt($pointX, $pointY, $field, $distance)
227    {
228        return $this->qparser(
229            'geofilt',
230            array(
231                'pt' => $pointX.','.$pointY,
232                'sfield' => $field,
233                'd' => $distance
234            )
235        );
236    }
237
238    /**
239     * Render a bbox (boundingbox) filter
240     *
241     * Exact distance calculations can be somewhat expensive and it can often
242     * make sense to use a quick approximation instead. The bbox filter is
243     * guaranteed to encompass all of the points of interest, but it may also
244     * include other points that are slightly outside of the required distance.
245     *
246     * @param string $pointX
247     * @param string $pointY
248     * @param string $field
249     * @param string $distance
250     * @return string
251     */
252    public function bbox($pointX, $pointY, $field, $distance)
253    {
254        return $this->qparser(
255            'bbox',
256            array(
257                'pt' => $pointX.','.$pointY,
258                'sfield' => $field,
259                'd' => $distance
260            )
261        );
262    }
263
264    /**
265     * Render a geodist function call
266     *
267     * geodist is a function query that yields the calculated distance.
268     * This gives the flexibility to do a number of interesting things,
269     * such as sorting by the distance (Solr can sort by any function query),
270     * or combining the distance with the relevancy score,
271     * such as boosting by the inverse of the distance.
272     *
273     * @param  $pointX
274     * @param  $pointY
275     * @param  $field
276     * @return string
277     */
278    public function geodist($pointX, $pointY, $field)
279    {
280        return $this->functionCall(
281            'geodist',
282            array($pointX, $pointY, $field)
283        );
284    }
285
286    /**
287     * Render a qparser plugin call
288     *
289     * @param string $name
290     * @param array $params
291     * @param boolean $dereferenced
292     * @return string
293     */
294    public function qparser($name, $params = array(), $dereferenced = false)
295    {
296        if ($dereferenced) {
297
298            if (!$this->_query) {
299                throw new Solarium_Exception(
300                    'Dereferenced params can only be used in a Solarium_Query_Helper instance retrieved from the query '
301                    . 'by using the getHelper() method, this instance was manually created'
302                );
303            }
304
305            foreach ($params as $paramKey => $paramValue) {
306                $this->_derefencedParamsLastKey++;
307                $derefKey = 'deref_' . $this->_derefencedParamsLastKey;
308                $this->_query->addParam($derefKey, $paramValue);
309                $params[$paramKey] = '$'.$derefKey;
310            }
311        }
312
313        $output = '{!'.$name;
314        foreach ($params as $key=>$value) {
315            $output .= ' ' . $key . '=' . $value;
316        }
317        $output .= '}';
318
319        return $output;
320    }
321
322    /**
323     * Render a functionCall
324     *
325     * @param string $name
326     * @param array $params
327     * @return string
328     */
329    public function functionCall($name, $params = array())
330    {
331        return $name . '(' . implode($params, ',') . ')';
332    }
333
334    /**
335     * Assemble a querystring with placeholders
336     *
337     * These placeholder modes are supported:
338     * %1% = no mode, will default to literal
339     * %L2% = literal
340     * %P3% = phrase-escaped
341     * %T4% = term-escaped
342     *
343     * Numbering starts at 1, so number 1 refers to the first entry
344     * of $parts (which has array key 0)
345     * You can use the same part multiple times, even in multiple modes.
346     * The mode letters are not case sensitive.
347     *
348     * The mode matching pattern can be customized by overriding the
349     * value of $this->_placeHolderPattern
350     *
351     * @since 2.1.0
352     *
353     * @param string $query
354     * @param array $parts Array of strings
355     * @return string
356     */
357    public function assemble($query, $parts)
358    {
359        $this->_assembleParts = $parts;
360
361        return preg_replace_callback(
362            $this->_placeHolderPattern,
363            array($this, '_renderPlaceHolder'),
364            $query
365        );
366    }
367
368    /**
369     * Render placeholders in a querystring
370     *
371     * @throws Solarium_Exception
372     * @param array $matches
373     * @return string
374     */
375    protected function _renderPlaceHolder($matches)
376    {
377        $partNumber = $matches[2];
378        $partMode = strtoupper($matches[1]);
379
380        if (isset($this->_assembleParts[$partNumber-1])) {
381            $value = $this->_assembleParts[$partNumber-1];
382        } else {
383            throw new Solarium_Exception('No value supplied for part #' . $partNumber . ' in query assembler');
384        }
385
386        switch($partMode)
387        {
388            case 'P':
389                $value = $this->escapePhrase($value);
390                break;
391            case 'T':
392                $value = $this->escapeTerm($value);
393                break;
394        }
395
396        return $value;
397    }
398
399    /**
400     * Render join localparams syntax
401     *
402     * @see http://wiki.apache.org/solr/Join
403     * @since 2.4.0
404     *
405     * @param string $from
406     * @param string $to
407     * @param boolean $dereferenced
408     * @return string
409     */
410    public function join($from, $to, $dereferenced = false)
411    {
412        return $this->qparser('join', array('from' => $from, 'to' => $to), $dereferenced);
413    }
414
415}
Note: See TracBrowser for help on using the repository browser.