Skip to content

Commit

Permalink
refactoring the parameters in the webcontroller search methods, relat…
Browse files Browse the repository at this point in the history
…ed to #393
  • Loading branch information
henriyli committed Dec 21, 2015
1 parent 9e03b1a commit 2a13e32
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 74 deletions.
71 changes: 20 additions & 51 deletions controller/WebController.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,27 +351,19 @@ public function invokeGlobalSearch($request)
$template = $this->twig->loadTemplate('vocab-search-listing.twig');
$this->setLanguageProperties($lang);

$term = $request->getQueryParam('q');
$content_lang = $request->getContentLang();
$search_lang = $request->getQueryParam('anylang') ? '' : $content_lang;
$type = $request->getQueryParam('type');
$group = $request->getQueryParam('group');
$parent = $request->getQueryParam('parent');
$offset = ($request->getQueryParam('offset') && is_numeric($request->getQueryParam('offset')) && $request->getQueryParam('offset') >= 0) ? $request->getQueryParam('offset') : 0;
if ($offset > 0) {
$rest = 1;
} else {
$rest = null;
}
$term = trim($term); // surrounding whitespace is not considered significant
$sterm = strpos($term, "*") === false ? $term . "*" : $term; // default to prefix search
$parameters = new ConceptSearchParameters($request, $this->model->getConfig());

$vocabs = $request->getQueryParam('vocabs'); # optional
// convert to vocids array to support multi-vocabulary search
$vocids = ($vocabs !== null && $vocabs !== '') ? explode(' ', $vocabs) : null;
$vocabObjects = array();
foreach($vocids as $vocid) {
$vocabObjects[] = $this->model->getVocabulary($vocid);
}
$parameters->setVocabularies($vocabObjects);

