Skip to content

Commit

Permalink
Merge pull request #88 from timohund/task/master/85-allow-to-see-extr…
Browse files Browse the repository at this point in the history
…acted-content-of-tika-in-the-backend

[TASK] Allow to see the extraction content from the backend
  • Loading branch information
timohund committed Mar 1, 2018
2 parents bc4bd19 + b6ec7de commit b40f3ec
Show file tree
Hide file tree
Showing 25 changed files with 530 additions and 1,533 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ env:
- TIKA_VERSION="1.17"
- TIKA_PATH=$HOME/tika
- EXT_SOLR_VERSION="dev-master"
- TYPO3_DATABASE_NAME="typo3_ci"
- TYPO3_DATABASE_HOST="127.0.0.1"
- TYPO3_DATABASE_USERNAME="root"
- TYPO3_DATABASE_PASSWORD=""
matrix:
- TYPO3_VERSION="^8.7"
- TYPO3_VERSION="dev-master"
Expand Down
26 changes: 26 additions & 0 deletions Build/Test/IntegrationTests.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertWarningsToExceptions="true"
forceCoversAnnotation="false"
processIsolation="true"
stopOnError="true"
stopOnFailure="true"
stopOnIncomplete="false"
stopOnSkipped="false"
verbose="false">

<testsuites>
<testsuite name="tika-integration-tests">
<directory>../../Tests/Integration/</directory>
</testsuite>

</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="false">
<directory suffix=".php">../../Classes/</directory>
</whitelist>
</filter>
</phpunit>
3 changes: 0 additions & 3 deletions Build/Test/UnitTests.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
<testsuite name="tika-unit-tests">
<directory>../../Tests/Unit/</directory>
</testsuite>
<testsuite name="tika-integration-tests">
<directory>../../Tests/Integration/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
Expand Down
1 change: 0 additions & 1 deletion Build/Test/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ echo "Using web path $TYPO3_PATH_WEB"

composer global require scrutinizer/ocular:"1.3.1"
composer require --dev typo3/cms="$TYPO3_VERSION"
composer require --dev --prefer-source typo3/testing-framework="1.0.7"
composer require apache-solr-for-typo3/solr:"$EXT_SOLR_VERSION"

# Restore composer.json
Expand Down
43 changes: 41 additions & 2 deletions Build/Test/cibuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,45 @@ test -n "$TIKA_PATH" || export TIKA_PATH="$HOME/bin"

export TYPO3_PATH_WEB=$(pwd)/.Build/Web

UNIT_BOOTSTRAP=".Build/vendor/typo3/testing-framework/Resources/Core/Build/UnitTestsBootstrap.php"
UNIT_BOOTSTRAP=".Build/vendor/nimut/testing-framework/res/Configuration/UnitTestsBootstrap.php"
.Build/bin/phpunit --colors -c Build/Test/UnitTests.xml --bootstrap=$UNIT_BOOTSTRAP --coverage-clover=coverage.unit.clover

.Build/bin/phpunit --colors -c Build/Test/UnitTests.xml --bootstrap=$UNIT_BOOTSTRAP
if [ $? -ne "0" ]; then
echo "Error during running the unit tests please check and fix them"
exit 1
fi

#
# Map the travis and shell variable names to the expected
# casing of the TYPO3 core.
#
if [ -n $TYPO3_DATABASE_NAME ]; then
export typo3DatabaseName=$TYPO3_DATABASE_NAME
else
echo "No environment variable TYPO3_DATABASE_NAME set. Please set it to run the integration tests."
exit 1
fi

if [ -n $TYPO3_DATABASE_HOST ]; then
export typo3DatabaseHost=$TYPO3_DATABASE_HOST
else
echo "No environment variable TYPO3_DATABASE_HOST set. Please set it to run the integration tests."
exit 1
fi

if [ -n $TYPO3_DATABASE_USERNAME ]; then
export typo3DatabaseUsername=$TYPO3_DATABASE_USERNAME
else
echo "No environment variable TYPO3_DATABASE_USERNAME set. Please set it to run the integration tests."
exit 1
fi

