33 $this->mCleanWikitext = $cleanupWikitext;
45 public function highlightText( $text, $terms, $contextlines, $contextchars ) {
47 global $wgSearchHighlightBoundaries;
55 $spat =
"/(\\{\\{)|(\\[\\[[^\\]:]+:)|(\n\\{\\|)";
58 1 =>
'/(\{\{)|(\}\})/',
59 2 =>
'/(\[\[)|(\]\])/',
60 3 =>
"/(\n\\{\\|)|(\n\\|\\})/" );
63 if ( function_exists(
'wfCite' ) ) {
65 $endPatterns[4] =
'/(<ref>)|(<\/ref>)/';
72 $textLen = strlen( $text );
74 while ( $start < $textLen ) {
76 if ( preg_match( $spat, $text,
$matches, PREG_OFFSET_CAPTURE, $start ) ) {
79 if ( $key > 0 && $val[1] != - 1 ) {
82 $ns = substr( $val[0], 2, - 1 );
88 $epat = $endPatterns[$key];
89 $this->
splitAndAdd( $textExt,
$count, substr( $text, $start, $val[1] - $start ) );
99 while ( preg_match( $epat, $text, $endMatches, PREG_OFFSET_CAPTURE, $offset ) ) {
100 if ( array_key_exists( 2, $endMatches ) ) {
103 $len = strlen( $endMatches[2][0] );
104 $off = $endMatches[2][1];
106 substr( $text, $start, $off + $len - $start ) );
107 $start = $off + $len;
118 $offset = $endMatches[0][1] + strlen( $endMatches[0][0] );
133 $all = $textExt + $otherExt;
138 foreach ( $terms
as $index =>
$term ) {
140 if ( preg_match(
'/[\x80-\xff]/',
$term ) ) {
141 $terms[$index] = preg_replace_callback(
'/./us',
array( $this,
'caseCallback' ), $terms[$index] );
143 $terms[$index] =
$term;
146 $anyterm = implode(
'|', $terms );
147 $phrase = implode(
"$wgSearchHighlightBoundaries+", $terms );
153 $scale = strlen( $anyterm ) / mb_strlen( $anyterm );
154 $contextchars = intval( $contextchars * $scale );
156 $patPre =
"(^|$wgSearchHighlightBoundaries)";
157 $patPost =
"($wgSearchHighlightBoundaries|$)";
159 $pat1 =
"/(" . $phrase .
")/ui";
160 $pat2 =
"/$patPre(" . $anyterm .
")$patPost/ui";
164 $left = $contextlines;
172 foreach ( $textExt
as $index =>
$line ) {
174 $firstText = $this->
extract(
$line, 0, $contextchars * $contextlines );
183 if ( ! preg_match(
"/$patPre" .
$term .
"$patPost/ui", $firstText ) ) {
189 $snippets[$first] = $firstText;
190 $offsets[$first] = 0;
195 $this->
process( $pat1, $textExt, $left, $contextchars, $snippets, $offsets );
197 $this->
process( $pat1, $otherExt, $left, $contextchars, $snippets, $offsets );
199 $this->
process( $pat2, $textExt, $left, $contextchars, $snippets, $offsets );
201 $this->
process( $pat2, $otherExt, $left, $contextchars, $snippets, $offsets );
208 if ( count( $snippets ) == 0 ) {
210 if ( array_key_exists( $first, $all ) ) {
211 $targetchars = $contextchars * $contextlines;
212 $snippets[$first] =
'';
213 $offsets[$first] = 0;
217 if ( array_key_exists( $first, $snippets ) && preg_match( $pat1, $snippets[$first] )
218 && $offsets[$first] < $contextchars * 2 ) {
219 $snippets =
array( $first => $snippets[$first] );
223 $targetchars = intval( ( $contextchars * $contextlines ) / count ( $snippets ) );
226 foreach ( $snippets
as $index =>
$line ) {
227 $extended[$index] =
$line;
228 $len = strlen(
$line );
229 if ( $len < $targetchars - 20 ) {
231 if ( $len < strlen( $all[$index] ) ) {
232 $extended[$index] = $this->
extract( $all[$index], $offsets[$index], $offsets[$index] + $targetchars, $offsets[$index] );
233 $len = strlen( $extended[$index] );
238 while ( $len < $targetchars - 20
239 && array_key_exists( $add, $all )
240 && !array_key_exists( $add, $snippets ) ) {
242 $tt =
"\n" . $this->
extract( $all[$add], 0, $targetchars - $len, $offsets[$add] );
243 $extended[$add] = $tt;
244 $len += strlen( $tt );
251 $snippets = $extended;
254 foreach ( $snippets
as $index =>
$line ) {
255 if (
$last == - 1 ) {
257 } elseif (
$last + 1 == $index && $offsets[
$last] + strlen( $snippets[
$last] ) >= strlen( $all[
$last] ) ) {
258 $extract .=
" " .
$line;
260 $extract .=
'<b> ... </b>' .
$line;
266 $extract .=
'<b> ... </b>';
272 $pat3 =
"/$patPre(" .
$term .
")$patPost/ui";
273 $extract = preg_replace( $pat3,
274 "\\1<span class='searchmatch'>\\2</span>\\3", $extract );
292 $split = explode(
"\n", $this->mCleanWikitext ? $this->
removeWiki( $text ) : $text );
296 $extracts[
$count++] = $tt;
326 function extract( $text, $start, $end, &$posStart =
null, &$posEnd =
null ) {
328 $start = $this->
position( $text, $start, 1 );
330 if ( $end >= strlen( $text ) ) {
331 $end = strlen( $text );
333 $end = $this->
position( $text, $end );
336 if ( !is_null( $posStart ) ) {
339 if ( !is_null( $posEnd ) ) {
343 if ( $end > $start ) {
344 return substr( $text, $start, $end - $start );
360 $s = max( 0, $point - $tolerance );
361 $l = min( strlen( $text ), $point + $tolerance ) -
$s;
363 if ( preg_match(
'/[ ,.!?~!@#$%^&*\(\)+=\-\\\|\[\]"\'<>]/', substr( $text,
$s, $l ), $m, PREG_OFFSET_CAPTURE ) ) {
364 return $m[0][1] +
$s + $offset;
367 $char = ord( $text[$point] );
368 while ( $char >= 0x80 && $char < 0xc0 ) {
371 if ( $point >= strlen( $text ) ) {
372 return strlen( $text );
374 $char = ord( $text[$point] );
392 function process( $pattern, $extracts, &$linesleft, &$contextchars, &
$out, &$offsets ) {
393 if ( $linesleft == 0 ) {
396 foreach ( $extracts
as $index =>
$line ) {
397 if ( array_key_exists( $index,
$out ) ) {
402 if ( !preg_match( $pattern,
$line, $m, PREG_OFFSET_CAPTURE ) ) {
407 $len = strlen( $m[0][0] );
408 if ( $offset + $len < $contextchars ) {
410 } elseif ( $len > $contextchars ) {
413 $begin = $offset + intval( ( $len - $contextchars ) / 2 );
416 $end = $begin + $contextchars;
421 $offsets[$index] = $posBegin;
423 if ( $linesleft == 0 ) {
444 $text = preg_replace(
"/\\{\\{([^|]+?)\\}\\}/",
"", $text );
445 $text = preg_replace(
"/\\{\\{([^|]+\\|)(.*?)\\}\\}/",
"\\2", $text );
446 $text = preg_replace(
"/\\[\\[([^|]+?)\\]\\]/",
"\\1", $text );
447 $text = preg_replace_callback(
"/\\[\\[([^|]+\\|)(.*?)\\]\\]/",
array( $this,
'linkReplace' ), $text );
449 $text = preg_replace(
"/<\/?[^>]+>/",
"", $text );
450 $text = preg_replace(
"/'''''/",
"", $text );
451 $text = preg_replace(
"/('''|<\/?[iIuUbB]>)/",
"", $text );
452 $text = preg_replace(
"/''/",
"", $text );
465 $colon = strpos(
$matches[1],
':' );
466 if ( $colon ===
false ) {
470 $ns = substr(
$matches[1], 0, $colon );
493 $lines = explode(
"\n", $text );
495 $terms = implode(
'|', $terms );
496 $max = intval( $contextchars ) + 1;
497 $pat1 =
"/(.*)($terms)(.{0,$max})/i";
504 if ( 0 == $contextlines ) {
509 if ( ! preg_match( $pat1,
$line, $m ) ) {
516 if ( count( $m ) < 3 ) {
519 $post =
$wgContLang->truncate( $m[3], $contextchars,
'...',
false );
524 $line = htmlspecialchars(
$pre . $found . $post );
525 $pat2 =
'/(' . $terms .
")/i";
526 $line = preg_replace( $pat2,
"<span class='searchmatch'>\\1</span>",
$line );
528 $extract .=
"${line}\n";