summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Elastic/QueryEngine/Excerpts.php
blob: 70c7ba87f6949922f5fd99427af6aa6defe7bde9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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>' ],
				[ '&lt;em&gt;', '&lt;/em&gt;' ],
				$text
			);

			// Remove tags to avoid any output disruption
			$text = strip_tags( $text );

			$text = str_replace(
				[ '&lt;em&gt;', '&lt;/em&gt;' ],
				[ '<em>', '</em>' ],
				$text
			);
		}

		if ( $this->noHighlight ) {
			$text = str_replace( [ '<em>', '</em>', "\n" ], [ '', '', ' ' ], $text );
		}

		return $text;
	}

}