if [ -n $TYPO3_DATABASE_PASSWORD ]; then
export typo3DatabasePassword=$TYPO3_DATABASE_PASSWORD
else
echo "No environment variable TYPO3_DATABASE_PASSWORD set. Please set it to run the integration tests."
exit 1
fi

INTEGRATION_BOOTSTRAP=".Build/vendor/nimut/testing-framework/res/Configuration/FunctionalTestsBootstrap.php"
.Build/bin/phpunit --colors -c Build/Test/IntegrationTests.xml --bootstrap=$INTEGRATION_BOOTSTRAP --coverage-clover=coverage.integration.clover
111 changes: 111 additions & 0 deletions Classes/ContextMenu/Preview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
namespace ApacheSolrForTypo3\Tika\ContextMenu;

use TYPO3\CMS\Backend\ContextMenu\ItemProviders\AbstractProvider;
use TYPO3\CMS\Core\Resource\File;

class Preview extends AbstractProvider{

protected $itemsConfiguration = [
'tika_preview' => [
'type' => 'item',
'label' => 'Tika Preview', // you can use "LLL:" syntax here
'iconIdentifier' => 'actions-document-view',
'callbackAction' => 'tikaPreview'
]
];

/**
* Checks if this provider may be called to provide the list of context menu items for given table.
*
* @return bool
*/
public function canHandle(): bool
{
if (!$GLOBALS['BE_USER']->isAdmin()) {
return false;
}

// Current table is: $this->table
// Current UID is: $this->identifier
if (!$this->table === 'sys_file') {
return false;
}

$resourceFactory = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
$item = $resourceFactory->retrieveFileOrFolderObject($this->identifier);

// we only handle files, no folders
return ($item instanceof File);
}

/**
* Returns the provider priority which is used for determining the order in which providers are processing items
* to the result array. Highest priority means provider is evaluated first.
*
* This item provider should be called after PageProvider which has priority 100.
*
* BEWARE: Returned priority should logically not clash with another provider.
* Please check @see \TYPO3\CMS\Backend\ContextMenu\ContextMenu::getAvailableProviders() if needed.
*
* @return int
*/
public function getPriority(): int
{
return 90;
}

/**
* Registers the additional JavaScript RequireJS callback-module which will allow to display a notification
* whenever the user tries to click on the "Hello World" item.
* The method is called from AbstractProvider::prepareItems() for each context menu item.
*
* @param string $itemName
* @return array
*/
protected function getAdditionalAttributes(string $itemName): array
{
return ['data-callback-module' => 'TYPO3/CMS/Tika/ContextMenuActions'];
}

/**
* This method adds custom item to list of items generated by item providers with higher priority value (PageProvider)
* You could also modify existing items here.
* The new item is added after the 'info' item.
*
* @param array $items
* @return array
*/
public function addItems(array $items): array
{

$this->initDisabledItems();
$localItems = $this->prepareItems($this->itemsConfiguration);
$items = $items + $localItems;
//passes array of items to the next item provider
return $items;
}

/**
* This method is called for each item this provider adds and checks if given item can be added
*
* @param string $itemName
* @param string $type
* @return bool
*/
protected function canRender(string $itemName, string $type): bool
{
// checking if item is disabled through TSConfig
if (in_array($itemName, $this->disabledItems, true)) {
return false;
}
switch ($itemName) {
case 'tika_preview':
$canRender = true;
break;
default:
$canRender = false;
}
return $canRender;
}
}
92 changes: 92 additions & 0 deletions Classes/Controller/Backend/PreviewController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
namespace ApacheSolrForTypo3\Tika\Controller\Backend;

use ApacheSolrForTypo3\Tika\Service\Tika\AppService;
use ApacheSolrForTypo3\Tika\Service\Tika\ServerService;
use ApacheSolrForTypo3\Tika\Service\Tika\ServiceFactory;
use ApacheSolrForTypo3\Tika\Service\Tika\SolrCellService;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;

