From e10acab86237fe8f7cda5004787866724bca46dd Mon Sep 17 00:00:00 2001 From: zYne Date: Tue, 16 Oct 2007 21:33:06 +0000 Subject: [PATCH] drafting the new class Doctrine_Search_File --- lib/Doctrine/Search.php | 76 +++++++++++++++++----------- lib/Doctrine/Search/File.php | 64 +++++++++++++++++++++++ lib/Doctrine/Search/Listener.php | 4 +- lib/Doctrine/Template/Searchable.php | 3 +- 4 files changed, 115 insertions(+), 32 deletions(-) create mode 100644 lib/Doctrine/Search/File.php diff --git a/lib/Doctrine/Search.php b/lib/Doctrine/Search.php index 97a55cd8c..f545bafec 100644 --- a/lib/Doctrine/Search.php +++ b/lib/Doctrine/Search.php @@ -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); diff --git a/lib/Doctrine/Search/File.php b/lib/Doctrine/Search/File.php new file mode 100644 index 000000000..ba0d85dfd --- /dev/null +++ b/lib/Doctrine/Search/File.php @@ -0,0 +1,64 @@ +. + */ + +/** + * Doctrine_Search + * + * @package Doctrine + * @subpackage Search + * @author Konsta Vesterinen + * @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))); + } + } +} diff --git a/lib/Doctrine/Search/Listener.php b/lib/Doctrine/Search/Listener.php index 310ffe683..8fdef09f6 100644 --- a/lib/Doctrine/Search/Listener.php +++ b/lib/Doctrine/Search/Listener.php @@ -51,6 +51,6 @@ class Doctrine_Search_Listener extends Doctrine_Record_Listener { $record = $event->getInvoker(); - $this->_search->updateIndex($record); + $this->_search->updateIndex($record->toArray()); } -} \ No newline at end of file +} diff --git a/lib/Doctrine/Template/Searchable.php b/lib/Doctrine/Template/Searchable.php index e3ce55a44..b18b69bb0 100644 --- a/lib/Doctrine/Template/Searchable.php +++ b/lib/Doctrine/Template/Searchable.php @@ -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));