drafting the new class Doctrine_Search_File
This commit is contained in:
parent
4fb5f7c201
commit
e10acab862
@ -44,8 +44,9 @@ class Doctrine_Search extends Doctrine_Plugin
|
||||
'batchUpdates' => false,
|
||||
'pluginTable' => false,
|
||||
'fields' => array(),
|
||||
'identifier' => null,
|
||||
'connection' => null);
|
||||
|
||||
protected $_built = false;
|
||||
|
||||
|
||||
public function __construct(array $options)
|
||||
@ -73,35 +74,41 @@ class Doctrine_Search extends Doctrine_Plugin
|
||||
* @param Doctrine_Record $record
|
||||
* @return integer
|
||||
*/
|
||||
public function updateIndex(Doctrine_Record $record)
|
||||
public function updateIndex(array $data)
|
||||
{
|
||||
$this->buildDefinition();
|
||||
|
||||
$fields = $this->getOption('fields');
|
||||
$class = $this->getOption('className');
|
||||
$name = $record->getTable()->getComponentName();
|
||||
$name = $this->getOption('resource')->getComponentName();
|
||||
|
||||
if ($this->_options['batchUpdates'] === true) {
|
||||
|
||||
$conn = $record->getTable()->getConnection();
|
||||
$conn = $this->getOption('resource')->getConnection();
|
||||
|
||||
$index = new $class();
|
||||
foreach ($record->identifier() as $id => $value) {
|
||||
$index->$id = $value;
|
||||
foreach ((array) $this->_options['resource']->getIdentifier() as $id) {
|
||||
$index->$id = $data[$id];
|
||||
}
|
||||
|
||||
|
||||
$index->save();
|
||||
} else {
|
||||
print 'joo';
|
||||
foreach ($fields as $field) {
|
||||
$data = $record->get($field);
|
||||
|
||||
$terms = $this->analyze($data);
|
||||
|
||||
$value = $data[$field];
|
||||
|
||||
$terms = $this->analyze($value);
|
||||
|
||||
foreach ($terms as $pos => $term) {
|
||||
$index = new $class();
|
||||
|
||||
|
||||
$index->keyword = $term;
|
||||
$index->position = $pos;
|
||||
$index->field = $field;
|
||||
$index->$name = $record;
|
||||
foreach ((array) $this->_options['resource']->getIdentifier() as $id) {
|
||||
$index->$id = $data[$id];
|
||||
}
|
||||
|
||||
$index->save();
|
||||
}
|
||||
@ -111,9 +118,11 @@ class Doctrine_Search extends Doctrine_Plugin
|
||||
|
||||
public function readTableData($limit = null, $offset = null)
|
||||
{
|
||||
$this->buildDefinition();
|
||||
|
||||
$conn = $this->_options['resource']->getConnection();
|
||||
$tableName = $this->_options['resource']->getTableName();
|
||||
$id = $this->_options['identifier'];
|
||||
$id = $this->_options['resource']->getIdentifier();
|
||||
|
||||
$query = 'SELECT * FROM ' . $conn->quoteIdentifier($tableName)
|
||||
. ' WHERE ' . $conn->quoteIdentifier($id)
|
||||
@ -124,12 +133,15 @@ class Doctrine_Search extends Doctrine_Plugin
|
||||
$query = $conn->modifyLimitQuery($query, $limit, $offset);
|
||||
|
||||
return $conn->fetchAll($query);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function processPending($limit = null, $offset = null)
|
||||
{
|
||||
$id = $this->_options['identifier'];
|
||||
$this->buildDefinition();
|
||||
|
||||
$id = $this->_options['resource']->getIdentifier();
|
||||
$class = $this->_options['className'];
|
||||
$fields = $this->_options['fields'];
|
||||
$conn = $this->_options['connection'];
|
||||
@ -174,27 +186,29 @@ class Doctrine_Search extends Doctrine_Plugin
|
||||
$conn->rollback();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* insertPending
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function insertPending($indexTableName, $id, $conn = null)
|
||||
{
|
||||
if ( ! ($conn instanceof Doctrine_Connection)) {
|
||||
$conn = Doctrine_Manager::connection();
|
||||
}
|
||||
|
||||
$conn->insert($indexTableName, array('foreign_id' => $id));
|
||||
}
|
||||
public function buildDefinition()
|
||||
{
|
||||
if ($this->_built) {
|
||||
return true;
|
||||
}
|
||||
$this->_built = true;
|
||||
|
||||
$componentName = $this->_options['resource']->getComponentName();
|
||||
|
||||
// check for placeholders
|
||||
if (strpos($this->_options['className'], '%') !== false) {
|
||||
$this->_options['className'] = str_replace('%CLASS%', $componentName, $this->_options['className']);
|
||||
}
|
||||
|
||||
$className = $this->getOption('className');
|
||||
|
||||
if (class_exists($className)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
$columns = array('keyword' => array('type' => 'string',
|
||||
'length' => 200,
|
||||
'primary' => true,
|
||||
@ -207,14 +221,18 @@ class Doctrine_Search extends Doctrine_Plugin
|
||||
'primary' => true,
|
||||
));
|
||||
|
||||
$id = $this->_options['identifier'];
|
||||
$id = $this->_options['resource']->getIdentifier();
|
||||
|
||||
$options = array('className' => $className);
|
||||
|
||||
$fk = $this->generateForeignKeys($this->_options['resource']);
|
||||
$columns += $fk;
|
||||
|
||||
$relations = $this->generateRelation($this->_options['resource'], $fk);
|
||||
$relations = array();
|
||||
// only generate relations for database based searches
|
||||
if ( ! $this instanceof Doctrine_Search_File) {
|
||||
$relations = $this->generateRelation($this->_options['resource'], $fk);
|
||||
}
|
||||
|
||||
$this->generateClass($options, $columns, $relations);
|
||||
|
||||
|
64
lib/Doctrine/Search/File.php
Normal file
64
lib/Doctrine/Search/File.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?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
|
||||
*
|
||||
* @package Doctrine
|
||||
* @subpackage Search
|
||||
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @version $Revision$
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
*/
|
||||
class Doctrine_Search_File extends Doctrine_Search
|
||||
{
|
||||
public function __construct(array $options = array())
|
||||
{
|
||||
parent::__construct($options);
|
||||
|
||||
if ( ! isset($this->_options['resource'])) {
|
||||
$table = new Doctrine_Table('File', Doctrine_Manager::connection());
|
||||
|
||||
$table->setColumn('url', 'string', 255, array('primary' => true));
|
||||
|
||||
$this->_options['resource'] = $table;
|
||||
}
|
||||
|
||||
if (empty($this->_options['fields'])) {
|
||||
$this->_options['fields'] = array('url', 'content');
|
||||
}
|
||||
|
||||
$this->buildDefinition();
|
||||
}
|
||||
|
||||
public function indexDirectory($dir)
|
||||
{
|
||||
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir),
|
||||
RecursiveIteratorIterator::LEAVES_ONLY);
|
||||
|
||||
foreach ($it as $file) {
|
||||
$this->updateIndex(array('url' => $file->getPathName(),
|
||||
'content' => file_get_contents($file)));
|
||||
}
|
||||
}
|
||||
}
|
@ -51,6 +51,6 @@ class Doctrine_Search_Listener extends Doctrine_Record_Listener
|
||||
{
|
||||
$record = $event->getInvoker();
|
||||
|
||||
$this->_search->updateIndex($record);
|
||||
$this->_search->updateIndex($record->toArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -35,6 +35,8 @@ class Doctrine_Template_Searchable extends Doctrine_Template
|
||||
public function __construct(array $options)
|
||||
{
|
||||
$this->_plugin = new Doctrine_Search($options);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function getPlugin()
|
||||
@ -53,7 +55,6 @@ class Doctrine_Template_Searchable extends Doctrine_Template
|
||||
$className = $this->_plugin->getOption('className');
|
||||
}
|
||||
$this->_plugin->setOption('resource', $this->_table);
|
||||
$this->_plugin->setOption('identifier', $this->_table->getIdentifier());
|
||||
$this->_plugin->buildDefinition();
|
||||
|
||||
$this->hasMany($className, array('local' => $id, 'foreign' => $id));
|
||||
|
Loading…
Reference in New Issue
Block a user