Source

php-libs / doctrine_lucene / library / DoctrineX / Template / Lucene.php

Full commit
<?php
class DoctrineX_Template_Lucene extends Doctrine_Template
{
    /**
     *
     * @var Zend_Search_Lucene_Interface
     */
    protected $index = null;

    /**
     *
     * @var array
     */
    protected $identifierFields = array();

    /**
     *
     * @var array
     */
    protected $userFields = array();

    /**
     *
     * @var array
     */
    protected $fields = array();

    /**
     *
     * @var string
     */
    protected $encoding = '';

    public function __construct(array $options = array())
    {
        if (!isset($options['indexesPath']) && !isset($options['indexFile'])) {
            throw new Doctrine_Record_Exception('Option "indexesPath" or "indexFile" is required');
        }

        if (!isset($options['fields'])) {
            throw new Doctrine_Record_Exception('Option "fields" is required');
        }

        if (isset($options['encoding'])) {
            $this->encoding = $options['encoding'];
            Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding($this->encoding);
        }

        if (isset($options['analyzer'])) {
            Zend_Search_Lucene_Analysis_Analyzer::setDefault(
                new $options['analyzer']()
            );
        }

        if (isset($options['filters'])) {
            $filters = $options['filters'];
        } else {
            $filters = array();
        }
        if (isset($options['stopWords'])) {
            $filters[] = new Zend_Search_Lucene_Analysis_TokenFilter_StopWords(
                    $options['stopWords']);
        }
        if (isset($options['stopWordsFile'])) {
            $stopWordsFilter = new Zend_Search_Lucene_Analysis_TokenFilter_StopWords();
            $stopWordsFilter->loadFromFile($options['stopWordsFile']);
            $filters[] = $stopWordsFilter;
        }
        if (!empty($filters)) {
            $analyzer = Zend_Search_Lucene_Analysis_Analyzer::getDefault();
            foreach ($filters as $filter) {
                $analyzer->addFilter($filter);
            }
        }

        parent::__construct($options);
    }

    public function setTableDefinition()
    {
        $this->addListener(new DoctrineX_Template_Listener_Lucene($this->_options));
    }

    /**
     *
     * @return string
     */
    public function getLuceneIndexFile()
    {
        if (isset($this->_options['indexFile'])) {
            return $this->_options['indexFile'];
        } else {
            return $this->_options['indexesPath'] . '/' . get_class($this->_invoker);
        }
    }

    /**
     *
     * @return Zend_Search_Lucene_Interface
     */
    public function getLuceneIndex()
    {
        if (null === $this->index) {
            if (file_exists($index = $this->getLuceneIndexFile())) {
                $this->index = Zend_Search_Lucene::open($index);
            } else {
                $this->index = Zend_Search_Lucene::create($index);
            }
        }
        return $this->index;
    }

    public function findLuceneDocument()
    {
        $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
        foreach ($this->getLuceneIdentifierFields() as $field) {
            $name = $field['name'];
            $query->addTerm(new Zend_Search_Lucene_Index_Term($this->_invoker->$name,
                    $name), true);
        }
        return $this->getLuceneIndex()->find($query);
    }

    public function createLuceneDocument()
    {
        $doc = new Zend_Search_Lucene_Document();
        foreach ($this->getLuceneFields() as $field) {
            $type = $field['type'];
            $name = $field['name'];

            $docField = Zend_Search_Lucene_Field::$type($name,
                    $this->_invoker->$name, $this->encoding);
            if (isset($field['boost'])) {
                $docField->boost = $field['boost'];
            }
            $doc->addField($docField);
        }
        return $doc;
    }

    /**
     *
     * @return array
     */
    public function getLuceneIdentifierFields()
    {
        if (array() === $this->identifierFields) {
            $identifier = $this->_invoker->getTable()->getIdentifier();
            foreach ((array) $identifier as $id) {
                $this->identifierFields[] = array(
                    'name' => $id,
                    'type' => 'Keyword',
                );
            }
        }
        return $this->identifierFields;
    }

    public function getLuceneUserFields()
    {
        if (array() === $this->userFields) {
            $this->userFields = $this->_options['fields'];
        }
        return $this->userFields;
    }

    /**
     *
     * @return array
     */
    public function getLuceneFields()
    {
        if (array() === $this->fields) {
            $this->fields = array_merge($this->getLuceneIdentifierFields(),
                    $this->getLuceneUserFields());
        }
        return $this->fields;
    }
}