Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Commit

Permalink
Merge refs #179 0179_Lucene_CharlesMarion
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Ma committed Jul 25, 2011
2 parents 8386121 + 293e5fa commit a535a3c
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 31 deletions.
23 changes: 23 additions & 0 deletions core/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
class Notification extends MIDAS_Notification
{
public $_components = array('Utility');
public $_models = array('User', 'Item');

/** init notification process*/
public function init()
{
$this->addCallBack('CALLBACK_CORE_GET_DASHBOARD', 'getDasboard');
$this->addTask('TASK_CORE_RESET_ITEM_INDEXES', 'resetItemIndexes', 'Recompute lucene indexes');
}//end init

/** generate Dasboard information */
Expand All @@ -33,5 +35,26 @@ public function getDasboard()

return $return;
}//end _getDasboard


/** reset item indexes */
public function resetItemIndexes()
{
$users = $this->User->getAll();
foreach($users as $user)
{
$items = $this->Item->getOwnedByUser($user, 999999);
foreach($items as $item)
{
$this->Item->save($item);
}
}

require_once BASE_PATH.'/core/controllers/components/SearchComponent.php';
$component = new SearchComponent();
$index = $component->getLuceneItemIndex();

$index->optimize();
}
} //end class
?>
28 changes: 28 additions & 0 deletions core/controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,34 @@ function resetdemoAction()
$this->disableView();
}

/** run a task **/
function taskAction()
{
set_time_limit(0);
if(!$this->logged)
{
$this->haveToBeLogged();
return;
}
if(!$this->userSession->Dao->getAdmin() == 1)
{
throw new Zend_Exception("You should be an administrator");
}

$task = $this->_getParam("task");
$params = $this->_getParam("params");
if(isset($params))
{
$params = JsonComponent::decode($params);
}

$modules = Zend_Registry::get('notifier')->modules;
$tasks = Zend_Registry::get('notifier')->tasks;
call_user_func(array($modules[$tasks[$task]['module']], $tasks[$task]['method']), $params);
$this->disableLayout();
$this->disableView();
}

/** index*/
function indexAction()
{
Expand Down
2 changes: 1 addition & 1 deletion core/controllers/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function viewAction()
$element = $this->_getParam('element');
$qualifier = $this->_getParam('qualifier');
$value = $this->_getParam('value');
if(isset($metadataId))
if(isset($metadataId) && !empty($metadataIds))
{
$this->ItemRevision->deleteMetadata($itemRevision, $metadataId);
}
Expand Down
20 changes: 20 additions & 0 deletions core/controllers/components/SearchComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@
/** Search */
class SearchComponent extends AppComponent
{

/** get Zend Lucene index */
public function getLuceneItemIndex()
{
$path = BASE_PATH.'/tmp/cache/searchIndex';
if(!file_exists($path))
{
mkdir($path);
}

$path .= '/item';
if(!file_exists($path))
{
mkdir($path);
Zend_Search_Lucene::create($path);
}

return Zend_Search_Lucene::open($path);
}

/** search all the results */
public function searchAll($userDao, $search, $order)
{
Expand Down
2 changes: 1 addition & 1 deletion core/layouts/layout.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ echo $this->doctype()
</div>
<div class="Footer"><a href="http://www.kitware.com/products/midas.html">MIDAS</a> <?php echo $this->version ?> by <a href="http://www.kitware.com">Kitware</a> © <?php echo date("Y") ?> -
<?php
echo $this->t('Generated in').' '.$this->generatedTimer." ms";
echo $this->t('Generated in').' '.$this->generatedTimer." s";
?>
- <a href="http://public.kitware.com/Bug/my_view_page.php"><?php echo $this->t('Report bug');?></a>
</div>
Expand Down
37 changes: 37 additions & 0 deletions core/models/base/ItemModelBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,43 @@ public function save($dao)
}
$dao->setDateUpdate(date('c'));
parent::save($dao);

require_once BASE_PATH.'/core/controllers/components/SearchComponent.php';
$component = new SearchComponent();
$index = $component->getLuceneItemIndex();

$hits = $index->find("item_id:".$dao->getKey());
foreach($hits as $hit)
{
$index->delete($hit->id);
}
$doc = new Zend_Search_Lucene_Document();
$doc->addField(Zend_Search_Lucene_Field::Text('title', $dao->getName()));
$doc->addField(Zend_Search_Lucene_Field::Keyword('item_id', $dao->getKey()));
$doc->addField(Zend_Search_Lucene_Field::UnStored('description', $dao->getDescription()));

$modelLoad = new MIDAS_ModelLoader();
$revisionModel = $modelLoad->loadModel('ItemRevision');
$revision = $this->getLastRevision($dao);

if($revision != false)
{
$metadata = $revisionModel->getMetadata($revision);
$metadataString = '';

foreach($metadata as $m)
{
$doc->addField(Zend_Search_Lucene_Field::Keyword($m->getElement().'-'.$m->getQualifier(), $m->getValue()));
if(!is_numeric($m->getValue()))
{
$metadataString .= ' '. $m->getValue();
}
}

$doc->addField(Zend_Search_Lucene_Field::Text('metadata', $metadataString));
}
$index->addDocument($doc);
$index->commit();
}

/** copy parent folder policies*/
Expand Down
12 changes: 12 additions & 0 deletions core/models/base/MetadataModelBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ function addMetadataValue($itemRevisionDao, $type, $element, $qualifier, $value)
{
throw new Zend_Exception("This metadata value already exists for that revision.");
}

$item = $itemRevisionDao->getItem();
$modelLoader = new MIDAS_ModelLoader();
$itemModel = $modelLoader->loadModel('Item');
$lastrevision = $itemModel->getLastRevision($item);

//refresh zend search index
if($lastrevision->getKey() == $itemRevisionDao->getKey())
{
$itemModel->save($item);
}

$this->saveMetadataValue($metadataDao);
} // end addMetadataValue()

