37 $this->mCleanWikitext = $cleanupWikitext;
50 public function highlightText( $text, $terms, $contextlines, $contextchars ) {
58 $spat =
"/(\\{\\{)|(\\[\\[[^\\]:]+:)|(\n\\{\\|)";
61 1 =>
'/(\{\{)|(\}\})/',
62 2 =>
'/(\[\[)|(\]\])/',
63 3 =>
"/(\n\\{\\|)|(\n\\|\\})/" ];
67 if ( class_exists(
'Cite' ) ) {
69 $endPatterns[4] =
'/(<ref>)|(<\/ref>)/';
75 $textLen = strlen( $text );
77 while ( $start < $textLen ) {
79 if ( preg_match( $spat, $text,
$matches, PREG_OFFSET_CAPTURE, $start ) ) {
82 if ( $key > 0 && $val[1] != -1 ) {
85 $ns = substr( $val[0], 2, -1 );
91 $epat = $endPatterns[$key];
92 $this->
splitAndAdd( $textExt, $count, substr( $text, $start, $val[1] - $start ) );
100 $offset = $start + 1;
102 while ( preg_match( $epat, $text, $endMatches, PREG_OFFSET_CAPTURE, $offset ) ) {
103 if ( array_key_exists( 2, $endMatches ) ) {
106 $len = strlen( $endMatches[2][0] );
107 $off = $endMatches[2][1];
109 substr( $text, $start, $off + $len - $start ) );
110 $start = $off + $len;
121 $offset = $endMatches[0][1] + strlen( $endMatches[0][0] );
132 $this->
splitAndAdd( $textExt, $count, substr( $text, $start ) );
136 $all = $textExt + $otherExt;
139 foreach ( $terms
as $index =>
$term ) {
141 if ( preg_match(
'/[\x80-\xff]/',
$term ) ) {
142 $terms[$index] = preg_replace_callback(
144 [ $this,
'caseCallback' ],
148 $terms[$index] =
$term;
151 $anyterm = implode(
'|', $terms );
152 $phrase = implode(
"$wgSearchHighlightBoundaries+", $terms );
157 $scale = strlen( $anyterm ) / mb_strlen( $anyterm );
158 $contextchars = intval( $contextchars * $scale );
160 $patPre =
"(^|$wgSearchHighlightBoundaries)";
161 $patPost =
"($wgSearchHighlightBoundaries|$)";
163 $pat1 =
"/(" . $phrase .
")/ui";
164 $pat2 =
"/$patPre(" . $anyterm .
")$patPost/ui";
166 $left = $contextlines;
174 foreach ( $textExt
as $index =>
$line ) {
176 $firstText = $this->
extract(
$line, 0, $contextchars * $contextlines );
185 if ( !preg_match(
"/$patPre" .
$term .
"$patPost/ui", $firstText ) ) {
191 $snippets[$first] = $firstText;
192 $offsets[$first] = 0;
197 $this->
process( $pat1, $textExt, $left, $contextchars, $snippets, $offsets );
199 $this->
process( $pat1, $otherExt, $left, $contextchars, $snippets, $offsets );
201 $this->
process( $pat2, $textExt, $left, $contextchars, $snippets, $offsets );
203 $this->
process( $pat2, $otherExt, $left, $contextchars, $snippets, $offsets );
210 if (
count( $snippets ) == 0 ) {
212 if ( array_key_exists( $first, $all ) ) {
213 $targetchars = $contextchars * $contextlines;
214 $snippets[$first] =
'';
215 $offsets[$first] = 0;
219 if ( array_key_exists( $first, $snippets ) && preg_match( $pat1, $snippets[$first] )
220 && $offsets[$first] < $contextchars * 2 ) {
221 $snippets = [ $first => $snippets[$first] ];
225 $targetchars = intval( ( $contextchars * $contextlines ) /
count( $snippets ) );
228 foreach ( $snippets
as $index =>
$line ) {
229 $extended[$index] =
$line;
230 $len = strlen(
$line );
231 if ( $len < $targetchars - 20 ) {
233 if ( $len < strlen( $all[$index] ) ) {
234 $extended[$index] = $this->
extract(
237 $offsets[$index] + $targetchars,
240 $len = strlen( $extended[$index] );
245 while ( $len < $targetchars - 20
246 && array_key_exists( $add, $all )
247 && !array_key_exists( $add, $snippets ) ) {
249 $tt =
"\n" . $this->
extract( $all[$add], 0, $targetchars - $len, $offsets[$add] );
250 $extended[$add] = $tt;
251 $len += strlen( $tt );
258 $snippets = $extended;
261 foreach ( $snippets
as $index =>
$line ) {
264 } elseif (
$last + 1 == $index
265 && $offsets[
$last] + strlen( $snippets[
$last] ) >= strlen( $all[
$last] )
267 $extract .=
" " .
$line;
269 $extract .=
'<b> ... </b>' .
$line;
275 $extract .=
'<b> ... </b>';
280 if ( !isset( $processed[
$term] ) ) {
281 $pat3 =
"/$patPre(" .
$term .
")$patPost/ui";
282 $extract = preg_replace( $pat3,
283 "\\1<span class='searchmatch'>\\2</span>\\3", $extract );
284 $processed[
$term] =
true;
299 $split = explode(
"\n", $this->mCleanWikitext ? $this->
removeWiki( $text ) : $text );
303 $extracts[$count++] = $tt;
333 function extract( $text, $start, $end, &$posStart =
null, &$posEnd =
null ) {
335 $start = $this->
position( $text, $start, 1 );
337 if ( $end >= strlen( $text ) ) {
338 $end = strlen( $text );
340 $end = $this->
position( $text, $end );
343 if ( !is_null( $posStart ) ) {
346 if ( !is_null( $posEnd ) ) {
350 if ( $end > $start ) {
351 return substr( $text, $start, $end - $start );
367 $s = max( 0, $point - $tolerance );
368 $l = min( strlen( $text ), $point + $tolerance ) -
$s;
372 '/[ ,.!?~!@#$%^&*\(\)+=\-\\\|\[\]"\'<>]/',
373 substr( $text,
$s, $l ),
377 return $m[0][1] +
$s + $offset;
380 $char = ord( $text[$point] );
381 while ( $char >= 0x80 && $char < 0xc0 ) {
384 if ( $point >= strlen( $text ) ) {
385 return strlen( $text );
387 $char = ord( $text[$point] );
406 function process( $pattern, $extracts, &$linesleft, &$contextchars, &
$out, &$offsets ) {
407 if ( $linesleft == 0 ) {
410 foreach ( $extracts
as $index =>
$line ) {
411 if ( array_key_exists( $index,
$out ) ) {
416 if ( !preg_match( $pattern,
$line, $m, PREG_OFFSET_CAPTURE ) ) {
421 $len = strlen( $m[0][0] );
422 if ( $offset + $len < $contextchars ) {
424 } elseif ( $len > $contextchars ) {
427 $begin = $offset + intval( ( $len - $contextchars ) / 2 );
430 $end = $begin + $contextchars;
435 $offsets[$index] = $posBegin;
437 if ( $linesleft == 0 ) {
450 $text = preg_replace(
"/\\{\\{([^|]+?)\\}\\}/",
"", $text );
451 $text = preg_replace(
"/\\{\\{([^|]+\\|)(.*?)\\}\\}/",
"\\2", $text );
452 $text = preg_replace(
"/\\[\\[([^|]+?)\\]\\]/",
"\\1", $text );
453 $text = preg_replace_callback(
454 "/\\[\\[([^|]+\\|)(.*?)\\]\\]/",
455 [ $this,
'linkReplace' ],
458 $text = preg_replace(
"/<\/?[^>]+>/",
"", $text );
459 $text = preg_replace(
"/'''''/",
"", $text );
460 $text = preg_replace(
"/('''|<\/?[iIuUbB]>)/",
"", $text );
461 $text = preg_replace(
"/''/",
"", $text );
466 $text = Sanitizer::escapeHtmlAllowEntities( $text );
478 $colon = strpos(
$matches[1],
':' );
479 if ( $colon ===
false ) {
483 $ns = substr(
$matches[1], 0, $colon );
507 $lines = explode(
"\n", $text );
509 $terms = implode(
'|', $terms );
510 $max = intval( $contextchars ) + 1;
511 $pat1 =
"/(.*)($terms)(.{0,$max})/i";
517 if ( 0 == $contextlines ) {
522 if ( !preg_match( $pat1,
$line, $m ) ) {
529 if (
count( $m ) < 3 ) {
532 $post =
$wgContLang->truncate( $m[3], $contextchars,
'...',
false );
537 $line = htmlspecialchars(
$pre . $found . $post );
538 $pat2 =
'/(' . $terms .
")/i";
539 $line = preg_replace( $pat2,
"<span class='searchmatch'>\\1</span>",
$line );
541 $extract .=
"${line}\n";
557 $text = ltrim( $text ) .
"\n";
558 $text = str_replace(
"\n\n",
"\n", $text );
559 preg_match(
"/^(.*\n){0,$contextlines}/", $text, $match );
562 $text = htmlspecialchars( substr( trim( $match[0] ), 0, $contextlines * $contextchars ) );
563 return str_replace(
"\n",
'<br>', $text );