Commit 614901b4 authored by Dainis Abols's avatar Dainis Abols
Browse files

LU Search integration

v2.6.1
parent a5af7609
...@@ -15,6 +15,7 @@ use Lu\LuApi\Models\Namedays; ...@@ -15,6 +15,7 @@ use Lu\LuApi\Models\Namedays;
use Lu\LuApi\Models\Programs; use Lu\LuApi\Models\Programs;
use Lu\LuApi\Models\ProgramsContinue; use Lu\LuApi\Models\ProgramsContinue;
use Lu\LuApi\Models\Purchases; use Lu\LuApi\Models\Purchases;
use Lu\LuApi\Models\Search;
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface; use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
......
...@@ -7,7 +7,7 @@ use Lu\LuApi\Helpers\ReturnPretty; ...@@ -7,7 +7,7 @@ use Lu\LuApi\Helpers\ReturnPretty;
use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\GeneralUtility;
/** /**
* Class LUContractsList * Class LuisContracts
* *
* @author Dainis Abols <dainis.abols@lu.lv> * @author Dainis Abols <dainis.abols@lu.lv>
* @owner University of Latvia * @owner University of Latvia
......
...@@ -7,7 +7,7 @@ use GuzzleHttp\Client as Guzzle; ...@@ -7,7 +7,7 @@ use GuzzleHttp\Client as Guzzle;
use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\GeneralUtility;
/** /**
* Class LuisLessons. Notice: This gathers and returns ONLY Tornakalns 1st building lessons!! * Class LuisLessons
* *
* @author Dainis Abols <dainis.abols@lu.lv> * @author Dainis Abols <dainis.abols@lu.lv>
* @owner University of Latvia * @owner University of Latvia
......
...@@ -7,7 +7,7 @@ use Lu\LuApi\Helpers\ReturnPretty; ...@@ -7,7 +7,7 @@ use Lu\LuApi\Helpers\ReturnPretty;
use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\GeneralUtility;
/** /**
* Class LUPurchasesList * Class LuisPurchases
* *
* @author Dainis Abols <dainis.abols@lu.lv> * @author Dainis Abols <dainis.abols@lu.lv>
* @owner University of Latvia * @owner University of Latvia
......
...@@ -145,6 +145,34 @@ class SolrLibrary ...@@ -145,6 +145,34 @@ class SolrLibrary
*/ */
private $useQuotes = false; private $useQuotes = false;
/**
* Highlist status
*
* @var string
*/
private $hl = "";
/**
* Highlist status
*
* @var string
*/
private $hlQuery = "";
/**
* Highlight pre string
*
* @var string
*/
private $hlSimplePre = "";
/**
* Highlight post string
*
* @var string
*/
private $hlSimplePost = "";
/** /**
* SolrLibrary constructor. * SolrLibrary constructor.
* *
...@@ -308,7 +336,7 @@ class SolrLibrary ...@@ -308,7 +336,7 @@ class SolrLibrary
} }
/** /**
* Set fiklter query * Set filter query
* *
* @param $fq * @param $fq
*/ */
...@@ -317,6 +345,46 @@ class SolrLibrary ...@@ -317,6 +345,46 @@ class SolrLibrary
$this->fq[] = $fq; $this->fq[] = $fq;
} }
/**
* Set highlight status
*
* @param $hl
*/
public function highlight($hl)
{
$this->hl = $hl;
}
/**
* Set highlight query
*
* @param $hlQuery
*/
public function highlightQuery($hlQuery)
{
$this->hlQuery = $hlQuery;
}
/**
* Set highlight pre string
*
* @param $hlSimplePre
*/
public function highlightSimplePre($hlSimplePre)
{
$this->hlSimplePre = $hlSimplePre;
}
/**
* Set highlight post string
*
* @param $hlSimplePost
*/
public function highlightSimplePost($hlSimplePost)
{
$this->hlSimplePost = $hlSimplePost;
}
/** /**
* Add more complicated request to query * Add more complicated request to query
* *
...@@ -402,15 +470,13 @@ class SolrLibrary ...@@ -402,15 +470,13 @@ class SolrLibrary
public function execute($fullResponse = false) public function execute($fullResponse = false)
{ {
// Initiate client // Initiate client
$SolrClient = new \SolrClient( $SolrClient = new \SolrClient([
[ 'secure' => $this->solr_secure,
'secure' => $this->solr_secure, 'hostname' => $this->solr_hostname,
'hostname' => $this->solr_hostname, 'port' => $this->solr_port,
'port' => $this->solr_port, 'path' => $this->solr_path.$this->core,
'path' => $this->solr_path.$this->core, 'wt' => 'json',
'wt' => 'json', ]);
]
);
// Get the request // Get the request
$request = $this->getRequestString(); $request = $this->getRequestString();
...@@ -424,7 +490,10 @@ class SolrLibrary ...@@ -424,7 +490,10 @@ class SolrLibrary
$response = $queryResponse->getResponse(); $response = $queryResponse->getResponse();
if ($fullResponse) { if ($fullResponse) {
if (!$this->distinct) { if ($this->hl == "on") {
$response = $response;
}
else if (!$this->distinct) {
$response = $response->response; $response = $response->response;
} }
} else { } else {
...@@ -464,7 +533,9 @@ class SolrLibrary ...@@ -464,7 +533,9 @@ class SolrLibrary
// Build the query // Build the query
$SolrQuery = new \SolrQuery(); $SolrQuery = new \SolrQuery();
foreach ($this->select as $item) { foreach ($this->select as $item) {
$SolrQuery->addField($item); if (!empty($item)) {
$SolrQuery->addField($item);
}
} }
$SolrQuery->setStart($this->offset); $SolrQuery->setStart($this->offset);
$SolrQuery->setRows($this->limit); $SolrQuery->setRows($this->limit);
...@@ -504,11 +575,23 @@ class SolrLibrary ...@@ -504,11 +575,23 @@ class SolrLibrary
} }
} }
// Add raw entries
foreach ($this->queryRaw as $item) { foreach ($this->queryRaw as $item) {
$SolrQuery->addParam($item['param'], $item['value']); $SolrQuery->addParam($item['param'], $item['value']);
} }
// Add highlight
if ($this->hl == "on") {
$SolrQuery->setHighlight(true);
$SolrQuery->addHighlightField("*");
$SolrQuery->setHighlightSimplePre($this->hlSimplePre);
$SolrQuery->setHighlightSimplePost($this->hlSimplePost);
$SolrQuery->setHighlightFragsize(1000);
// @TODO: Add 'hl.q='.$this->hlQuery'`to this
}
// Add sort
if ($this->sort_field) { if ($this->sort_field) {
$SolrQuery->addSortField($this->sort_field, $this->sort_order); $SolrQuery->addSortField($this->sort_field, $this->sort_order);
} }
...@@ -557,6 +640,7 @@ class SolrLibrary ...@@ -557,6 +640,7 @@ class SolrLibrary
*/ */
private function getRequestString() private function getRequestString()
{ {
// Make query
$query = implode(' AND ', $this->query); $query = implode(' AND ', $this->query);
if ($this->queryStr) { if ($this->queryStr) {
if ($this->query) { if ($this->query) {
......
...@@ -6,7 +6,7 @@ use Lu\LuApi\Helpers\ReturnPretty; ...@@ -6,7 +6,7 @@ use Lu\LuApi\Helpers\ReturnPretty;
use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\GeneralUtility;
/** /**
* Class LuisProgrammes * Class SolrRegulations
* *
* @author Dainis Abols <dainis.abols@lu.lv> * @author Dainis Abols <dainis.abols@lu.lv>
* @owner University of Latvia * @owner University of Latvia
...@@ -91,7 +91,7 @@ class SolrRegulations ...@@ -91,7 +91,7 @@ class SolrRegulations
* *
* @param string $statusName * @param string $statusName
*/ */
public function hideStatus(string $statusName = '') { public function hideStatus(string $statusName = null) {
if ($statusName) { if ($statusName) {
$this->hiddenStatus = $statusName; $this->hiddenStatus = $statusName;
} }
......
<?php
namespace Lu\LuApi\DataSources;
use Lu\LuApi\Helpers\ReturnPretty;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Class LuisProgrammes
*
* @author Dainis Abols <dainis.abols@lu.lv>
* @owner University of Latvia
* @version 2.6.0
* @since 03.09.2020
*/
class SolrSearch
{
/**
* Extension configuration
*
* @array
*/
private $conf = [];
/**
* Filter request
*
* @var array
*/
private $filters = [];
/**
* Types request
*
* @var array
*/
private $types = [];
/**
* Solr cores
*
* @var string
*/
private $solrCore = '';
/**
* Facet switcher
*
* @var bool
*/
private $facet = false;
/**
* Highlist status
*
* @var string
*/
private $highlight = "";
/**
* Highlight pre string
*
* @var string
*/
private $highlightPre = "";
/**
* Highlight post string
*
* @var string
*/
private $highlightPost = "";
/**
* Set parent IDs
*
* @var array
*/
private $parentIDs = [];
/**
* SolrRegulations constructor.
*
* Gathers and sets connection data from environmental values. Prepares and opens connection.
*/
public function __construct()
{
// Read config file
$this->conf = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Configuration\ExtensionConfiguration::class)->get('lu_api');
// Set Solr Core
if ($this->conf) {
$this->solrCore = $this->conf['solrDatabase']['search'];
}
}
/**
* Sets filters
*
* @param array $filters
*/
public function setFilters(array $filters)
{
$this->filters = $filters;
}
/**
* Set limit
*
* @param int $limit
*/
public function setLimit($limit)
{
$this->limit = $limit;
}
/**
* Set parent IDs
*
* @param int $parentIDs
*/
public function setParentIDs($parentIDs)
{
$this->parentIDs = $parentIDs;
}
/**
* Set offset
*
* @param int $offset
*/
public function setOffset($offset)
{
$this->offset = $offset;
}
/**
* Sets types
*
* @param array $types
*/
public function setTypes(array $types)
{
$this->types = $types;
}
/**
* Sets weather to use or not use facets
*
* @param bool $facet
*/
public function useFacets(bool $facet)
{
$this->facet = $facet;
}
/**
* Set highlight options diveded by |
* If non provided default <em>|</em> is used
*
* @param null $highlight
*/
public function setHighlight($highlight = null)
{
$this->highlight = "on";
if ($highlight != null) {
$simple = explode("|", $highlight);
$this->highlightPre = $simple[0];
$this->highlightPost = $simple[1];
} else {
$this->highlightPre = '<em>';
$this->highlightPost = '</em>';
}
}
/**
* Fetch data from provided endpoint
*
* Return [Programme Object, Catalogue Object]
*
* @param bool $fullReponse
*
* @return string
*/
public function fetch($fullReponse = false)
{
// Build solr request
$SolrLibrary = new SolrLibrary();
// Execute request
// $SolrLibrary->select(['*'])
$SolrLibrary->select(['*'])
->from($this->solrCore)
->where('*', '*')
// ->orderBy('date_published', 'desc')
->limit($SolrLibrary::MAXINT);
// Build request with filters and types
if (!empty($this->filters) && !empty($this->types)) {
$SolrLibrary->whereStr("(".implode(" OR ", $this->filters).")");
$SolrLibrary->whereStr("(".implode(" OR ", $this->types).")");
}
// Add parent IDs
if (!empty($this->parentIDs)) {
$SolrLibrary->whereStr("parent_id:(".implode(" OR ", $this->parentIDs).")");
}
// // Filter acceptance date
// if (!empty($this->filters['accepted_date_from']) || !empty($this->filters['accepted_date_till'])) {
// $dateFilter = '[';
// if (!empty($this->filters['accepted_date_from'])) {
// $dateFrom = new \DateTime($this->filters['accepted_date_from']);
// $dateFilter .= $dateFrom->format('Y-m-d').'T00:00:00Z';
// } else {
// $dateFilter .= '*';
// }
// $dateFilter .= ' TO ';
// if (!empty($this->filters['accepted_date_till'])) {
// $dateTill = new \DateTime($this->filters['accepted_date_till']);
// $dateFilter .= $dateTill->format('Y-m-d').'T23:59:59Z';
// } else {
// $dateFilter .= '*';
// }
// $dateFilter .= ']';
// $SolrLibrary->where('date_accepted', $dateFilter);
// }
// // Check facet request
// if ($this->facet) {
// // Set facet usage
// $SolrLibrary->facet([
// 'field' => [
// 'rikojuma_dokum',
// 'document_type',
// 'issuer',
// ],
// 'limit' => -1,
// ]);
// }
//
// // Add facet where
// if (!empty($this->filters['facet_field'])) {
// foreach ($this->filters['facet_field'] as $field=>$value) {
// $SolrLibrary->fq($field.':"'.$value.'"');
// }
// }
// Set highlights
if ($this->highlight == "on") {
$SolrLibrary->highlight("on");
if (!empty($this->highlightPre)) {
$SolrLibrary->highlightSimplePre($this->highlightPre);
$SolrLibrary->highlightSimplePost($this->highlightPost);
$SolrLibrary->highlightQuery(implode(" OR ", $this->filters));
}
}
// Set limit
$SolrLibrary->limit($this->limit);
$SolrLibrary->offset($this->offset);
// Fetch results
$result = $SolrLibrary->execute($fullReponse);
$data = !empty($result['data']) ? $result['data'] : [];
// Return back to the request
return ReturnPretty::json($data, 200);
}
}
{ {
"name": "luitd/lu-api", "name": "luitd/lu-api",
"version": "2.5.0", "version": "2.6.1",
"description": "REST API Integration for TYPO3", "description": "REST API Integration for TYPO3",
"type": "typo3-cms-extension", "type": "typo3-cms-extension",
"keywords": [ "keywords": [
......
...@@ -16,6 +16,9 @@ extraImagesStorage = ...@@ -16,6 +16,9 @@ extraImagesStorage =
# cat=Ctypes/set; type=string; label=Contracts List Url # cat=Ctypes/set; type=string; label=Contracts List Url
endPoints.ContractsList = http://luis.lu.lv/luis/LU_uznl.html endPoints.ContractsList = http://luis.lu.lv/luis/LU_uznl.html
# cat=SOLR/set; type=string; label=LU Search DB
solrDatabase.search = lusearch
# cat=SOLR/set; type=string; label=Programme catalogue DB # cat=SOLR/set; type=string; label=Programme catalogue DB
solrDatabase.programmeCatalogue = kursu_katal_program_dati solrDatabase.programmeCatalogue = kursu_katal_program_dati
......
...@@ -11,19 +11,19 @@ ...@@ -11,19 +11,19 @@
***************************************************************/ ***************************************************************/
$EM_CONF[$_EXTKEY] = [ $EM_CONF[$_EXTKEY] = [
'title' => 'LU API', 'title' => 'LU API',
'description' => 'REST API Integration for TYPO3', 'description' => 'REST API Integration for TYPO3',
'category' => 'plugin', 'category' => 'plugin',
'version' => '2.5.0', 'version' => '2.6.1',
'dependencies' => 'typo3', 'dependencies' => 'typo3',
'state' => 'stable', 'state' => 'stable',
'clearCacheOnLoad' => true, 'clearCacheOnLoad' => true,
'author' => 'Dainis Abols', 'author' => 'Dainis Abols',
'author_email' => 'dainis.abols@lu.lv', 'author_email' => 'dainis.abols@lu.lv',
'author_company' => 'University of Latvia', 'author_company' => 'University of Latvia',
'constraints' => [ 'constraints' => [
'depends' => [ 'depends' => [
'typo3' => '9.5.99-9.9.99', 'typo3' => '9.5.99-9.9.99',
], ],
], ],
'autoload' => [ 'autoload' => [
......