diff options
Diffstat (limited to 'www/wiki/extensions/SemanticMediaWiki/src/Elastic/QueryEngine/Excerpts.php')
-rw-r--r-- | www/wiki/extensions/SemanticMediaWiki/src/Elastic/QueryEngine/Excerpts.php | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/www/wiki/extensions/SemanticMediaWiki/src/Elastic/QueryEngine/Excerpts.php b/www/wiki/extensions/SemanticMediaWiki/src/Elastic/QueryEngine/Excerpts.php new file mode 100644 index 00000000..70c7ba87 --- /dev/null +++ b/www/wiki/extensions/SemanticMediaWiki/src/Elastic/QueryEngine/Excerpts.php @@ -0,0 +1,85 @@ +<?php + +namespace SMW\Elastic\QueryEngine; + +use SMW\DIWikiPage; + +/** + * @license GNU GPL v2+ + * @since 3.0 + * + * @author mwjames + */ +class Excerpts extends \SMW\Query\Excerpts { + + /** + * @since 3.0 + * + * @param DIWikiPage|string $hash + * + * @return string|integer|false + */ + public function getExcerpt( $hash ) { + + if ( $hash instanceof DIWikiPage ) { + $hash = $hash->getHash(); + } + + foreach ( $this->excerpts as $map ) { + if ( $map[0] === $hash ) { + return $this->format( $map[1] ); + } + } + + return false; + } + + /** + * @since 3.0 + * + * @return boolean + */ + public function hasHighlight() { + return $this->noHighlight ? false : true; + } + + private function format( $v ) { + + // https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html + // By default, highlighted text is wrapped in <em> and </em> tags + + $text = ''; + + if ( is_array( $v ) ) { + foreach ( $v as $key => $value ) { + $text .= implode( ' ', $value ) ; + } + } else { + $text = $v; + } + + if ( $this->stripTags ) { + $text = str_replace( + [ '<em>', '</em>' ], + [ '<em>', '</em>' ], + $text + ); + + // Remove tags to avoid any output disruption + $text = strip_tags( $text ); + + $text = str_replace( + [ '<em>', '</em>' ], + [ '<em>', '</em>' ], + $text + ); + } + + if ( $this->noHighlight ) { + $text = str_replace( [ '<em>', '</em>', "\n" ], [ '', '', ' ' ], $text ); + } + + return $text; + } + +} |