Skip to content

Commit

Permalink
Merge pull request #158 from albig/fix-sru-search
Browse files Browse the repository at this point in the history
Fix SRU search with TYPO 9.5
  • Loading branch information
Alexander Bigga authored Jul 21, 2020
2 parents 8438072 + 0a0e247 commit 9d43e31
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 85 deletions.
6 changes: 1 addition & 5 deletions Classes/Plugins/Sru/Sru.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public function main($content, $conf)
'###SRU_URL###' => $sruLink,
'###LANG_ID###' => $this->LLkey,
'###LABEL_SUBMIT###' => $this->pi_getLL('label.submit'),
'###LABEL_NORESULT###' => htmlspecialchars($this->pi_getLL('label.noresult')),
'###FIELD_QUERY###' => $this->prefixId . '[query]',
'###QUERY###' => htmlspecialchars($lastQuery),
'###CURRENT_DOCUMENT###' => $this->doc->location,
Expand All @@ -127,7 +128,6 @@ public function main($content, $conf)
protected function addSearchFormJS()
{
$pageRenderer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Page\PageRenderer::class);

// Add SRU specific Javascript.
$pageRenderer->addJsFooterFile(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::siteRelPath($this->extKey) . 'Resources/Public/Javascript/tx_dfgviewer_sru.js');
}
Expand All @@ -142,22 +142,18 @@ protected function addSearchFormJS()
protected function addSruResultsJS()
{
if (!empty($this->piVars['highlight']) && !empty($this->piVars['origimage'])) {

$highlight = unserialize(urldecode($this->piVars['highlight']));
$origImage = $this->piVars['origimage'];

// Add SRU Results if any
$javascriptFooter = '$(window).load(function(){';

foreach ($highlight as $field) {
$javascriptFooter .= 'tx_dlf_viewer.addHighlightField([' . $field . '],' . $origImage . ');';
}

$javascriptFooter .= '})';

$pageRenderer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Page\PageRenderer::class);
$pageRenderer->addJsFooterInlineCode('tx-dfgviewer-footer', $javascriptFooter);
}
}

}
92 changes: 21 additions & 71 deletions Classes/Plugins/Sru/SruEid.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Http\Response;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\Plugin\AbstractPlugin;

