38 $this->mCleanWikitext = $cleanupWikitext;
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 ) ) {
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 );
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>';
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;
300 $split = explode(
"\n", $this->mCleanWikitext ? $this->
removeWiki( $text ) : $text );
304 $extracts[$count++] = $tt;
334 function extract( $text, $start, $end, &$posStart =
null, &$posEnd =
null ) {
336 $start = $this->
position( $text, $start, 1 );
338 if ( $end >= strlen( $text ) ) {
339 $end = strlen( $text );
341 $end = $this->
position( $text, $end );
344 if ( !is_null( $posStart ) ) {
347 if ( !is_null( $posEnd ) ) {
351 if ( $end > $start ) {
352 return substr( $text, $start, $end - $start );
368 $s = max( 0, $point - $tolerance );
369 $l = min( strlen( $text ), $point + $tolerance ) -
$s;
373 '/[ ,.!?~!@#$%^&*\(\)+=\-\\\|\[\]"\'<>]/',
374 substr( $text,
$s, $l ),
378 return $m[0][1] +
$s + $offset;
381 $char = ord( $text[$point] );
382 while ( $char >= 0x80 && $char < 0xc0 ) {
385 if ( $point >= strlen( $text ) ) {
386 return strlen( $text );
388 $char = ord( $text[$point] );
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 ) {
451 $text = preg_replace(
"/\\{\\{([^|]+?)\\}\\}/",
"", $text );
452 $text = preg_replace(
"/\\{\\{([^|]+\\|)(.*?)\\}\\}/",
"\\2", $text );
453 $text = preg_replace(
"/\\[\\[([^|]+?)\\]\\]/",
"\\1", $text );
454 $text = preg_replace_callback(
455 "/\\[\\[([^|]+\\|)(.*?)\\]\\]/",
456 [ $this,
'linkReplace' ],
459 $text = preg_replace(
"/<\/?[^>]+>/",
"", $text );
460 $text = preg_replace(
"/'''''/",
"", $text );
461 $text = preg_replace(
"/('''|<\/?[iIuUbB]>)/",
"", $text );
462 $text = preg_replace(
"/''/",
"", $text );
467 $text = Sanitizer::escapeHtmlAllowEntities( $text );
479 $colon = strpos(
$matches[1],
':' );
480 if ( $colon ===
false ) {
484 $ns = substr(
$matches[1], 0, $colon );
508 $lines = explode(
"\n", $text );
510 $terms = implode(
'|', $terms );
511 $max = intval( $contextchars ) + 1;
512 $pat1 =
"/(.*)($terms)(.{0,$max})/i";
518 if ( 0 == $contextlines ) {
523 if ( !preg_match( $pat1,
$line, $m ) ) {
530 if (
count( $m ) < 3 ) {
533 $post =
$wgContLang->truncate( $m[3], $contextchars,
'...',
false );
538 $line = htmlspecialchars(
$pre . $found . $post );
539 $pat2 =
'/(' . $terms .
")/i";
540 $line = preg_replace( $pat2,
"<span class='searchmatch'>\\1</span>",
$line );
542 $extract .=
"${line}\n";
558 $text = ltrim( $text ) .
"\n";
559 $text = str_replace(
"\n\n",
"\n", $text );
560 preg_match(
"/^(.*\n){0,$contextlines}/", $text, $match );
563 $text = htmlspecialchars( substr( trim( $match[0] ), 0, $contextlines * $contextchars ) );
564 return str_replace(
"\n",
'<br>', $text );