Skip to content

Commit

Permalink
Update schema using Schema API
Browse files Browse the repository at this point in the history
  • Loading branch information
frank-ulrich-weber committed Nov 4, 2024
1 parent f0b6815 commit 25891a4
Show file tree
Hide file tree
Showing 3 changed files with 210 additions and 0 deletions.
205 changes: 205 additions & 0 deletions Classes/Updates/UpdateSolrSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
<?php

/**
* (c) Kitodo. Key to digital objects e.V. <[email protected]>
*
* This file is part of the Kitodo and TYPO3 projects.
*
* @license GNU General Public License version 3 or later.
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/

namespace Kitodo\Dlf\Updates;

use Kitodo\Dlf\Common\Solr\Solr;
use Solarium\Core\Client\Request;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Install\Updates\DatabaseUpdatedPrerequisite;
use TYPO3\CMS\Install\Updates\UpgradeWizardInterface;

/**
* Class UpdateSolrSchema
*

Check notice on line 24 in Classes/Updates/UpdateSolrSchema.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Classes/Updates/UpdateSolrSchema.php#L24

Whitespace found at end of line
* @package TYPO3
* @subpackage dlf
*
* @internal
*/
class UpdateSolrSchema implements UpgradeWizardInterface
{

/**
* Return the identifier for this wizard
* This should be the same string as used in the ext_localconf class registration
*
* @access public
*
* @return string
*/
public function getIdentifier(): string
{
return self::class;
}

/**
* Return the speaking name of this wizard
*
* @access public
*
* @return string
*/
public function getTitle(): string
{
return 'Update Solr schema';
}

/**
* Return the description for this wizard
*
* @access public
*
* @return string
*/
public function getDescription(): string
{
return 'This wizard updates the schema of all available Solr cores';
}

/**
* Execute the update
*
* Called when a wizard reports that an update is necessary
*
* @access public
*
* @return bool
*/
public function executeUpdate(): bool
{
$affectedSolrCores = $this->getAllAffectedSolrCores();

foreach($affectedSolrCores as $affectedSolrCore) {

Check notice on line 83 in Classes/Updates/UpdateSolrSchema.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Classes/Updates/UpdateSolrSchema.php#L83

Expected "foreach (...) {\n"; found "foreach(...) {\n"

Check notice on line 83 in Classes/Updates/UpdateSolrSchema.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Classes/Updates/UpdateSolrSchema.php#L83

Expected 1 space after FOREACH keyword; 0 found

$solr = Solr::getInstance($affectedSolrCore['uid']);
if (!$solr->ready) {
continue;
}

$query = $solr->service->createApi([

Check notice on line 90 in Classes/Updates/UpdateSolrSchema.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Classes/Updates/UpdateSolrSchema.php#L90

Opening parenthesis of a multi-line function call must be the last content on the line
'version' => Request::API_V1,
'handler' => $affectedSolrCore['index_name'].'/schema',

Check notice on line 92 in Classes/Updates/UpdateSolrSchema.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Classes/Updates/UpdateSolrSchema.php#L92

Expected at least 1 space before "."; 0 found
'method' => Request::METHOD_POST,
'rawdata' => json_encode([

Check notice on line 94 in Classes/Updates/UpdateSolrSchema.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Classes/Updates/UpdateSolrSchema.php#L94

Opening parenthesis of a multi-line function call must be the last content on the line
'replace-field' => [
'name' => 'autocomplete',
'type' => 'autocomplete',
'indexed' => true,
'stored' => true,
'multiValued' => true,
],
]),

Check notice on line 102 in Classes/Updates/UpdateSolrSchema.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Classes/Updates/UpdateSolrSchema.php#L102

Closing parenthesis of a multi-line function call must be on a line by itself
]);

Check notice on line 103 in Classes/Updates/UpdateSolrSchema.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Classes/Updates/UpdateSolrSchema.php#L103

Closing parenthesis of a multi-line function call must be on a line by itself
$result = $solr->service->execute($query);

if ($result->getResponse()->getStatusCode() == 400) {
return false;
}
}
return true;
}

/**
* Is an update necessary?
*
* Looks for all affected Solr cores
*
* @access public
*
* @return bool
*/
public function updateNecessary(): bool
{
if(count($this->getAllAffectedSolrCores())) {

Check notice on line 124 in Classes/Updates/UpdateSolrSchema.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Classes/Updates/UpdateSolrSchema.php#L124

Expected "if (...) {\n"; found "if(...) {\n"

Check notice on line 124 in Classes/Updates/UpdateSolrSchema.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Classes/Updates/UpdateSolrSchema.php#L124

Expected 1 space after IF keyword; 0 found
return true;
}
return false;
}

/**
* Returns an array of class names of Prerequisite classes
*
* This way a wizard can define dependencies like "database up-to-date" or
* "reference index updated"
*
* @access public
*
* @return string[]
*/
public function getPrerequisites(): array
{
return [
DatabaseUpdatedPrerequisite::class
];
}

/**
* Returns all affected Solr cores
*
* @access private
*
* @return array
*/
private function getAllAffectedSolrCores(): array
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_dlf_solrcores');

$allSolrCores = $queryBuilder->select('uid', 'index_name')
->from('tx_dlf_solrcores')
->execute()
->fetchAllAssociative();

$affectedSolrCores = [];

foreach($allSolrCores as $solrCore) {

Check notice on line 165 in Classes/Updates/UpdateSolrSchema.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Classes/Updates/UpdateSolrSchema.php#L165

Expected "foreach (...) {\n"; found "foreach(...) {\n"

Check notice on line 165 in Classes/Updates/UpdateSolrSchema.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Classes/Updates/UpdateSolrSchema.php#L165

Expected 1 space after FOREACH keyword; 0 found
$solr = Solr::getInstance($solrCore['uid']);
if (!$solr->ready) {
continue;
}

$query = $solr->service->createApi([
'version' => Request::API_V1,
'handler' => $solrCore['index_name'].'/config/schemaFactory',
'method' => Request::METHOD_GET

Check notice on line 175 in Classes/Updates/UpdateSolrSchema.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Classes/Updates/UpdateSolrSchema.php#L175

Whitespace found at end of line
]);
$result = $solr->service->execute($query)->getData();

if(!isset($result['config']['schemaFactory']['class'])) {

Check notice on line 179 in Classes/Updates/UpdateSolrSchema.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Classes/Updates/UpdateSolrSchema.php#L179

Expected "if (...) {\n"; found "if(...) {\n"

Check notice on line 179 in Classes/Updates/UpdateSolrSchema.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Classes/Updates/UpdateSolrSchema.php#L179

Expected 1 space after IF keyword; 0 found
continue;
}

Check notice on line 181 in Classes/Updates/UpdateSolrSchema.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Classes/Updates/UpdateSolrSchema.php#L181

Expected 1 space after closing brace; newline found
else if($result['config']['schemaFactory']['class'] != 'ManagedIndexSchemaFactory') {

Check notice on line 182 in Classes/Updates/UpdateSolrSchema.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Classes/Updates/UpdateSolrSchema.php#L182

Expected 1 space after IF keyword; 0 found

Check notice on line 182 in Classes/Updates/UpdateSolrSchema.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Classes/Updates/UpdateSolrSchema.php#L182

Usage of ELSE IF is discouraged; use ELSEIF instead
continue;
}

$query = $solr->service->createApi([
'version' => Request::API_V1,
'handler' => $solrCore['index_name'].'/schema/fields/autocomplete',

Check notice on line 188 in Classes/Updates/UpdateSolrSchema.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Classes/Updates/UpdateSolrSchema.php#L188

Expected at least 1 space before "."; 0 found
'method' => Request::METHOD_GET

]);
$result = $solr->service->execute($query)->getData();

if(!isset($result['field']['stored'])) {

Check notice on line 194 in Classes/Updates/UpdateSolrSchema.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Classes/Updates/UpdateSolrSchema.php#L194

Expected "if (...) {\n"; found "if(...) {\n"

Check notice on line 194 in Classes/Updates/UpdateSolrSchema.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Classes/Updates/UpdateSolrSchema.php#L194

Expected 1 space after IF keyword; 0 found
continue;
}
else if($result['field']['stored'] === true) {

Check notice on line 197 in Classes/Updates/UpdateSolrSchema.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Classes/Updates/UpdateSolrSchema.php#L197

Expected "} else if (...) {\n"; found "}\n else if(...) {\n"

Check notice on line 197 in Classes/Updates/UpdateSolrSchema.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Classes/Updates/UpdateSolrSchema.php#L197

Expected 1 space after IF keyword; 0 found
continue;
}

$affectedSolrCores[] = $solrCore;
}
return $affectedSolrCores;
}
}

