DefaultIndexStrategy.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 namespace wcmf\lib\search\impl;
12 
14 use ZendSearch\Lucene\Document;
15 use ZendSearch\Lucene\Document\Field;
16 
17 /**
18  * DefaultIndexStrategy implements indexing of PersistentObject values
19  * and might be customized by overriding the isIncluded and/or enhanceDocument
20  * methods.
21  *
22  * Which values will be added to the index is controlled by the tag
23  * SEARCHABLE (see AttributeDescription) in the following way:
24  *
25  * - If no value is tagged as SEARCHABLE, all values will be included in the index
26  * - If at least one value is tagged as SEARCHABLE, only values with this tag will
27  * be included in the index
28  *
29  * This allows to exclude certain values from the index by omitting the tag while
30  * setting it on the other values.
31  */
33 
34  /**
35  * @see IndexStrategy::getDocument()
36  */
37  public function getDocument(PersistentObject $obj, $language) {
38  $doc = null;
39  if ($this->isIncluded($obj, $language)) {
40  $doc = new Document();
41 
42  // create document
43  $doc->addField(Field::keyword('oid', $obj->getOID()->__toString(), 'UTF-8'));
44  $typeField = Field::keyword('type', $obj->getType(), 'UTF-8');
45  $typeField->isStored = false;
46  $doc->addField($typeField);
47  if ($language != null) {
48  $languageField = Field::keyword('lang', $language, 'UTF-8');
49  $doc->addField($languageField);
50  }
51 
52  // get values to add
53  $mapper = $obj->getMapper();
54  $allAttributes = $mapper->getAttributes();
55  $includedAttributes = array_filter($allAttributes, function($attribute) {
56  return $attribute->hasTag('SEARCHABLE');
57  });
58  if (sizeof($includedAttributes) == 0) {
59  $includedAttributes = $allAttributes;
60  }
61 
62  // add values
63  foreach ($includedAttributes as $attribute) {
64  $valueName = $attribute->getName();
65  $inputType = $obj->getValueProperty($valueName, 'input_type');
66  $value = $obj->getValue($valueName);
67  if (!is_object($value) && !is_array($value)) {
68  $value = $this->encodeValue($value, $inputType);
69  if (preg_match('/^text|^f?ckeditor/', $inputType)) {
70  $value = strip_tags($value);
71  $doc->addField(Field::unStored($valueName, $value, 'UTF-8'));
72  }
73  else {
74  $field = Field::keyword($valueName, $value, 'UTF-8');
75  $field->isStored = false;
76  $doc->addField($field);
77  }
78  }
79  }
80  $this->enhanceDocument($doc, $obj, $language);
81  }
82  return $doc;
83  }
84 
85  /**
86  * @see IndexStrategy::encodeValue()
87  */
88  public function encodeValue($value, $inputType) {
89  if (preg_match('/^f?ckeditor/', $inputType)) {
90  $value = html_entity_decode($value, ENT_QUOTES, 'UTF-8');
91  }
92  return trim($value);
93  }
94 
95  /**
96  * Determine whether the object is included in the index or not
97  * @param $obj The object to index
98  * @param $language The language
99  * @return Boolean
100  */
101  protected function isIncluded(PersistentObject $obj, $language) {
102  return true;
103  }
104 
105  /**
106  * Customize the lucene document according the the application requirements
107  * @param $doc The lucene document
108  * @param $obj The object to index
109  * @param $language The language
110  */
111  protected function enhanceDocument(Document $doc, PersistentObject $obj, $language) {}
112 }
113 ?>
getType()
Get the type of the object.
DefaultIndexStrategy implements indexing of PersistentObject values and might be customized by overri...
enhanceDocument(Document $doc, PersistentObject $obj, $language)
Customize the lucene document according the the application requirements.
getOID()
Get the object id of the PersistentObject.
getMapper()
Get the PersistenceMapper of the object.
getValue($name)
Get the value of an attribute.
getValueProperty($name, $property)
Get the value of one property of an attribute.
isIncluded(PersistentObject $obj, $language)
Determine whether the object is included in the index or not.
getDocument(PersistentObject $obj, $language)
IndexStrategy defines the interface for indexing implementations.
PersistentObject defines the interface of all persistent objects.