Skip to content

Commit

Permalink
Extend SolrSearch to index all items, regardless of their public / pr…
Browse files Browse the repository at this point in the history
…ivate status, which gets added to the index as well. We then in turn modify the search query based on whether the user_role has sufficient privileges to view non-public items by utilizing Omeka's is_allowed('Items','showNotPublic'))
  • Loading branch information
Anurag committed Jan 14, 2015
1 parent 9fdb60f commit 20f79e5
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 24 deletions.
Binary file added .DS_Store
Binary file not shown.
29 changes: 11 additions & 18 deletions SolrSearchPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ public function hookUninstall()
$this->_db->query(<<<SQL
DROP TABLE IF EXISTS {$this->_db->prefix}solr_search_fields
SQL
);
);
$this->_db->query(<<<SQL
DROP TABLE IF EXISTS {$this->_db->prefix}solr_search_excludes
SQL
);
);

try {
$solr = SolrSearch_Helpers_Index::connect();
Expand Down Expand Up @@ -189,20 +189,11 @@ public function hookAfterSaveItem($args)

$solr = SolrSearch_Helpers_Index::connect();

// If the item is public, add/update the Solr document.
if ($item['public'] == true) {
$doc = SolrSearch_Helpers_Index::itemToDocument($item);
$solr->addDocuments(array($doc));
$solr->commit();
$solr->optimize();
}

// If the item's is being set private, remove it from Solr.
else {
$solr->deleteById('Item_' . $item['id']);
$solr->commit();
$solr->optimize();
}
// Both public and private items will be indexed
$doc = SolrSearch_Helpers_Index::itemToDocument($item);
$solr->addDocuments(array($doc));
$solr->commit();
$solr->optimize();

}

Expand Down Expand Up @@ -323,7 +314,7 @@ protected function _createSolrTables()
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
SQL
);
);

$this->_db->query(<<<SQL
CREATE TABLE IF NOT EXISTS {$this->_db->prefix}solr_search_excludes (
Expand All @@ -332,7 +323,7 @@ protected function _createSolrTables()
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
SQL
);
);
}


Expand Down Expand Up @@ -395,6 +386,7 @@ protected function _setOptions()
set_option('solr_search_hl', '1');
set_option('solr_search_hl_snippets', '1');
set_option('solr_search_hl_fragsize', '250');
set_option('solr_search_display_private_items', '1');
}


Expand All @@ -411,6 +403,7 @@ protected function _clearOptions()
delete_option('solr_search_hl');
delete_option('solr_search_hl_snippets');
delete_option('solr_search_hl_fragsize');
delete_option('solr_search_display_private_items');
}


Expand Down
30 changes: 26 additions & 4 deletions controllers/ResultsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,25 @@ public function indexAction()
$page = $this->_request->page ? $this->_request->page : 1;
$start = ($page-1) * $limit;


// determine whether to display private items or not
// items will only be displayed if:
// solr_search_display_private_items has been enabled in the Solr Search admin panel
// user is logged in
// user_role has sufficient permissions

$user = current_user();
if(get_option('solr_search_display_private_items')
&& $user
&& is_allowed('Items','showNotPublic')) {
// limit to public items
$limitToPublicItems = false;
} else {
$limitToPublicItems = true;
}

// Execute the query.
$results = $this->_search($start, $limit);
$results = $this->_search($start, $limit, $limitToPublicItems);

// Set the pagination.
Zend_Registry::set('pagination', array(
Expand All @@ -67,7 +84,7 @@ public function indexAction()
* @param int $limit Limit per page
* @return SolrResultDoc Solr results
*/
protected function _search($offset, $limit)
protected function _search($offset, $limit, $limitToPublicItems = true)
{

// Connect to Solr.
Expand All @@ -77,7 +94,7 @@ protected function _search($offset, $limit)
$params = $this->_getParameters();

// Construct the query.
$query = $this->_getQuery();
$query = $this->_getQuery($limitToPublicItems);

// Execute the query.
return $solr->search($query, $offset, $limit, $params);
Expand All @@ -90,7 +107,7 @@ protected function _search($offset, $limit)
*
* @return string The Solr query.
*/
protected function _getQuery()
protected function _getQuery($limitToPublicItems = true)
{

// Get the `q` GET parameter.
Expand All @@ -106,6 +123,11 @@ protected function _getQuery()
// Form the composite Solr query.
if (!empty($facet)) $query .= " AND {$facet}";

// Limit the query to public items if required
if($limitToPublicItems) {
$query .= ' AND public:"true"';
}

return $query;

}
Expand Down
11 changes: 10 additions & 1 deletion forms/SolrSearch_Form_Results.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ public function init()
)
));

// Display Private Items:
$this->addElement('checkbox', 'solr_search_display_private_items', array(
'label' => __('Display private items'),
'description' => __('Display private items for all user roles with sufficient permission to view them.'),
'value' => get_option('solr_search_display_private_items')
));


// Submit:
$this->addElement('submit', 'submit', array(
'label' => __('Save Settings')
Expand All @@ -99,7 +107,8 @@ public function init()
'solr_search_hl_snippets',
'solr_search_hl_fragsize',
'solr_search_facet_sort',
'solr_search_facet_limit'
'solr_search_facet_limit',
'solr_search_display_private_items'
), 'fields');

$this->addDisplayGroup(array(
Expand Down
7 changes: 6 additions & 1 deletion helpers/SolrSearch_Helpers_Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public static function itemToDocument($item)
$doc->setField('model', 'Item');
$doc->setField('modelid', $item->id);

// extend $doc to to include and items public / private status
$doc->setField('public', $item->public);

// Title:
$title = metadata($item, array('Dublin Core', 'Title'));
$doc->setField('title', $title);
Expand Down Expand Up @@ -222,7 +225,9 @@ public static function indexAll($options=array())
$table = $db->getTable('Item');
$select = $table->getSelect();

$table->filterByPublic($select, true);
// Removed in order to index both public and private items
// $table->filterByPublic($select, true);

$table->applySorting($select, 'id', 'ASC');

$excTable = $db->getTable('SolrSearchExclude');
Expand Down
Binary file added lib/.DS_Store
Binary file not shown.
Binary file added lib/SolrSearch/.DS_Store
Binary file not shown.
4 changes: 4 additions & 0 deletions lib/SolrSearch/Addon/Indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ public function indexRecord($record, $addon)
$doc->addField('model', $addon->table);
$doc->addField('modelid', $record->id);

// extend $doc to include public / private records
// not sure if required
//$doc->addField('public', $record->public);

$titleField = $addon->getTitleField();
foreach ($addon->fields as $field) {
$solrName = $this->makeSolrName($addon, $field->name);
Expand Down
Binary file added solr-core/.DS_Store
Binary file not shown.
Binary file added solr-core/omeka/.DS_Store
Binary file not shown.

0 comments on commit 20f79e5

Please sign in to comment.