"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "includes/search/SearchHighlighter.php" between
mediawiki-1.31.1.tar.gz and mediawiki-1.32.0.tar.gz

About: MediaWiki is a wiki engine (the collaborative editing software that runs for e.g. Wikipedia, the free encyclopedia).

SearchHighlighter.php  (mediawiki-1.31.1):SearchHighlighter.php  (mediawiki-1.32.0)
skipping to change at line 24 skipping to change at line 24
* *
* You should have received a copy of the GNU General Public License along * You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., * with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html * http://www.gnu.org/copyleft/gpl.html
* *
* @file * @file
* @ingroup Search * @ingroup Search
*/ */
use MediaWiki\MediaWikiServices;
/** /**
* Highlight bits of wikitext * Highlight bits of wikitext
* *
* @ingroup Search * @ingroup Search
*/ */
class SearchHighlighter { class SearchHighlighter {
protected $mCleanWikitext = true; protected $mCleanWikitext = true;
/** /**
* @warning If you pass false to this constructor, then * @warning If you pass false to this constructor, then
skipping to change at line 52 skipping to change at line 54
* Wikitext highlighting when $wgAdvancedSearchHighlighting = true * Wikitext highlighting when $wgAdvancedSearchHighlighting = true
* *
* @param string $text * @param string $text
* @param array $terms Terms to highlight (not html escaped but * @param array $terms Terms to highlight (not html escaped but
* regex escaped via SearchDatabase::regexTerm()) * regex escaped via SearchDatabase::regexTerm())
* @param int $contextlines * @param int $contextlines
* @param int $contextchars * @param int $contextchars
* @return string * @return string
*/ */
public function highlightText( $text, $terms, $contextlines, $contextchar s ) { public function highlightText( $text, $terms, $contextlines, $contextchar s ) {
global $wgContLang, $wgSearchHighlightBoundaries; global $wgSearchHighlightBoundaries;
if ( $text == '' ) { if ( $text == '' ) {
return ''; return '';
} }
// spli text into text + templates/links/tables // spli text into text + templates/links/tables
$spat = "/(\\{\\{)|(\\[\\[[^\\]:]+:)|(\n\\{\\|)"; $spat = "/(\\{\\{)|(\\[\\[[^\\]:]+:)|(\n\\{\\|)";
// first capture group is for detecting nested templates/links/ta bles/references // first capture group is for detecting nested templates/links/ta bles/references
$endPatterns = [ $endPatterns = [
1 => '/(\{\{)|(\}\})/', // template 1 => '/(\{\{)|(\}\})/', // template
skipping to change at line 87 skipping to change at line 89
$count = 0; // sequence number to maintain ordering $count = 0; // sequence number to maintain ordering
while ( $start < $textLen ) { while ( $start < $textLen ) {
// find start of template/image/table // find start of template/image/table
if ( preg_match( $spat, $text, $matches, PREG_OFFSET_CAPT URE, $start ) ) { if ( preg_match( $spat, $text, $matches, PREG_OFFSET_CAPT URE, $start ) ) {
$epat = ''; $epat = '';
foreach ( $matches as $key => $val ) { foreach ( $matches as $key => $val ) {
if ( $key > 0 && $val[1] != -1 ) { if ( $key > 0 && $val[1] != -1 ) {
if ( $key == 2 ) { if ( $key == 2 ) {
// see if this is an imag e link // see if this is an imag e link
$ns = substr( $val[0], 2, -1 ); $ns = substr( $val[0], 2, -1 );
if ( $wgContLang->getNsIn if (
dex( $ns ) != NS_FILE ) { MediaWikiServices
::getInstance()->getContentLanguage()->
getNsIndex( $ns )
!= NS_FILE
) {
break; break;
} }
} }
$epat = $endPatterns[$key]; $epat = $endPatterns[$key];
$this->splitAndAdd( $textExt, $co unt, substr( $text, $start, $val[1] - $start ) ); $this->splitAndAdd( $textExt, $co unt, substr( $text, $start, $val[1] - $start ) );
$start = $val[1]; $start = $val[1];
break; break;
} }
} }
skipping to change at line 316 skipping to change at line 321
} }
} }
/** /**
* Do manual case conversion for non-ascii chars * Do manual case conversion for non-ascii chars
* *
* @param array $matches * @param array $matches
* @return string * @return string
*/ */
function caseCallback( $matches ) { function caseCallback( $matches ) {
global $wgContLang;
if ( strlen( $matches[0] ) > 1 ) { if ( strlen( $matches[0] ) > 1 ) {
return '[' . $wgContLang->lc( $matches[0] ) . $wgContLang $contLang = MediaWikiServices::getInstance()->getContentL
->uc( $matches[0] ) . ']'; anguage();
return '[' . $contLang->lc( $matches[0] ) .
$contLang->uc( $matches[0] ) . ']';
} else { } else {
return $matches[0]; return $matches[0];
} }
} }
/** /**
* Extract part of the text from start to end, but by * Extract part of the text from start to end, but by
* not chopping up words * not chopping up words
* @param string $text * @param string $text
* @param int $start * @param int $start
* @param int $end * @param int $end
* @param int &$posStart (out) actual start position * @param int|null &$posStart (out) actual start position
* @param int &$posEnd (out) actual end position * @param int|null &$posEnd (out) actual end position
* @return string * @return string
*/ */
function extract( $text, $start, $end, &$posStart = null, &$posEnd = null ) { function extract( $text, $start, $end, &$posStart = null, &$posEnd = null ) {
if ( $start != 0 ) { if ( $start != 0 ) {
$start = $this->position( $text, $start, 1 ); $start = $this->position( $text, $start, 1 );
} }
if ( $end >= strlen( $text ) ) { if ( $end >= strlen( $text ) ) {
$end = strlen( $text ); $end = strlen( $text );
} else { } else {
$end = $this->position( $text, $end ); $end = $this->position( $text, $end );
skipping to change at line 483 skipping to change at line 489
* the target is category or image, leave it * the target is category or image, leave it
* *
* @param array $matches * @param array $matches
* @return string * @return string
*/ */
function linkReplace( $matches ) { function linkReplace( $matches ) {
$colon = strpos( $matches[1], ':' ); $colon = strpos( $matches[1], ':' );
if ( $colon === false ) { if ( $colon === false ) {
return $matches[2]; // replace with caption return $matches[2]; // replace with caption
} }
global $wgContLang;
$ns = substr( $matches[1], 0, $colon ); $ns = substr( $matches[1], 0, $colon );
$index = $wgContLang->getNsIndex( $ns ); $index = MediaWikiServices::getInstance()->getContentLanguage()-> getNsIndex( $ns );
if ( $index !== false && ( $index == NS_FILE || $index == NS_CATE GORY ) ) { if ( $index !== false && ( $index == NS_FILE || $index == NS_CATE GORY ) ) {
return $matches[0]; // return the whole thing return $matches[0]; // return the whole thing
} else { } else {
return $matches[2]; return $matches[2];
} }
} }
/** /**
* Simple & fast snippet extraction, but gives completely unrelevant * Simple & fast snippet extraction, but gives completely unrelevant
* snippets * snippets
* *
* Used when $wgAdvancedSearchHighlighting is false. * Used when $wgAdvancedSearchHighlighting is false.
* *
* @param string $text * @param string $text
* @param array $terms Escaped for regex by SearchDatabase::regexTerm() * @param array $terms Escaped for regex by SearchDatabase::regexTerm()
* @param int $contextlines * @param int $contextlines
* @param int $contextchars * @param int $contextchars
* @return string * @return string
*/ */
public function highlightSimple( $text, $terms, $contextlines, $contextch ars ) { public function highlightSimple( $text, $terms, $contextlines, $contextch ars ) {
global $wgContLang;
$lines = explode( "\n", $text ); $lines = explode( "\n", $text );
$terms = implode( '|', $terms ); $terms = implode( '|', $terms );
$max = intval( $contextchars ) + 1; $max = intval( $contextchars ) + 1;
$pat1 = "/(.*)($terms)(.{0,$max})/i"; $pat1 = "/(.*)($terms)(.{0,$max})/i";
$lineno = 0; $lineno = 0;
$extract = ""; $extract = "";
$contLang = MediaWikiServices::getInstance()->getContentLanguage( );
foreach ( $lines as $line ) { foreach ( $lines as $line ) {
if ( 0 == $contextlines ) { if ( 0 == $contextlines ) {
break; break;
} }
++$lineno; ++$lineno;
$m = []; $m = [];
if ( !preg_match( $pat1, $line, $m ) ) { if ( !preg_match( $pat1, $line, $m ) ) {
continue; continue;
} }
--$contextlines; --$contextlines;
// truncate function changes ... to relevant i18n message . // truncate function changes ... to relevant i18n message .
$pre = $wgContLang->truncate( $m[1], - $contextchars, '.. .', false ); $pre = $contLang->truncateForVisual( $m[1], - $contextcha rs, '...', false );
if ( count( $m ) < 3 ) { if ( count( $m ) < 3 ) {
$post = ''; $post = '';
} else { } else {
$post = $wgContLang->truncate( $m[3], $contextcha rs, '...', false ); $post = $contLang->truncateForVisual( $m[3], $con textchars, '...', false );
} }
$found = $m[2]; $found = $m[2];
$line = htmlspecialchars( $pre . $found . $post ); $line = htmlspecialchars( $pre . $found . $post );
$pat2 = '/(' . $terms . ")/i"; $pat2 = '/(' . $terms . ")/i";
$line = preg_replace( $pat2, "<span class='searchmatch'>\ \1</span>", $line ); $line = preg_replace( $pat2, "<span class='searchmatch'>\ \1</span>", $line );
$extract .= "${line}\n"; $extract .= "${line}\n";
} }
 End of changes. 12 change blocks. 
14 lines changed or deleted 19 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)