Skip to content

Commit

Permalink
Merge pull request #1229 from ezsystems/impl-EZP-24269-indexable-text…
Browse files Browse the repository at this point in the history
…block

Implement EZP-24269: Indexable TextBlock field type
  • Loading branch information
pspanja committed Apr 30, 2015
2 parents b72e85b + d7fe79b commit 34c772a
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ protected function createContentTypeDraft()
$bodyFieldCreate->fieldSettings = array(
'textRows' => 80
);
$bodyFieldCreate->isSearchable = true;
$bodyFieldCreate->isSearchable = false;

$typeCreate->addFieldDefinition( $bodyFieldCreate );

Expand Down
39 changes: 39 additions & 0 deletions eZ/Publish/API/Repository/Tests/FieldType/SearchableTextBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* This file is part of the eZ Publish Kernel package
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
*/

namespace eZ\Publish\API\Repository\Tests\FieldType;

use eZ\Publish\Core\FieldType\TextBlock\Type;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;

/**
* TextBlock field type is not searchable in Legacy search engine, but will
* be searchable with Solr and Elasticsearch search engines.
*
* This is implementation simply extends the original implementation in order to
* define the field type as searchable, so that it can be tested.
*/
class SearchableTextBlock extends Type
{
public function isSearchable()
{
return true;
}

static protected function checkValueType( $value )
{
$fieldTypeFQN = "eZ\\Publish\\Core\\FieldType\\TextBlock\\Value";
$valueFQN = substr_replace( $fieldTypeFQN, "Value", strrpos( $fieldTypeFQN, "\\" ) + 1 );

if ( !$value instanceof $valueFQN )
{
throw new InvalidArgumentType( "\$value", $valueFQN, $value );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @group integration
* @group field-type
*/
class TextBlockIntegrationTest extends BaseIntegrationTest
class TextBlockIntegrationTest extends SearchBaseIntegrationTest
{
/**
* Get name of tested field type
Expand Down Expand Up @@ -320,4 +320,14 @@ public function providerForTestIsNotEmptyValue()
array( new TextBlockValue( "0" ) ),
);
}

protected function getValidSearchValueOne()
{
return 'caution is the " path to \\mediocrity';
}

protected function getValidSearchValueTwo()
{
return "truth suffers from ' \\too much analysis";
}
}
2 changes: 1 addition & 1 deletion eZ/Publish/Core/FieldType/Integer/SearchField.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use eZ\Publish\SPI\Search;

/**
* Indexable definition for TextLine field type
* Indexable definition for Integer field type
*/
class SearchField implements Indexable
{
Expand Down
65 changes: 65 additions & 0 deletions eZ/Publish/Core/FieldType/TextBlock/SearchField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* This file is part of the eZ Publish Kernel package
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
*/

namespace eZ\Publish\Core\FieldType\TextBlock;

use eZ\Publish\SPI\Persistence\Content\Field;
use eZ\Publish\SPI\FieldType\Indexable;
use eZ\Publish\SPI\Search;

/**
* Indexable definition for TextBlock field type
*/
class SearchField implements Indexable
{
/**
* Get index data for field for search backend
*
* @param Field $field
*
* @return \eZ\Publish\SPI\Search\Field[]
*/
public function getIndexData( Field $field )
{
return array(
new Search\Field(
'value',
$field->value->data,
new Search\FieldType\MultipleStringField()
),
);
}

/**
* Get index field types for search backend
*
* @return \eZ\Publish\SPI\Search\FieldType[]
*/
public function getIndexDefinition()
{
return array(
'value' => new Search\FieldType\MultipleStringField(),
);
}

/**
* Get name of the default field to be used for query and sort.
*
* As field types can index multiple fields (see MapLocation field type's
* implementation of this interface), this method is used to define default
* field for query and sort. Default field is typically used by Field
* criterion and sort clause.
*
* @return string
*/
public function getDefaultField()
{
return "value";
}
}
2 changes: 1 addition & 1 deletion eZ/Publish/Core/FieldType/TextBlock/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function toHash( SPIValue $value )
*/
public function isSearchable()
{
return true;
return false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ protected function createContentType( $publish = true, $creatorId = null )
$bodyFieldCreate->isTranslatable = true;
$bodyFieldCreate->isRequired = false;
$bodyFieldCreate->isInfoCollector = false;
$bodyFieldCreate->isSearchable = true;
$bodyFieldCreate->isSearchable = false;
$bodyFieldCreate->defaultValue = "";
//$bodyFieldCreate->validatorConfiguration
$bodyFieldCreate->fieldSettings = array(
Expand Down Expand Up @@ -1168,7 +1168,7 @@ public function testCreateContentType()
$bodyFieldCreate->isTranslatable = true;
$bodyFieldCreate->isRequired = false;
$bodyFieldCreate->isInfoCollector = false;
$bodyFieldCreate->isSearchable = true;
$bodyFieldCreate->isSearchable = false;
$bodyFieldCreate->defaultValue = "";
//$bodyFieldCreate->validatorConfiguration
$bodyFieldCreate->fieldSettings = array(
Expand Down Expand Up @@ -3356,7 +3356,7 @@ public function testUpdateFieldDefinition()
$fieldDefinitionUpdateStruct->fieldSettings = array(
"textRows" => $fieldDefinition->fieldSettings["textRows"] + 1
);
$fieldDefinitionUpdateStruct->isSearchable = !$fieldDefinition->isSearchable;
$fieldDefinitionUpdateStruct->isSearchable = $fieldDefinition->isSearchable;

$contentTypeService->updateFieldDefinition(
$contentTypeDraft,
Expand Down
27 changes: 27 additions & 0 deletions eZ/Publish/Core/Search/Solr/Content/CriterionVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ protected function getRange( $operator, $start, $end )
$endValue = '*';
$endBrace = ']';

$start = $this->prepareValue( $start );
$end = $this->prepareValue( $end );

switch ( $operator )
{
case Operator::GT:
Expand Down Expand Up @@ -92,5 +95,29 @@ protected function getRange( $operator, $start, $end )

return "$startBrace$startValue TO $endValue$endBrace";
}

/**
* Converts given $value to the appropriate Solr representation.
*
* The value will be converted to string representation and escaped if needed.
*
* @param mixed $value
*
* @return string
*/
protected function prepareValue( $value )
{
switch ( gettype( $value ) )
{
case "boolean":
return ( $value ? "true" : "false" );

case "string":
return '"' . preg_replace( '/("|\\\)/', '\\\$1', $value ) . '"';

default:
return (string)$value;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ public function visit( Criterion $criterion, CriterionVisitor $subVisitor = null
$queries = array();
foreach ( $criterion->value as $value )
{
$preparedValue = $this->prepareValue( $value );

foreach ( $fieldNames as $name )
{
$queries[] = $name . ':"' . $value . '"';
$queries[] = $name . ':' . $preparedValue;
}
}

Expand Down
4 changes: 2 additions & 2 deletions eZ/Publish/Core/settings/indexable_fieldtypes.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
parameters:
ezpublish.fieldType.indexable.ezstring.class: eZ\Publish\Core\FieldType\TextLine\SearchField
ezpublish.fieldType.indexable.eztext.class: eZ\Publish\Core\FieldType\TextBlock\SearchField
ezpublish.fieldType.indexable.ezprice.class: eZ\Publish\Core\FieldType\Price\SearchField
ezpublish.fieldType.indexable.ezgmaplocation.class: eZ\Publish\Core\FieldType\MapLocation\SearchField
ezpublish.fieldType.indexable.ezcountry.class: eZ\Publish\Core\FieldType\Country\SearchField
Expand Down Expand Up @@ -38,9 +39,8 @@ services:
tags:
- {name: ezpublish.fieldType.indexable, alias: ezfloat}

# TODO: define proper type
ezpublish.fieldType.indexable.eztext:
class: %ezpublish.fieldType.indexable.ezstring.class%
class: %ezpublish.fieldType.indexable.eztext.class%
tags:
- {name: ezpublish.fieldType.indexable, alias: eztext}

Expand Down

0 comments on commit 34c772a

Please sign in to comment.