-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
- Loading branch information
There are no files selected for viewing
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 | ||
* | ||
* @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 Codacy Production / Codacy Static Code AnalysisClasses/Updates/UpdateSolrSchema.php#L83
|
||
|
||
$solr = Solr::getInstance($affectedSolrCore['uid']); | ||
if (!$solr->ready) { | ||
continue; | ||
} | ||
|
||
$query = $solr->service->createApi([ | ||
'version' => Request::API_V1, | ||
'handler' => $affectedSolrCore['index_name'].'/schema', | ||
'method' => Request::METHOD_POST, | ||
'rawdata' => json_encode([ | ||
'replace-field' => [ | ||
'name' => 'autocomplete', | ||
'type' => 'autocomplete', | ||
'indexed' => true, | ||
'stored' => true, | ||
'multiValued' => true, | ||
], | ||
]), | ||
]); | ||
$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 Codacy Production / Codacy Static Code AnalysisClasses/Updates/UpdateSolrSchema.php#L124
|
||
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 Codacy Production / Codacy Static Code AnalysisClasses/Updates/UpdateSolrSchema.php#L165
|
||
$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 | ||
|
||
]); | ||
$result = $solr->service->execute($query)->getData(); | ||
|
||
if(!isset($result['config']['schemaFactory']['class'])) { | ||
Check notice on line 179 in Classes/Updates/UpdateSolrSchema.php Codacy Production / Codacy Static Code AnalysisClasses/Updates/UpdateSolrSchema.php#L179
|
||
continue; | ||
} | ||
else if($result['config']['schemaFactory']['class'] != 'ManagedIndexSchemaFactory') { | ||
Check notice on line 182 in Classes/Updates/UpdateSolrSchema.php Codacy Production / Codacy Static Code AnalysisClasses/Updates/UpdateSolrSchema.php#L182
|
||
continue; | ||
} | ||
|
||
$query = $solr->service->createApi([ | ||
'version' => Request::API_V1, | ||
'handler' => $solrCore['index_name'].'/schema/fields/autocomplete', | ||
'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 Codacy Production / Codacy Static Code AnalysisClasses/Updates/UpdateSolrSchema.php#L194
|
||
continue; | ||
} | ||
else if($result['field']['stored'] === true) { | ||
Check notice on line 197 in Classes/Updates/UpdateSolrSchema.php Codacy Production / Codacy Static Code AnalysisClasses/Updates/UpdateSolrSchema.php#L197
|
||
continue; | ||
} | ||
|
||
$affectedSolrCores[] = $solrCore; | ||
} | ||
return $affectedSolrCores; | ||
} | ||
} | ||
Check notice on line 205 in Classes/Updates/UpdateSolrSchema.php Codacy Production / Codacy Static Code AnalysisClasses/Updates/UpdateSolrSchema.php#L205
|