/**
* Class PreviewController
* @package ApacheSolrForTypo3\Tika\Controller\Backend
*/
class PreviewController {

/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @return string
*/
public function previewAction(ServerRequestInterface $request, ResponseInterface $response)
{
if (!$this->getIsAdmin()) {
$response->getBody()->write('Only admins can see the tika preview');
return $response;
}

$identifier = (string)$request->getQueryParams()['identifier'];
$file = $this->getFileResourceFactory()->getFileObjectFromCombinedIdentifier($identifier);

$tikaService = $this->getConfiguredTikaService();
$metadata = $tikaService->extractMetaData($file);
$content = $tikaService->extractText($file);

try {
$language = $tikaService->detectLanguageFromFile($file);
} catch (\Exception $e) {
$language = 'not detectable';
}

$view = $this->getInitializedPreviewView();

$view->assign('metadata', $metadata);
$view->assign('content', $content);
$view->assign('language', $language);

$response->getBody()->write($view->render());

return $response;
}

/**
* @return AppService|ServerService|SolrCellService
*/
protected function getConfiguredTikaService()
{
return ServiceFactory::getConfiguredTika();
}

/**
* @return \TYPO3\CMS\Core\Resource\ResourceFactory
*/
protected function getFileResourceFactory(): ResourceFactory
{
return \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
}

/**
* @return StandaloneView
*/
protected function getInitializedPreviewView(): StandaloneView
{
/** @var $view StandaloneView */
$view = GeneralUtility::makeInstance(StandaloneView::class);
$view->getRequest()->setControllerExtensionName('tika');
$templatePathAndFile = 'EXT:tika/Resources/Private/Templates/Backend/Preview.html';
$view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($templatePathAndFile));
return $view;
}

/**
* @return boolean
*/
protected function getIsAdmin()
{
return (bool)$GLOBALS['BE_USER']->isAdmin();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
use ApacheSolrForTypo3\Solr\Controller\Backend\Search\AbstractModuleController;
use ApacheSolrForTypo3\Tika\Service\Tika\ServiceFactory;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\FileRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;

Expand Down
25 changes: 25 additions & 0 deletions Classes/Hooks/BackendControllerHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace ApacheSolrForTypo3\Tika\Hooks;

use TYPO3\CMS\Backend\Controller\BackendController;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* This class adds Filelist related JavaScript to the backend
*/
class BackendControllerHook
{
/**
* Adds Filelist JavaScript used e.g. by context menu
*
* @param array $configuration
* @param BackendController $backendController
*/
public function addJavaScript(array $configuration, BackendController $backendController)
{
$pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
$pageRenderer->addInlineSetting('TikaPreview', 'moduleUrl', BackendUtility::getModuleUrl('tika_preview'));
}
}
11 changes: 11 additions & 0 deletions Classes/Service/Tika/ServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,15 @@ public static function getTika(
}
}

/**
* Creates a tika service instance from the extension configuration.
*
* @return AppService|ServerService|SolrCellService
*/
public static function getConfiguredTika()
{
$tikaConfiguration = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['tika']);
return static::getTika($tikaConfiguration['extractor'], $tikaConfiguration);
}

}
19 changes: 19 additions & 0 deletions Configuration/Backend/Routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* Definitions for routes provided by EXT:backend
* Contains all "regular" routes for entry points
*
* Please note that this setup is preliminary until all core use-cases are set up here.
* Especially some more properties regarding modules will be added until TYPO3 CMS 7 LTS, and might change.
*
* Currently the "access" property is only used so no token creation + validation is made,
* but will be extended further.
*/
return [
//
'tika_preview' => [
'path' => '/tika/preview',
'target' => \ApacheSolrForTypo3\Tika\Controller\Backend\PreviewController::class . '::previewAction'
],
];

0 comments on commit b40f3ec

Please sign in to comment.