r110207 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r110206‎ | r110207 | r110208 >
Date:14:23, 28 January 2012
Author:liangent
Status:reverted (Comments)
Tags:miscextensions 
Comment:
Do language variant conversion in feeds
Modified paths:
  • /trunk/extensions/FeaturedFeeds/ApiFeaturedFeeds.php (modified) (history)
  • /trunk/extensions/FeaturedFeeds/FeaturedFeeds.body.php (modified) (history)
  • /trunk/extensions/FeaturedFeeds/SpecialFeedItem.php (modified) (history)

Diff [purge]

Index: trunk/extensions/FeaturedFeeds/ApiFeaturedFeeds.php
@@ -15,6 +15,7 @@
1616 }
1717
1818 public function execute() {
 19+ global $wgContLang;
1920 wfProfileIn( __METHOD__ );
2021
2122 $params = $this->extractRequestParams();
@@ -31,7 +32,12 @@
3233 wfProfileOut( __METHOD__ );
3334 $this->dieUsage( 'Invalid language code', 'language-invalid' );
3435 }
35 - $feeds = FeaturedFeeds::getFeeds( $language );
 36+ $variant = isset( $params['variant'] ) ? $params['variant'] : false;
 37+ if ( $variant !== false && !in_array( $variant, $wgContLang->getVariants() ) ) {
 38+ wfProfileOut( __METHOD__ );
 39+ $this->dieUsage( 'Invalid variant code', 'variant-invalid' );
 40+ }
 41+ $feeds = FeaturedFeeds::getFeeds( $language, $variant );
3642 $ourFeed = $feeds[$params['feed']];
3743
3844 $feedClass = new $wgFeedClasses[$params['feedformat']] (
@@ -52,7 +58,7 @@
5359 public function getAllowedParams() {
5460 global $wgFeedClasses;
5561 $feedFormatNames = array_keys( $wgFeedClasses );
56 - $availableFeeds = array_keys( FeaturedFeeds::getFeeds( false ) );
 62+ $availableFeeds = array_keys( FeaturedFeeds::getFeeds( false, false ) );
5763 return array (
5864 'feedformat' => array(
5965 ApiBase::PARAM_DFLT => 'rss',
@@ -64,7 +70,10 @@
6571 ),
6672 'language' => array(
6773 ApiBase::PARAM_TYPE => 'string',
68 - )
 74+ ),
 75+ 'variant' => array(
 76+ ApiBase::PARAM_TYPE => 'string',
 77+ ),
6978 );
7079 }
7180
@@ -72,7 +81,8 @@
7382 return array(
7483 'feedformat' => 'The format of the feed',
7584 'feed' => 'Feed name',
76 - 'language' => 'Feed language code. Ignored by some feeds.'
 85+ 'language' => 'Feed language code. Ignored by some feeds.',
 86+ 'variant' => 'Feed variant code.',
7787 );
7888 }
7989
@@ -84,6 +94,7 @@
8595 return array_merge( parent::getPossibleErrors(), array(
8696 array( 'code' => 'feed-invalid', 'info' => 'Invalid subscription feed type' ),
8797 array( 'code' => 'language-invalid', 'info' => 'Invalid language code' ),
 98+ array( 'code' => 'variant-invalid', 'info' => 'Invalid variant code' ),
8899 ) );
89100 }
90101
@@ -91,7 +102,7 @@
92103 global $wgVersion;
93104 // attempt to find a valid feed name
94105 // if none available, just use an example value
95 - $availableFeeds = array_keys( FeaturedFeeds::getFeeds( false ) );
 106+ $availableFeeds = array_keys( FeaturedFeeds::getFeeds( false, false ) );
