Index: trunk/extensions/FeaturedFeeds/ApiFeaturedFeeds.php |
— | — | @@ -0,0 +1,81 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +class ApiFeaturedFeeds extends ApiBase { |
| 5 | + public function __construct( $main, $action ) { |
| 6 | + parent::__construct( $main, $action ); |
| 7 | + } |
| 8 | + |
| 9 | + /** |
| 10 | + * This module uses a custom feed wrapper printer. |
| 11 | + * |
| 12 | + * @return ApiFormatFeedWrapper |
| 13 | + */ |
| 14 | + public function getCustomPrinter() { |
| 15 | + return new ApiFormatFeedWrapper( $this->getMain() ); |
| 16 | + } |
| 17 | + |
| 18 | + public function execute() { |
| 19 | + $params = $this->extractRequestParams(); |
| 20 | + |
| 21 | + global $wgFeedClasses; |
| 22 | + |
| 23 | + if ( !isset( $wgFeedClasses[$params['feedformat']] ) ) { |
| 24 | + $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' ); |
| 25 | + } |
| 26 | + |
| 27 | + $feeds = FeaturedFeeds::getFeeds( false ); |
| 28 | + $ourFeed = $feeds[$params['channel']]; |
| 29 | + |
| 30 | + $feedClass = new $wgFeedClasses[$params['feedformat']] ( |
| 31 | + $ourFeed['name'], |
| 32 | + $ourFeed['description'], |
| 33 | + FeaturedFeeds::getFeedURL( $ourFeed, $params['feedformat'] ) |
| 34 | + ); |
| 35 | + |
| 36 | + ApiFormatFeedWrapper::setResult( $this->getResult(), $feedClass, $ourFeed['entries'] ); |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + public function getAllowedParams() { |
| 41 | + global $wgFeedClasses; |
| 42 | + $feedFormatNames = array_keys( $wgFeedClasses ); |
| 43 | + $availableFeeds = array_keys( FeaturedFeeds::getFeeds( false ) ); |
| 44 | + return array ( |
| 45 | + 'feedformat' => array( |
| 46 | + ApiBase::PARAM_DFLT => 'rss', |
| 47 | + ApiBase::PARAM_TYPE => $feedFormatNames |
| 48 | + ), |
| 49 | + 'channel' => array( |
| 50 | + ApiBase::PARAM_TYPE => $availableFeeds, |
| 51 | + ApiBase::PARAM_REQUIRED => true, |
| 52 | + ), |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + public function getParamDescription() { |
| 57 | + return array( |
| 58 | + 'feedformat' => 'The format of the feed', |
| 59 | + 'channel' => 'Feed channel', |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + public function getDescription() { |
| 64 | + return 'Returns a user contributions feed'; |
| 65 | + } |
| 66 | + |
| 67 | + public function getPossibleErrors() { |
| 68 | + return array_merge( parent::getPossibleErrors(), array( |
| 69 | + array( 'code' => 'feed-invalid', 'info' => 'Invalid subscription feed type' ), |
| 70 | + ) ); |
| 71 | + } |
| 72 | + |
| 73 | + public function getExamples() { |
| 74 | + return array( |
| 75 | + 'api.php?action=featuredfeed&channel=featured', //@todo |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + public function getVersion() { |
| 80 | + return __CLASS__ . ': $Id$'; |
| 81 | + } |
| 82 | +} |
\ No newline at end of file |
Property changes on: trunk/extensions/FeaturedFeeds/ApiFeaturedFeeds.php |
___________________________________________________________________ |
Added: svn:keywords |
1 | 83 | + Id |
Added: svn:eol-style |
2 | 84 | + native |
Index: trunk/extensions/FeaturedFeeds/FeaturedFeedsWMF.php |
— | — | @@ -0,0 +1,40 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/* |
| 5 | + * Feed settings for WMF projects |
| 6 | + */ |
| 7 | + |
| 8 | +$wgHooks['FeaturedFeeds::getFeeds'][] = 'wfFeaturedFeedsWMF_getFeeds'; |
| 9 | + |
| 10 | +function wfFeaturedFeedsWMF_getFeeds( &$feeds ) { |
| 11 | + global $wgConf, $wgDBname; |
| 12 | + list( $site, $lang ) = $wgConf->siteFromDB( $wgDBname ); |
| 13 | + switch ( $site ) { |
| 14 | + case 'wikipedia': |
| 15 | + $feeds += array( |
| 16 | + 'featured' => array( |
| 17 | + 'page' => 'ffeed-wp-featured-page', |
| 18 | + 'feedName' => 'ffeed-wp-featured-title', |
| 19 | + 'description' => 'ffeed-wp-featured-desc', |
| 20 | + 'entryName' => 'ffeed-wp-featured-entry', |
| 21 | + ), |
| 22 | + 'onthisday' => array( |
| 23 | + 'page' => 'ffeed-wp-onthisday-page', |
| 24 | + 'feedName' => 'ffeed-wp-onthisday-title', |
| 25 | + 'description' => 'ffeed-wp-onthisday-desc', |
| 26 | + 'entryName' => 'ffeed-wp-onthisday-entry', |
| 27 | + ), |
| 28 | + ); |
| 29 | + break; |
| 30 | + case 'commons': |
| 31 | + $feeds['featured'] = array( |
| 32 | + 'page' => 'ffeed-com-featured-page', |
| 33 | + 'feedName' => 'ffeed-com-featured-title', |
| 34 | + 'description' => 'ffeed-com-featured-desc', |
| 35 | + 'entryName' => 'ffeed-com-featured-entry', |
| 36 | + 'inUserLanguage' => true, |
| 37 | + ); |
| 38 | + break; |
| 39 | + } |
| 40 | + return true; |
| 41 | +} |
Property changes on: trunk/extensions/FeaturedFeeds/FeaturedFeedsWMF.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 42 | + native |
Index: trunk/extensions/FeaturedFeeds/FeaturedFeeds.body.php |
— | — | @@ -0,0 +1,141 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +class FeaturedFeeds { |
| 5 | + |
| 6 | + /** |
| 7 | + * Returns the list of feeds |
| 8 | + * |
| 9 | + * @param $langCode string||bool Code of language to use or false if default |
| 10 | + * @return array Feeds in format of 'name' => array of FeedItem |
| 11 | + */ |
| 12 | + public static function getFeeds( $langCode ) { |
| 13 | + global $wgMemc, $wgContLang; |
| 14 | + |
| 15 | + if ( !$langCode ) { |
| 16 | + $langCode = $wgContLang->getCode(); |
| 17 | + } |
| 18 | + static $cache = array(); |
| 19 | + if ( isset( $cache[$langCode] ) ) { |
| 20 | + return $cache[$langCode]; |
| 21 | + } |
| 22 | + |
| 23 | + $key = wfMemcKey( 'featured-feeds', $langCode ); |
| 24 | + $feeds = $wgMemc->get( $key ); |
| 25 | + |
| 26 | + if ( !$feeds ) { |
| 27 | + $feeds = self::getFeedsInternal( $langCode ); |
| 28 | + $wgMemc->set( $key, $feeds, 3600 ); //FIXME |
| 29 | + } |
| 30 | + $cache[$langCode] = $feeds; |
| 31 | + return $feeds; |
| 32 | + } |
| 33 | + |
| 34 | + private static function getFeedsInternal( $langCode ) { |
| 35 | + global $wgFeaturedFeeds, $wgFeaturedFeedsDefaults, $wgContLang; |
| 36 | + |
| 37 | + $feedDefs = $wgFeaturedFeeds; |
| 38 | + wfRunHooks( 'FeaturedFeeds::getFeeds', array( &$feedDefs ) ); |
| 39 | + |
| 40 | + // fill defaults |
| 41 | + foreach ( $feedDefs as $name => $opts ) { |
| 42 | + foreach ( $wgFeaturedFeedsDefaults as $setting => $value ) { |
| 43 | + if ( !isset( $opts[$setting] ) ) { |
| 44 | + $feedDefs[$name][$setting] = $value; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + $feeds = array(); |
| 50 | + $parserOptions = new ParserOptions(); |
| 51 | + $title = Title::newMainPage(); |
| 52 | + $requestedLang = Language::factory( $langCode ); |
| 53 | + $parser = new Parser(); |
| 54 | + foreach ( $feedDefs as $name => $opts ) { |
| 55 | + $feed = array( 'channel' => $name ); |
| 56 | + |
| 57 | + $pageMsg = wfMessage( $opts['page'] )->inContentLanguage(); |
| 58 | + if ( $pageMsg->isDisabled() ) { |
| 59 | + continue; |
| 60 | + } |
| 61 | + $page = $pageMsg->plain(); |
| 62 | + |
| 63 | + $feed['inUserLanguage'] = $opts['inUserLanguage']; |
| 64 | + $lang = $opts['inUserLanguage'] ? $requestedLang : $wgContLang; |
| 65 | + $feed['language'] = $lang->getCode(); |
| 66 | + $feed['name'] = wfMessage( $opts['feedName'] )->inLanguage( $lang )->text(); |
| 67 | + $feed['description'] = wfMessage( $opts['description'] )->inLanguage( $lang )->text(); |
| 68 | + $entryName = wfMessage( $opts['entryName'] )->inLanguage( $lang )->plain(); |
| 69 | + $feed['entries'] = array(); |
| 70 | + |
| 71 | + $parserOptions->setUserLang( $lang ); |
| 72 | + for ( $i = 1 - $opts['limit']; $i <= 0; $i++ ) { |
| 73 | + $time = self::todaysStart() + $i * 24 * 3600; |
| 74 | + $parserOptions->setTimestamp( $time ); |
| 75 | + |
| 76 | + $titleText = $parser->transformMsg( $page, $parserOptions ); |
| 77 | + $title = Title::newFromText( $titleText ); |
| 78 | + if ( !$title ) { |
| 79 | + throw new MWException( "Invalid page name $titleText" ); |
| 80 | + } |
| 81 | + $rev = Revision::newFromTitle( $title ); |
| 82 | + if ( !$rev ) { |
| 83 | + continue; // page does not exist |
| 84 | + } |
| 85 | + $text = $rev->getText(); |
| 86 | + if ( !$text ) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + $text = $parser->parse( $text, $title, $parserOptions )->getText(); |
| 90 | + $feed['entries'][] = new FeedItem( |
| 91 | + $parser->transformMsg( $entryName, $parserOptions ), |
| 92 | + $text, |
| 93 | + wfExpandUrl( $title->getFullURL() ), |
| 94 | + $time |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + $feeds[$name] = $feed; |
| 99 | + } |
| 100 | + |
| 101 | + return $feeds; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Returns the Unix timestamp of current day's first second |
| 106 | + * |
| 107 | + * @return int Timestamp |
| 108 | + */ |
| 109 | + public static function todaysStart() { |
| 110 | + static $time = false; |
| 111 | + if ( !$time ) { |
| 112 | + $time = wfTimestamp( TS_UNIX, substr( wfTimestamp( TS_MW ), 0, 8 ) . '000000' ); |
| 113 | + } |
| 114 | + return $time; |
| 115 | + } |
| 116 | + |
| 117 | + public static function getFeedURL( $feed, $format ) { |
| 118 | + global $wgContLang; |
| 119 | + |
| 120 | + $options = array( |
| 121 | + 'action' => 'featuredfeed', |
| 122 | + 'channel' => $feed['channel'], |
| 123 | + 'feedformat' => $format, |
| 124 | + ); |
| 125 | + if ( $feed['inUserLanguage'] && $feed['language'] != $wgContLang->getCode() ) { |
| 126 | + $options['language'] = $feed['language']; |
| 127 | + } |
| 128 | + return wfScript( 'api' ) . '?' . wfArrayToCGI( $options ); |
| 129 | + } |
| 130 | + |
| 131 | + public static function beforePageDisplay( OutputPage &$out ) { |
| 132 | +// global $wgFeedClasses; |
| 133 | +// if ( $out->getTitle()->isMainPage() ) { |
| 134 | +// foreach ( self::getFeeds( $out->getLanguage()->getCode() ) as $name => $feed ) { |
| 135 | +// foreach ( array_keys( $wgFeedClasses ) as $format ) { |
| 136 | +// $out->addFeedLink( $format, FeaturedFeeds::getFeedURL( $feed, $format ) ); |
| 137 | +// } |
| 138 | +// } |
| 139 | +// } |
| 140 | + return true; |
| 141 | + } |
| 142 | +} |
\ No newline at end of file |
Property changes on: trunk/extensions/FeaturedFeeds/FeaturedFeeds.body.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 143 | + native |
Index: trunk/extensions/FeaturedFeeds/FeaturedFeeds.php |
— | — | @@ -0,0 +1,35 @@ |
| 2 | +<?php |
| 3 | +/* |
| 4 | + * Featured Feed extension by Max Semenik. |
| 5 | + * License: WTFPL 2.0 |
| 6 | + */ |
| 7 | + |
| 8 | + |
| 9 | +$wgExtensionCredits['other'][] = array( |
| 10 | + 'path' => __FILE__, |
| 11 | + 'name' => 'FeaturedFeeds', |
| 12 | + 'author' => array( 'Max Semenik' ), |
| 13 | + 'url' => '//mediawiki.org/wiki/Extension:FeaturedFeeds', |
| 14 | + 'descriptionmsg' => 'ffeed-desc', |
| 15 | +); |
| 16 | + |
| 17 | +$dir = dirname( __FILE__ ); |
| 18 | + |
| 19 | +$wgAutoloadClasses['FeaturedFeeds'] = "$dir/FeaturedFeeds.body.php"; |
| 20 | +$wgAutoloadClasses['ApiFeaturedFeeds'] = "$dir/ApiFeaturedFeeds.php"; |
| 21 | + |
| 22 | +$wgAPIModules['featuredfeed'] = 'ApiFeaturedFeeds'; |
| 23 | + |
| 24 | +$wgHooks['BeforePageDisplay'][] = 'FeaturedFeeds::beforePageDisplay'; |
| 25 | + |
| 26 | +/** |
| 27 | + * Configuration settings |
| 28 | + */ |
| 29 | + |
| 30 | +$wgFeaturedFeedsDefaults = array( |
| 31 | + 'limit' => 10, |
| 32 | + 'inUserLanguage' => false, |
| 33 | +); |
| 34 | + |
| 35 | +$wgFeaturedFeeds = array(); |
| 36 | + |
Property changes on: trunk/extensions/FeaturedFeeds/FeaturedFeeds.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 37 | + native |