53 $contextlines = self::DEFAULT_CONTEXT_LINES,
54 $contextchars = self::DEFAULT_CONTEXT_CHARS
56 $searchHighlightBoundaries = MediaWikiServices::getInstance()
57 ->getMainConfig()->get( MainConfigNames::SearchHighlightBoundaries );
64 $spat =
"/(\\{\\{)|(\\[\\[[^\\]:]+:)|(\n\\{\\|)";
67 1 =>
'/(\{\{)|(\}\})/',
68 2 =>
'/(\[\[)|(\]\])/',
69 3 =>
"/(\n\\{\\|)|(\n\\|\\})/" ];
73 if ( ExtensionRegistry::getInstance()->isLoaded(
'Cite' ) ) {
75 $endPatterns[4] =
'/(<ref>)|(<\/ref>)/';
81 $textLen = strlen( $text );
83 while ( $start < $textLen ) {
85 if ( preg_match( $spat, $text,
$matches, PREG_OFFSET_CAPTURE, $start ) ) {
87 foreach (
$matches as $key => $val ) {
88 if ( $key > 0 && $val[1] != -1 ) {
91 $ns = substr( $val[0], 2, -1 );
93 MediaWikiServices::getInstance()->getContentLanguage()->
100 $epat = $endPatterns[$key];
101 $this->splitAndAdd( $textExt, $count, substr( $text, $start, $val[1] - $start ) );
109 $offset = $start + 1;
111 while ( preg_match( $epat, $text, $endMatches, PREG_OFFSET_CAPTURE, $offset ) ) {
112 if ( array_key_exists( 2, $endMatches ) ) {
115 $len = strlen( $endMatches[2][0] );
116 $off = $endMatches[2][1];
117 $this->splitAndAdd( $otherExt, $count,
118 substr( $text, $start, $off + $len - $start ) );
119 $start = $off + $len;
130 $offset = $endMatches[0][1] + strlen( $endMatches[0][0] );
134 $this->splitAndAdd( $textExt, $count, substr( $text, $start, strlen(
$matches[0][0] ) ) );
141 $this->splitAndAdd( $textExt, $count, substr( $text, $start ) );
144 '@phan-var string[] $textExt';
146 $all = $textExt + $otherExt;
149 foreach ( $terms as $index => $term ) {
151 if ( preg_match(
'/[\x80-\xff]/', $term ) ) {
152 $terms[$index] = preg_replace_callback(
154 $this->caseCallback( ... ),
158 $terms[$index] = $term;
161 $anyterm = implode(
'|', $terms );
162 $phrase = implode(
"{$searchHighlightBoundaries}+", $terms );
167 $scale = strlen( $anyterm ) / mb_strlen( $anyterm );
168 $contextchars = intval( $contextchars * $scale );
170 $patPre =
"(^|{$searchHighlightBoundaries})";
171 $patPost =
"({$searchHighlightBoundaries}|$)";
173 $pat1 =
"/(" . $phrase .
")/ui";
174 $pat2 =
"/$patPre(" . $anyterm .
")$patPost/ui";
176 $left = $contextlines;
184 foreach ( $textExt as $index => $line ) {
185 if ( $line !==
'' && $line[0] !=
';' && $line[0] !=
':' ) {
186 $firstText = $this->extract( $line, 0, $contextchars * $contextlines );
194 foreach ( $terms as $term ) {
195 if ( !preg_match(
"/$patPre" . $term .
"$patPost/ui", $firstText ) ) {
201 $snippets[$first] = $firstText;
202 $offsets[$first] = 0;
207 $this->process( $pat1, $textExt, $left, $contextchars, $snippets, $offsets );
209 $this->process( $pat1, $otherExt, $left, $contextchars, $snippets, $offsets );
211 $this->process( $pat2, $textExt, $left, $contextchars, $snippets, $offsets );
213 $this->process( $pat2, $otherExt, $left, $contextchars, $snippets, $offsets );
220 if ( count( $snippets ) == 0 ) {
222 if ( array_key_exists( $first, $all ) ) {
223 $targetchars = $contextchars * $contextlines;
224 $snippets[$first] =
'';
225 $offsets[$first] = 0;
229 if ( array_key_exists( $first, $snippets ) && preg_match( $pat1, $snippets[$first] )
230 && $offsets[$first] < $contextchars * 2 ) {
231 $snippets = [ $first => $snippets[$first] ];
235 $targetchars = intval( ( $contextchars * $contextlines ) / count( $snippets ) );
238 foreach ( $snippets as $index => $line ) {
239 $extended[$index] = $line;
240 $len = strlen( $line );
243 if ( $len < $targetchars - 20 ) {
245 if ( $len < strlen( $all[$index] ) ) {
246 $extended[$index] = $this->extract(
251 $offsets[$index] + $targetchars,
254 $len = strlen( $extended[$index] );
261 while ( $len < $targetchars - 20
262 && array_key_exists( $add, $all )
263 && !array_key_exists( $add, $snippets ) ) {
267 $tt =
"\n" . $this->extract( $all[$add], 0, $targetchars - $len, $offsets[$add] );
268 $extended[$add] = $tt;
269 $len += strlen( $tt );
276 $snippets = $extended;
279 foreach ( $snippets as $index => $line ) {
282 } elseif ( $last + 1 == $index
283 && $offsets[$last] + strlen( $snippets[$last] ) >= strlen( $all[$last] )
285 $extract .=
" " . $line;
287 $extract .=
'<b> ... </b>' . $line;
293 $extract .=
'<b> ... </b>';
297 foreach ( $terms as $term ) {
298 if ( !isset( $processed[$term] ) ) {
299 $pat3 =
"/$patPre(" . $term .
")$patPost/ui";
300 $extract = preg_replace( $pat3,
301 "\\1<span class='searchmatch'>\\2</span>\\3", $extract );
302 $processed[$term] =
true;