67 $contextlines = self::DEFAULT_CONTEXT_LINES,
68 $contextchars = self::DEFAULT_CONTEXT_CHARS
70 $searchHighlightBoundaries = MediaWikiServices::getInstance()
71 ->getMainConfig()->get( MainConfigNames::SearchHighlightBoundaries );
78 $spat =
"/(\\{\\{)|(\\[\\[[^\\]:]+:)|(\n\\{\\|)";
81 1 =>
'/(\{\{)|(\}\})/',
82 2 =>
'/(\[\[)|(\]\])/',
83 3 =>
"/(\n\\{\\|)|(\n\\|\\})/" ];
87 if ( ExtensionRegistry::getInstance()->isLoaded(
'Cite' ) ) {
89 $endPatterns[4] =
'/(<ref>)|(<\/ref>)/';
95 $textLen = strlen( $text );
97 while ( $start < $textLen ) {
99 if ( preg_match( $spat, $text,
$matches, PREG_OFFSET_CAPTURE, $start ) ) {
101 foreach (
$matches as $key => $val ) {
102 if ( $key > 0 && $val[1] != -1 ) {
105 $ns = substr( $val[0], 2, -1 );
107 MediaWikiServices::getInstance()->getContentLanguage()->
114 $epat = $endPatterns[$key];
115 $this->splitAndAdd( $textExt, $count, substr( $text, $start, $val[1] - $start ) );
123 $offset = $start + 1;
125 while ( preg_match( $epat, $text, $endMatches, PREG_OFFSET_CAPTURE, $offset ) ) {
126 if ( array_key_exists( 2, $endMatches ) ) {
129 $len = strlen( $endMatches[2][0] );
130 $off = $endMatches[2][1];
131 $this->splitAndAdd( $otherExt, $count,
132 substr( $text, $start, $off + $len - $start ) );
133 $start = $off + $len;
144 $offset = $endMatches[0][1] + strlen( $endMatches[0][0] );
148 $this->splitAndAdd( $textExt, $count, substr( $text, $start, strlen(
$matches[0][0] ) ) );
155 $this->splitAndAdd( $textExt, $count, substr( $text, $start ) );
158 '@phan-var string[] $textExt';
160 $all = $textExt + $otherExt;
163 foreach ( $terms as $index => $term ) {
165 if ( preg_match(
'/[\x80-\xff]/', $term ) ) {
166 $terms[$index] = preg_replace_callback(
168 [ $this,
'caseCallback' ],
172 $terms[$index] = $term;
175 $anyterm = implode(
'|', $terms );
176 $phrase = implode(
"{$searchHighlightBoundaries}+", $terms );
181 $scale = strlen( $anyterm ) / mb_strlen( $anyterm );
182 $contextchars = intval( $contextchars * $scale );
184 $patPre =
"(^|{$searchHighlightBoundaries})";
185 $patPost =
"({$searchHighlightBoundaries}|$)";
187 $pat1 =
"/(" . $phrase .
")/ui";
188 $pat2 =
"/$patPre(" . $anyterm .
")$patPost/ui";
190 $left = $contextlines;
198 foreach ( $textExt as $index => $line ) {
199 if ( strlen( $line ) > 0 && $line[0] !=
';' && $line[0] !=
':' ) {
200 $firstText = $this->extract( $line, 0, $contextchars * $contextlines );
208 foreach ( $terms as $term ) {
209 if ( !preg_match(
"/$patPre" . $term .
"$patPost/ui", $firstText ) ) {
215 $snippets[$first] = $firstText;
216 $offsets[$first] = 0;
221 $this->process( $pat1, $textExt, $left, $contextchars, $snippets, $offsets );
223 $this->process( $pat1, $otherExt, $left, $contextchars, $snippets, $offsets );
225 $this->process( $pat2, $textExt, $left, $contextchars, $snippets, $offsets );
227 $this->process( $pat2, $otherExt, $left, $contextchars, $snippets, $offsets );
234 if ( count( $snippets ) == 0 ) {
236 if ( array_key_exists( $first, $all ) ) {
237 $targetchars = $contextchars * $contextlines;
238 $snippets[$first] =
'';
239 $offsets[$first] = 0;
243 if ( array_key_exists( $first, $snippets ) && preg_match( $pat1, $snippets[$first] )
244 && $offsets[$first] < $contextchars * 2 ) {
245 $snippets = [ $first => $snippets[$first] ];
249 $targetchars = intval( ( $contextchars * $contextlines ) / count( $snippets ) );
252 foreach ( $snippets as $index => $line ) {
253 $extended[$index] = $line;
254 $len = strlen( $line );
257 if ( $len < $targetchars - 20 ) {
259 if ( $len < strlen( $all[$index] ) ) {
260 $extended[$index] = $this->extract(
265 $offsets[$index] + $targetchars,
268 $len = strlen( $extended[$index] );
275 while ( $len < $targetchars - 20
276 && array_key_exists( $add, $all )
277 && !array_key_exists( $add, $snippets ) ) {
281 $tt =
"\n" . $this->extract( $all[$add], 0, $targetchars - $len, $offsets[$add] );
282 $extended[$add] = $tt;
283 $len += strlen( $tt );
290 $snippets = $extended;
293 foreach ( $snippets as $index => $line ) {
296 } elseif ( $last + 1 == $index
297 && $offsets[$last] + strlen( $snippets[$last] ) >= strlen( $all[$last] )
299 $extract .=
" " . $line;
301 $extract .=
'<b> ... </b>' . $line;
307 $extract .=
'<b> ... </b>';
311 foreach ( $terms as $term ) {
312 if ( !isset( $processed[$term] ) ) {
313 $pat3 =
"/$patPre(" . $term .
")$patPost/ui";
314 $extract = preg_replace( $pat3,
315 "\\1<span class='searchmatch'>\\2</span>\\3", $extract );
316 $processed[$term] =
true;