Skip to content

Commit

Permalink
[FEATURE] Allow categories to be added dynamically by raw UIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
xperseguers committed Jan 21, 2021
1 parent dcd2985 commit 3f0f0c8
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 40 deletions.
82 changes: 57 additions & 25 deletions Classes/Service/Extraction/AbstractExtractionService.php
Expand Up @@ -311,6 +311,8 @@ protected function remapServiceOutput(array $data, array $mapping = null)
return $output;
}

$output['__category_uids__'] = $data['__category_uids__'] ?? null;

foreach ($mapping as $m) {
$falKey = $m['FAL'];
$alternativeKeys = $m['DATA'];
Expand Down Expand Up @@ -415,15 +417,30 @@ protected function enforceStringLengths(array &$output): void
protected function processCategories(File $file, array &$metadata)
{
$categories = [];
$categoryUids = [];
$key = '__categories__';
$keyUid = '__category_uids__';
if (isset($metadata[$key])) {
$categories = GeneralUtility::trimExplode(',', $metadata[$key], true);
unset($metadata[$key]);
}
if (empty($categories) || $file->getUid() === 0) {
if (isset($metadata[$keyUid])) {
$categoryUids = GeneralUtility::intExplode(',', $metadata[$keyUid], true);
unset($metadata[$keyUid]);
}

if ((empty($categories) && empty($categoryUids)) || $file->getUid() === 0) {
return;
}

$typo3Branch = class_exists(\TYPO3\CMS\Core\Information\Typo3Version::class)
? (new \TYPO3\CMS\Core\Information\Typo3Version())->getBranch()
: TYPO3_branch;
if (version_compare($typo3Branch, '10.0', '>')) {
// Since TYPO3 v10, the sys_file_metadata record is not yet available at this point
$file->getMetaData()->save();
}

// Fetch the uid associated to the corresponding sys_file_metadata record
$row = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('sys_file_metadata')
Expand All @@ -441,17 +458,8 @@ protected function processCategories(File $file, array &$metadata)
return;
}
$fileMetadataUid = $row['uid'];

$typo3Categories = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('sys_category')
->select(
['uid', 'title'],
'sys_category',
[
'sys_language_uid' => 0,
]
)
->fetchAll();
$sorting = 1;
$data = [];

// Remove currently associated categories for this file
$relationTable = 'sys_category_record_mm';
Expand All @@ -466,26 +474,50 @@ protected function processCategories(File $file, array &$metadata)
]
);

