diff --git a/Classes/Eid/SruEid.php b/Classes/Eid/SruEid.php deleted file mode 100644 index ef7808bda..000000000 --- a/Classes/Eid/SruEid.php +++ /dev/null @@ -1,156 +0,0 @@ - - * All rights reserved - * - * This script is part of the TYPO3 project. The TYPO3 project is - * free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * The GNU General Public License can be found at - * http://www.gnu.org/copyleft/gpl.html. - * - * This script is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * 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; - -/** - * Plugin 'DFG-Viewer: SRU Client eID script' for the 'dfgviewer' extension. - * - * @author Alexander Bigga - * @copyright Copyright (c) 2014, Alexander Bigga, SLUB Dresden - * @package TYPO3 - * @subpackage tx_dfgviewer - * @access public - */ -class SruEid -{ - - /** - * The main method of the eID script - * - * @access public - * - * @param ServerRequestInterface $request - * @return ResponseInterface - */ - public function main(ServerRequestInterface $request) - { - // parameters are sent by POST --> use getParsedBody() instead of getQueryParams() - $parameters = $request->getParsedBody(); - $sru = (string)$parameters['sru']; - $query = (string)$parameters['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); - - $results = []; - - if ($sruXML !== FALSE) { - // the result may be a valid or some HTML code - $sruResponse = $sruXML->xpath('/srw:searchRetrieveResponse'); - - if ($sruResponse === FALSE) { - $results['error'] = ''; - } else { - $sruRecords = $sruXML->xpath('/srw:searchRetrieveResponse/srw:records/srw:record'); - if ($sruRecords === FALSE || empty($sruRecords)) { - $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); - } - - $page = (string)$pageAttributes['id']; - - // get METS file of search hit - $parentUrl = (string)$fullTextHit[$id]->children('http://dfg-viewer.de/')->page->parent->attributes()->url; - - // unset $hightlightParams but make sure, it's an array() - $highlightParams = []; - - // 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) { - $spanPreview = ''; - $spanText = ''; - if (!empty($hit['attributes']['preview'])) { - $spanPreview = ''; - } - - if (is_object($hit['text'])) { - $spanText = ''; - foreach ($hit['text'] as $key => $text) { - if ($text->attributes()->class[0] == 'highlight') { - $spanText .= '' . $text . ''; - } else { - $spanText .= $text; - } - } - $spanText .= ''; - } - - $origImageParams = '0,' . $pageAttributes['width'] . ',' . $pageAttributes ['height']; - - unset($data); - - $data['link'] = $parentUrl; - $data['page'] = $page; - $data['text'] = $spanText; - $data['previewImage'] = $spanPreview; - $data['previewText'] = $spanText; - $data['origImage'] = $origImageParams; - $data['highlight'] = urlencode(serialize($highlightParams)); - - $results[] = $data; - } - } - } - } - - // create response object - /** @var Response $response */ - $response = GeneralUtility::makeInstance(Response::class); - $response->getBody()->write(json_encode($results)); - return $response; - } -} diff --git a/Classes/Middleware/SruMiddleware.php b/Classes/Middleware/SruMiddleware.php new file mode 100644 index 000000000..1f20dc861 --- /dev/null +++ b/Classes/Middleware/SruMiddleware.php @@ -0,0 +1,182 @@ + + * (c) 2023 Beatrycze Volk + * All rights reserved + * + * This script is part of the TYPO3 project. The TYPO3 project is + * free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * The GNU General Public License can be found at + * http://www.gnu.org/copyleft/gpl.html. + * + * This script is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * This copyright notice MUST APPEAR in all copies of the script! + ***************************************************************/ + +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\MiddlewareInterface; +use Psr\Http\Server\RequestHandlerInterface; +use TYPO3\CMS\Core\Http\Response; +use TYPO3\CMS\Core\Utility\GeneralUtility; + +/** + * Plugin 'DFG-Viewer: SRU Client Middleware for the 'dfgviewer' extension. + * + * @package TYPO3 + * @subpackage tx_dfgviewer + * @access public + */ +class SruMiddleware implements MiddlewareInterface +{ + + /** + * The main method of the middleware. + * + * @access public + * + * @param ServerRequestInterface $request + * @param RequestHandlerInterface $handler + * + * @return ResponseInterface + */ + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface + { + $response = $handler->handle($request); + // parameters are sent by POST --> use getParsedBody() instead of getQueryParams() + $parameters = $request->getParsedBody(); + // Return if not this middleware + if (!isset($parameters['middleware']) || ($parameters['middleware'] != 'dfgviewer/sru')) { + return $response; + } + + $sru = (string) $parameters['sru']; + $query = (string) $parameters['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); + + $results = []; + + if ($sruXML !== FALSE) { + // the result may be a valid or some HTML code + $sruResponse = $sruXML->xpath('/srw:searchRetrieveResponse'); + + if ($sruResponse === FALSE) { + $results['error'] = ''; + } else { + $sruRecords = $sruXML->xpath('/srw:searchRetrieveResponse/srw:records/srw:record'); + if ($sruRecords === FALSE || empty($sruRecords)) { + $results['error'] = ''; + } else { + $results = $this->getSruRecords($sruRecords); + } + } + } + + // create response object + /** @var Response $response */ + $response = GeneralUtility::makeInstance(Response::class); + $response->getBody()->write(json_encode($results)); + return $response; + } + + /** + * Gets parsed SRU records. + * + * @access private + * + * @param array $sruRecords + * + * @return array + */ + private function getSruRecords(array $sruRecords): array { + $results = []; + + 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); + } + + $page = (string) $pageAttributes['id']; + + // get METS file of search hit + $parentUrl = (string) $fullTextHit[$id]->children('http://dfg-viewer.de/')->page->parent->attributes()->url; + + // unset $highlightParams but make sure, it's an array() + $highlightParams = []; + + // 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) { + $spanPreview = ''; + $spanText = ''; + if (!empty($hit['attributes']['preview'])) { + $spanPreview = ''; + } + + if (is_object($hit['text'])) { + $spanText = ''; + foreach ($hit['text'] as $key => $text) { + if ($text->attributes()->class[0] == 'highlight') { + $spanText .= '' . $text . ''; + } else { + $spanText .= $text; + } + } + $spanText .= ''; + } + + $origImageParams = '0,' . $pageAttributes['width'] . ',' . $pageAttributes ['height']; + + unset($data); + + $data['link'] = $parentUrl; + $data['page'] = $page; + $data['text'] = $spanText; + $data['previewImage'] = $spanPreview; + $data['previewText'] = $spanText; + $data['origImage'] = $origImageParams; + $data['highlight'] = urlencode(serialize($highlightParams)); + + $results[] = $data; + } + } + + return $results; + } +} diff --git a/Configuration/RequestMiddlewares.php b/Configuration/RequestMiddlewares.php new file mode 100644 index 000000000..33f2f8df9 --- /dev/null +++ b/Configuration/RequestMiddlewares.php @@ -0,0 +1,35 @@ + + * All rights reserved + * + * This script is part of the TYPO3 project. The TYPO3 project is + * free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * The GNU General Public License can be found at + * http://www.gnu.org/copyleft/gpl.html. + * + * This script is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * This copyright notice MUST APPEAR in all copies of the script! + ***************************************************************/ + +return [ + 'frontend' => [ + 'dfgviewer/sru' => [ + 'target' => \Kitodo\Dlf\Middleware\SruMiddleware::class, + 'after' => [ + 'typo3/cms-frontend/prepare-tsfe-rendering' + ] + ] + ], +]; diff --git a/Resources/Private/JavaScript/dfgviewerSru.js b/Resources/Private/JavaScript/dfgviewerSru.js index 61ce15165..cb85d7a66 100644 --- a/Resources/Private/JavaScript/dfgviewerSru.js +++ b/Resources/Private/JavaScript/dfgviewerSru.js @@ -35,7 +35,7 @@ $("#tx-dfgviewer-sru-form").submit(function( event ) { $.post( "/", { - eID: "tx_dfgviewer_sru_eid", + middleware: "dfgviewer/sru", q: $( "input[name='tx_dlf[query]']" ).val(), L: $( "input[name='tx_dfgviewer[L]']" ).val(), id: $( "input[name='tx_dfgviewer[id]']" ).val(), diff --git a/Resources/Public/JavaScript/allScripts.js b/Resources/Public/JavaScript/allScripts.js index b68f7a5a2..968c69afe 100644 --- a/Resources/Public/JavaScript/allScripts.js +++ b/Resources/Public/JavaScript/allScripts.js @@ -1,6 +1,6 @@ /*! modernizr 3.5.0 (Custom Build) | MIT * * https://modernizr.com/download/?-csstransforms3d-csstransitions-objectfit-touchevents-domprefixes-prefixed-prefixes-setclasses-testallprops-testprop-teststyles !*/ -!function(e,t,n){function r(e,t){return typeof e===t}function i(){var e,t,n,i,o,s,a;for(var l in x)if(x.hasOwnProperty(l)){if(e=[],t=x[l],t.name&&(e.push(t.name.toLowerCase()),t.options&&t.options.aliases&&t.options.aliases.length))for(n=0;nf;f++)if(v=e[f],g=q.style[v],a(v,"-")&&(v=s(v)),q.style[v]!==n){if(o||r(i,"undefined"))return c(),"pfx"==t?v:!0;try{q.style[v]=i}catch(e){}if(q.style[v]!=g)return c(),"pfx"==t?v:!0}return c(),!1}function h(e,t,n,i,o){var s=e.charAt(0).toUpperCase()+e.slice(1),a=(e+" "+E.join(s+" ")+s).split(" ");return r(t,"string")||r(t,"undefined")?g(a,t,i,o):(a=(e+" "+_.join(s+" ")+s).split(" "),f(a,t,n))}function w(e,t,r){return h(e,n,n,t,r)}var b=[],x=[],$={_version:"3.5.0",_config:{classPrefix:"",enableClasses:!0,enableJSClass:!0,usePrefixes:!0},_q:[],on:function(e,t){var n=this;setTimeout((function(){t(n[e])}),0)},addTest:function(e,t,n){x.push({name:e,fn:t,options:n})},addAsyncTest:function(e){x.push({name:null,fn:e})}},C=function(){};C.prototype=$,C=new C;var y=$._config.usePrefixes?" -webkit- -moz- -o- -ms- ".split(" "):["",""];$._prefixes=y;var k=t.documentElement,S="svg"===k.nodeName.toLowerCase(),z="Moz O ms Webkit",_=$._config.usePrefixes?z.toLowerCase().split(" "):[];$._domPrefixes=_;var T="CSS"in e&&"supports"in e.CSS,j="supportsCSS"in e;C.addTest("supports",T||j);var E=$._config.usePrefixes?z.split(" "):[];$._cssomPrefixes=E;var P=function(t){var r,i=y.length,o=e.CSSRule;if("undefined"==typeof o)return n;if(!t)return!1;if(t=t.replace(/^@/,""),r=t.replace(/-/g,"_").toUpperCase()+"_RULE",r in o)return"@"+t;for(var s=0;i>s;s++){var a=y[s],l=a.toUpperCase()+"_"+r;if(l in o)return"@-"+a.toLowerCase()+"-"+t}return!1};$.atRule=P;var F=$.testStyles=u;C.addTest("touchevents",(function(){var n;if("ontouchstart"in e||e.DocumentTouch&&t instanceof DocumentTouch)n=!0;else{var r=["@media (",y.join("touch-enabled),("),"heartz",")","{#modernizr{top:9px;position:absolute}}"].join("");F(r,(function(e){n=9===e.offsetTop}))}return n}));var M={elem:l("modernizr")};C._q.push((function(){delete M.elem}));var q={style:M.elem.style};C._q.unshift((function(){delete q.style}));$.testProp=function(e,t,r){return g([e],n,t,r)};$.testAllProps=h;var D=$.prefixed=function(e,t,n){return 0===e.indexOf("@")?P(e):(-1!=e.indexOf("-")&&(e=s(e)),t?h(e,t,n):h(e,"pfx"))};$.testAllProps=w,C.addTest("csstransforms3d",(function(){var e=!!w("perspective","1px",!0),t=C._config.usePrefixes;if(e&&(!t||"webkitPerspective"in k.style)){var n,r="#modernizr{width:0;height:0}";C.supports?n="@supports (perspective: 1px)":(n="@media (transform-3d)",t&&(n+=",(-webkit-transform-3d)")),n+="{#modernizr{width:7px;height:18px;margin:0;padding:0;border:0}}",F(r+n,(function(t){e=7===t.offsetWidth&&18===t.offsetHeight}))}return e})),C.addTest("csstransitions",w("transition","all",!0)),C.addTest("objectfit",!!D("objectFit"),{aliases:["object-fit"]}),i(),o(b),delete $.addTest,delete $.addAsyncTest;for(var U=0;U"}else{for(var r=0;r'+e[r].previewImage)}if(e[r].previewText){t.push(''+e[r].previewText)}}if(t.length>0){t.forEach((function(e,t){n+="
  • "+e+"
  • "}))}else{n+='
  • '+$("#tx-dfgviewer-sru-label-noresult").text()+"
  • "}}n+="";$("#tx-dfgviewer-sru-results").html(n)}),"json").done((function(e){$("#tx-dfgviewer-sru-results-loading").hide();$("#tx-dfgviewer-sru-results-clearing").show()}))}));$("#tx-dfgviewer-sru-results-clearing").click((function(){$("#tx-dfgviewer-sru-results ul").remove();$(".sru-results-active-indicator").remove();$("#tx-dfgviewer-sru-query").val("")}))})); +!function(e,t,n){function r(e,t){return typeof e===t}function i(){var e,t,n,i,o,s,a;for(var l in x)if(x.hasOwnProperty(l)){if(e=[],t=x[l],t.name&&(e.push(t.name.toLowerCase()),t.options&&t.options.aliases&&t.options.aliases.length))for(n=0;nf;f++)if(v=e[f],g=q.style[v],a(v,"-")&&(v=s(v)),q.style[v]!==n){if(o||r(i,"undefined"))return c(),"pfx"==t?v:!0;try{q.style[v]=i}catch(e){}if(q.style[v]!=g)return c(),"pfx"==t?v:!0}return c(),!1}function h(e,t,n,i,o){var s=e.charAt(0).toUpperCase()+e.slice(1),a=(e+" "+E.join(s+" ")+s).split(" ");return r(t,"string")||r(t,"undefined")?g(a,t,i,o):(a=(e+" "+_.join(s+" ")+s).split(" "),f(a,t,n))}function w(e,t,r){return h(e,n,n,t,r)}var b=[],x=[],$={_version:"3.5.0",_config:{classPrefix:"",enableClasses:!0,enableJSClass:!0,usePrefixes:!0},_q:[],on:function(e,t){var n=this;setTimeout((function(){t(n[e])}),0)},addTest:function(e,t,n){x.push({name:e,fn:t,options:n})},addAsyncTest:function(e){x.push({name:null,fn:e})}},C=function(){};C.prototype=$,C=new C;var y=$._config.usePrefixes?" -webkit- -moz- -o- -ms- ".split(" "):["",""];$._prefixes=y;var k=t.documentElement,S="svg"===k.nodeName.toLowerCase(),z="Moz O ms Webkit",_=$._config.usePrefixes?z.toLowerCase().split(" "):[];$._domPrefixes=_;var T="CSS"in e&&"supports"in e.CSS,j="supportsCSS"in e;C.addTest("supports",T||j);var E=$._config.usePrefixes?z.split(" "):[];$._cssomPrefixes=E;var P=function(t){var r,i=y.length,o=e.CSSRule;if("undefined"==typeof o)return n;if(!t)return!1;if(t=t.replace(/^@/,""),r=t.replace(/-/g,"_").toUpperCase()+"_RULE",r in o)return"@"+t;for(var s=0;i>s;s++){var a=y[s],l=a.toUpperCase()+"_"+r;if(l in o)return"@-"+a.toLowerCase()+"-"+t}return!1};$.atRule=P;var F=$.testStyles=u;C.addTest("touchevents",(function(){var n;if("ontouchstart"in e||e.DocumentTouch&&t instanceof DocumentTouch)n=!0;else{var r=["@media (",y.join("touch-enabled),("),"heartz",")","{#modernizr{top:9px;position:absolute}}"].join("");F(r,(function(e){n=9===e.offsetTop}))}return n}));var M={elem:l("modernizr")};C._q.push((function(){delete M.elem}));var q={style:M.elem.style};C._q.unshift((function(){delete q.style}));$.testProp=function(e,t,r){return g([e],n,t,r)};$.testAllProps=h;var D=$.prefixed=function(e,t,n){return 0===e.indexOf("@")?P(e):(-1!=e.indexOf("-")&&(e=s(e)),t?h(e,t,n):h(e,"pfx"))};$.testAllProps=w,C.addTest("csstransforms3d",(function(){var e=!!w("perspective","1px",!0),t=C._config.usePrefixes;if(e&&(!t||"webkitPerspective"in k.style)){var n,r="#modernizr{width:0;height:0}";C.supports?n="@supports (perspective: 1px)":(n="@media (transform-3d)",t&&(n+=",(-webkit-transform-3d)")),n+="{#modernizr{width:7px;height:18px;margin:0;padding:0;border:0}}",F(r+n,(function(t){e=7===t.offsetWidth&&18===t.offsetHeight}))}return e})),C.addTest("csstransitions",w("transition","all",!0)),C.addTest("objectfit",!!D("objectFit"),{aliases:["object-fit"]}),i(),o(b),delete $.addTest,delete $.addAsyncTest;for(var U=0;U"}else{for(var r=0;r'+e[r].previewImage)}if(e[r].previewText){t.push('
    '+e[r].previewText)}}if(t.length>0){t.forEach((function(e,t){n+="
  • "+e+"
  • "}))}else{n+='
  • '+$("#tx-dfgviewer-sru-label-noresult").text()+"
  • "}}n+="";$("#tx-dfgviewer-sru-results").html(n)}),"json").done((function(e){$("#tx-dfgviewer-sru-results-loading").hide();$("#tx-dfgviewer-sru-results-clearing").show()}))}));$("#tx-dfgviewer-sru-results-clearing").click((function(){$("#tx-dfgviewer-sru-results ul").remove();$(".sru-results-active-indicator").remove();$("#tx-dfgviewer-sru-query").val("")}))})); /*! Custom scripts diff --git a/ext_localconf.php b/ext_localconf.php index 4d8b1bd40..e7fd0d660 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -5,6 +5,7 @@ * Copyright notice * * (c) 2012 Sebastian Meyer +* (c) 2023 Beatrycze Volk * All rights reserved * * This script is part of the TYPO3 project. The TYPO3 project is @@ -24,14 +25,9 @@ * This copyright notice MUST APPEAR in all copies of the script! ***************************************************************/ -use \TYPO3\CMS\Core\Utility\ExtensionManagementUtility; - // Register plugins. // ExtensionManagementUtility::addPItoST43('dfgviewer', 'Classes/Plugins/Sru/Sru.php', '_sru', 'list_type', TRUE); -// Register eID handlers. -$GLOBALS['TYPO3_CONF_VARS']['FE']['eID_include']['tx_dfgviewer_sru_eid'] = \Slub\Dfgviewer\Eid\SruEid::class . '::main'; - // Register Extbase plugins \TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin( 'Slub.Dfgviewer',