Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.git] / includes / search / SearchHighlighter.php
index c3c3a8f..2bd1955 100644 (file)
@@ -45,8 +45,6 @@ class SearchHighlighter {
        public function highlightText( $text, $terms, $contextlines, $contextchars ) {
                global $wgContLang, $wgSearchHighlightBoundaries;
 
-               $fname = __METHOD__;
-
                if ( $text == '' ) {
                        return '';
                }
@@ -54,20 +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 .= '|(<ref>)'; // references via cite extension
                        $endPatterns[4] = '/(<ref>)|(<\/ref>)/';
                }
                $spat .= '/';
-               $textExt = array(); // text extracts
-               $otherExt = array(); // other extracts
-               wfProfileIn( "$fname-split" );
+               $textExt = []; // text extracts
+               $otherExt = []; // other extracts
                $start = 0;
                $textLen = strlen( $text );
                $count = 0; // sequence number to maintain ordering
@@ -132,15 +130,13 @@ class SearchHighlighter {
 
                $all = $textExt + $otherExt; // these have disjunct key sets
 
-               wfProfileOut( "$fname-split" );
-
                // prepare regexps
                foreach ( $terms as $index => $term ) {
                        // manually do upper/lowercase stuff for utf-8 since PHP won't do it
                        if ( preg_match( '/[\x80-\xff]/', $term ) ) {
                                $terms[$index] = preg_replace_callback(
                                        '/./us',
-                                       array( $this, 'caseCallback' ),
+                                       [ $this, 'caseCallback' ],
                                        $terms[$index]
                                );
                        } else {
@@ -163,12 +159,10 @@ class SearchHighlighter {
                $pat1 = "/(" . $phrase . ")/ui";
                $pat2 = "/$patPre(" . $anyterm . ")$patPost/ui";
 
-               wfProfileIn( "$fname-extract" );
-
                $left = $contextlines;
 
-               $snippets = array();
-               $offsets = array();
+               $snippets = [];
+               $offsets = [];
 
                // show beginning only if it contains all words
                $first = 0;
@@ -208,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 ) ) {
@@ -220,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 ) {
@@ -277,7 +271,7 @@ class SearchHighlighter {
                        $extract .= '<b> ... </b>';
                }
 
-               $processed = array();
+               $processed = [];
                foreach ( $terms as $term ) {
                        if ( !isset( $processed[$term] ) ) {
                                $pat3 = "/$patPre(" . $term . ")$patPost/ui"; // highlight word
@@ -287,8 +281,6 @@ class SearchHighlighter {
                        }
                }
 
-               wfProfileOut( "$fname-extract" );
-
                return $extract;
        }
 
@@ -370,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(
                        '/[ ,.!?~!@#$%^&*\(\)+=\-\\\|\[\]"\'<>]/',
@@ -416,7 +408,7 @@ class SearchHighlighter {
                                continue; // this line already highlighted
                        }
 
-                       $m = array();
+                       $m = [];
                        if ( !preg_match( $pattern, $line, $m, PREG_OFFSET_CAPTURE ) ) {
                                continue;
                        }
@@ -451,30 +443,19 @@ class SearchHighlighter {
         * @return mixed
         */
        function removeWiki( $text ) {
-               $fname = __METHOD__;
-               wfProfileIn( $fname );
-
-               // $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 );
                $text = preg_replace( "/''/", "", $text );
 
-               wfProfileOut( $fname );
                return $text;
        }
 
@@ -512,7 +493,6 @@ class SearchHighlighter {
         */
        public function highlightSimple( $text, $terms, $contextlines, $contextchars ) {
                global $wgContLang;
-               $fname = __METHOD__;
 
                $lines = explode( "\n", $text );
 
@@ -523,13 +503,12 @@ class SearchHighlighter {
                $lineno = 0;
 
                $extract = "";
-               wfProfileIn( "$fname-extract" );
                foreach ( $lines as $line ) {
                        if ( 0 == $contextlines ) {
                                break;
                        }
                        ++$lineno;
-                       $m = array();
+                       $m = [];
                        if ( !preg_match( $pat1, $line, $m ) ) {
                                continue;
                        }
@@ -551,7 +530,6 @@ class SearchHighlighter {
 
                        $extract .= "${line}\n";
                }
-               wfProfileOut( "$fname-extract" );
 
                return $extract;
        }
@@ -565,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", '<br>', $text );
        }
 }