Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from netgen/xmltext_richtext_string_limit
Implement local Indexables for XmlText and RichText
- Loading branch information
Showing
15 changed files
with
576 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace Netgen\Bundle\EzPlatformSearchExtraBundle\DependencyInjection; | ||
|
||
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; | ||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
use Symfony\Component\Config\Definition\ConfigurationInterface; | ||
|
||
class Configuration implements ConfigurationInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $rootNodeName; | ||
|
||
/** | ||
* @param string $rootNodeName | ||
*/ | ||
public function __construct($rootNodeName) | ||
{ | ||
$this->rootNodeName = $rootNodeName; | ||
} | ||
|
||
public function getConfigTreeBuilder() | ||
{ | ||
$treeBuilder = new TreeBuilder(); | ||
$rootNode = $treeBuilder->root($this->rootNodeName); | ||
|
||
$this->addIndexableFieldTypeSection($rootNode); | ||
|
||
return $treeBuilder; | ||
} | ||
|
||
private function addIndexableFieldTypeSection(ArrayNodeDefinition $nodeDefinition) | ||
{ | ||
$nodeDefinition | ||
->children() | ||
->arrayNode('indexable_field_type') | ||
->info('Configure override for field type Indexable interface implementation') | ||
->addDefaultsIfNotSet() | ||
->children() | ||
->arrayNode('ezxmltext') | ||
->addDefaultsIfNotSet() | ||
->canBeDisabled() | ||
->children() | ||
->integerNode('short_text_limit') | ||
->info("Maximum number of characters for the indexed short text ('value' string type field)") | ||
->defaultValue(256) | ||
->end() | ||
->end() | ||
->end() | ||
->arrayNode('ezrichtext') | ||
->addDefaultsIfNotSet() | ||
->canBeDisabled() | ||
->children() | ||
->integerNode('short_text_limit') | ||
->info("Maximum number of characters for the indexed short text ('value' string type field)") | ||
->defaultValue(256) | ||
->end() | ||
->end() | ||
->end() | ||
->end() | ||
->end() | ||
->end(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
lib/Container/Compiler/FieldType/RichTextIndexablePass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Netgen\EzPlatformSearchExtra\Container\Compiler\FieldType; | ||
|
||
use EzSystems\EzPlatformRichText\eZ\FieldType\RichText\SearchField; | ||
use Netgen\EzPlatformSearchExtra\Core\FieldType\XmlText\Indexable as IndexableXmlText; | ||
use RuntimeException; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
class RichTextIndexablePass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$enabled = $container->getParameter('netgen_ez_platform_search_extra.indexable_field_type.ezrichtext.enabled'); | ||
$shortTextLimit = $container->getParameter('netgen_ez_platform_search_extra.indexable_field_type.ezrichtext.short_text_limit'); | ||
|
||
if ($enabled === true) { | ||
$this->redefineIndexableImplementation($container, $shortTextLimit); | ||
} | ||
} | ||
|
||
private function redefineIndexableImplementation(ContainerBuilder $container, $shortTextLimit) | ||
{ | ||
try { | ||
$originalServiceId = $this->getOriginalServiceId($container); | ||
} catch (RuntimeException $e) { | ||
return; | ||
} | ||
|
||
$definition = $container->findDefinition($originalServiceId); | ||
|
||
$definition->setClass(IndexableXmlText::class); | ||
$definition->setArgument(0, $shortTextLimit); | ||
$definition->addTag('ezpublish.fieldType.indexable', ['alias' => 'ezrichtext']); | ||
|
||
$container->setDefinition($originalServiceId, $definition); | ||
} | ||
|
||
private function getOriginalServiceId(ContainerBuilder $container) | ||
{ | ||
$newServiceId = SearchField::class; | ||
$oldServiceId = 'ezpublish.fieldType.indexable.ezrichtext'; | ||
|
||
if ($container->has($newServiceId)) { | ||
return $newServiceId; | ||
} | ||
|
||
if ($container->has($oldServiceId)) { | ||
return $oldServiceId; | ||
} | ||
|
||
throw new RuntimeException('Could not find Indexable service'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Netgen\EzPlatformSearchExtra\Container\Compiler\FieldType; | ||
|
||
use Netgen\EzPlatformSearchExtra\Core\FieldType\XmlText\Indexable as IndexableXmlText; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
class XmlTextIndexablePass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$enabled = $container->getParameter('netgen_ez_platform_search_extra.indexable_field_type.ezxmltext.enabled'); | ||
$shortTextLimit = $container->getParameter('netgen_ez_platform_search_extra.indexable_field_type.ezxmltext.short_text_limit'); | ||
|
||
if ($enabled === true) { | ||
$this->redefineIndexableImplementation($container, $shortTextLimit); | ||
} | ||
} | ||
|
||
private function redefineIndexableImplementation(ContainerBuilder $container, $shortTextLimit) | ||
{ | ||
$originalServiceId = 'ezpublish.fieldType.indexable.ezxmltext'; | ||
|
||
if (!$container->has($originalServiceId)) { | ||
return; | ||
} | ||
|
||
$definition = $container->findDefinition($originalServiceId); | ||
|
||
$definition->setClass(IndexableXmlText::class); | ||
$definition->setArgument(0, $shortTextLimit); | ||
$definition->addTag('ezpublish.fieldType.indexable', ['alias' => 'ezxmltext']); | ||
|
||
$container->setDefinition($originalServiceId, $definition); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
namespace Netgen\EzPlatformSearchExtra\Core\FieldType\RichText; | ||
|
||
use eZ\Publish\SPI\Persistence\Content\Field; | ||
use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition; | ||
use eZ\Publish\SPI\FieldType\Indexable as IndexableInterface; | ||
use eZ\Publish\SPI\Search; | ||
use DOMDocument; | ||
use DOMNode; | ||
|
||
/** | ||
* Indexable definition for RichText field type. | ||
*/ | ||
final class Indexable implements IndexableInterface | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
private $shortTextMaxLength; | ||
|
||
public function __construct($shortTextMaxLength = 256) | ||
{ | ||
$this->shortTextMaxLength = $shortTextMaxLength; | ||
} | ||
|
||
public function getIndexData(Field $field, FieldDefinition $fieldDefinition) | ||
{ | ||
$document = new DOMDocument(); | ||
$document->loadXML($field->value->data); | ||
$text = $this->extractText($document->documentElement); | ||
$shortText = $this->shortenText($text); | ||
|
||
return [ | ||
new Search\Field( | ||
'fulltext', | ||
$text, | ||
new Search\FieldType\FullTextField() | ||
), | ||
new Search\Field( | ||
'value', | ||
$shortText, | ||
new Search\FieldType\StringField() | ||
), | ||
]; | ||
} | ||
|
||
/** | ||
* Extracts text content of the given $node. | ||
* | ||
* @param \DOMNode $node | ||
* | ||
* @return string | ||
*/ | ||
private function extractText(DOMNode $node) | ||
{ | ||
$text = ''; | ||
|
||
if ($node->childNodes) { | ||
foreach ($node->childNodes as $child) { | ||
$text .= $this->extractText($child); | ||
} | ||
} else { | ||
$text .= $node->nodeValue . ' '; | ||
} | ||
|
||
return $text; | ||
} | ||
|
||
/** | ||
* Shorten text from the given $text. | ||
* | ||
* @param string $text | ||
* | ||
* @return string | ||
*/ | ||
private function shortenText($text) | ||
{ | ||
return mb_substr(trim(strtok($text, "\r\n")), 0, $this->shortTextMaxLength); | ||
} | ||
|
||
public function getIndexDefinition() | ||
{ | ||
return [ | ||
'value' => new Search\FieldType\StringField(), | ||
]; | ||
} | ||
|
||
public function getDefaultMatchField() | ||
{ | ||
return 'value'; | ||
} | ||
|
||
public function getDefaultSortField() | ||
{ | ||
return $this->getDefaultMatchField(); | ||
} | ||
} |
Oops, something went wrong.