1 |
<?php
|
2 |
/*
|
3 |
* $Id$
|
4 |
*
|
5 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
6 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
7 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
8 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
9 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
10 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
11 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
12 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
13 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
14 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
15 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
16 |
*
|
17 |
* This software consists of voluntary contributions made by many individuals
|
18 |
* and is licensed under the LGPL. For more information, see
|
19 |
* <http://www.phpdoctrine.com>.
|
20 |
*/
|
21 |
|
22 |
/**
|
23 |
* Doctrine_Search
|
24 |
*
|
25 |
* @package Doctrine
|
26 |
* @subpackage Search
|
27 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
28 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
29 |
* @version $Revision$
|
30 |
* @link www.phpdoctrine.com
|
31 |
* @since 1.0
|
32 |
*/
|
33 |
class Doctrine_Search extends Doctrine_Plugin
|
34 |
{
|
35 |
const INDEX_FILES = 0;
|
36 |
|
37 |
const INDEX_TABLES = 1;
|
38 |
|
39 |
protected $_options = array('generateFiles' => false,
|
40 |
'type' => self::INDEX_TABLES,
|
41 |
'className' => '%CLASS%Index',
|
42 |
'generatePath' => false,
|
43 |
'resource' => null,
|
44 |
'batchUpdates' => false,
|
45 |
'pluginTable' => false,
|
46 |
'fields' => array(),
|
47 |
'connection' => null);
|
48 |
|
49 |
protected $_built = false;
|
50 |
|
51 |
|
52 |
public function __construct(array $options)
|
53 |
{
|
54 |
$this->_options = array_merge($this->_options, $options);
|
55 |
|
56 |
if ( ! isset($this->_options['analyzer'])) {
|
57 |
$this->_options['analyzer'] = new Doctrine_Search_Analyzer_Standard();
|
58 |
}
|
59 |
if ( ! isset($this->_options['connection'])) {
|
60 |
$this->_options['connection'] = Doctrine_Manager::connection();
|
61 |
}
|
62 |
}
|
63 |
|
64 |
|
65 |
public function search($query)
|
66 |
{
|
67 |
$q = new Doctrine_Search_Query($this->_options['pluginTable']);
|
68 |
|
69 |
$q->query($query);
|
70 |
|
71 |
return $this->_options['connection']->fetchAll($q->getSql(), $q->getParams());;
|
72 |
}
|
73 |
|
74 |
public function analyze($text)
|
75 |
{
|
76 |
return $this->_options['analyzer']->analyze($text);
|
77 |
}
|
78 |
|
79 |
/**
|
80 |
* updateIndex
|
81 |
* updates the index
|
82 |
*
|
83 |
* @param Doctrine_Record $record
|
84 |
* @return integer
|
85 |
*/
|
86 |
public function updateIndex(array $data)
|
87 |
{
|
88 |
$this->buildDefinition();
|
89 |
|
90 |
$fields = $this->getOption('fields');
|
91 |
$class = $this->getOption('className');
|
92 |
$name = $this->getOption('resource')->getComponentName();
|
93 |
$conn = $this->getOption('resource')->getConnection();
|
94 |
$identifier = $this->_options['resource']->getIdentifier();
|
95 |
|
96 |
$q = Doctrine_Query::create()->delete()
|
97 |
->from($class);
|
98 |
foreach ((array) $identifier as $id) {
|
99 |
$q->addWhere($id . ' = ?', array($data[$id]));
|
100 |
}
|
101 |
$q->execute();
|
102 |
|
103 |
if ($this->_options['batchUpdates'] === true) {
|
104 |
$index = new $class();
|
105 |
|
106 |
foreach ((array) $this->_options['resource']->getIdentifier() as $id) {
|
107 |
$index->$id = $data[$id];
|
108 |
}
|
109 |
|
110 |
$index->save();
|
111 |
} else {
|
112 |
foreach ($fields as $field) {
|
113 |
|
114 |
$value = $data[$field];
|
115 |
|
116 |
$terms = $this->analyze($value);
|
117 |
|
118 |
foreach ($terms as $pos => $term) {
|
119 |
$index = new $class();
|
120 |
|
121 |
$index->keyword = $term;
|
122 |
$index->position = $pos;
|
123 |
$index->field = $field;
|
124 |
foreach ((array) $this->_options['resource']->getIdentifier() as $id) {
|
125 |
$index->$id = $data[$id];
|
126 |
}
|
127 |
|
128 |
$index->save();
|
129 |
}
|
130 |
}
|
131 |
}
|
132 |
}
|
133 |
|
134 |
public function readTableData($limit = null, $offset = null)
|
135 |
{
|
136 |
$this->buildDefinition();
|
137 |
|
138 |
$conn = $this->_options['resource']->getConnection();
|
139 |
$tableName = $this->_options['resource']->getTableName();
|
140 |
$id = $this->_options['resource']->getIdentifier();
|
141 |
|
142 |
$query = 'SELECT * FROM ' . $conn->quoteIdentifier($tableName)
|
143 |
. ' WHERE ' . $conn->quoteIdentifier($id)
|
144 |
. ' IN (SELECT ' . $conn->quoteIdentifier($id)
|
145 |
. ' FROM ' . $conn->quoteIdentifier($this->_options['pluginTable']->getTableName())
|
146 |
. ' WHERE keyword IS NULL)';
|
147 |
|
148 |
$query = $conn->modifyLimitQuery($query, $limit, $offset);
|
149 |
|
150 |
return $conn->fetchAll($query);
|
151 |
}
|
152 |
|
153 |
|
154 |
|
155 |
public function batchUpdateIndex($limit = null, $offset = null)
|
156 |
{
|
157 |
$this->buildDefinition();
|
158 |
|
159 |
$id = $this->_options['resource']->getIdentifier();
|
160 |
$class = $this->_options['className'];
|
161 |
$fields = $this->_options['fields'];
|
162 |
$conn = $this->_options['connection'];
|
163 |
try {
|
164 |
|
165 |
$conn->beginTransaction();
|
166 |
|
167 |
$rows = $this->readTableData($limit, $offset);
|
168 |
|
169 |
foreach ($rows as $row) {
|
170 |
$ids[] = $row[$id];
|
171 |
}
|
172 |
|
173 |
$conn->exec('DELETE FROM '
|
174 |
. $conn->quoteIdentifier($this->_options['pluginTable']->getTableName())
|
175 |
. ' WHERE ' . $conn->quoteIdentifier($id) . ' IN (' . implode(', ', $ids) . ')');
|
176 |
|
177 |
foreach ($rows as $row) {
|
178 |
foreach ($fields as $field) {
|
179 |
$data = $row[$field];
|
180 |
|
181 |
$terms = $this->analyze($data);
|
182 |
|
183 |
foreach ($terms as $pos => $term) {
|
184 |
$index = new $class();
|
185 |
|
186 |
$index->keyword = $term;
|
187 |
$index->position = $pos;
|
188 |
$index->field = $field;
|
189 |
|
190 |
foreach ((array) $id as $identifier) {
|
191 |
$index->$identifier = $row[$identifier];
|
192 |
}
|
193 |
|
194 |
$index->save();
|
195 |
}
|
196 |
}
|
197 |
}
|
198 |
|
199 |
$conn->commit();
|
200 |
} catch (Doctrine_Exception $e) {
|
201 |
$conn->rollback();
|
202 |
}
|
203 |
}
|
204 |
|
205 |
public function buildDefinition()
|
206 |
{
|
207 |
if ($this->_built) {
|
208 |
return true;
|
209 |
}
|
210 |
$this->_built = true;
|
211 |
|
212 |
$componentName = $this->_options['resource']->getComponentName();
|
213 |
|
214 |
// check for placeholders
|
215 |
if (strpos($this->_options['className'], '%') !== false) {
|
216 |
$this->_options['className'] = str_replace('%CLASS%', $componentName, $this->_options['className']);
|
217 |
}
|
218 |
|
219 |
$className = $this->getOption('className');
|
220 |
|
221 |
if (class_exists($className)) {
|
222 |
return false;
|
223 |
}
|
224 |
|
225 |
$columns = array('keyword' => array('type' => 'string',
|
226 |
'length' => 200,
|
227 |
'primary' => true,
|
228 |
),
|
229 |
'field' => array('type' => 'string',
|
230 |
'length' => 50,
|
231 |
'primary' => true),
|
232 |
'position' => array('type' => 'integer',
|
233 |
'length' => 8,
|
234 |
'primary' => true,
|
235 |
));
|
236 |
|
237 |
$id = $this->_options['resource']->getIdentifier();
|
238 |
|
239 |
$options = array('className' => $className);
|
240 |
|
241 |
$fk = $this->generateForeignKeys($this->_options['resource']);
|
242 |
$columns += $fk;
|
243 |
|
244 |
$relations = array();
|
245 |
// only generate relations for database based searches
|
246 |
if ( ! $this instanceof Doctrine_Search_File) {
|
247 |
$relations = $this->generateRelation($this->_options['resource'], $fk);
|
248 |
}
|
249 |
|
250 |
$this->generateClass($options, $columns, $relations);
|
251 |
|
252 |
$this->_options['pluginTable'] = $this->_options['connection']->getTable($this->_options['className']);
|
253 |
|
254 |
return true;
|
255 |
}
|
256 |
}
|