From a8c39c0debca35b7f1a33425775bca4c668b3b72 Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Fri, 14 Dec 2012 13:10:24 -0500 Subject: [PATCH] Revised Solr Tree Data Source to use record drivers instead of direct array access to Solr response. --- module/VuFind/config/module.config.php | 2 + .../VuFind/Hierarchy/TreeDataSource/Solr.php | 48 ++++++++++++------- .../src/VuFind/RecordDriver/SolrDefault.php | 17 +++++++ 3 files changed, 49 insertions(+), 18 deletions(-) diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php index 8534f90b03b..21bc531770a 100644 --- a/module/VuFind/config/module.config.php +++ b/module/VuFind/config/module.config.php @@ -191,6 +191,8 @@ $config = array( 'solr' => function ($sm) { $cacheDir = $sm->getServiceLocator()->get('VuFind\CacheManager')->getCacheDir(); return new \VuFind\Hierarchy\TreeDataSource\Solr( + \VuFind\Connection\Manager::connectToIndex(), + $sm->getServiceLocator()->get('VuFind\RecordDriverPluginManager'), rtrim($cacheDir, '/') . '/hierarchy' ); }, diff --git a/module/VuFind/src/VuFind/Hierarchy/TreeDataSource/Solr.php b/module/VuFind/src/VuFind/Hierarchy/TreeDataSource/Solr.php index 62552371d95..77978d2c9d6 100644 --- a/module/VuFind/src/VuFind/Hierarchy/TreeDataSource/Solr.php +++ b/module/VuFind/src/VuFind/Hierarchy/TreeDataSource/Solr.php @@ -44,9 +44,16 @@ class Solr extends AbstractBase /** * Solr connection * - * @var object + * @var \VuFind\Connection\Solr */ - protected $db; + protected $solr; + + /** + * Record driver plugin manager + * + * @var \VuFind\RecordDriver\PluginManager + */ + protected $driverFactory; /** * Cache directory @@ -58,11 +65,16 @@ class Solr extends AbstractBase /** * Constructor. * - * @param string $cacheDir Directory to use for caching results (optional) + * @param \VuFind\Connection\Solr $solr Solr connection + * @param \VuFind\RecordDriver\PluginManager $factory Record driver manager + * @param string $cacheDir Directory to hold cache + * results (optional) */ - public function __construct($cacheDir = null) - { - $this->db = ConnectionManager::connectToIndex(); + public function __construct(\VuFind\Connection\Solr $solr, + \VuFind\RecordDriver\PluginManager $factory, $cacheDir = null + ) { + $this->solr = $solr; + $this->driverFactory = $factory; $this->cacheDir = rtrim($cacheDir, '/'); } @@ -79,7 +91,7 @@ class Solr extends AbstractBase */ public function getXML($id, $options = array()) { - $top = $this->db->getRecord($id); + $top = $this->driverFactory->getSolrRecord($this->solr->getRecord($id)); $cacheFile = (null !== $this->cacheDir) ? $this->cacheDir . '/hierarchyTree_' . urlencode($id) . '.xml' : false; @@ -97,7 +109,7 @@ class Solr extends AbstractBase $xml = '<root><item id="' . htmlspecialchars($id) . '">' . - '<content><name>' . htmlspecialchars($top['title']) . + '<content><name>' . htmlspecialchars($top->getTitle()) . '</name></content>'; $count = 0; $xml .= $this->getChildren($id, $count); @@ -129,7 +141,7 @@ class Solr extends AbstractBase protected function getChildren($parentID, &$count) { $query = 'hierarchy_parent_id:"' . addcslashes($parentID, '"') . '"'; - $results = $this->db->search(array('query' => $query, 'limit' => 10000)); + $results = $this->solr->search(array('query' => $query, 'limit' => 10000)); if ($results === false) { return ''; } @@ -137,23 +149,23 @@ class Solr extends AbstractBase $sorting = $this->getHierarchyDriver()->treeSorting(); foreach ($results['response']['docs'] as $doc) { + $current = $this->driverFactory->getSolrRecord($doc); ++$count; if ($sorting) { - foreach ($doc['hierarchy_parent_id'] as $key => $val) { - if ($val == $parentID) { - $sequence = $doc['hierarchy_sequence'][$key]; - } + $positions = $current->getHierarchyPositionsInParents(); + if (isset($positions[$parentID])) { + $sequence = $positions[$parentID]; } } - $this->debug("$parentID: " . $doc['id']); + $this->debug("$parentID: " . $current->getUniqueID()); $xmlNode = ''; - $xmlNode .= '<item id="' . htmlspecialchars($doc['id']) . + $xmlNode .= '<item id="' . htmlspecialchars($current->getUniqueID()) . '"><content><name>' . - htmlspecialchars($doc['title_full']) . '</name></content>'; - $xmlNode .= $this->getChildren($doc['id'], $count); + htmlspecialchars($current->getTitle()) . '</name></content>'; + $xmlNode .= $this->getChildren($current->getUniqueID(), $count); $xmlNode .= '</item>'; - array_push($xml, array((isset($sequence)?$sequence: 0),$xmlNode)); + array_push($xml, array((isset($sequence) ? $sequence : 0), $xmlNode)); } if ($sorting) { diff --git a/module/VuFind/src/VuFind/RecordDriver/SolrDefault.php b/module/VuFind/src/VuFind/RecordDriver/SolrDefault.php index ff98733ecc0..acea10715c5 100644 --- a/module/VuFind/src/VuFind/RecordDriver/SolrDefault.php +++ b/module/VuFind/src/VuFind/RecordDriver/SolrDefault.php @@ -1129,6 +1129,23 @@ class SolrDefault extends AbstractBase ? $this->fields['hierarchy_top_title'] : array(); } + /** + * Get the positions of this item within parent collections. Returns an array + * of parent ID => sequence number. + * + * @return array + */ + public function getHierarchyPositionsInParents() + { + $retVal = array(); + if (isset($this->fields['hierarchy_parent_id'])) { + foreach ($this->fields['hierarchy_parent_id'] as $key => $val) { + $retVal[$val] = $this->fields['hierarchy_sequence'][$key]; + } + } + return $retVal; + } + /** * Get a list of hierarchy trees containing this record. * -- GitLab