| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.search.solr.internal.metadata; |
| 21 |
|
|
| 22 |
|
import java.util.List; |
| 23 |
|
import java.util.Locale; |
| 24 |
|
import java.util.Map; |
| 25 |
|
|
| 26 |
|
import javax.inject.Inject; |
| 27 |
|
import javax.inject.Named; |
| 28 |
|
import javax.inject.Singleton; |
| 29 |
|
|
| 30 |
|
import org.apache.commons.lang3.StringUtils; |
| 31 |
|
import org.apache.solr.common.SolrInputDocument; |
| 32 |
|
import org.xwiki.component.annotation.Component; |
| 33 |
|
import org.xwiki.model.EntityType; |
| 34 |
|
import org.xwiki.model.reference.DocumentReference; |
| 35 |
|
import org.xwiki.model.reference.EntityReference; |
| 36 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
| 37 |
|
import org.xwiki.rendering.renderer.BlockRenderer; |
| 38 |
|
import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter; |
| 39 |
|
import org.xwiki.rendering.renderer.printer.WikiPrinter; |
| 40 |
|
import org.xwiki.rendering.syntax.Syntax; |
| 41 |
|
import org.xwiki.search.solr.internal.api.FieldUtils; |
| 42 |
|
import org.xwiki.search.solr.internal.api.SolrFieldNameEncoder; |
| 43 |
|
|
| 44 |
|
import com.xpn.xwiki.XWikiContext; |
| 45 |
|
import com.xpn.xwiki.XWikiException; |
| 46 |
|
import com.xpn.xwiki.doc.XWikiAttachment; |
| 47 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
| 48 |
|
import com.xpn.xwiki.objects.BaseObject; |
| 49 |
|
import com.xpn.xwiki.objects.BaseProperty; |
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
@version |
| 55 |
|
@since |
| 56 |
|
|
| 57 |
|
@Component |
| 58 |
|
@Named("document") |
| 59 |
|
@Singleton |
| |
|
| 96.5% |
Uncovered Elements: 3 (86) |
Complexity: 14 |
Complexity Density: 0.2 |
|
| 60 |
|
public class DocumentSolrMetadataExtractor extends AbstractSolrMetadataExtractor |
| 61 |
|
{ |
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
@Inject |
| 66 |
|
@Named("plain/1.0") |
| 67 |
|
private BlockRenderer renderer; |
| 68 |
|
|
| 69 |
|
@Inject |
| 70 |
|
private EntityReferenceSerializer<String> entityReferenceSerializer; |
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
@Inject |
| 76 |
|
@Named("solr") |
| 77 |
|
private EntityReferenceSerializer<String> fieldNameSerializer; |
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
@Inject |
| 83 |
|
private SolrFieldNameEncoder fieldNameEncoder; |
| 84 |
|
|
| |
|
| 92% |
Uncovered Elements: 2 (25) |
Complexity: 2 |
Complexity Density: 0.09 |
|
| 85 |
876 |
@Override... |
| 86 |
|
public boolean setFieldsInternal(LengthSolrInputDocument solrDocument, EntityReference entityReference) |
| 87 |
|
throws Exception |
| 88 |
|
{ |
| 89 |
876 |
DocumentReference documentReference = new DocumentReference(entityReference); |
| 90 |
|
|
| 91 |
876 |
XWikiContext xcontext = this.xcontextProvider.get(); |
| 92 |
|
|
| 93 |
876 |
XWikiDocument translatedDocument = getTranslatedDocument(documentReference); |
| 94 |
876 |
if (translatedDocument == null) { |
| 95 |
0 |
return false; |
| 96 |
|
} |
| 97 |
|
|
| 98 |
876 |
Locale locale = getLocale(documentReference); |
| 99 |
|
|
| 100 |
876 |
solrDocument.setField(FieldUtils.FULLNAME, localSerializer.serialize(documentReference)); |
| 101 |
|
|
| 102 |
|
|
| 103 |
876 |
String plainTitle = translatedDocument.getRenderedTitle(Syntax.PLAIN_1_0, xcontext); |
| 104 |
876 |
solrDocument.setField(FieldUtils.getFieldName(FieldUtils.TITLE, locale), plainTitle); |
| 105 |
|
|
| 106 |
|
|
| 107 |
876 |
solrDocument.setField(FieldUtils.getFieldName(FieldUtils.DOCUMENT_RAW_CONTENT, locale), |
| 108 |
|
translatedDocument.getContent()); |
| 109 |
|
|
| 110 |
|
|
| 111 |
876 |
WikiPrinter plainContentPrinter = new DefaultWikiPrinter(); |
| 112 |
876 |
this.renderer.render(translatedDocument.getXDOM(), plainContentPrinter); |
| 113 |
876 |
solrDocument.setField(FieldUtils.getFieldName(FieldUtils.DOCUMENT_RENDERED_CONTENT, locale), |
| 114 |
|
plainContentPrinter.toString()); |
| 115 |
|
|
| 116 |
876 |
solrDocument.setField(FieldUtils.VERSION, translatedDocument.getVersion()); |
| 117 |
876 |
solrDocument.setField(FieldUtils.COMMENT, translatedDocument.getComment()); |
| 118 |
|
|
| 119 |
876 |
solrDocument.setField(FieldUtils.DOCUMENT_LOCALE, translatedDocument.getLocale().toString()); |
| 120 |
|
|
| 121 |
|
|
| 122 |
876 |
addLocales(translatedDocument, translatedDocument.getLocale(), solrDocument); |
| 123 |
|
|
| 124 |
|
|
| 125 |
876 |
setAuthors(solrDocument, translatedDocument, entityReference); |
| 126 |
|
|
| 127 |
|
|
| 128 |
876 |
solrDocument.setField(FieldUtils.CREATIONDATE, translatedDocument.getCreationDate()); |
| 129 |
876 |
solrDocument.setField(FieldUtils.DATE, translatedDocument.getContentUpdateDate()); |
| 130 |
|
|
| 131 |
|
|
| 132 |
876 |
solrDocument.setField(FieldUtils.HIDDEN, translatedDocument.isHidden()); |
| 133 |
|
|
| 134 |
|
|
| 135 |
876 |
setExtras(documentReference, solrDocument, locale); |
| 136 |
|
|
| 137 |
876 |
return true; |
| 138 |
|
} |
| 139 |
|
|
| 140 |
|
|
| 141 |
|
@param |
| 142 |
|
@param |
| 143 |
|
@param |
| 144 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
| 145 |
876 |
private void setAuthors(SolrInputDocument solrDocument, XWikiDocument translatedDocument,... |
| 146 |
|
EntityReference entityReference) |
| 147 |
|
{ |
| 148 |
876 |
XWikiContext xcontext = this.xcontextProvider.get(); |
| 149 |
|
|
| 150 |
876 |
String authorString = entityReferenceSerializer.serialize(translatedDocument.getAuthorReference()); |
| 151 |
876 |
solrDocument.setField(FieldUtils.AUTHOR, authorString); |
| 152 |
876 |
String authorDisplayString = |
| 153 |
|
xcontext.getWiki().getPlainUserName(translatedDocument.getAuthorReference(), xcontext); |
| 154 |
876 |
solrDocument.setField(FieldUtils.AUTHOR_DISPLAY, authorDisplayString); |
| 155 |
|
|
| 156 |
876 |
String creatorString = entityReferenceSerializer.serialize(translatedDocument.getCreatorReference()); |
| 157 |
876 |
solrDocument.setField(FieldUtils.CREATOR, creatorString); |
| 158 |
876 |
String creatorDisplayString = |
| 159 |
|
xcontext.getWiki().getPlainUserName(translatedDocument.getCreatorReference(), xcontext); |
| 160 |
876 |
solrDocument.setField(FieldUtils.CREATOR_DISPLAY, creatorDisplayString); |
| 161 |
|
} |
| 162 |
|
|
| 163 |
|
|
| 164 |
|
@param |
| 165 |
|
@param |
| 166 |
|
@param |
| 167 |
|
@throws |
| 168 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 169 |
876 |
protected void setExtras(DocumentReference documentReference, SolrInputDocument solrDocument, Locale locale)... |
| 170 |
|
throws XWikiException |
| 171 |
|
{ |
| 172 |
|
|
| 173 |
|
|
| 174 |
|
|
| 175 |
|
|
| 176 |
|
|
| 177 |
|
|
| 178 |
|
|
| 179 |
|
|
| 180 |
|
|
| 181 |
876 |
XWikiDocument originalDocument = getDocument(documentReference); |
| 182 |
|
|
| 183 |
|
|
| 184 |
|
|
| 185 |
|
|
| 186 |
876 |
setObjects(solrDocument, locale, originalDocument); |
| 187 |
876 |
setAttachments(solrDocument, locale, originalDocument); |
| 188 |
|
} |
| 189 |
|
|
| 190 |
|
|
| 191 |
|
@param |
| 192 |
|
@param |
| 193 |
|
@param |
| 194 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
| 195 |
876 |
protected void setObjects(SolrInputDocument solrDocument, Locale locale, XWikiDocument originalDocument)... |
| 196 |
|
{ |
| 197 |
876 |
for (Map.Entry<DocumentReference, List<BaseObject>> objects : originalDocument.getXObjects().entrySet()) { |
| 198 |
675 |
boolean hasObjectsOfThisType = false; |
| 199 |
675 |
for (BaseObject object : objects.getValue()) { |
| 200 |
|
|
| 201 |
878 |
hasObjectsOfThisType |= object != null; |
| 202 |
878 |
setObjectContent(solrDocument, object, locale); |
| 203 |
|
} |
| 204 |
675 |
if (hasObjectsOfThisType) { |
| 205 |
674 |
solrDocument.addField(FieldUtils.CLASS, localSerializer.serialize(objects.getKey())); |
| 206 |
|
} |
| 207 |
|
} |
| 208 |
|
} |
| 209 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 5 |
Complexity Density: 0.36 |
|
| 210 |
4357 |
@Override... |
| 211 |
|
protected void setPropertyValue(SolrInputDocument solrDocument, BaseProperty<EntityReference> property, |
| 212 |
|
TypedValue typedValue, Locale locale) |
| 213 |
|
{ |
| 214 |
4357 |
Object value = typedValue.getValue(); |
| 215 |
4357 |
String type = typedValue.getType(); |
| 216 |
|
|
| 217 |
|
|
| 218 |
4357 |
EntityReference classReference = property.getObject().getRelativeXClassReference(); |
| 219 |
4357 |
EntityReference propertyReference = |
| 220 |
|
new EntityReference(property.getName(), EntityType.CLASS_PROPERTY, classReference); |
| 221 |
4357 |
String serializedPropertyReference = fieldNameEncoder.encode(fieldNameSerializer.serialize(propertyReference)); |
| 222 |
4357 |
String prefix = "property." + serializedPropertyReference; |
| 223 |
|
|
| 224 |
|
|
| 225 |
4357 |
solrDocument.addField(FieldUtils.getFieldName(prefix, type, locale), value); |
| 226 |
|
|
| 227 |
|
|
| 228 |
|
|
| 229 |
|
|
| 230 |
4357 |
if ((type != TypedValue.TEXT && type != TypedValue.STRING) |
| 231 |
|
|| String.valueOf(value).length() <= SHORT_TEXT_LIMIT) { |
| 232 |
|
|
| 233 |
|
|
| 234 |
4188 |
String sortType = "sort" + StringUtils.capitalize(type == TypedValue.TEXT ? TypedValue.STRING : type); |
| 235 |
|
|
| 236 |
|
|
| 237 |
|
|
| 238 |
4188 |
solrDocument.setField(FieldUtils.getFieldName(prefix, sortType, locale), value); |
| 239 |
|
} |
| 240 |
|
|
| 241 |
|
|
| 242 |
4357 |
String serializedClassReference = fieldNameEncoder.encode(fieldNameSerializer.serialize(classReference)); |
| 243 |
4357 |
String objectOfTypeFieldName = "object." + serializedClassReference; |
| 244 |
|
|
| 245 |
|
|
| 246 |
|
|
| 247 |
4357 |
addFieldValueOnce(solrDocument, FieldUtils.getFieldName(objectOfTypeFieldName, locale), value); |
| 248 |
|
|
| 249 |
|
|
| 250 |
4357 |
super.setPropertyValue(solrDocument, property, typedValue, locale); |
| 251 |
|
} |
| 252 |
|
|
| 253 |
|
|
| 254 |
|
@param |
| 255 |
|
@param |
| 256 |
|
@param |
| 257 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 258 |
876 |
private void setAttachments(SolrInputDocument solrDocument, Locale locale, XWikiDocument originalDocument)... |
| 259 |
|
{ |
| 260 |
876 |
for (XWikiAttachment attachment : originalDocument.getAttachmentList()) { |
| 261 |
195 |
setAttachment(solrDocument, locale, attachment); |
| 262 |
|
} |
| 263 |
|
} |
| 264 |
|
|
| 265 |
|
|
| 266 |
|
|
| 267 |
|
|
| 268 |
|
@param |
| 269 |
|
@param |
| 270 |
|
@param |
| 271 |
|
|
| |
|
| 92.3% |
Uncovered Elements: 1 (13) |
Complexity: 2 |
Complexity Density: 0.15 |
|
| 272 |
195 |
private void setAttachment(SolrInputDocument solrDocument, Locale locale, XWikiAttachment attachment)... |
| 273 |
|
{ |
| 274 |
195 |
XWikiContext xcontext = xcontextProvider.get(); |
| 275 |
|
|
| 276 |
195 |
solrDocument.addField(FieldUtils.FILENAME, attachment.getFilename()); |
| 277 |
195 |
solrDocument.addField(FieldUtils.MIME_TYPE, attachment.getMimeType(xcontext)); |
| 278 |
195 |
solrDocument.addField(FieldUtils.ATTACHMENT_DATE, attachment.getDate()); |
| 279 |
195 |
solrDocument.addField(FieldUtils.ATTACHMENT_SIZE, attachment.getFilesize()); |
| 280 |
|
|
| 281 |
195 |
String attachmentTextContent = getContentAsText(attachment); |
| 282 |
195 |
solrDocument.addField(FieldUtils.getFieldName(FieldUtils.ATTACHMENT_CONTENT, locale), attachmentTextContent); |
| 283 |
|
|
| 284 |
|
|
| 285 |
195 |
String authorStringReference = entityReferenceSerializer.serialize(attachment.getAuthorReference()); |
| 286 |
195 |
solrDocument.addField(FieldUtils.ATTACHMENT_AUTHOR, authorStringReference); |
| 287 |
195 |
try { |
| 288 |
|
|
| 289 |
195 |
String authorDisplayName = xcontext.getWiki().getPlainUserName(attachment.getAuthorReference(), xcontext); |
| 290 |
195 |
solrDocument.addField(FieldUtils.ATTACHMENT_AUTHOR_DISPLAY, authorDisplayName); |
| 291 |
|
} catch (Exception e) { |
| 292 |
0 |
this.logger.error("Failed to get author display name for attachment [{}]", attachment.getReference(), e); |
| 293 |
|
} |
| 294 |
|
} |
| 295 |
|
} |