$sorting = 1;
$data = [];
foreach ($categories as $category) {
foreach ($typo3Categories as $typo3Category) {
if ($typo3Category['title'] === $category) {
$data[] = [
'uid_local' => (int)$typo3Category['uid'],
'uid_foreign' => $fileMetadataUid,
'tablenames' => 'sys_file_metadata',
'fieldname' => 'categories',
'sorting_foreign' => $sorting++,
];
if (!empty($categories)) {
$typo3Categories = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('sys_category')
->select(
['uid', 'title'],
'sys_category',
[
'sys_language_uid' => 0,
]
)
->fetchAll();

foreach ($categories as $category) {
foreach ($typo3Categories as $typo3Category) {
if ($typo3Category['title'] === $category
|| (string)$typo3Category['uid'] === $category
) {
$categoryUid = (int)$typo3Category['uid'];
$data[$categoryUid] = [
'uid_local' => $categoryUid,
'uid_foreign' => $fileMetadataUid,
'tablenames' => 'sys_file_metadata',
'fieldname' => 'categories',
'sorting_foreign' => $sorting++,
];
}
}
}
}

foreach ($categoryUids as $categoryUid) {
$data[$categoryUid] = [
'uid_local' => $categoryUid,
'uid_foreign' => $fileMetadataUid,
'tablenames' => 'sys_file_metadata',
'fieldname' => 'categories',
'sorting_foreign' => $sorting++,
];
}

if (!empty($data)) {
$tableConnection->bulkInsert(
$relationTable,
$data,
array_values($data),
['uid_local', 'uid_foreign', 'tablenames', 'fieldname', 'sorting_foreign']
);
}
Expand Down
22 changes: 22 additions & 0 deletions Documentation/DeveloperManual/Index.rst
Expand Up @@ -176,3 +176,25 @@ This requires a PHP class
// your code
}
}
.. _developer-manual-signal-categories:

Associated TYPO3 categories
"""""""""""""""""""""""""""

By default TYPO3 categories are automatically assigned using keywords found in
the metadata due to the mapping associating them to the special FAL field
``__categories__``. This virtual field expects a comma-separated list of TYPO3
category **titles**.

Since version 2.1.0, we added another special FAL field ``__category_uids__``
which works similarly but expecting a comma-separated list of category **uids**
instead. One would use the signal/event and expand extracted metadata with a
custom business logic.

An real-life example is using the geographical coordinates latitude/longitude,
send them to the
`Google reverse geocoding service <https://developers.google.com/maps/documentation/geocoding/overview?hl=en_US#ReverseGeocoding>`__
to translate them into a human-readable address and thus populating the fields
"location", "region" and "country" and possibly assign geographical-related
TYPO3 categories based on the API output.
4 changes: 2 additions & 2 deletions Documentation/Settings.cfg
@@ -1,8 +1,8 @@
[general]

project = Metadata and Content Analysis Service
version = 2.0
release = 2.0.0
version = 2.1
release = 2.1.0-dev
t3author = Xavier Perseguers
copyright = 2014-2021

Expand Down
4 changes: 2 additions & 2 deletions Documentation/Settings.yml
Expand Up @@ -6,8 +6,8 @@
conf.py:
copyright: 2014-2021
project: Metadata and Content Analysis Service
version: 2.0
release: 2.0.0
version: 2.1
release: 2.1.0-dev
latex_documents:
- - Index
- extractor.tex
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Expand Up @@ -63,8 +63,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.7.x-dev",
"dev-feature/TYPO3_v11": "2.0.x-dev"
"dev-master": "2.1.x-dev"
},
"typo3/cms": {
"extension-key": "extractor",
Expand Down
2 changes: 1 addition & 1 deletion ext_emconf.php
Expand Up @@ -21,7 +21,7 @@
'uploadfolder' => 0,
'createDirs' => '',
'clearCacheOnLoad' => 0,
'version' => '2.0.0',
'version' => '2.1.0-dev',
'constraints' => [
'depends' => [
'php' => '7.2.0-7.4.99',
Expand Down
14 changes: 6 additions & 8 deletions ext_tables.php
Expand Up @@ -2,12 +2,10 @@
defined('TYPO3_MODE') || die();

(static function (string $_EXTKEY) {
if (TYPO3_MODE === 'BE') {
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports']['sv']['extractor'] = [
'title' => 'LLL:EXT:' . $_EXTKEY . '/Resources/Private/Language/locallang_reports.xlf:report_title',
'description' => 'LLL:EXT:' . $_EXTKEY . '/Resources/Private/Language/locallang_reports.xlf:report_description',
'icon' => 'EXT:' . $_EXTKEY . '/Resources/Public/Images/tx_sv_report.png',
'report' => \Causal\Extractor\Report\ServicesListReport::class
];
}
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports']['sv']['extractor'] = [
'title' => 'LLL:EXT:' . $_EXTKEY . '/Resources/Private/Language/locallang_reports.xlf:report_title',
'description' => 'LLL:EXT:' . $_EXTKEY . '/Resources/Private/Language/locallang_reports.xlf:report_description',
'icon' => 'EXT:' . $_EXTKEY . '/Resources/Public/Images/tx_sv_report.png',
'report' => \Causal\Extractor\Report\ServicesListReport::class
];
})('extractor');

0 comments on commit 3f0f0c8

Please sign in to comment.