55 $contextlines = self::DEFAULT_CONTEXT_LINES,
56 $contextchars = self::DEFAULT_CONTEXT_CHARS
66 $spat =
"/(\\{\\{)|(\\[\\[[^\\]:]+:)|(\n\\{\\|)";
69 1 =>
'/(\{\{)|(\}\})/',
70 2 =>
'/(\[\[)|(\]\])/',
71 3 =>
"/(\n\\{\\|)|(\n\\|\\})/" ];
75 if ( ExtensionRegistry::getInstance()->isLoaded(
'Cite' ) ) {
77 $endPatterns[4] =
'/(<ref>)|(<\/ref>)/';
83 $textLen = strlen( $text );
85 while ( $start < $textLen ) {
87 if ( preg_match( $spat, $text,
$matches, PREG_OFFSET_CAPTURE, $start ) ) {
89 foreach (
$matches as $key => $val ) {
90 if ( $key > 0 && $val[1] != -1 ) {
93 $ns = substr( $val[0], 2, -1 );
102 $epat = $endPatterns[$key];
103 $this->splitAndAdd( $textExt, $count, substr( $text, $start, $val[1] - $start ) );
111 $offset = $start + 1;
113 while ( preg_match( $epat, $text, $endMatches, PREG_OFFSET_CAPTURE, $offset ) ) {
114 if ( array_key_exists( 2, $endMatches ) ) {
117 $len = strlen( $endMatches[2][0] );
118 $off = $endMatches[2][1];
119 $this->splitAndAdd( $otherExt, $count,
120 substr( $text, $start, $off + $len - $start ) );
121 $start = $off + $len;
132 $offset = $endMatches[0][1] + strlen( $endMatches[0][0] );
136 $this->splitAndAdd( $textExt, $count, substr( $text, $start, strlen(
$matches[0][0] ) ) );
143 $this->splitAndAdd( $textExt, $count, substr( $text, $start ) );
146 '@phan-var string[] $textExt';
148 $all = $textExt + $otherExt;
151 foreach ( $terms as $index => $term ) {
153 if ( preg_match(
'/[\x80-\xff]/', $term ) ) {
154 $terms[$index] = preg_replace_callback(
156 $this->caseCallback( ... ),
160 $terms[$index] = $term;
163 $anyterm = implode(
'|', $terms );
164 $phrase = implode(
"{$searchHighlightBoundaries}+", $terms );
169 $scale = strlen( $anyterm ) / mb_strlen( $anyterm );
170 $contextchars = intval( $contextchars * $scale );
172 $patPre =
"(^|{$searchHighlightBoundaries})";
173 $patPost =
"({$searchHighlightBoundaries}|$)";
175 $pat1 =
"/(" . $phrase .
")/ui";
176 $pat2 =
"/$patPre(" . $anyterm .
")$patPost/ui";
178 $left = $contextlines;
186 foreach ( $textExt as $index => $line ) {
187 if ( $line !==
'' && $line[0] !=
';' && $line[0] !=
':' ) {
188 $firstText = $this->extract( $line, 0, $contextchars * $contextlines );
196 foreach ( $terms as $term ) {
197 if ( !preg_match(
"/$patPre" . $term .
"$patPost/ui", $firstText ) ) {
203 $snippets[$first] = $firstText;
204 $offsets[$first] = 0;
209 $this->process( $pat1, $textExt, $left, $contextchars, $snippets, $offsets );
211 $this->process( $pat1, $otherExt, $left, $contextchars, $snippets, $offsets );
213 $this->process( $pat2, $textExt, $left, $contextchars, $snippets, $offsets );
215 $this->process( $pat2, $otherExt, $left, $contextchars, $snippets, $offsets );
222 if ( count( $snippets ) == 0 ) {
224 if ( array_key_exists( $first, $all ) ) {
225 $targetchars = $contextchars * $contextlines;
226 $snippets[$first] =
'';
227 $offsets[$first] = 0;
231 if ( array_key_exists( $first, $snippets ) && preg_match( $pat1, $snippets[$first] )
232 && $offsets[$first] < $contextchars * 2 ) {
233 $snippets = [ $first => $snippets[$first] ];
237 $targetchars = intval( ( $contextchars * $contextlines ) / count( $snippets ) );
240 foreach ( $snippets as $index => $line ) {
241 $extended[$index] = $line;
242 $len = strlen( $line );
245 if ( $len < $targetchars - 20 ) {
247 if ( $len < strlen( $all[$index] ) ) {
248 $extended[$index] = $this->extract(
253 $offsets[$index] + $targetchars,
256 $len = strlen( $extended[$index] );
263 while ( $len < $targetchars - 20
264 && array_key_exists( $add, $all )
265 && !array_key_exists( $add, $snippets ) ) {
269 $tt =
"\n" . $this->extract( $all[$add], 0, $targetchars - $len, $offsets[$add] );
270 $extended[$add] = $tt;
271 $len += strlen( $tt );
278 $snippets = $extended;
281 foreach ( $snippets as $index => $line ) {
284 } elseif ( $last + 1 == $index
285 && $offsets[$last] + strlen( $snippets[$last] ) >= strlen( $all[$last] )
287 $extract .=
" " . $line;
289 $extract .=
'<b> ... </b>' . $line;
295 $extract .=
'<b> ... </b>';
299 foreach ( $terms as $term ) {
300 if ( !isset( $processed[$term] ) ) {
301 $pat3 =
"/$patPre(" . $term .
")$patPost/ui";
302 $extract = preg_replace( $pat3,
303 "\\1<span class='searchmatch'>\\2</span>\\3", $extract );
304 $processed[$term] =
true;