From 91ee810ad1f34b20a94239f59dd455a701c768a0 Mon Sep 17 00:00:00 2001 From: zYne Date: Tue, 24 Jul 2007 19:58:44 +0000 Subject: [PATCH] --- lib/Doctrine/Search/Indexer.php | 59 +++++++++++++++++++++++ lib/Doctrine/Search/Indexer/Dir.php | 47 ++++++++++++++++++ lib/Doctrine/Search/Indexer/Exception.php | 34 +++++++++++++ lib/Doctrine/Search/Parser.php | 41 ++++++++++++++++ 4 files changed, 181 insertions(+) create mode 100644 lib/Doctrine/Search/Indexer/Dir.php create mode 100644 lib/Doctrine/Search/Indexer/Exception.php create mode 100644 lib/Doctrine/Search/Parser.php diff --git a/lib/Doctrine/Search/Indexer.php b/lib/Doctrine/Search/Indexer.php index 5bbe1b49b..acd8e59ed 100644 --- a/lib/Doctrine/Search/Indexer.php +++ b/lib/Doctrine/Search/Indexer.php @@ -32,5 +32,64 @@ */ class Doctrine_Search_Indexer { + public function indexDirectory($dir) + { + if ( ! file_exists($dir)) { + throw new Doctrine_Search_Indexer_Exception('Unknown directory ' . $dir); + } + + $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::LEAVES_ONLY); + + $q = new Doctrine_Query(); + $q->delete() + ->from('Doctrine_Search_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 = ?') + ->execute(); + + + $conn = Doctrine_Manager::connection(); + + $coll = new Doctrine_Collection('Doctrine_Search_File'); + + foreach ($it as $file) { + $coll[]->url = $file->getFilePath(); + } + + $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(); + } + } + } } diff --git a/lib/Doctrine/Search/Indexer/Dir.php b/lib/Doctrine/Search/Indexer/Dir.php new file mode 100644 index 000000000..bebacedfc --- /dev/null +++ b/lib/Doctrine/Search/Indexer/Dir.php @@ -0,0 +1,47 @@ +. + */ + +/** + * Doctrine_Search_Indexer_Dir + * + * @author Konsta Vesterinen + * @package Doctrine + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @version $Revision$ + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + */ +class Doctrine_Search_Indexer_Dir +{ + public function add($dir) + { + if ( ! file_exists($dir)) { + throw new Doctrine_Search_Indexer_Exception('Unknown directory ' . $dir); + } + + $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::LEAVES_ONLY); + + foreach ($it as $file) { + $this->indexFile($file); + } + } +} diff --git a/lib/Doctrine/Search/Indexer/Exception.php b/lib/Doctrine/Search/Indexer/Exception.php new file mode 100644 index 000000000..f99297ac7 --- /dev/null +++ b/lib/Doctrine/Search/Indexer/Exception.php @@ -0,0 +1,34 @@ +. + */ + +/** + * Doctrine_Search_Indexer + * + * @author Konsta Vesterinen + * @package Doctrine + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @version $Revision$ + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + */ +class Doctrine_Search_Indexer_Exception extends Doctrine_Search_Exception +{ } diff --git a/lib/Doctrine/Search/Parser.php b/lib/Doctrine/Search/Parser.php new file mode 100644 index 000000000..51173d88c --- /dev/null +++ b/lib/Doctrine/Search/Parser.php @@ -0,0 +1,41 @@ +. + */ + +/** + * Doctrine_Search_Parser_Standard + * + * @author Konsta Vesterinen + * @package Doctrine + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @version $Revision$ + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + */ +class Doctrine_Search_Parser +{ + public function parse($file) + { + $contents = file_get_contents($file); + + return array('url' => $file, 'contents' => $contents); + } +}