Expand Down
40 changes: 12 additions & 28 deletions core/models/pdo/ItemKeywordModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,21 @@ function getItemsFromSearch($searchterm, $userDao, $limit = 14, $group = true, $
}
}

$searchterms = explode(' ', $searchterm);
// Apparently it's slow to do a like in a subquery so we run it first
$sql = $this->database->select()->from(array('i' => 'itemkeyword'), array())
->setIntegrityCheck(false);
$sql->join(array('i2k' => 'item2keyword'), 'i.keyword_id = i2k.keyword_id', array('item_id'));

if(empty($searchterms))
require_once BASE_PATH.'/core/controllers/components/SearchComponent.php';
$component = new SearchComponent();
$index = $component->getLuceneItemIndex();
Zend_Search_Lucene_Search_QueryParser::setDefaultOperator(Zend_Search_Lucene_Search_QueryParser::B_AND);
Zend_Search_Lucene::setResultSetLimit($limit * 3);
if($group && strpos($searchterm, ':') === false)
{
return array();
$rowset = $index->find('title:'.$searchterm);
}

foreach($searchterms as $key => $term)
else
{
if($key == 0)
{
$sql->where('value LIKE ?', '%'.$term.'%');
}
else
{
$sql->orWhere('value LIKE ?', '%'.$term.'%');
}
$rowset = $index->find($searchterm);
}
$rowset = $this->database->fetchAll($sql);


$return = array();
$itemIdsCount = array();
$itemIds = array();
Expand All @@ -85,14 +76,7 @@ function getItemsFromSearch($searchterm, $userDao, $limit = 14, $group = true, $
}
foreach($itemIdsCount as $key => $n)
{
if($n < count($searchterms))
{
unset($itemIdsCount[$key]);
}
else
{
$itemIds[] = $key;
}
$itemIds[] = $key;
}

if(empty($itemIds))
Expand Down
11 changes: 11 additions & 0 deletions core/models/pdo/ItemModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,17 @@ function delete($itemdao)
$policy_user_model->delete($policy);
}


require_once BASE_PATH.'/core/controllers/components/SearchComponent.php';
$component = new SearchComponent();
$index = $component->getLuceneItemIndex();

$hits = $index->find("item_id:".$itemdao->getKey());
foreach($hits as $hit)
{
$index->delete($hit->id);
}

parent::delete($itemdao);
unset($itemdao->item_id);
$itemdao->saved = false;
Expand Down
11 changes: 11 additions & 0 deletions core/models/pdo/ItemRevisionModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ function deleteMetadata($revisiondao, $metadataId)
}

Zend_Registry::get('dbAdapter')->delete('metadatavalue', 'itemrevision_id = '.$revisiondao->getKey().' AND metadata_id = '.$metadataId);

$item = $revisiondao->getItem();
$modelLoader = new MIDAS_ModelLoader();
$itemModel = $modelLoader->loadModel('Item');
$lastrevision = $itemModel->getLastRevision($item);

//refresh zend search index
if($lastrevision->getKey() == $revisiondao->getKey())
{
$itemModel->save($item);
}
return;
} // end getMetadata

Expand Down
2 changes: 1 addition & 1 deletion notification/MIDASNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function getNotifications()
}

/** get Tasks */
public function getTaks()
public function getTasks()
{
return $this->tasks;
}
Expand Down
2 changes: 2 additions & 0 deletions tmp/cache/searchIndex/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore

0 comments on commit a535a3c

Please sign in to comment.