Skip to content

Commit

Permalink
[TASK] Make updating the indexer status via AJAX compatible with TYPO…
Browse files Browse the repository at this point in the history
…3 11
  • Loading branch information
christianbltr committed Apr 5, 2024
1 parent 4925335 commit 983d363
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 20 deletions.
29 changes: 17 additions & 12 deletions Classes/Controller/BackendModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Http\HtmlResponse;
use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Registry;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
Expand All @@ -53,15 +54,18 @@ class BackendModuleController
protected int $pageId = 0;
protected ?string $do;
protected array $extConf;
protected PageRenderer $pageRenderer;

public function __construct(
Registry $registry,
IndexRepository $indexRepository,
ModuleTemplateFactory $moduleTemplateFactory
ModuleTemplateFactory $moduleTemplateFactory,
PageRenderer $pageRenderer
) {
$this->registry = $registry;
$this->indexRepository = $indexRepository;
$this->moduleTemplateFactory = $moduleTemplateFactory;
$this->pageRenderer = $pageRenderer;
}

public function __invoke(ServerRequestInterface $request): ResponseInterface
Expand Down Expand Up @@ -137,6 +141,13 @@ public function startIndexingAction(ServerRequestInterface $request, ModuleTempl
$indexerConfigurations = $indexer->getConfigurations();
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);

if (GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion() < 12) {
$this->pageRenderer->addJsFile('EXT:ke_search/Resources/Public/JavaScript/v11/getIndexerStatusRequest.js');
} else {
// @phpstan-ignore-next-line
$this->pageRenderer->loadJavaScriptModule('@tpwd/ke-search/getIndexerStatusRequest.js');
}

