}
/**
- * Default implementation of wikitext highlighting
+ * Wikitext highlighting when $wgAdvancedSearchHighlighting = true
*
* @param string $text
- * @param array $terms Terms to highlight (unescaped)
+ * @param array $terms Terms to highlight (not html escaped but
+ * regex escaped via SearchDatabase::regexTerm())
* @param int $contextlines
* @param int $contextchars
* @return string
// spli text into text + templates/links/tables
$spat = "/(\\{\\{)|(\\[\\[[^\\]:]+:)|(\n\\{\\|)";
// first capture group is for detecting nested templates/links/tables/references
- $endPatterns = array(
+ $endPatterns = [
1 => '/(\{\{)|(\}\})/', // template
2 => '/(\[\[)|(\]\])/', // image
- 3 => "/(\n\\{\\|)|(\n\\|\\})/" ); // table
+ 3 => "/(\n\\{\\|)|(\n\\|\\})/" ]; // table
// @todo FIXME: This should prolly be a hook or something
// instead of hardcoding a class name from the Cite extension
$endPatterns[4] = '/(<ref>)|(<\/ref>)/';
}
$spat .= '/';
- $textExt = array(); // text extracts
- $otherExt = array(); // other extracts
+ $textExt = []; // text extracts
+ $otherExt = []; // other extracts
$start = 0;
$textLen = strlen( $text );
$count = 0; // sequence number to maintain ordering
if ( preg_match( '/[\x80-\xff]/', $term ) ) {
$terms[$index] = preg_replace_callback(
'/./us',
- array( $this, 'caseCallback' ),
+ [ $this, 'caseCallback' ],
$terms[$index]
);
} else {
}
$anyterm = implode( '|', $terms );
$phrase = implode( "$wgSearchHighlightBoundaries+", $terms );
-
// @todo FIXME: A hack to scale contextchars, a correct solution
// would be to have contextchars actually be char and not byte
// length, and do proper utf-8 substrings and lengths everywhere,
$left = $contextlines;
- $snippets = array();
- $offsets = array();
+ $snippets = [];
+ $offsets = [];
// show beginning only if it contains all words
$first = 0;
}
// add extra chars to each snippet to make snippets constant size
- $extended = array();
+ $extended = [];
if ( count( $snippets ) == 0 ) {
// couldn't find the target words, just show beginning of article
if ( array_key_exists( $first, $all ) ) {
// if begin of the article contains the whole phrase, show only that !!
if ( array_key_exists( $first, $snippets ) && preg_match( $pat1, $snippets[$first] )
&& $offsets[$first] < $contextchars * 2 ) {
- $snippets = array( $first => $snippets[$first] );
+ $snippets = [ $first => $snippets[$first] ];
}
// calc by how much to extend existing snippets
$extract .= '<b> ... </b>';
}
- $processed = array();
+ $processed = [];
foreach ( $terms as $term ) {
if ( !isset( $processed[$term] ) ) {
$pat3 = "/$patPre(" . $term . ")$patPost/ui"; // highlight word
$tolerance = 10;
$s = max( 0, $point - $tolerance );
$l = min( strlen( $text ), $point + $tolerance ) - $s;
- $m = array();
+ $m = [];
if ( preg_match(
'/[ ,.!?~!@#$%^&*\(\)+=\-\\\|\[\]"\'<>]/',
continue; // this line already highlighted
}
- $m = array();
+ $m = [];
if ( !preg_match( $pattern, $line, $m, PREG_OFFSET_CAPTURE ) ) {
continue;
}
$text = preg_replace( "/\\[\\[([^|]+?)\\]\\]/", "\\1", $text );
$text = preg_replace_callback(
"/\\[\\[([^|]+\\|)(.*?)\\]\\]/",
- array( $this, 'linkReplace' ),
+ [ $this, 'linkReplace' ],
$text
);
$text = preg_replace( "/<\/?[^>]+>/", "", $text );
* Simple & fast snippet extraction, but gives completely unrelevant
* snippets
*
+ * Used when $wgAdvancedSearchHighlighting is false.
+ *
* @param string $text
- * @param array $terms
+ * @param array $terms Escaped for regex by SearchDatabase::regexTerm()
* @param int $contextlines
* @param int $contextchars
* @return string
break;
}
++$lineno;
- $m = array();
+ $m = [];
if ( !preg_match( $pat1, $line, $m ) ) {
continue;
}
* @return string
*/
public function highlightNone( $text, $contextlines, $contextchars ) {
- $match = array();
+ $match = [];
$text = ltrim( $text ) . "\n"; // make sure the preg_match may find the last line
$text = str_replace( "\n\n", "\n", $text ); // remove empty lines
preg_match( "/^(.*\n){0,$contextlines}/", $text, $match );
- $text = htmlspecialchars( substr( trim( $match[0] ), 0, $contextlines * $contextchars ) ); // trim and limit to max number of chars
+
+ // Trim and limit to max number of chars
+ $text = htmlspecialchars( substr( trim( $match[0] ), 0, $contextlines * $contextchars ) );
return str_replace( "\n", '<br>', $text );
}
}