/**
* Plugin 'DFG-Viewer: SRU Client eID script' for the 'dfgviewer' extension.
Expand All @@ -38,82 +39,54 @@
* @subpackage tx_dfgviewer
* @access public
*/
class SruEid extends AbstractPlugin
class SruEid
{

/**
* The main method of the eID script
*
*/
public $cObj;


/**
* The main method of the eID-Script
* @access public
*
* @access public
*
* @return string JSON encoded return value
* @param ServerRequestInterface $request
* @return ResponseInterface
*/
public function main()
public function main(ServerRequestInterface $request)
{
$this->cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);

$this->extKey = 'dfgviewer';

$this->scriptRelPath = 'Classes/Plugins/Sru/SruEid.php';
// parameters are sent by POST --> use getParsedBody() instead of getQueryParams()
$parameters = $request->getParsedBody();
$sru = (string)$parameters['sru'];
$query = (string)$parameters['q'];

$this->LLkey = GeneralUtility::_GP('L') ? GeneralUtility::_GP('L') : 'default';

$this->pi_loadLL();

$url = GeneralUtility::_GP('sru') . '?operation=searchRetrieve&version=1.2&startRecord=1&maximumRecords=10&recordSchema=dfg-viewer/page&query=' . urlencode(GeneralUtility::_GP('q'));
$url = $sru . '?operation=searchRetrieve&version=1.2&startRecord=1&maximumRecords=10&recordSchema=dfg-viewer/page&query=' . urlencode($query);

// make request to SRU service
$sruXML = simplexml_load_file($url);

if ($sruXML !== FALSE) {

// the result may be a valid <srw:searchRetrieveResponse> or some HTML code

$sruResponse = $sruXML->xpath('/srw:searchRetrieveResponse');

if ($sruResponse === FALSE) {

$results['error'] = $this->pi_getLL('label.noresults') . ' ' . GeneralUtility::_GP('q');

$results['error'] = '';
} else {

$sruRecords = $sruXML->xpath('/srw:searchRetrieveResponse/srw:records/srw:record');

if ($sruRecords === FALSE || empty($sruRecords)) {

$results['error'] = $this->pi_getLL('label.noresults') . ' ' . GeneralUtility::_GP('q');

$results['error'] = '';
}

foreach ($sruRecords as $id => $record) {

$fullTextHit = $record->xpath('//srw:recordData');

$pageAttributes = [];

foreach ($fullTextHit[$id]->children('http://dfg-viewer.de/')->page->attributes() as $key => $val) {

$pageAttributes[$key] = $val;

}

$hitFound = [];

// there may be multiple hits on a page per search query
foreach ($fullTextHit[$id]->children('http://dfg-viewer.de/')->page->fulltexthit as $hit) {

$hitAttributes = [];

foreach ($hit->attributes() as $key => $val) {

$hitAttributes[$key] = $val;

}

$hitFound[] = array('text' => $hit->span, 'attributes' => $hitAttributes);
Expand All @@ -129,49 +102,29 @@ public function main()

// get highlight boxes for all results of a page
foreach ($hitFound as $key => $hit) {

$highlightField = $hit['attributes']['x1'] . ',' . $hit['attributes']['y1'] . ',' . $hit['attributes']['x2'] . ',' . $hit['attributes']['y2'];

if (!in_array($highlightField, $highlightParams)) {

$highlightParams[] = $highlightField;

}

}

foreach ($hitFound as $key => $hit) {

unset($spanPreview);

unset($spanText);

if (!empty($hit['attributes']['preview'])) {

$spanPreview = '<span class="sru-preview"><img src="' . $hit['attributes']['preview'] . '"></span>';

}

if (is_object($hit['text'])) {

$spanText = '<span class="sru-textsnippet">';

foreach ($hit['text'] as $key => $text) {

if ($text->attributes()->class[0] == 'highlight') {

$spanText .= '<span class="highlight">' . $text . '</span>';

} else {

$spanText .= $text;

}

}

$spanText .= '</span>';

}

$origImageParams = '0,' . $pageAttributes['width'] . ',' . $pageAttributes ['height'];
Expand All @@ -189,16 +142,13 @@ public function main()
$results[] = $data;
}
}

}

} else {

$results['error'] = $this->pi_getLL('label.noresults') . ' ' . GeneralUtility::_GP('q');

}

echo json_encode($results);
// create response object
/** @var Response $response */
$response = GeneralUtility::makeInstance(Response::class);
$response->getBody()->write(json_encode($results));
return $response;
}

}
4 changes: 2 additions & 2 deletions Resources/Private/Language/Sru.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<label index="label.delete_search">Delete Search</label>
<label index="label.loading">Loading Results</label>
<label index="label.submit">Search</label>
<label index="label.noresults">No results for your search: </label>
<label index="label.noresult">No results found for your search.</label>
<label index="search">Search</label>
</languageKey>
<languageKey index="de" type="array">
Expand All @@ -22,7 +22,7 @@
<label index="label.delete_search">Suche löschen</label>
<label index="label.loading">Lade Ergebnisse</label>
<label index="label.submit">Suchen</label>
<label index="label.noresults">Kein Ergebnis für Ihre Suche: </label>
<label index="label.noresult">Kein Ergebnis für Ihre Suche gefunden.</label>
<label index="search">Suche</label>
</languageKey>
</data>
Expand Down
3 changes: 3 additions & 0 deletions Resources/Private/Templates/Plugins/Dfgviewer/Sru.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
<div id="tx-dfgviewer-sru-results-loading" style="display: none;">###LABEL_LOADING###...</div>
<div id="tx-dfgviewer-sru-results-clearing">###LABEL_DELETE_SEARCH###...</div>
<div id="tx-dfgviewer-sru-results"></div>
<div id="tx-dfgviewer-sru-labels" style="display:none;">
<span id="tx-dfgviewer-sru-label-noresult">###LABEL_NORESULT###</span>
</div>
</div>
<!-- ###TEMPLATE### end -->
18 changes: 11 additions & 7 deletions Resources/Public/Javascript/tx_dfgviewer_sru.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ $("#tx-dfgviewer-sru-form").submit(function( event ) {
action: $( "input[name='tx_dfgviewer[action]']" ).val(),
},
function(data) {

var resultItems = [];
var resultList = '<div class="sru-results-active-indicator"></div><ul>';

if (data.error) {

resultList += '<li>' + data.error + '</li>';
resultList += '<li class="noresult">' + $('#tx-dfgviewer-sru-label-noresult').text() + '</li>';

} else {

Expand Down Expand Up @@ -78,13 +78,19 @@ $("#tx-dfgviewer-sru-form").submit(function( event ) {
+ '&tx_dlf[page]=' + (data[i].page));

if (data[i].previewImage) {
resultList += '<li><a href=\"' + newlink + '\">' + data[i].previewImage + '</li>';
resultItems.push('<a href=\"' + newlink + '\">' + data[i].previewImage);
}
if (data[i].previewText) {
resultList += '<li><a href=\"' + newlink + '\">' + data[i].previewText + '</li>';
resultItems.push('<a href=\"' + newlink + '\">' + data[i].previewText);
}
}

if (resultItems.length > 0) {
resultItems.forEach(function(item, index){
resultList += '<li>' + item + '</li>';
});
} else {
resultList += '<li class="noresult">' + $('#tx-dfgviewer-sru-label-noresult').text() + '</li>';
}
}
resultList += '</ul>';

Expand All @@ -108,5 +114,3 @@ $('#tx-dfgviewer-sru-results-clearing').click(function() {


});


0 comments on commit 9d43e31

Please sign in to comment.