Commit e10acab8 authored by zYne's avatar zYne

drafting the new class Doctrine_Search_File

parent 4fb5f7c2
...@@ -44,8 +44,9 @@ class Doctrine_Search extends Doctrine_Plugin ...@@ -44,8 +44,9 @@ class Doctrine_Search extends Doctrine_Plugin
'batchUpdates' => false, 'batchUpdates' => false,
'pluginTable' => false, 'pluginTable' => false,
'fields' => array(), 'fields' => array(),
'identifier' => null,
'connection' => null); 'connection' => null);
protected $_built = false;
public function __construct(array $options) public function __construct(array $options)
...@@ -73,35 +74,41 @@ class Doctrine_Search extends Doctrine_Plugin ...@@ -73,35 +74,41 @@ class Doctrine_Search extends Doctrine_Plugin
* @param Doctrine_Record $record * @param Doctrine_Record $record
* @return integer * @return integer
*/ */
public function updateIndex(Doctrine_Record $record) public function updateIndex(array $data)
{ {
$this->buildDefinition();
$fields = $this->getOption('fields'); $fields = $this->getOption('fields');
$class = $this->getOption('className'); $class = $this->getOption('className');
$name = $record->getTable()->getComponentName(); $name = $this->getOption('resource')->getComponentName();
if ($this->_options['batchUpdates'] === true) { if ($this->_options['batchUpdates'] === true) {
$conn = $record->getTable()->getConnection(); $conn = $this->getOption('resource')->getConnection();
$index = new $class(); $index = new $class();
foreach ($record->identifier() as $id => $value) { foreach ((array) $this->_options['resource']->getIdentifier() as $id) {
$index->$id = $value; $index->$id = $data[$id];
} }
$index->save(); $index->save();
} else { } else {
print 'joo';
foreach ($fields as $field) { foreach ($fields as $field) {
$data = $record->get($field);
$value = $data[$field];
$terms = $this->analyze($data);
$terms = $this->analyze($value);
foreach ($terms as $pos => $term) { foreach ($terms as $pos => $term) {
$index = new $class(); $index = new $class();
$index->keyword = $term; $index->keyword = $term;
$index->position = $pos; $index->position = $pos;
$index->field = $field; $index->field = $field;
$index->$name = $record; foreach ((array) $this->_options['resource']->getIdentifier() as $id) {
$index->$id = $data[$id];
}
$index->save(); $index->save();
} }
...@@ -111,9 +118,11 @@ class Doctrine_Search extends Doctrine_Plugin ...@@ -111,9 +118,11 @@ class Doctrine_Search extends Doctrine_Plugin
public function readTableData($limit = null, $offset = null) public function readTableData($limit = null, $offset = null)
{ {
$this->buildDefinition();
$conn = $this->_options['resource']->getConnection(); $conn = $this->_options['resource']->getConnection();
$tableName = $this->_options['resource']->getTableName(); $tableName = $this->_options['resource']->getTableName();
$id = $this->_options['identifier']; $id = $this->_options['resource']->getIdentifier();
$query = 'SELECT * FROM ' . $conn->quoteIdentifier($tableName) $query = 'SELECT * FROM ' . $conn->quoteIdentifier($tableName)
. ' WHERE ' . $conn->quoteIdentifier($id) . ' WHERE ' . $conn->quoteIdentifier($id)
...@@ -124,12 +133,15 @@ class Doctrine_Search extends Doctrine_Plugin ...@@ -124,12 +133,15 @@ class Doctrine_Search extends Doctrine_Plugin
$query = $conn->modifyLimitQuery($query, $limit, $offset); $query = $conn->modifyLimitQuery($query, $limit, $offset);
return $conn->fetchAll($query); return $conn->fetchAll($query);
} }
public function processPending($limit = null, $offset = null) public function processPending($limit = null, $offset = null)
{ {
$id = $this->_options['identifier']; $this->buildDefinition();
$id = $this->_options['resource']->getIdentifier();
$class = $this->_options['className']; $class = $this->_options['className'];
$fields = $this->_options['fields']; $fields = $this->_options['fields'];
$conn = $this->_options['connection']; $conn = $this->_options['connection'];
...@@ -174,27 +186,29 @@ class Doctrine_Search extends Doctrine_Plugin ...@@ -174,27 +186,29 @@ class Doctrine_Search extends Doctrine_Plugin
$conn->rollback(); $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() 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'); $className = $this->getOption('className');
if (class_exists($className)) { if (class_exists($className)) {
return false; return false;
} }
$columns = array('keyword' => array('type' => 'string', $columns = array('keyword' => array('type' => 'string',
'length' => 200, 'length' => 200,
'primary' => true, 'primary' => true,
...@@ -207,14 +221,18 @@ class Doctrine_Search extends Doctrine_Plugin ...@@ -207,14 +221,18 @@ class Doctrine_Search extends Doctrine_Plugin
'primary' => true, 'primary' => true,
)); ));
$id = $this->_options['identifier']; $id = $this->_options['resource']->getIdentifier();
$options = array('className' => $className); $options = array('className' => $className);
$fk = $this->generateForeignKeys($this->_options['resource']); $fk = $this->generateForeignKeys($this->_options['resource']);
$columns += $fk; $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); $this->generateClass($options, $columns, $relations);
......
<?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 ...@@ -51,6 +51,6 @@ class Doctrine_Search_Listener extends Doctrine_Record_Listener
{ {
$record = $event->getInvoker(); $record = $event->getInvoker();
$this->_search->updateIndex($record); $this->_search->updateIndex($record->toArray());
} }
} }
\ No newline at end of file
...@@ -35,6 +35,8 @@ class Doctrine_Template_Searchable extends Doctrine_Template ...@@ -35,6 +35,8 @@ class Doctrine_Template_Searchable extends Doctrine_Template
public function __construct(array $options) public function __construct(array $options)
{ {
$this->_plugin = new Doctrine_Search($options); $this->_plugin = new Doctrine_Search($options);
} }
public function getPlugin() public function getPlugin()
...@@ -53,7 +55,6 @@ class Doctrine_Template_Searchable extends Doctrine_Template ...@@ -53,7 +55,6 @@ class Doctrine_Template_Searchable extends Doctrine_Template
$className = $this->_plugin->getOption('className'); $className = $this->_plugin->getOption('className');
} }
$this->_plugin->setOption('resource', $this->_table); $this->_plugin->setOption('resource', $this->_table);
$this->_plugin->setOption('identifier', $this->_table->getIdentifier());
$this->_plugin->buildDefinition(); $this->_plugin->buildDefinition();
$this->hasMany($className, array('local' => $id, 'foreign' => $id)); $this->hasMany($className, array('local' => $id, 'foreign' => $id));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment