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.  */
  21.  
  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. {
  34.     protected $_options = array('generateFiles' => true,
  35.                                 'className'     => '%CLASS%Index');
  36.  
  37.     
  38.     public function __construct(array $options)
  39.     {
  40.         $this->_options array_merge($this->_options$options);
  41.         
  42.         if isset($this->_options['analyzer'])) {
  43.             $this->_options['analyzer'new Doctrine_Search_Analyzer_Standard();
  44.         }
  45.     }
  46.  
  47.     public function getOption($option)
  48.     {
  49.         if (isset($this->_options[$option])) {
  50.             return $this->_options[$option];
  51.         }
  52.         
  53.         throw new Doctrine_Search_Exception('Unknown option ' $option);
  54.     }
  55.     
  56.     public function analyze($text)
  57.     {
  58.         return $this->_options['analyzer']->analyze($text);
  59.     }
  60.  
  61.     public function setOption($option$value)
  62.     {
  63.         $this->_options[$option$value;
  64.  
  65.         return $this;
  66.     }
  67.     /**
  68.      * updateIndex
  69.      * updates the index
  70.      *
  71.      * @param Doctrine_Record $record 
  72.      * @return integer 
  73.      */
  74.     public function updateIndex(Doctrine_Record $record
  75.     {
  76.         $fields $this->getOption('fields');
  77.         $class  $this->getOption('className');
  78.         $name   $record->getTable()->getComponentName();
  79.  
  80.         foreach ($fields as $field{
  81.             $data  $record->get($field);
  82.  
  83.             $terms $this->analyze($data);
  84.  
  85.             foreach ($terms as $pos => $term{
  86.                 $index new $class();
  87.  
  88.                 $index->keyword $term;
  89.                 $index->position $pos;
  90.                 $index->field $field;
  91.                 $index->$name $record;
  92.                 
  93.                 $index->save();
  94.             }
  95.         }
  96.     }
  97.     public function buildDefinition(Doctrine_Table $table)
  98.     {
  99.         $name $table->getComponentName();
  100.  
  101.         $className $this->getOption('className');
  102.         
  103.         if (class_exists($className)) {
  104.             return false;
  105.         }
  106.  
  107.         $columns array('keyword'  => array('type'    => 'string',
  108.                                              'length'  => 200,
  109.                                              'notnull' => true,
  110.                                              'primary' => true,
  111.                                              ),
  112.                          'field'    => array('type'    => 'string',
  113.                                              'length'  => 50,
  114.                                              'notnull' => true,
  115.                                              'primary' => true),
  116.                          'position' => array('type'    => 'integer',
  117.                                              'length'  => 8,
  118.                                              'primary' => true,
  119.                                              ));
  120.  
  121.         $id $table->getIdentifier();
  122.  
  123.         $options array('className' => $className);
  124.  
  125.  
  126.         $fk array();
  127.         foreach ((array) $id as $column{
  128.             $def $table->getDefinitionOf($column);
  129.  
  130.             unset($def['autoincrement']);
  131.             unset($def['sequence']);
  132.             unset($def['primary']);
  133.  
  134.             $col strtolower(Doctrine::tableize($name'_' $column);
  135.  
  136.             $def['primary'true;
  137.             $fk[$col$def;
  138.         }
  139.         
  140.         $local (count($fk1array_keys($fkkey($fk);
  141.         
  142.         $relations array($name => array('local' => $local,
  143.                                           'foreign' => $id
  144.                                           'onDelete' => 'CASCADE',
  145.                                           'onUpdate' => 'CASCADE'));
  146.  
  147.  
  148.         $columns += $fk;
  149.  
  150.         $builder new Doctrine_Import_Builder();
  151.  
  152.         $def $builder->buildDefinition($options$columns$relations);
  153.     
  154.         if $this->_options['generateFiles']{
  155.             eval($def);
  156.         }
  157.         return true;
  158.     }
  159. }