33 $this->mCleanWikitext = $cleanupWikitext;
46 public function highlightText( $text, $terms, $contextlines, $contextchars ) {
54 $spat =
"/(\\{\\{)|(\\[\\[[^\\]:]+:)|(\n\\{\\|)";
57 1 =>
'/(\{\{)|(\}\})/',
58 2 =>
'/(\[\[)|(\]\])/',
59 3 =>
"/(\n\\{\\|)|(\n\\|\\})/" ];
63 if ( class_exists(
'Cite' ) ) {
65 $endPatterns[4] =
'/(<ref>)|(<\/ref>)/';
71 $textLen = strlen( $text );
73 while ( $start < $textLen ) {
75 if ( preg_match( $spat, $text,
$matches, PREG_OFFSET_CAPTURE, $start ) ) {
78 if ( $key > 0 && $val[1] != - 1 ) {
81 $ns = substr( $val[0], 2, - 1 );
82 if ( $wgContLang->getNsIndex( $ns ) !=
NS_FILE ) {
87 $epat = $endPatterns[$key];
88 $this->
splitAndAdd( $textExt,
$count, substr( $text, $start, $val[1] - $start ) );
98 while ( preg_match( $epat, $text, $endMatches, PREG_OFFSET_CAPTURE, $offset ) ) {
99 if ( array_key_exists( 2, $endMatches ) ) {
102 $len = strlen( $endMatches[2][0] );
103 $off = $endMatches[2][1];
105 substr( $text, $start, $off + $len - $start ) );
106 $start = $off + $len;
117 $offset = $endMatches[0][1] + strlen( $endMatches[0][0] );
132 $all = $textExt + $otherExt;
135 foreach ( $terms
as $index =>
$term ) {
137 if ( preg_match(
'/[\x80-\xff]/',
$term ) ) {
138 $terms[$index] = preg_replace_callback(
140 [ $this,
'caseCallback' ],
144 $terms[$index] =
$term;
147 $anyterm = implode(
'|', $terms );
148 $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";
162 $left = $contextlines;
170 foreach ( $textExt
as $index =>
$line ) {
172 $firstText = $this->
extract(
$line, 0, $contextchars * $contextlines );
181 if ( !preg_match(
"/$patPre" . $term .
"$patPost/ui", $firstText ) ) {
187 $snippets[$first] = $firstText;
188 $offsets[$first] = 0;
193 $this->
process( $pat1, $textExt, $left, $contextchars, $snippets, $offsets );
195 $this->
process( $pat1, $otherExt, $left, $contextchars, $snippets, $offsets );
197 $this->
process( $pat2, $textExt, $left, $contextchars, $snippets, $offsets );
199 $this->
process( $pat2, $otherExt, $left, $contextchars, $snippets, $offsets );
206 if ( count( $snippets ) == 0 ) {
208 if ( array_key_exists( $first, $all ) ) {
209 $targetchars = $contextchars * $contextlines;
210 $snippets[$first] =
'';
211 $offsets[$first] = 0;
215 if ( array_key_exists( $first, $snippets ) && preg_match( $pat1, $snippets[$first] )
216 && $offsets[$first] < $contextchars * 2 ) {
217 $snippets = [ $first => $snippets[$first] ];
221 $targetchars = intval( ( $contextchars * $contextlines ) / count( $snippets ) );
224 foreach ( $snippets
as $index =>
$line ) {
225 $extended[$index] =
$line;
226 $len = strlen(
$line );
227 if ( $len < $targetchars - 20 ) {
229 if ( $len < strlen( $all[$index] ) ) {
230 $extended[$index] = $this->
extract(
233 $offsets[$index] + $targetchars,
236 $len = strlen( $extended[$index] );
241 while ( $len < $targetchars - 20
242 && array_key_exists( $add, $all )
243 && !array_key_exists( $add, $snippets ) ) {
245 $tt =
"\n" . $this->
extract( $all[$add], 0, $targetchars - $len, $offsets[$add] );
246 $extended[$add] = $tt;
247 $len += strlen( $tt );
254 $snippets = $extended;
257 foreach ( $snippets
as $index =>
$line ) {
258 if (
$last == - 1 ) {
260 } elseif (
$last + 1 == $index
261 && $offsets[
$last] + strlen( $snippets[
$last] ) >= strlen( $all[$last] )
263 $extract .=
" " .
$line;
265 $extract .=
'<b> ... </b>' .
$line;
271 $extract .=
'<b> ... </b>';
277 $pat3 =
"/$patPre(" . $term .
")$patPost/ui";
278 $extract = preg_replace( $pat3,
279 "\\1<span class='searchmatch'>\\2</span>\\3", $extract );
295 $split = explode(
"\n", $this->mCleanWikitext ? $this->
removeWiki( $text ) : $text );
299 $extracts[
$count++] = $tt;
313 return '[' . $wgContLang->lc(
$matches[0] ) . $wgContLang->uc(
$matches[0] ) .
']';
329 function extract( $text, $start, $end, &$posStart = null, &$posEnd = null ) {
331 $start = $this->
position( $text, $start, 1 );
333 if ( $end >= strlen( $text ) ) {
334 $end = strlen( $text );
336 $end = $this->
position( $text, $end );
339 if ( !is_null( $posStart ) ) {
342 if ( !is_null( $posEnd ) ) {
346 if ( $end > $start ) {
347 return substr( $text, $start, $end - $start );
363 $s = max( 0, $point - $tolerance );
364 $l = min( strlen( $text ), $point + $tolerance ) -
$s;
368 '/[ ,.!?~!@#$%^&*\(\)+=\-\\\|\[\]"\'<>]/',
369 substr( $text,
$s, $l ),
373 return $m[0][1] +
$s + $offset;
376 $char = ord( $text[$point] );
377 while ( $char >= 0x80 && $char < 0xc0 ) {
380 if ( $point >= strlen( $text ) ) {
381 return strlen( $text );
383 $char = ord( $text[$point] );
402 function process( $pattern, $extracts, &$linesleft, &$contextchars, &
$out, &$offsets ) {
403 if ( $linesleft == 0 ) {
406 foreach ( $extracts
as $index =>
$line ) {
407 if ( array_key_exists( $index,
$out ) ) {
412 if ( !preg_match( $pattern,
$line, $m, PREG_OFFSET_CAPTURE ) ) {
417 $len = strlen( $m[0][0] );
418 if ( $offset + $len < $contextchars ) {
420 } elseif ( $len > $contextchars ) {
423 $begin = $offset + intval( ( $len - $contextchars ) / 2 );
426 $end = $begin + $contextchars;
431 $offsets[$index] = $posBegin;
433 if ( $linesleft == 0 ) {
446 $text = preg_replace(
"/\\{\\{([^|]+?)\\}\\}/",
"", $text );
447 $text = preg_replace(
"/\\{\\{([^|]+\\|)(.*?)\\}\\}/",
"\\2", $text );
448 $text = preg_replace(
"/\\[\\[([^|]+?)\\]\\]/",
"\\1", $text );
449 $text = preg_replace_callback(
450 "/\\[\\[([^|]+\\|)(.*?)\\]\\]/",
451 [ $this,
'linkReplace' ],
454 $text = preg_replace(
"/<\/?[^>]+>/",
"", $text );
455 $text = preg_replace(
"/'''''/",
"", $text );
456 $text = preg_replace(
"/('''|<\/?[iIuUbB]>)/",
"", $text );
457 $text = preg_replace(
"/''/",
"", $text );
470 $colon = strpos(
$matches[1],
':' );
471 if ( $colon ===
false ) {
475 $ns = substr(
$matches[1], 0, $colon );
476 $index = $wgContLang->getNsIndex( $ns );
499 $lines = explode(
"\n", $text );
501 $terms = implode(
'|', $terms );
502 $max = intval( $contextchars ) + 1;
503 $pat1 =
"/(.*)($terms)(.{0,$max})/i";
509 if ( 0 == $contextlines ) {
514 if ( !preg_match( $pat1, $line, $m ) ) {
519 $pre = $wgContLang->truncate( $m[1], - $contextchars,
'...',
false );
521 if ( count( $m ) < 3 ) {
524 $post = $wgContLang->truncate( $m[3], $contextchars,
'...',
false );
529 $line = htmlspecialchars(
$pre . $found . $post );
530 $pat2 =
'/(' . $terms .
")/i";
531 $line = preg_replace( $pat2,
"<span class='searchmatch'>\\1</span>", $line );
533 $extract .=
"${line}\n";
549 $text = ltrim( $text ) .
"\n";
550 $text = str_replace(
"\n\n",
"\n", $text );
551 preg_match(
"/^(.*\n){0,$contextlines}/", $text, $match );
554 $text = htmlspecialchars( substr( trim( $match[0] ), 0, $contextlines * $contextchars ) );
555 return str_replace(
"\n",
'<br>', $text );
process($pattern, $extracts, &$linesleft, &$contextchars, &$out, &$offsets)
Search extracts for a pattern, and return snippets.
external whereas SearchGetNearMatch runs after $term
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
highlightSimple($text, $terms, $contextlines, $contextchars)
Simple & fast snippet extraction, but gives completely unrelevant snippets.
splitAndAdd(&$extracts, &$count, $text)
Split text into lines and add it to extracts array.
__construct($cleanupWikitext=true)
when a variable name is used in a it is silently declared as a new local masking the global
Highlight bits of wikitext.
position($text, $point, $offset=0)
Find a nonletter near a point (index) in the text.
highlightText($text, $terms, $contextlines, $contextchars)
Wikitext highlighting when $wgAdvancedSearchHighlighting = true.
extract($text, $start, $end, &$posStart=null, &$posEnd=null)
Extract part of the text from start to end, but by not chopping up words.
linkReplace($matches)
callback to replace [[target|caption]] kind of links, if the target is category or image...
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
removeWiki($text)
Basic wikitext removal.
caseCallback($matches)
Do manual case conversion for non-ascii chars.
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the local content language as $wgContLang
return true to allow those checks to and false if checking is done remove or add to the links of a group of changes in EnhancedChangesList Hook subscribers can return false to omit this line from recentchanges use this to change the tables headers temp or archived zone change it to an object instance and return false override the list derivative used the name of the old file when set the default code will be skipped $pre
highlightNone($text, $contextlines, $contextchars)
Returns the first few lines of the text.