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

Revision 7576, 12.8 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 * Update query
41 *
42 * Can be used to send multiple update commands to solr, e.g. add, delete,
43 * rollback, commit, optimize.
44 * Multiple commands of any type can be combined into a single update query.
45 *
46 * @package Solarium
47 * @subpackage Query
48 */
49class Solarium_Query_Update extends Solarium_Query
50{
51
52    /**
53     * Update command add
54     */
55    const COMMAND_ADD = 'add';
56
57    /**
58     * Update command delete
59     */
60    const COMMAND_DELETE = 'delete';
61
62    /**
63     * Update command commit
64     */
65    const COMMAND_COMMIT = 'commit';
66
67    /**
68     * Update command rollback
69     */
70    const COMMAND_ROLLBACK = 'rollback';
71
72    /**
73     * Update command optimize
74     */
75    const COMMAND_OPTIMIZE = 'optimize';
76
77    /**
78     * Update command types
79     *
80     * @var array
81     */
82    protected $_commandTypes = array(
83        self::COMMAND_ADD => 'Solarium_Query_Update_Command_Add',
84        self::COMMAND_DELETE => 'Solarium_Query_Update_Command_Delete',
85        self::COMMAND_COMMIT => 'Solarium_Query_Update_Command_Commit',
86        self::COMMAND_OPTIMIZE => 'Solarium_Query_Update_Command_Optimize',
87        self::COMMAND_ROLLBACK => 'Solarium_Query_Update_Command_Rollback',
88    );
89
90    /**
91     * Default options
92     *
93     * @var array
94     */
95    protected $_options = array(
96        'handler'       => 'update',
97        'resultclass'   => 'Solarium_Result_Update',
98        'documentclass' => 'Solarium_Document_ReadWrite',
99    );
100
101    /**
102     * Array of commands
103     *
104     * The commands will be executed in the order of this array, this can be
105     * important in some cases. For instance a rollback.
106     *
107     * @var array
108     */
109    protected $_commands = array();
110
111    /**
112     * Get type for this query
113     *
114     * @return string
115     */
116    public function getType()
117    {
118        return Solarium_Client::QUERYTYPE_UPDATE;
119    }
120
121    /**
122     * Initialize options
123     *
124     * Several options need some extra checks or setup work, for these options
125     * the setters are called.
126     *
127     * @return void
128     */
129    protected function _init()
130    {
131        if (isset($this->_options['command'])) {
132            foreach ($this->_options['command'] as $key => $value) {
133
134                $type = $value['type'];
135
136                if ($type == self::COMMAND_ADD) {
137                    throw new Solarium_Exception(
138                        "Adding documents is not supported in configuration, use the API for this"
139                    );
140                }
141
142                $this->add($key, $this->createCommand($type, $value));
143            }
144        }
145
146    }
147
148    /**
149     * Create a command instance
150     *
151     * @throws Solarium_Exception
152     * @param string $type
153     * @param mixed $options
154     * @return Solarium_Query_Update_Command
155     */
156    public function createCommand($type, $options = null)
157    {
158        $type = strtolower($type);
159
160        if (!isset($this->_commandTypes[$type])) {
161            throw new Solarium_Exception("Update commandtype unknown: " . $type);
162        }
163
164        $class = $this->_commandTypes[$type];
165        return new $class($options);
166    }
167
168    /**
169     * Get all commands for this update query
170     *
171     * @return array
172     */
173    public function getCommands()
174    {
175        return $this->_commands;
176    }
177
178    /**
179     * Add a command to this update query
180     *
181     * The command must be an instance of one of the Solarium_Query_Update_*
182     * classes.
183     *
184     * @param string $key
185     * @param object $command
186     * @return Solarium_Query_Update Provides fluent interface
187     */
188    public function add($key, $command)
189    {
190        if (0 !== strlen($key)) {
191            $this->_commands[$key] = $command;
192        } else {
193            $this->_commands[] = $command;
194        }
195
196        return $this;
197    }
198
199    /**
200     * Remove a command
201     *
202     * You can remove a command by passing it's key or by passing the command instance
203     *
204     * @param string|Solarium_Query_Update_Command $command
205     * @return Solarium_Query_Update Provides fluent interface
206     */
207    public function remove($command)
208    {
209        if (is_object($command)) {
210            foreach ($this->_commands as $key => $instance) {
211                if ($instance === $command) {
212                    unset($this->_commands[$key]);
213                    break;
214                }
215            }
216        } else {
217            if (isset($this->_commands[$command])) {
218                unset($this->_commands[$command]);
219            }
220        }
221
222        return $this;
223    }
224
225    /**
226     * Convenience method for adding a rollback command
227     *
228     * If you need more control, like choosing a key for the command you need to
229     * create you own command instance and use the add method.
230     *
231     * @return Solarium_Query_Update Provides fluent interface
232     */
233    public function addRollback()
234    {
235        return $this->add(null, new Solarium_Query_Update_Command_Rollback);
236    }
237
238    /**
239     * Convenience method for adding a delete query command
240     *
241     * If you need more control, like choosing a key for the command you need to
242     * create you own command instance and use the add method.
243     *
244     * @param string $query
245     * @return Solarium_Query_Update Provides fluent interface
246     */
247    public function addDeleteQuery($query)
248    {
249        $delete = new Solarium_Query_Update_Command_Delete;
250        $delete->addQuery($query);
251
252        return $this->add(null, $delete);
253    }
254
255    /**
256     * Convenience method to add a multi delete query command
257     *
258     * If you need more control, like choosing a key for the command you need to
259     * create you own command instance and use the add method.
260     *
261     * @param array $queries
262     * @return Solarium_Query_Update Provides fluent interface
263     */
264    public function addDeleteQueries($queries)
265    {
266        $delete = new Solarium_Query_Update_Command_Delete;
267        $delete->addQueries($queries);
268
269        return $this->add(null, $delete);
270    }
271
272    /**
273     * Convenience method to add a delete by ID command
274     *
275     * If you need more control, like choosing a key for the command you need to
276     * create you own command instance and use the add method.
277     *
278     * @param int|string $id
279     * @return Solarium_Query_Update Provides fluent interface
280     */
281    public function addDeleteById($id)
282    {
283        $delete = new Solarium_Query_Update_Command_Delete;
284        $delete->addId($id);
285
286        return $this->add(null, $delete);
287    }
288
289    /**
290     * Convenience method to add a delete by IDs command
291     *
292     * If you need more control, like choosing a key for the command you need to
293     * create you own command instance and use the add method.
294     *
295     * @param array $ids
296     * @return Solarium_Query_Update Provides fluent interface
297     */
298    public function addDeleteByIds($ids)
299    {
300        $delete = new Solarium_Query_Update_Command_Delete;
301        $delete->addIds($ids);
302
303        return $this->add(null, $delete);
304    }
305
306    /**
307     * Convenience method to add a 'add document' command
308     *
309     * If you need more control, like choosing a key for the command you need to
310     * create you own command instance and use the add method.
311     *
312     * @param Solarium_Document_ReadWrite $document
313     * @param boolean $overwrite
314     * @param int $commitWithin
315     * @return Solarium_Query_Update Provides fluent interface
316     */
317    public function addDocument($document, $overwrite = null,
318                                $commitWithin = null)
319    {
320        return $this->addDocuments(array($document), $overwrite, $commitWithin);
321    }
322
323    /**
324     * Convenience method to add a 'add documents' command
325     *
326     * If you need more control, like choosing a key for the command you need to
327     * create you own command instance and use the add method.
328     *
329     * @param array $documents
330     * @param boolean $overwrite
331     * @param int $commitWithin
332     * @return Solarium_Query_Update Provides fluent interface
333     */
334    public function addDocuments($documents, $overwrite = null,
335                                 $commitWithin = null)
336    {
337        $add = new Solarium_Query_Update_Command_Add;
338        if (null !== $overwrite) $add->setOverwrite($overwrite);
339        if (null !== $commitWithin) $add->setCommitWithin($commitWithin);
340
341        $add->addDocuments($documents);
342        return $this->add(null, $add);
343    }
344
345    /**
346     * Convenience method to add a commit command
347     *
348     * If you need more control, like choosing a key for the command you need to
349     * create you own command instance and use the add method.
350     *
351     * @param boolean $waitFlush
352     * @param boolean $waitSearcher
353     * @param boolean $expungeDeletes
354     * @return Solarium_Query_Update Provides fluent interface
355     */
356    public function addCommit($waitFlush = null, $waitSearcher = null,
357                              $expungeDeletes = null)
358    {
359        $commit = new Solarium_Query_Update_Command_Commit();
360        if (null !== $waitFlush) $commit->setWaitFlush($waitFlush);
361        if (null !== $waitSearcher) $commit->setWaitSearcher($waitSearcher);
362        if (null !== $expungeDeletes)
363            $commit->setExpungeDeletes($expungeDeletes);
364
365        return $this->add(null, $commit);
366    }
367
368    /**
369     * Convenience method to add an optimize command
370     *
371     * If you need more control, like choosing a key for the command you need to
372     * create you own command instance and use the add method.
373     *
374     * @param boolean $waitFlush
375     * @param boolean $waitSearcher
376     * @param int $maxSegments
377     * @return Solarium_Query_Update Provides fluent interface
378     */
379   public function addOptimize($waitFlush = null, $waitSearcher = null,
380                               $maxSegments = null)
381   {
382       $optimize = new Solarium_Query_Update_Command_Optimize();
383       if (null !== $waitFlush) $optimize->setWaitFlush($waitFlush);
384       if (null !== $waitSearcher) $optimize->setWaitSearcher($waitSearcher);
385       if (null !== $maxSegments) $optimize->setMaxSegments($maxSegments);
386
387       return $this->add(null, $optimize);
388   }
389
390   /**
391    * Set a custom document class for use in the createDocument method
392    *
393    * This class should extend Solarium_Document_ReadWrite or
394    * at least be compatible with it's interface
395    *
396    * @param string $value classname
397    * @return Solarium_Query
398    */
399    public function setDocumentClass($value)
400    {
401        return $this->_setOption('documentclass', $value);
402    }
403
404    /**
405     * Get the current documentclass option
406     *
407     * The value is a classname, not an instance
408     *
409     * @return string
410     */
411    public function getDocumentClass()
412    {
413        return $this->getOption('documentclass');
414    }
415
416    /**
417     * Create a document object instance
418     *
419     * You can optionally directly supply the fields and boosts
420     * to get a ready-made document instance for direct use in an add command
421     *
422     * @since 2.1.0
423     *
424     * @param array $fields
425     * @param array $boosts
426     * @return Solarium_Document_ReadWrite
427     */
428    public function createDocument($fields = array(), $boosts = array())
429    {
430        $class = $this->getDocumentClass();
431
432        return new $class($fields, $boosts);
433    }
434
435}
Note: See TracBrowser for help on using the repository browser.