Actions

Source Code of This Project

/lib/model/Search.php

<?php
/*
 * This file is part of the pwp package.
 * (c) 2009-2010 Victor Rad' <victor.v.rad[at]gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
*/

/**
 * Base class for search system
 * Represent of Composite pattern
 *
 * @package    pwp
 * @subpackage Search
 * @author     Victor Rad'
 */
abstract class Search
{
    // instance Zend_Search_Lucene index
    protected $index = null;
    // added to $indexPath
    protected $indexName = null;
    // file system path to index
    protected $indexPath = null;
    // found identifiers
    protected $foundIds = null;
    // found data
    protected $foundData = null;
    // e.g. "google"
    protected $query = null;

    /**
     * Get data by found ids and render template
     *
     * @return string
     */
    abstract public function render();
    /**
     * Default logic for a search in an index by query
     *
     * @param string $query e.g. "google"
     * @return array found ids
     */
    abstract public function find($query);

    /**
     * Delete current index and load all data from scratch
     */
    abstract public function rebuildIndex();

    /**
     * Init class
     */
    public function __construct()
    {
        ProjectConfiguration::registerZend();
        if (null !== $this->indexName) {
            $this->indexPath = sfConfig::get('sf_data_dir').'/search/'.$this->indexName.'.'.sfConfig::get('sf_environment').'.index';
            $this->index = $this->getIndex();
        }
    }

    /**
     * Getter found ids
     *
     * @return array
     */
    public function getFoundIds()
    {
        return $this->foundIds;
    }

    /**
     * Setter found ids
     */
    public function setFoundIds($ids)
    {
        $this->foundIds = $ids;
    }

    /**
     * Getter found data
     *
     * @return array
     */
    public function getFoundData()
    {
        return $this->foundData;
    }

    /**
     * Setter found data
     */
    public function setFoundData($data)
    {
        $this->foundData = $data;
    }

    /**
     * Create or open index instance
     *
     * @param string $method 'open' or 'create'
     * @return Zend_Search_Lucene
     */
    public function getIndex($method = 'open')
    {
        if ('open' == $method and file_exists($this->indexPath)){
            return Zend_Search_Lucene::open($this->indexPath);
        }
        elseif('create' == $method or !file_exists($this->indexPath)){
            return Zend_Search_Lucene::create($this->indexPath);
        }
    }

    /**
     * Delete document in index
     *
     * @param int $id
     */
    public function delete($id)
    {
        foreach ($this->index->find('pk:'.$id) as $hit){
            $this->index->delete($hit->id);
        }
    }

    /**
     * Default logic for a search in an index by query
     *
     * @param string $query e.g. "google"
     * @return array found ids
     */
    protected function _find($query)
    {
        // prepare data
        $query = trim($query);
        $Ids = array();
        $queryParse = '';

        foreach (explode(' ', $query) as $word){
            $queryParse .= '"'.$word.'"';
        }
        $queryParse = str_replace('""', '" AND "', $queryParse);

        $this->query = Zend_Search_Lucene_Search_QueryParser::parse($queryParse);
        $hits = $this->index->find($this->query);
        foreach ($hits as $hit){
            $Ids[] = $hit->pk;
        }

        $this->setFoundIds($Ids);

        return $Ids;
    }

}