X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fsearch%2FSearchHighlighter.php;h=5087e8d585b6e14673ad10712463a31f08aeb24b;hb=e3bd13db0c285f312e31bb1b7271af4628cca80c;hp=255d005cc405d0735a6fc193313efb20bce5fbfb;hpb=dfc27758488a54368c795b669c40690c3204db3f;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/search/SearchHighlighter.php b/includes/search/SearchHighlighter.php index 255d005cc4..2bd19558a2 100644 --- a/includes/search/SearchHighlighter.php +++ b/includes/search/SearchHighlighter.php @@ -45,8 +45,6 @@ class SearchHighlighter { public function highlightText( $text, $terms, $contextlines, $contextchars ) { global $wgContLang, $wgSearchHighlightBoundaries; - $fname = __METHOD__; - if ( $text == '' ) { return ''; } @@ -54,19 +52,20 @@ 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 - if ( function_exists( 'wfCite' ) ) { + // instead of hardcoding a class name from the Cite extension + if ( class_exists( 'Cite' ) ) { $spat .= '|()'; // references via cite extension $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 @@ -137,7 +136,7 @@ class SearchHighlighter { if ( preg_match( '/[\x80-\xff]/', $term ) ) { $terms[$index] = preg_replace_callback( '/./us', - array( $this, 'caseCallback' ), + [ $this, 'caseCallback' ], $terms[$index] ); } else { @@ -162,8 +161,8 @@ class SearchHighlighter { $left = $contextlines; - $snippets = array(); - $offsets = array(); + $snippets = []; + $offsets = []; // show beginning only if it contains all words $first = 0; @@ -203,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 ) ) { @@ -215,11 +214,11 @@ 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 - $targetchars = intval( ( $contextchars * $contextlines ) / count ( $snippets ) ); + $targetchars = intval( ( $contextchars * $contextlines ) / count( $snippets ) ); } foreach ( $snippets as $index => $line ) { @@ -272,7 +271,7 @@ class SearchHighlighter { $extract .= ' ... '; } - $processed = array(); + $processed = []; foreach ( $terms as $term ) { if ( !isset( $processed[$term] ) ) { $pat3 = "/$patPre(" . $term . ")$patPost/ui"; // highlight word @@ -363,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( '/[ ,.!?~!@#$%^&*\(\)+=\-\\\|\[\]"\'<>]/', @@ -409,7 +408,7 @@ class SearchHighlighter { continue; // this line already highlighted } - $m = array(); + $m = []; if ( !preg_match( $pattern, $line, $m, PREG_OFFSET_CAPTURE ) ) { continue; } @@ -444,23 +443,14 @@ class SearchHighlighter { * @return mixed */ function removeWiki( $text ) { - $fname = __METHOD__; - - // $text = preg_replace( "/'{2,5}/", "", $text ); - // $text = preg_replace( "/\[[a-z]+:\/\/[^ ]+ ([^]]+)\]/", "\\2", $text ); - // $text = preg_replace( "/\[\[([^]|]+)\]\]/", "\\1", $text ); - // $text = preg_replace( "/\[\[([^]]+\|)?([^|]]+)\]\]/", "\\2", $text ); - // $text = preg_replace( "/\\{\\|(.*?)\\|\\}/", "", $text ); - // $text = preg_replace( "/\\[\\[[A-Za-z_-]+:([^|]+?)\\]\\]/", "", $text ); $text = preg_replace( "/\\{\\{([^|]+?)\\}\\}/", "", $text ); $text = preg_replace( "/\\{\\{([^|]+\\|)(.*?)\\}\\}/", "\\2", $text ); $text = preg_replace( "/\\[\\[([^|]+?)\\]\\]/", "\\1", $text ); $text = preg_replace_callback( "/\\[\\[([^|]+\\|)(.*?)\\]\\]/", - array( $this, 'linkReplace' ), + [ $this, 'linkReplace' ], $text ); - // $text = preg_replace("/\\[\\[([^|]+\\|)(.*?)\\]\\]/", "\\2", $text); $text = preg_replace( "/<\/?[^>]+>/", "", $text ); $text = preg_replace( "/'''''/", "", $text ); $text = preg_replace( "/('''|<\/?[iIuUbB]>)/", "", $text ); @@ -503,7 +493,6 @@ class SearchHighlighter { */ public function highlightSimple( $text, $terms, $contextlines, $contextchars ) { global $wgContLang; - $fname = __METHOD__; $lines = explode( "\n", $text ); @@ -519,7 +508,7 @@ class SearchHighlighter { break; } ++$lineno; - $m = array(); + $m = []; if ( !preg_match( $pat1, $line, $m ) ) { continue; } @@ -554,11 +543,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 ); } }