X-Git-Url: https://git.cyclocoop.org/%27.WWW_URL.%27admin/?a=blobdiff_plain;f=includes%2Fsearch%2FSearchHighlighter.php;h=dd41a6e1e4e0fe2e438b0891acad6538c7586b13;hb=48df0714bbfe0d5cf3ced3de9081c02d583d78c3;hp=7d5d38f22b3dd6e032ef9bf8518640748399d319;hpb=59ebff658ce912c1b0e7ef8d8f9bfec5a4e17b39;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/search/SearchHighlighter.php b/includes/search/SearchHighlighter.php index 7d5d38f22b..dd41a6e1e4 100644 --- a/includes/search/SearchHighlighter.php +++ b/includes/search/SearchHighlighter.php @@ -34,10 +34,11 @@ class SearchHighlighter { } /** - * 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 @@ -52,10 +53,10 @@ class SearchHighlighter { // 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 @@ -64,8 +65,8 @@ class SearchHighlighter { $endPatterns[4] = '/()|(<\/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 @@ -136,7 +137,7 @@ class SearchHighlighter { if ( preg_match( '/[\x80-\xff]/', $term ) ) { $terms[$index] = preg_replace_callback( '/./us', - array( $this, 'caseCallback' ), + [ $this, 'caseCallback' ], $terms[$index] ); } else { @@ -145,7 +146,6 @@ class SearchHighlighter { } $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, @@ -161,8 +161,8 @@ class SearchHighlighter { $left = $contextlines; - $snippets = array(); - $offsets = array(); + $snippets = []; + $offsets = []; // show beginning only if it contains all words $first = 0; @@ -202,7 +202,7 @@ class SearchHighlighter { } // 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 ) ) { @@ -214,7 +214,7 @@ class SearchHighlighter { // 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 @@ -271,7 +271,7 @@ class SearchHighlighter { $extract .= ' ... '; } - $processed = array(); + $processed = []; foreach ( $terms as $term ) { if ( !isset( $processed[$term] ) ) { $pat3 = "/$patPre(" . $term . ")$patPost/ui"; // highlight word @@ -362,7 +362,7 @@ class SearchHighlighter { $tolerance = 10; $s = max( 0, $point - $tolerance ); $l = min( strlen( $text ), $point + $tolerance ) - $s; - $m = array(); + $m = []; if ( preg_match( '/[ ,.!?~!@#$%^&*\(\)+=\-\\\|\[\]"\'<>]/', @@ -408,7 +408,7 @@ class SearchHighlighter { continue; // this line already highlighted } - $m = array(); + $m = []; if ( !preg_match( $pattern, $line, $m, PREG_OFFSET_CAPTURE ) ) { continue; } @@ -448,7 +448,7 @@ class SearchHighlighter { $text = preg_replace( "/\\[\\[([^|]+?)\\]\\]/", "\\1", $text ); $text = preg_replace_callback( "/\\[\\[([^|]+\\|)(.*?)\\]\\]/", - array( $this, 'linkReplace' ), + [ $this, 'linkReplace' ], $text ); $text = preg_replace( "/<\/?[^>]+>/", "", $text ); @@ -485,8 +485,10 @@ class SearchHighlighter { * 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 @@ -508,7 +510,7 @@ class SearchHighlighter { break; } ++$lineno; - $m = array(); + $m = []; if ( !preg_match( $pat1, $line, $m ) ) { continue; } @@ -543,11 +545,13 @@ class SearchHighlighter { * @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", '
', $text ); } }