r84199 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r84198‎ | r84199 | r84200 >
Date:20:21, 17 March 2011
Author:bawolff
Status:ok
Tags:
Comment:
Change how the getKeywords code works in this extension slightly
*Use wfMsgForContent, since it shouldn't change with userlang.
*Change it to use Title objects instead of doing its own parsing on pagename's with strreplace.
I imagine that will make it more robust in general.
*Make it return an array, instead of comma seperated list (Seemed more correct, but thats debatable)
Modified paths:
  • /trunk/extensions/GoogleNewsSitemap/FeedSMItem.php (modified) (history)
  • /trunk/extensions/GoogleNewsSitemap/GoogleNewsSitemap_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/GoogleNewsSitemap/GoogleNewsSitemap_body.php
@@ -115,6 +115,10 @@
116116 /**
117117 * Get the cached version of the feed if possible.
118118 * Checks to see if the cached version is still valid.
 119+ *
 120+ * Note, this doesn't take into account changes to the keyword
 121+ * mapping message (See getKeywords). I don't think that's a major issue.
 122+ *
119123 * @param $key String Cache key
120124 * @param $invalidInfo String String to check if cache is clean from getCacheInvalidationInfo.
121125 * @return Mixed String or Boolean: The cached feed, or false.
@@ -509,42 +513,55 @@
510514 /**
511515 * Given a title, figure out what keywords. Use the message googlenewssitemap_categorymap
512516 * to map local categories to Google News Keywords.
 517+ *
 518+ * The format of this message is *Category name|What to map to
 519+ * or *Category name|__MASK__ to hide the category completely.
 520+ *
513521 * @see http://www.google.com/support/news_pub/bin/answer.py?answer=116037
514522 * @param Title $title
515 - * @return String Comma separated list of keywords
 523+ * @return Array of String: list of keywords
516524 */
517525 function getKeywords ( $title ) {
518526 $cats = $title->getParentCategories();
519 - $str = '';
 527+ $res = array();
520528
521529 # the following code is based (stolen) from r56954 of flagged revs.
522530 $catMap = array();
523531 $catMask = array();
524 - $msg = wfMsg( 'googlenewssitemap_categorymap' );
525532 if ( !wfEmptyMsg( 'googlenewssitemap_categorymap' ) ) {
 533+ $msg = wfMsgForContent( 'googlenewssitemap_categorymap' );
526534 $list = explode( "\n*", "\n$msg" );
527535 foreach ( $list as $item ) {
528536 $mapping = explode( '|', $item, 2 );
529537 if ( count( $mapping ) == 2 ) {
530 - if ( trim( $mapping[1] ) == '__MASK__' ) {
531 - $catMask[trim( $mapping[0] )] = true;
 538+
 539+ $targetTitle = Title::newFromText( $mapping[0], NS_CATEGORY );
 540+ if ( !$targetTitle ) {
 541+ continue;
 542+ }
 543+ $target = $targetTitle->getPrefixedDBkey();
 544+ $mapTo = trim( $mapping[1] );
 545+
 546+ if ( $mapTo === '__MASK__' ) {
 547+ $catMask[$target] = true;
532548 } else {
533 - $catMap[trim( $mapping[0] )] = trim( $mapping[1] );
 549+ $catMap[$target] = $mapTo;
534550 }
535551 }
536552 }
537553 }
538 - foreach ( $cats as $key => $val ) {
539 - $cat = str_replace( '_', ' ', trim( substr( $key, strpos( $key, ':' ) + 1 ) ) );
540 - if ( !isset( $catMask[$cat] ) ) {
541 - if ( isset( $catMap[$cat] ) ) {
542 - $str .= ', ' . str_replace( '_', ' ', trim ( $catMap[$cat] ) );
543 - } else {
544 - $str .= ', ' . $cat;
545 - }
 554+ foreach ( $cats as $cat => $val ) {
 555+ if ( !isset( $catMask[$cat] ) ) {
 556+ if ( isset( $catMap[$cat] ) ) {
 557+ // Its mapped.
 558+ $res[] = $catMap[$cat];
 559+ } else {
 560+ // No mapping, so use just page name sans namespace.
 561+ $cTitle = Title::newFromText( $cat );
 562+ $res[] = $cTitle->getText();
546563 }
 564+ }
547565 }
548 - $str = substr( $str, 2 ); # to remove leading ', '
549 - return $str;
 566+ return $res;
550567 }
551568 }
Index: trunk/extensions/GoogleNewsSitemap/FeedSMItem.php
@@ -8,7 +8,7 @@
99 **/
1010 class FeedSMItem extends FeedItem {
1111
12 - private $keywords = '';
 12+ private $keywords = array();
1313
1414 /**
1515 * @var Title
@@ -18,7 +18,7 @@
1919 /**
2020 * @param Title $title Title object that this entry is for.
2121 * @param String $pubDate Publish date formattable by wfTimestamp.
22 - * @param String $keywords Comma separated list of keywords
 22+ * @param Array $keywords list of (String) keywords
2323 * @param Mixed Boolean or Integer. Namespace containing comments page for entry.
2424 * True for the corresponding talk page of $title
2525 * False for none
@@ -72,7 +72,10 @@
7373 }
7474
7575 public function getKeywords() {
76 - return $this->xmlEncode( $this->keywords );
 76+ // Note, not using $wgContLang->commaList, as this is for
 77+ // computers not humans, so we don't want to vary with
 78+ // language conventions.
 79+ return $this->xmlEncode( implode( ', ', $this->keywords ) );
7780 }
7881
7982 /**

Status & tagging log