This commit is contained in:
parent
0f1fb82d8d
commit
cbf6e48aa3
@ -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;
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user