51 public function highlightText( $text, $terms, $contextlines, $contextchars ) {
59 $spat =
"/(\\{\\{)|(\\[\\[[^\\]:]+:)|(\n\\{\\|)";
62 1 =>
'/(\{\{)|(\}\})/',
63 2 =>
'/(\[\[)|(\]\])/',
64 3 =>
"/(\n\\{\\|)|(\n\\|\\})/" ];
68 if ( class_exists(
'Cite' ) ) {
70 $endPatterns[4] =
'/(<ref>)|(<\/ref>)/';
76 $textLen = strlen( $text );
78 while ( $start < $textLen ) {
80 if ( preg_match( $spat, $text,
$matches, PREG_OFFSET_CAPTURE, $start ) ) {
82 foreach (
$matches as $key => $val ) {
83 if ( $key > 0 && $val[1] != -1 ) {
86 $ns = substr( $val[0], 2, -1 );
92 $epat = $endPatterns[$key];
93 $this->
splitAndAdd( $textExt, $count, substr( $text, $start, $val[1] - $start ) );
101 $offset = $start + 1;
103 while ( preg_match( $epat, $text, $endMatches, PREG_OFFSET_CAPTURE, $offset ) ) {
104 if ( array_key_exists( 2, $endMatches ) ) {
107 $len = strlen( $endMatches[2][0] );
108 $off = $endMatches[2][1];
110 substr( $text, $start, $off + $len - $start ) );
111 $start = $off + $len;
122 $offset = $endMatches[0][1] + strlen( $endMatches[0][0] );
133 $this->
splitAndAdd( $textExt, $count, substr( $text, $start ) );
137 $all = $textExt + $otherExt;
140 foreach ( $terms as $index =>
$term ) {
142 if ( preg_match(
'/[\x80-\xff]/',
$term ) ) {
143 $terms[$index] = preg_replace_callback(
145 [ $this,
'caseCallback' ],
149 $terms[$index] =
$term;
152 $anyterm = implode(
'|', $terms );
153 $phrase = implode(
"$wgSearchHighlightBoundaries+", $terms );
158 $scale = strlen( $anyterm ) / mb_strlen( $anyterm );
159 $contextchars = intval( $contextchars * $scale );
161 $patPre =
"(^|$wgSearchHighlightBoundaries)";
162 $patPost =
"($wgSearchHighlightBoundaries|$)";
164 $pat1 =
"/(" . $phrase .
")/ui";
165 $pat2 =
"/$patPre(" . $anyterm .
")$patPost/ui";
167 $left = $contextlines;
175 foreach ( $textExt as $index =>
$line ) {
177 $firstText = $this->
extract(
$line, 0, $contextchars * $contextlines );
185 foreach ( $terms as
$term ) {
186 if ( !preg_match(
"/$patPre" .
$term .
"$patPost/ui", $firstText ) ) {
192 $snippets[$first] = $firstText;
193 $offsets[$first] = 0;
198 $this->
process( $pat1, $textExt, $left, $contextchars, $snippets, $offsets );
200 $this->
process( $pat1, $otherExt, $left, $contextchars, $snippets, $offsets );
202 $this->
process( $pat2, $textExt, $left, $contextchars, $snippets, $offsets );
204 $this->
process( $pat2, $otherExt, $left, $contextchars, $snippets, $offsets );
211 if ( count( $snippets ) == 0 ) {
213 if ( array_key_exists( $first, $all ) ) {
214 $targetchars = $contextchars * $contextlines;
215 $snippets[$first] =
'';
216 $offsets[$first] = 0;
220 if ( array_key_exists( $first, $snippets ) && preg_match( $pat1, $snippets[$first] )
221 && $offsets[$first] < $contextchars * 2 ) {
222 $snippets = [ $first => $snippets[$first] ];
226 $targetchars = intval( ( $contextchars * $contextlines ) / count( $snippets ) );
229 foreach ( $snippets as $index =>
$line ) {
230 $extended[$index] =
$line;
231 $len = strlen(
$line );
232 if ( $len < $targetchars - 20 ) {
234 if ( $len < strlen( $all[$index] ) ) {
235 $extended[$index] = $this->
extract(
238 $offsets[$index] + $targetchars,
241 $len = strlen( $extended[$index] );
246 while ( $len < $targetchars - 20
247 && array_key_exists( $add, $all )
248 && !array_key_exists( $add, $snippets ) ) {
250 $tt =
"\n" . $this->
extract( $all[$add], 0, $targetchars - $len, $offsets[$add] );
251 $extended[$add] = $tt;
252 $len += strlen( $tt );
259 $snippets = $extended;
262 foreach ( $snippets as $index =>
$line ) {
265 } elseif (
$last + 1 == $index
266 && $offsets[
$last] + strlen( $snippets[
$last] ) >= strlen( $all[
$last] )
268 $extract .=
" " .
$line;
270 $extract .=
'<b> ... </b>' .
$line;
276 $extract .=
'<b> ... </b>';
280 foreach ( $terms as
$term ) {
281 if ( !isset( $processed[
$term] ) ) {
282 $pat3 =
"/$patPre(" .
$term .
")$patPost/ui";
283 $extract = preg_replace( $pat3,
284 "\\1<span class='searchmatch'>\\2</span>\\3", $extract );
285 $processed[
$term] =
true;
407 function process( $pattern, $extracts, &$linesleft, &$contextchars, &
$out, &$offsets ) {
408 if ( $linesleft == 0 ) {
411 foreach ( $extracts as $index =>
$line ) {
412 if ( array_key_exists( $index,
$out ) ) {
417 if ( !preg_match( $pattern,
$line, $m, PREG_OFFSET_CAPTURE ) ) {
422 $len = strlen( $m[0][0] );
423 if ( $offset + $len < $contextchars ) {
425 } elseif ( $len > $contextchars ) {
428 $begin = $offset + intval( ( $len - $contextchars ) / 2 );
431 $end = $begin + $contextchars;
436 $offsets[$index] = $posBegin;
438 if ( $linesleft == 0 ) {