r75862 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r75861‎ | r75862 | r75863 >
Date:17:56, 2 November 2010
Author:mah
Status:resolved (Comments)
Tags:brion 
Comment:
re r75046 - implement highlighting in a (hopefully) less ugly way
Modified paths:
  • /trunk/extensions/RSS/RSS.php (modified) (history)

Diff [purge]

Index: trunk/extensions/RSS/RSS.php
@@ -138,7 +138,8 @@
139139
140140 # Get highlight terms from argument array
141141 if ( isset( $args['highlight'] ) ) {
142 - $this->highlight = self::explodeOnSpaces( $args['highlight'] );
 142+ # mapping to lowercase here so the regex can be case insensitive below.
 143+ $this->highlight = array_flip( array_map( 'strtolower', self::explodeOnSpaces( $args['highlight'] ) ) );
143144 }
144145
145146 # Get filter terms from argument array
@@ -396,36 +397,31 @@
397398 return false;
398399 }
399400
 401+ static function highlightThis( $term, $match ) {
 402+ $styleStart = "<span style='font-weight: bold; background: none repeat scroll 0%% 0%% rgb(%s); color: %s;'>";
 403+ $styleEnd = "</span>";
400404
401 - function highlightTerms( $text ) {
402 - $i = 0;
403 - $starttag = 'v8x5u3t3u8h';
404 - $endtag = 'q8n4f6n4n4x';
 405+ # bg colors cribbed from Google's highlighting of search teerms
 406+ $bgcolor = array( '255, 255, 102', '160, 255, 255', '153, 255, 153',
 407+ '255, 153, 153', '255, 102, 255', '136, 0, 0', '0, 170, 0', '136, 104, 0',
 408+ '0, 70, 153', '153, 0, 153' );
 409+ # Spelling out the fg colors instead of using processing time to create this list
 410+ $color = array("black", "black", "black", "black", "black",
 411+ "white", "white", "white", "white", "white" );
405412
406 - $color[] = 'coral';
407 - $color[] = 'greenyellow';
408 - $color[] = 'lightskyblue';
409 - $color[] = 'gold';
410 - $color[] = 'violet';
411 - $count_color = count( $color );
 413+ $index = $term[strtolower($match[0])] % count( $bgcolor );
412414
413 - foreach ( $this->highlight as $term ) {
414 - if ( $term ) {
415 - $text = preg_replace( "/\b(\w*?" . preg_quote( $term, '/' ) . "\w*?)\b/i", "$starttag" . "_" . $i . "\\1$endtag", $text );
416 - $i++;
417 - if ( $i == $count_color ) {
418 - $i = 0;
419 - }
420 - }
421 - }
 415+ return sprintf($styleStart, $bgcolor[$index], $color[$index]). $match[0] .$styleEnd;
 416+ }
422417
423 - # To avoid trouble should someone wants to highlight the terms "span", "style", …
424 - for ( $i = 0; $i < 5; $i++ ) {
425 - $text = preg_replace( "/$starttag" . "_" . preg_quote( $i, '/' ) . "/",
426 - "<span style=\"background-color:" . $color[$i] . "; font-weight: bold;\">", $text );
427 - $text = preg_replace( "|$endtag|", '</span>', $text );
 418+ function highlightTerms( $text ) {
 419+ if ( count( $this->highlight ) === 0 ) {
 420+ return $text;
428421 }
 422+ # SIGH ... anonymous functions are not available until 5.3
 423+ $f = create_function('$match', '$term = '.var_export($this->highlight, true).'; return RSS::highlightThis($term, $match);');
429424
430 - return $text;
 425+ $highlight = '/'. implode( "|", array_map( 'preg_quote', array_keys( $this->highlight ) ) ) . '/i';
 426+ return preg_replace_callback( $highlight, $f, $text );
431427 }
432428 }
\ No newline at end of file

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r75046* Use [[Template:RSSPost]] (and [[Mediawiki:Rss-item]]) (or another...mah21:54, 19 October 2010

Comments

#Comment by Brion VIBBER (talk | contribs)   03:24, 8 February 2011

create_function is an eval() equivalent, aka "tool of the devil". Replace with a cleaner callback...

#Comment by Brion VIBBER (talk | contribs)   03:27, 8 February 2011

This was done in r76053. Yay!

#Comment by Brion VIBBER (talk | contribs)   03:28, 8 February 2011

I would recommend changing the horrid explicit 'style' attribute with sensible classes, or at least being less verbose. But these don't block.

Status & tagging log