From cbf6e48aa30792dd1f24385df193bcbe213d4303 Mon Sep 17 00:00:00 2001 From: zYne Date: Tue, 24 Jul 2007 20:38:21 +0000 Subject: [PATCH] --- lib/Doctrine/Search.php | 14 +++++++--- lib/Doctrine/Search/Indexer.php | 48 ++++++++++----------------------- 2 files changed, 25 insertions(+), 37 deletions(-) diff --git a/lib/Doctrine/Search.php b/lib/Doctrine/Search.php index 7b4f88fe5..3f371b604 100644 --- a/lib/Doctrine/Search.php +++ b/lib/Doctrine/Search.php @@ -32,7 +32,8 @@ */ class Doctrine_Search { - protected $_options = array('generateFiles' => true); + protected $_options = array('generateFiles' => true, + 'className' => '%CLASS%Index'); public function __construct(array $options) @@ -50,7 +51,7 @@ class Doctrine_Search return $this->_options[$option]; } - return null; + throw new Doctrine_Search_Exception('Unknown option ' . $option); } public function analyze($text) @@ -64,6 +65,13 @@ class Doctrine_Search return $this; } + /** + * updateIndex + * updates the index + * + * @param Doctrine_Record $record + * @return integer + */ public function updateIndex(Doctrine_Record $record) { $fields = $this->getOption('fields'); @@ -91,7 +99,7 @@ class Doctrine_Search { $name = $table->getComponentName(); - $className = $name . 'Index'; + $className = $this->getOption('className'); if (class_exists($className)) { return false; diff --git a/lib/Doctrine/Search/Indexer.php b/lib/Doctrine/Search/Indexer.php index acd8e59ed..298a5a3f0 100644 --- a/lib/Doctrine/Search/Indexer.php +++ b/lib/Doctrine/Search/Indexer.php @@ -40,56 +40,36 @@ class Doctrine_Search_Indexer $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::LEAVES_ONLY); + $files = array(); + foreach ($it as $file) { + $name = $file->getPathName(); + if (strpos($name, '.svn') === false) { + $files[] = $name; + } + } + $q = new Doctrine_Query(); $q->delete() - ->from('Doctrine_Search_File f') + ->from('Doctrine_File f') ->where('f.url LIKE ?', array($dir . '%')) ->execute(); // clear the index $q = new Doctrine_Query(); $q->delete() - ->from('Doctrine_Search_File_Index i') - ->where('i.foreign_id = ?') + ->from('Doctrine_File_Index i') + ->where('i.file_id = ?') ->execute(); $conn = Doctrine_Manager::connection(); - $coll = new Doctrine_Collection('Doctrine_Search_File'); + $coll = new Doctrine_Collection('Doctrine_File'); - foreach ($it as $file) { - $coll[]->url = $file->getFilePath(); + foreach ($files as $file) { + $coll[]->url = $file; } $coll->save(); - - foreach ($coll as $record) { - $this->updateIndex($record); - } - } - - public function updateIndex(Doctrine_Record $record) - { - $fields = array('url', 'content'); - - $class = 'Doctrine_Search_File_Index'; - - foreach ($fields as $field) { - $data = $record->get($field); - - $terms = $this->analyze($data); - - foreach ($terms as $pos => $term) { - $index = new $class(); - - $index->keyword = $term; - $index->position = $pos; - $index->field = $field; - $index->$name = $record; - - $index->save(); - } - } } }