# Source for file Search.php

Documentation is available at Search.php

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. */
22. /**
23. * Doctrine_Search
24. *
25. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
26. * @package Doctrine
27. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
28. * @version $Revision$
29. * @category Object Relational Mapping
30. * @link www.phpdoctrine.com
31. * @since 1.0
32. */
33. class Doctrine_Search
34. {
35. protected $_options = array('generateFiles' => true,
36. 'className' => '%CLASS%Index');
39. public function __construct(array $options)
40. {
41. $this->_options = array_merge($this->_options, $options);
43. if ( ! isset($this->_options['analyzer'])) {
44. $this->_options['analyzer'] = new Doctrine_Search_Analyzer_Standard();
45. }
46. }
48. public function getOption($option)
49. {
50. if (isset($this->_options[$option])) {
51. return $this->_options[$option];
52. }
54. throw new Doctrine_Search_Exception('Unknown option ' . $option);
55. }
57. public function analyze($text)
58. {
59. return $this->_options['analyzer']->analyze($text);
60. }
62. public function setOption($option, $value)
63. {
64. $this->_options[$option] = $value;
66. return $this;
67. }
68. /**
69. * updateIndex
70. * updates the index
71. *
72. * @param Doctrine_Record $record
73. * @return integer
74. */
75. public function updateIndex(Doctrine_Record $record)
76. {
77. $fields = $this->getOption('fields');
78. $class = $this->getOption('className');
79. $name = $record->getTable()->getComponentName();
81. foreach ($fields as $field) {
82. $data = $record->get($field);
84. $terms = $this->analyze($data);
86. foreach ($terms as $pos => $term) {
87. $index = new $class();
89. $index->keyword = $term;
90. $index->position = $pos;
91. $index->field = $field;
92. $index->$name = $record;
94. $index->save();
95. }
96. }
97. }
98. public function buildDefinition(Doctrine_Table $table)
99. {
100. $name = $table->getComponentName();
102. $className = $this->getOption('className');
104. if (class_exists($className)) {
105. return false;
106. }
108. $columns = array('keyword' => array('type' => 'string',
109. 'length' => 200,
110. 'notnull' => true,
111. 'primary' => true,
112. ),
113. 'field' => array('type' => 'string',
114. 'length' => 50,
115. 'notnull' => true,
116. 'primary' => true),
117. 'position' => array('type' => 'integer',
118. 'length' => 8,
119. 'primary' => true,
120. ));
122. $id = $table->getIdentifier();
124. $options = array('className' => $className);
127. $fk = array();
128. foreach ((array) $id as $column) {
129. $def = $table->getDefinitionOf($column);
131. unset($def['autoincrement']);
132. unset($def['sequence']);
133. unset($def['primary']);
135. $col = strtolower(Doctrine::tableize($name) . '_' . $column);
137. $def['primary'] = true;
138. $fk[$col] = $def;
139. }
141. $local = (count($fk) > 1) ? array_keys($fk) : key($fk);
143. $relations = array($name => array('local' => $local,
144. 'foreign' => $id,
145. 'onDelete' => 'CASCADE',
146. 'onUpdate' => 'CASCADE'));
149. $columns += $fk;
151. $builder = new Doctrine_Import_Builder();
153. $def = $builder->buildDefinition($options, $columns, $relations);
155. if ( ! $this->_options['generateFiles']) {
156. eval($def);
157. }
158. return true;
159. }
160. }