Search.php 5.2 KB
Newer Older
zYne's avatar
zYne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
<?php
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */

/**
 * Doctrine_Search
 *
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @package     Doctrine
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @version     $Revision$
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 */
class Doctrine_Search
{
zYne's avatar
zYne committed
35 36
    protected $_options = array('generateFiles' => true,
                                'className'     => '%CLASS%Index');
zYne's avatar
zYne committed
37

zYne's avatar
zYne committed
38 39 40 41
    
    public function __construct(array $options)
    {
        $this->_options = array_merge($this->_options, $options);
42 43 44 45
        
        if ( ! isset($this->_options['analyzer'])) {
            $this->_options['analyzer'] = new Doctrine_Search_Analyzer_Standard();
        }
zYne's avatar
zYne committed
46 47 48 49 50
    }

    public function getOption($option)
    {
        if (isset($this->_options[$option])) {
51
            return $this->_options[$option];
zYne's avatar
zYne committed
52 53
        }
        
zYne's avatar
zYne committed
54
        throw new Doctrine_Search_Exception('Unknown option ' . $option);
zYne's avatar
zYne committed
55 56
    }
    
57 58
    public function analyze($text)
    {
zYne's avatar
zYne committed
59
        return $this->_options['analyzer']->analyze($text);
60 61
    }

zYne's avatar
zYne committed
62 63 64 65 66 67
    public function setOption($option, $value)
    {
        $this->_options[$option] = $value;

        return $this;
    }
zYne's avatar
zYne committed
68 69 70 71 72 73 74
    /**
     * updateIndex
     * updates the index
     *
     * @param Doctrine_Record $record
     * @return integer
     */
zYne's avatar
zYne committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    public function updateIndex(Doctrine_Record $record) 
    {
    	$fields = $this->getOption('fields');
        $class  = $this->getOption('className');
        $name   = $record->getTable()->getComponentName();

        foreach ($fields as $field) {
            $data  = $record->get($field);

            $terms = $this->analyze($data);

            foreach ($terms as $pos => $term) {
                $index = new $class();

                $index->keyword = $term;
                $index->position = $pos;
                $index->field = $field;
                $index->$name = $record;
                
                $index->save();
            }
        }
    }
zYne's avatar
zYne committed
98
    public function buildDefinition(Doctrine_Table $table)
zYne's avatar
zYne committed
99
    {
zYne's avatar
zYne committed
100 101
        $name = $table->getComponentName();

zYne's avatar
zYne committed
102
        $className = $this->getOption('className');
zYne's avatar
zYne committed
103 104 105 106
        
        if (class_exists($className)) {
            return false;
        }
zYne's avatar
zYne committed
107

zYne's avatar
zYne committed
108 109
        $columns = array('keyword'  => array('type'    => 'string',
                                             'length'  => 200,
zYne's avatar
zYne committed
110 111 112
                                             'notnull' => true,
                                             'primary' => true,
                                             ),
zYne's avatar
zYne committed
113 114
                         'field'    => array('type'    => 'string',
                                             'length'  => 50,
zYne's avatar
zYne committed
115 116
                                             'notnull' => true,
                                             'primary' => true),
zYne's avatar
zYne committed
117
                         'position' => array('type'    => 'integer',
zYne's avatar
zYne committed
118 119 120
                                             'length'  => 8,
                                             'primary' => true,
                                             ));
zYne's avatar
zYne committed
121

zYne's avatar
zYne committed
122
        $id = $table->getIdentifier();
zYne's avatar
zYne committed
123

zYne's avatar
zYne committed
124
        $options = array('className' => $className);
zYne's avatar
zYne committed
125 126 127 128


        $fk = array();
        foreach ((array) $id as $column) {
zYne's avatar
zYne committed
129
            $def = $table->getDefinitionOf($column);
zYne's avatar
zYne committed
130 131 132 133 134

            unset($def['autoincrement']);
            unset($def['sequence']);
            unset($def['primary']);

zYne's avatar
zYne committed
135
            $col = strtolower(Doctrine::tableize($name) . '_' . $column);
zYne's avatar
zYne committed
136

zYne's avatar
zYne committed
137
            $def['primary'] = true;
zYne's avatar
zYne committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
            $fk[$col] = $def;
        }
        
        $local = (count($fk) > 1) ? array_keys($fk) : key($fk);
        
        $relations = array($name => array('local' => $local,
                                          'foreign' => $id, 
                                          'onDelete' => 'CASCADE',
                                          'onUpdate' => 'CASCADE'));


        $columns += $fk;

        $builder = new Doctrine_Import_Builder();

        $def = $builder->buildDefinition($options, $columns, $relations);
    
zYne's avatar
zYne committed
155 156 157
        if ( ! $this->_options['generateFiles']) {
            eval($def);
        }
zYne's avatar
zYne committed
158
        return true;
zYne's avatar
zYne committed
159
    }
zYne's avatar
zYne committed
160
}