96107 $feed = reset( $availableFeeds );
97108 if ( !$feed ) {
98109 $feed = 'featured';
Index: trunk/extensions/FeaturedFeeds/FeaturedFeeds.body.php
@@ -7,27 +7,31 @@
88 * Returns the list of feeds
99 *
1010 * @param $langCode string|bool Code of language to use or false if default
 11+ * @param $variantCode string|bool Code of variant to use or false if default or empty string if don't convert
1112 * @return array Feeds in format of 'name' => array of FeedItem
1213 */
13 - public static function getFeeds( $langCode ) {
14 - global $wgMemc, $wgLangCode;
 14+ public static function getFeeds( $langCode, $variantCode ) {
 15+ global $wgMemc, $wgLangCode, $wgContLang;
1516
1617 if ( !$langCode || self::allInContentLanguage() ) {
1718 $langCode = $wgLangCode;
1819 }
 20+ if ( $variantCode === false ) {
 21+ $variantCode = $wgContLang->getPreferredVariant();
 22+ }
1923 static $cache = array();
20 - if ( isset( $cache[$langCode] ) ) {
21 - return $cache[$langCode];
 24+ if ( isset( $cache[$langCode][$variantCode] ) ) {
 25+ return $cache[$langCode][$variantCode];
2226 }
2327
24 - $key = self::getCacheKey( $langCode );
 28+ $key = self::getCacheKey( $langCode, $variantCode );
2529 $feeds = $wgMemc->get( $key );
2630
2731 if ( !$feeds ) {
28 - $feeds = self::getFeedsInternal( $langCode );
 32+ $feeds = self::getFeedsInternal( $langCode, $variantCode );
2933 $wgMemc->set( $key, $feeds, self::getMaxAge() );
3034 }
31 - $cache[$langCode] = $feeds;
 35+ $cache[$langCode][$variantCode] = $feeds;
3236 return $feeds;
3337 }
3438
@@ -36,8 +40,8 @@
3741 * @param String $langCode: Feed language code
3842 * @return String
3943 */
40 - private static function getCacheKey( $langCode ) {
41 - return wfMemcKey( 'featured-feeds', $langCode );
 44+ private static function getCacheKey( $langCode, $variantCode ) {
 45+ return wfMemcKey( 'featured-feeds', $langCode, $variantCode );
4246 }
4347
4448 /**
@@ -83,9 +87,13 @@
8488 * @return bool
8589 */
8690 public static function beforePageDisplay( OutputPage &$out ) {
87 - global $wgAdvertisedFeedTypes;
 91+ global $wgAdvertisedFeedTypes, $wgContLang;
8892 if ( $out->getTitle()->isMainPage() ) {
89 - foreach ( self::getFeeds( $out->getLanguage()->getCode() ) as $feed ) {
 93+ $feeds = self::getFeeds(
 94+ $out->getLanguage()->getCode(),
 95+ $wgContLang->getPreferredVariant()
 96+ );
 97+ foreach ( $feeds as $feed ) {
9098 foreach ( $wgAdvertisedFeedTypes as $type ) {
9199 $out->addLink( array(
92100 'rel' => 'alternate',
@@ -106,10 +114,13 @@
107115 * @return Boolean
108116 */
109117 public static function skinTemplateOutputPageBeforeExec( &$sk, &$tpl ) {
110 - global $wgDisplayFeedsInSidebar, $wgAdvertisedFeedTypes;
 118+ global $wgDisplayFeedsInSidebar, $wgAdvertisedFeedTypes, $wgContLang;
111119
112120 if ( $wgDisplayFeedsInSidebar && $sk->getContext()->getTitle()->isMainPage() ) {
113 - $feeds = self::getFeeds( $sk->getContext()->getLanguage()->getCode() );
 121+ $feeds = self::getFeeds(
 122+ $sk->getContext()->getLanguage()->getCode(),
 123+ $wgContLang->getPreferredVariant()
 124+ );
114125 $links = array();
115126 $format = $wgAdvertisedFeedTypes[0]; // @fixme:
116127 foreach ( $feeds as $feed ) {
@@ -163,7 +174,7 @@
164175 * @return array
165176 * @throws MWException
166177 */
167 - private static function getFeedsInternal( $langCode ) {
 178+ private static function getFeedsInternal( $langCode, $variantCode ) {
168179 wfProfileIn( __METHOD__ );
169180 $feedDefs = self::getFeedDefinitions();
170181
@@ -171,7 +182,7 @@
172183 $requestedLang = Language::factory( $langCode );
173184 $parser = new Parser();
174185 foreach ( $feedDefs as $name => $opts ) {
175 - $feed = new FeaturedFeedChannel( $name, $opts, $requestedLang );
 186+ $feed = new FeaturedFeedChannel( $name, $opts, $requestedLang, $variantCode );
176187 if ( !$feed->isOK() ) {
177188 continue;
178189 }
@@ -252,7 +263,7 @@
253264 public $shortTitle;
254265 public $description;
255266
256 - public function __construct( $name, $options, $lang ) {
 267+ public function __construct( $name, $options, $lang, $variant ) {
257268 global $wgContLang;
258269 if ( !self::$parserOptions ) {
259270 self::$parserOptions = new ParserOptions();
@@ -266,6 +277,7 @@
267278 } else {
268279 $this->language = $wgContLang;
269280 }
 281+ $this->variant = $variant;
270282 }
271283
272284 private function msg( $key ) {
@@ -286,13 +298,19 @@
287299 }
288300
289301 public function init() {
290 - global $wgLanguageCode;
 302+ global $wgLanguageCode, $wgContLang;
291303 if ( $this->title !== false ) {
292304 return;
293305 }
294306 $this->title = $this->msg( $this->options['title'] )->text();
295307 $this->shortTitle = $this->msg( $this->options['short-title'] );
296308 $this->description = $this->msg( $this->options['description'] )->text();
 309+ // Convert the messages if the content language has variants.
 310+ if ( $wgContLang->hasVariants() && $this->variant ) {
 311+ $this->title = $wgContLang->mConverter->convertTo( $this->title, $this->variant );
 312+ $this->shortTitle = $wgContLang->mConverter->convertTo( $this->shortTitle, $this->variant );
 313+ $this->description = $wgContLang->mConverter->convertTo( $this->description, $this->variant );
 314+ }
297315 $pageMsg = $this->msg( $this->options['page'] )->params( $this->language->getCode() );
298316 if ( $pageMsg->isDisabled() ) {
299317 // fall back manually, messages can be existent but empty
@@ -330,6 +348,7 @@
331349 * @return FeaturedFeedItem
332350 */
333351 public function getFeedItem( $date ) {
 352+ global $wgContLang;
334353 self::$parserOptions->setTimestamp( $date );
335354 self::$parserOptions->setUserLang( $this->language );
336355
@@ -347,16 +366,20 @@
348367 return false;
349368 }
350369 $text = self::$parser->parse( $text, $title, self::$parserOptions )->getText();
351 - $url = SpecialPage::getTitleFor( 'FeedItem' ,
 370+ $special = SpecialPage::getTitleFor( 'FeedItem' ,
352371 $this->name . '/' . wfTimestamp( TS_MW, $date ) . '/' . $this->language->getCode()
353 - )->getFullURL();
 372+ );
 373+ $entry = self::$parser->transformMsg( $this->entryName, self::$parserOptions );
 374+ if ( $wgContLang->hasVariants() && $this->variant ) {
 375+ $text = $wgContLang->mConverter->convertTo( $text, $this->variant );
 376+ $entry = $wgContLang->mConverter->convertTo( $entry, $this->variant );
 377+ // bug 34010. otherwise variant specified in the second argument can be ignored.
 378+ $url = $special->getFullURL( array( 'variant' => $this->variant ) );
 379+ } else {
 380+ $url = $special->getFullURL();
 381+ }
354382
355 - return new FeaturedFeedItem(
356 - self::$parser->transformMsg( $this->entryName, self::$parserOptions ),
357 - wfExpandUrl( $url ),
358 - $text,
359 - $date
360 - );
 383+ return new FeaturedFeedItem( $entry, wfExpandUrl( $url ), $text, $date );
361384 }
362385
363386 /**
@@ -376,6 +399,9 @@
377400 if ( $this->options['inUserLanguage'] && $this->language->getCode() != $wgContLang->getCode() ) {
378401 $options['language'] = $this->language->getCode();
379402 }
 403+ if ( $wgContLang->hasVariants() && $this->variant ) {
 404+ $options['variant'] = $this->variant;
 405+ }
380406 return wfScript( 'api' ) . '?' . wfArrayToCGI( $options );
381407 }
382408 }
Index: trunk/extensions/FeaturedFeeds/SpecialFeedItem.php
@@ -15,7 +15,7 @@
1616 return;
1717 }
1818 list( $feedName, $date, $langCode ) = $parts;
19 - $feeds = FeaturedFeeds::getFeeds( $langCode );
 19+ $feeds = FeaturedFeeds::getFeeds( $langCode, '' );
2020 if ( !isset( $feeds[$feedName] ) ) {
2121 $out->showErrorPage( 'error', 'ffeed-feed-not-found', array( $feedName ) );
2222 return;
@@ -54,8 +54,10 @@
5555 }
5656
5757 private function displayItem( FeaturedFeedItem $item ) {
 58+ global $wgContLang;
5859 $out = $this->getOutput();
59 - $out->setPageTitle( $item->getRawTitle() );
 60+ // The language converter converts page content automatically but not page title.
 61+ $out->setPageTitle( $wgContLang->convert( $item->getRawTitle() ) );
6062 $out->addHTML( $item->getRawText() );
6163 }
6264 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r110478*temporary* revert of r110207 and r110271, will recommit with my fixes latermaxsem14:22, 1 February 2012

Comments

#Comment by MaxSem (talk | contribs)   11:05, 29 January 2012
  • Is this separation between language and variant really needed, e.g. why have &language=zh&variant=zh-tw instead of &language=zh-tw?
  • Please use accessors e.g. $language->getConverter() instead of $language->mConverter.

Status & tagging log