$indexingMode = (int)($request->getQueryParams()['indexingMode'] ?? IndexerBase::INDEXING_MODE_FULL);
if (!in_array($indexingMode, [IndexerBase::INDEXING_MODE_INCREMENTAL, IndexerBase::INDEXING_MODE_FULL])) {
$indexingMode = IndexerBase::INDEXING_MODE_FULL;
Expand Down Expand Up @@ -223,7 +234,7 @@ public function startIndexingAction(ServerRequestInterface $request, ModuleTempl
'do' => 'startindexer',
]
);
$content .= '<a class="btn btn-primary" id="kesearch-button-start-full" href="' . $moduleUrl . '">'
$content .= '<a class="btn btn-info" id="kesearch-button-start-full" href="' . $moduleUrl . '">'
. LocalizationUtility::translate('backend.start_indexer_full', 'ke_search')
. '</a>';
$moduleUrl = $uriBuilder->buildUriFromRoute(
Expand Down Expand Up @@ -464,15 +475,9 @@ public function printIndexerConfigurations($indexerConfigurations)
foreach ($indexerConfigurations as $indexerConfiguration) {
$content .= '<tr>'
. '<td>' . $this->encode($indexerConfiguration['title']) . '</td>'
. '<td>'
. '<span class="label label-primary">' . $indexerConfiguration['type'] . '</span>'
. '</td>'
. '<td>'
. $indexerConfiguration['uid']
. '</td>'
. '<td>'
. $indexerConfiguration['pid']
. '</td>'
. '<td>' . $indexerConfiguration['type'] . '</td>'
. '<td>' . $indexerConfiguration['uid'] . '</td>'
. '<td>' . $indexerConfiguration['pid'] . '</td>'
. '</tr>';
}
$content .= '</table></div>';
Expand Down Expand Up @@ -525,7 +530,7 @@ public function printNumberOfRecords()
$indexRepository = GeneralUtility::makeInstance(IndexRepository::class);
$results_per_type = $indexRepository->getNumberOfRecordsInIndexPerType();
foreach ($results_per_type as $type => $count) {
$content .= '<tr><td><span class="label label-primary">' . $type . '</span></td><td>' . $count . '</td></tr>';
$content .= '<tr><td>' . $type . '</td><td>' . $count . '</td></tr>';
}

$content .= '</table></div>';
Expand Down
4 changes: 2 additions & 2 deletions Classes/Indexer/IndexerRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public function startIndexing($verbose = true, $extConf = [], $mode = '', $index
// log finishing
$indexingTime = $this->endTime - $this->startTime;
$content .= '<div class="alert alert-success">';
$content .= '<h3>Finished</h3>' . chr(10);
$content .= chr(10) . '<h3>Finished</h3>' . chr(10);

$message = 'Indexing finished at ' . SearchHelper::formatTimestamp($this->endTime) . ' (took ' . $this->formatTime($indexingTime) . ').';
$content .= $message;
Expand Down Expand Up @@ -503,7 +503,7 @@ public function cleanUpProcessAfterIndexing()
public function cleanUpIndex(int $indexingMode)
{
$content = '<div class="alert alert-notice">';
$content .= '<h3>Cleanup</h3>' . chr(10);
$content .= chr(10) . '<h3>Cleanup</h3>' . chr(10);
if ($indexingMode == IndexerBase::INDEXING_MODE_FULL) {
$this->logger->info('Cleanup started');
$startMicrotime = microtime(true);
Expand Down
7 changes: 6 additions & 1 deletion Classes/Service/IndexerStatusService.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function setFinishedStatus(array $indexerConfig)
public function setRunningStatus(array $indexerConfig, int $currentRecordCount = -1, int $totalRecordCount = -1)
{
$indexerStatus = $this->getIndexerStatus();
$oldStatus = $indexerStatus['indexers'][$indexerConfig['uid']]['status'] ?? null;
$indexerStatus['indexers'][$indexerConfig['uid']] = [
'status' => self::INDEXER_STATUS_RUNNING,
'currentRecordCount' => $currentRecordCount,
Expand All @@ -96,7 +97,11 @@ public function setRunningStatus(array $indexerConfig, int $currentRecordCount =
$indexerStatus['indexers'][$indexerConfig['uid']]['statusText'] .=
' (' . $currentRecordCount . ' / ' . $totalRecordCount . ' records)';
}
$this->setIndexerStatus($indexerStatus);
// To reduce the amount of database access, we only update the registry if the status was
// not "running" before and every 100 records
if ($oldStatus !== self::INDEXER_STATUS_RUNNING || $currentRecordCount % 100 === 0) {
$this->setIndexerStatus($indexerStatus);
}
}

public function getIndexerStatus(): array
Expand Down
5 changes: 0 additions & 5 deletions Resources/Private/Templates/BackendModule/StartIndexing.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
</div>
</div>
<f:format.raw>{content}</f:format.raw>
<f:be.pageRenderer
includeJavaScriptModules="{
0: '@tpwd/ke-search/getIndexerStatusRequest.js'
}"
/>
</f:section>

</html>
32 changes: 32 additions & 0 deletions Resources/Public/JavaScript/v11/getIndexerStatusRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require(['TYPO3/CMS/Core/Ajax/AjaxRequest'], function (AjaxRequest) {
setInterval(function () {
new AjaxRequest(TYPO3.settings.ajaxUrls.kesearch_indexerstatus_getstatus)
.get()
.then(async function (response) {
const indexerStatus = await response.resolve();
document.getElementById("kesearch-indexer-status").innerHTML = indexerStatus.html;
if (indexerStatus.running === true) {
let hideElements= ["kesearch-indexer-overview", "kesearch-button-start-full", "kesearch-indexer-report", "kesearch-button-start-incremental", "kesearch-button-reload"];
hideElements.forEach((element) => {
if (document.getElementById(element)) {
document.getElementById(element).style.display = 'none';
}
});
} else {
let hideElements= ["kesearch-indexer-running-warning"];
hideElements.forEach((element) => {
if (document.getElementById(element)) {
document.getElementById(element).style.display = 'none';
}
});
let showElements= ["kesearch-button-reload"];
showElements.forEach((element) => {
if (document.getElementById(element)) {
document.getElementById(element).style.display = 'inline-flex';
}
});
}
});
}, 1000);
});

0 comments on commit 983d363

Please sign in to comment.