Check notice on line 205 in Classes/Updates/UpdateSolrSchema.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Classes/Updates/UpdateSolrSchema.php#L205

Expected 1 newline at end of file; 0 found

Check notice on line 205 in Classes/Updates/UpdateSolrSchema.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Classes/Updates/UpdateSolrSchema.php#L205

Expected 1 newline at end of file; 0 found
3 changes: 3 additions & 0 deletions Documentation/Administrator/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ b. Rename solrconfig_8.11.2.xml in $SOLR_HOME/configsets/dlf/ to solrconfig.xml
c. Restart Solr.
d. Reindex all documents. This can be done by the kitodo:reindex CLI command with the '-a' (all) flag. See: :ref:`reindex_collections`.

Furthermore version 5.1 supports the use of Solr Managed Schemas to update the schemas automatically during the update of the extension.
To use this feature you have to change the schemaFactory within solrconfig.xml from "ClassicIndexSchemaFactory" to "ManagedIndexSchemaFactory".

*******
Logging
*******
Expand Down
2 changes: 2 additions & 0 deletions ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@
= \Kitodo\Dlf\Updates\MigrateSettings::class;
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update'][\Kitodo\Dlf\Updates\FileLocationUpdater::class]
= \Kitodo\Dlf\Updates\FileLocationUpdater::class;
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update'][\Kitodo\Dlf\Updates\UpdateSolrSchema::class]
= \Kitodo\Dlf\Updates\UpdateSolrSchema::class;

\TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin(
'Dlf',
Expand Down

0 comments on commit 25891a4

Please sign in to comment.