r75831 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r75830‎ | r75831 | r75832 >
Date:21:14, 1 November 2010
Author:mah
Status:ok (Comments)
Tags:
Comment:
* Add member $displayFields so that we don't highlight and/or filter
on stuff the user will never see.
* Clean up and fix canDisplay() and filter() methods so they actually
works.
Modified paths:
  • /trunk/extensions/RSS/RSS.php (modified) (history)

Diff [purge]

Index: trunk/extensions/RSS/RSS.php
@@ -68,6 +68,7 @@
6969 protected $lastModified;
7070 protected $xml;
7171 protected $ERROR;
 72+ protected $displayFields = array( 'author', 'title', 'encodedContent', 'description' );
7273
7374 public $client;
7475
@@ -308,6 +309,12 @@
309310 $output = "";
310311 if ( count( $parts ) > 1 && isset( $parser ) && isset( $frame ) ) {
311312 $rendered = array();
 313+ foreach ( $this->displayFields as $field ) {
 314+ if ( isset( $item[$field] ) ) {
 315+ $item[$field] = $this->highlightTerms( $item[$field] );
 316+ }
 317+ }
 318+
312319 foreach ( $parts as $part ) {
313320 $bits = explode( '=', $part );
314321 $left = null;
@@ -317,7 +324,8 @@
318325 }
319326
320327 if ( isset( $item[$left] ) ) {
321 - $leftValue = preg_replace( '#{{{' . $left . '}}}#', $item[$left], $bits[1] );
 328+ $leftValue = preg_replace( '#{{{' . $left . '}}}#',
 329+ $item[$left], $bits[1] );
322330 $rendered[] = implode( '=', array( $left, $leftValue ) );
323331 } else {
324332 $rendered[] = $part;
@@ -343,7 +351,8 @@
344352 if ( $this->rss && !$this->rss->ERROR ) {
345353 $this->etag = $this->client->getResponseHeader( 'Etag' );
346354 $this->lastModified = $this->client->getResponseHeader( 'Last-Modified' );
347 - wfDebugLog( 'RSS', 'Stored etag (' . $this->etag . ') and Last-Modified (' . $this->lastModified . ') and items (' . count( $this->rss->items ) . ')!' );
 355+ wfDebugLog( 'RSS', 'Stored etag (' . $this->etag . ') and Last-Modified (' .
 356+ $this->lastModified . ') and items (' . count( $this->rss->items ) . ')!' );
348357 $this->storeInCache( $key );
349358
350359 return Status::newGood();
@@ -353,7 +362,17 @@
354363 }
355364
356365 function canDisplay( $item ) {
357 - if ( $this->filter( $item['description'], 'filterOut' ) ) {
 366+ $check = "";
 367+ foreach ( $this->displayFields as $field ) {
 368+ if ( isset( $item[$field] ) ) {
 369+ $check .= $item[$field];
 370+ }
 371+ }
 372+
 373+ if ( $this->filter( $check, 'filterOut' ) ) {
 374+ return false;
 375+ }
 376+ if ( $this->filter( $check, 'filter' ) ) {
358377 return true;
359378 }
360379 return false;
@@ -361,28 +380,20 @@
362381
363382 function filter( $text, $filterType ) {
364383 if ( $filterType === 'filterOut' ) {
365 - $keep = false;
366384 $filter = $this->filterOut;
367385 } else {
368 - $keep = true;
369386 $filter = $this->filter;
370387 }
371388
372 - if ( count( $filter ) == 0 ) return !$keep;
 389+ if ( count( $filter ) == 0 ) return $filterType !== 'filterOut';
373390
374 - foreach ( $filter as $term ) {
375 - if ( $term ) {
376 - $match = preg_match( "|$term|i", $text );
 391+ $match = preg_match( '#(' . implode( "|", $filter ) . ')#i', $text );
377392 if ( $match ) {
378 - return $keep;
 393+ return true;
379394 }
 395+ return false;
380396 }
381 - return !$keep;
382 - }
383397
384 - }
385 -
386 -
387398 function highlightTerms( $text ) {
388399 $i = 0;
389400 $starttag = 'v8x5u3t3u8h';

Comments

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

The commit comment seems a little baffling, but the code seems to make reasonable sense. :)

Note there are changes not mentioned in the comment, such as changing filtering from just on description to all listed fields, which is what baffled me in context of "so that we don't highlight and/or filter on stuff the user will never see." It looks like the listed fields may still get highlighted even if not used though.

Status & tagging log