try {
$count_and_results = $this->model->searchConceptsAndInfo($sterm, $vocids, $content_lang, $search_lang, $offset, 20, $type, $parent, $group);
$count_and_results = $this->model->searchConceptsAndInfo($parameters);
} catch (Exception $e) {
header("HTTP/1.0 404 Not Found");
if ($this->model->getConfig()->getLogCaughtExceptions()) {
Expand All @@ -391,9 +383,9 @@ public function invokeGlobalSearch($request)
'search_count' => $counts,
'languages' => $this->languages,
'search_results' => $search_results,
'term' => $term,
'rest' => $rest,
'rest' => $parameters->getOffset()>0,
'global_search' => true,
'term' => $request->getQueryParam('q'),
'lang_list' => $langList,
'vocabs' => $vocabs,
'vocab_list' => $vocabList,
Expand All @@ -409,10 +401,9 @@ public function invokeVocabularySearch($request)
{
$template = $this->twig->loadTemplate('vocab-search-listing.twig');
$this->setLanguageProperties($request->getLang());
$lang = $request->getLang();
$vocab = $request->getVocab();
try {
$vocab_types = $this->model->getTypes($request->getVocabid(), $lang);
$vocab_types = $this->model->getTypes($request->getVocabid(), $request->getLang());
} catch (Exception $e) {
header("HTTP/1.0 404 Not Found");
if ($this->model->getConfig()->getLogCaughtExceptions()) {
Expand All @@ -426,35 +417,12 @@ public function invokeVocabularySearch($request)

return;
}
$term = $request->getQueryParam('q');
$content_lang = $request->getContentLang();
$groups = $vocab->listConceptGroups($content_lang);
$search_lang = $request->getQueryParam('anylang') ? '' : $content_lang;
$type = $request->getQueryParam('type') !== '' ? $request->getQueryParam('type') : null;
if ($type && strpos($type, '+')) {
$type = explode('+', $type);
} else if ($type && !is_array($type)) {
// if only one type param given place it into an array regardless
$type = array($type);
}

$group = $request->getQueryParam('group') !== '' ? $request->getQueryParam('group') : null;
$parent = $request->getQueryParam('parent') !== '' ? $request->getQueryParam('parent') : null;
$offset = ($request->getQueryParam('offset') && is_numeric($request->getQueryParam('offset')) && $request->getQueryParam('offset') >= 0) ? $request->getQueryParam('offset') : 0;
$langcodes = $vocab->getConfig()->getShowLangCodes();
if ($offset > 0) {
$rest = 1;
$template = $this->twig->loadTemplate('vocab-search-listing.twig');
} else {
$rest = null;
}
$parameters = new ConceptSearchParameters($request, $this->model->getConfig());

$term = trim($term); // surrounding whitespace is not considered significant
$sterm = strpos($term, "*") === false ? $term . "*" : $term; // default to prefix search
$parameters = new ConceptSearchParameters($request);
try {
//$count_and_results = $this->model->searchConceptsAndInfo($parameters);
$count_and_results = $this->model->searchConceptsAndInfo($sterm, $request->getVocabid(), $content_lang, $search_lang, $offset, 20, $type, $parent, $group, $parameters);
$count_and_results = $this->model->searchConceptsAndInfo($parameters);
$counts = $count_and_results['count'];
$search_results = $count_and_results['results'];
} catch (Exception $e) {
Expand All @@ -467,7 +435,7 @@ public function invokeVocabularySearch($request)
array(
'languages' => $this->languages,
'vocab' => $vocab,
'term' => $term,
'term' => $request->getQueryParam('q'),
'rest' => $rest,
));
return;
Expand All @@ -478,12 +446,13 @@ public function invokeVocabularySearch($request)
'vocab' => $vocab,
'search_results' => $search_results,
'search_count' => $counts,
'term' => $term,
'rest' => $rest,
'limit_parent' => $parent,
'limit_type' => $type,
'limit_group' => $group,
'group_index' => $groups,
'rest' => $parameters->getOffset()>0,
'limit_parent' => $parameters->getParentLimit(),
'limit_type' => $request->getQueryParam('type'),
'limit_group' => $parameters->getGroupLimit(),
'group_index' => $vocab->listConceptGroups($request->getContentLang()),
'parameters' => $parameters,
'term' => $request->getQueryParam('q'),
'types' => $vocab_types,
'explicit_langcodes' => $langcodes,
'request' => $request,
Expand Down
28 changes: 21 additions & 7 deletions model/ConceptSearchParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,42 @@
class ConceptSearchParameters
{

private $config;
private $request;
private $vocab;
private $vocabs;

public function __construct($request)
public function __construct($request, $config)
{
$this->request = $request;
$this->config = $config;
}

public function getLang()
{
return $this->request->getLang();
}

public function getVocab()
public function getVocabs()
{
return $this->request->getVocab();
if ($this->vocabs) {
return $this->vocabs;
}
return array($this->request->getVocab());
}

public function getVocabIds()
{
return array($this->getVocab()->getId());
if ($this->request->getQueryParam('vocabs')) {
$vocabs = $this->request->getQueryParam('vocabs');
return ($vocabs !== null && $vocabs !== '') ? explode(' ', $vocabs) : null;
}
return array(reset($this->getVocabs())->getId());
}

public function setVocabularies($vocabs)
{
$this->vocabs = $vocabs;
}
/*
if (sizeof($vocids) == 1) { // search within vocabulary
$voc = $vocabs[0];
Expand All @@ -43,7 +56,7 @@ public function getVocabIds()
public function getArrayClass()
{
if (sizeof($this->getVocabIds()) == 1) { // search within vocabulary
return $this->getVocab()->getConfig()->getArrayClassURI();
return reset($this->getVocabs())->getConfig()->getArrayClassURI();
}
return null;
}
Expand Down Expand Up @@ -94,13 +107,14 @@ public function getParentLimit()
{
return $this->request->getQueryParam('parent') !== '' ? $this->request->getQueryParam('parent') : null;
}

public function getOffset()
{
return ($this->request->getQueryParam('offset') && is_numeric($this->request->getQueryParam('offset')) && $this->request->getQueryParam('offset') >= 0) ? $this->request->getQueryParam('offset') : 0;
}

public function getSearchLimit()
{
return null;
return $this->config->getDefaultSearchLimit();
}
}
19 changes: 5 additions & 14 deletions model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public function getRDF($vocid, $uri, $format)
* @param boolean $unique restrict results to unique concepts (default: false)
* @return array search results
*/
public function searchConcepts($term, $vocids, $lang, $search_lang, $type = null, $parent = null, $group = null, $offset = 0, $limit = null, $hidden = true, $fields = null, $unique = false, $params)
public function searchConcepts($term, $vocids, $offset = 0, $limit = null, $hidden = true, $fields = null, $unique = false, $params)
{
if ($limit === null) {
$limit = $this->getConfig()->getDefaultSearchLimit();
Expand Down Expand Up @@ -308,19 +308,10 @@ public function searchConcepts($term, $vocids, $lang, $search_lang, $type = null
* @param string $group limit search to concepts which are in the given group
* @return array array with keys 'count' and 'results'
*/
public function searchConceptsAndInfo($term, $vocids, $lang, $search_lang, $offset = 0, $limit = 20, $type = null, $parent = null, $group = null, $params)
public function searchConceptsAndInfo($params)
{
// make vocids an array in every case
if ($vocids === null) {
$vocids = array();
}

if (!is_array($vocids)) {
$vocids = array($vocids);
}

$allhits = $this->searchConcepts($term, $vocids, $lang, $search_lang, $type, $parent, $group, 0, 0, true, null, true, $params);
$hits = array_slice($allhits, $offset, $limit);
$allhits = $this->searchConcepts($params->getSearchTerm(), $params->getVocabIds(), 0, 0, true, null, true, $params);
$hits = array_slice($allhits, $params->getOffset(), $params->getSearchLimit());

$uris = array();
$vocabs = array();
Expand All @@ -338,7 +329,7 @@ public function searchConceptsAndInfo($term, $vocids, $lang, $search_lang, $offs
$arrayClass = null;
$sparql = $this->getDefaultSparql();
}
$ret = $sparql->queryConceptInfo($uris, $arrayClass, $vocabs, null, $search_lang);
$ret = $sparql->queryConceptInfo($uris, $arrayClass, $vocabs, null, $params->getSearchLang());

// For marking that the concept has been found through an alternative label, hidden
// label or a label in another language
Expand Down
4 changes: 2 additions & 2 deletions model/sparql/GenericSparql.php
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ protected function generateConceptSearchQuery($vocabs, $hidden, $fields, $unique
$props[] = 'skos:hiddenLabel';
}

$filter_graph = $this->formatFilterGraph($params->getVocab());
$filter_graph = $this->formatFilterGraph($params->getVocabs());

// remove futile asterisks from the search term
$term = $params->getSearchTerm();
Expand Down Expand Up @@ -957,7 +957,7 @@ private function transformConceptSearchResults($results, $vocabs) {
public function queryConcepts($vocabs, $hidden = true, $fields = null, $unique = false, $params) {
$query = $this->generateConceptSearchQuery($vocabs, $hidden, $fields, $unique, $params);
$results = $this->client->query($query);
return $this->transformConceptSearchResults($results, array($params->getVocab()));
return $this->transformConceptSearchResults($results, $params->getVocabs());
}

/**
Expand Down
4 changes: 4 additions & 0 deletions test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
require_once 'vendor/autoload.php';
$graph = new EasyRdf_Graph();
$graph->parse(file_get_contents('vocabularies.ttl'));

0 comments on commit 2a13e32

Please sign in to comment.