User:Sachein/Bullet Feed

From MediaWiki.org

Jump to: navigation, search

           

Manual on MediaWiki Extensions
List of MediaWiki Extensions
Crystal Clear action run.png
Bullet Feed

Release status: Beta

Implementation  Tag, Page action
Description Create Feeds for your website.
Author(s)  Cory Perry & Sachein (cperryTalk)
Last Version  1.2b (2/5/08)
MediaWiki  Tested on 1.9.1 and 1.11.1
License No license specified
Download Extension:Bullet Feed#Code
Example  Feed

Contents

[edit] What can this extension do?

Create Feeds for your website.

[edit] Why I did this extension

I couldn't find one which did! The ones I did find were outdated.

[edit] Usage

<bullet_feed title=BBC Online News desc=Latest Five Reports From BBC>
*[http://www.tamileelamonline/en Fishes cannot swim!]
<rss-desc>Latest discoveries have found that some fished cannot swim.</rss-desc> 
*[http://www.tamileelamonline/en War with out Witness
<rss-desc>There is a genocide happening in Sri Lanka against Tamils just for asking for a separate homeland</rss-desc> 
</bullet_feed>

Pay attention! this is an normal site: http://news.bbc.co.uk/1/hi/england/london/Faulty_TV_caused_tower_block_fire

So we have to do this [http://news.bbc.co.uk/1/hi/england/london/ Faulty_TV_caused_tower_block_fire]

[edit] Installation

To install this extension, add the following to LocalSettings.php:

require_once("$IP/extensions/bullet_feed.php");

[edit] Code

<?php
/*
*  Bullet_feed, by Cory Perry with the help of Sachein.  Made to keep the formatting 
*  on the main page of the hdwiki while still parsing the data
*  into an xml file built on the fly and accessed in real-time
*
*  Usage:
*  <bullet_feed title="My Title" desc="My Feed's Description">
*  *Title of item #1
*  <rss-desc>Description of news item</rss-desc>
*  *Title of item #2
*  *Title of [[item #3]]
*  <rss-desc>This item has an automatic link</rss-desc>
*  </bullet_feed>
*/
require_once("$IP/includes/Feed.php");
require_once("$IP/includes/Sanitizer.php");
$wgExtensionCredits['parserhook'][] = array(
    'name' => 'Bullet Feed',
    'version' => '1.2b',
    'author' => 'Cory Perry & Sachein',
    'url' => 'http://www.mediawiki.org/wiki/User:Sachein/Bullet_Feed',
    'description' => 'Allows a minimalistic bullet-style news page thrown into a simple RSS feed',
    'descriptionmsg' => 'bulletfeed-desc',
);
$wgHooks['UnknownAction'][]='feedAction';
$wgExtensionFunctions[] = 'feedSetup';
/*feedSetup:
* Setup the tag parsing hook functions.
*/
function feedSetup() {
    global $wgParser;
    $wgParser->setHook( 'bullet_feed', 'feedParse' );
    $wgParser->setHook('rss-url','urlParse');
    $wgParser->setHook('rss-desc','descParse');
}
/*descParse:
*Parse the <rss-desc> tags and output 
*the html code in comments for parsing later in the action field.
*Used to grab the description of the news item.
*Params:
*$input: text inclosed by the wiki tags
*$args: any arguments passed to the tags, ignored here.
*$parser: pointer to the parser class that holds all 
*the tag/extension modifiers (usually $wgOut)
*/
function descParse( $input, $args, $parser ) {
    return '<!-- rss-desc text="'.$parser->recursiveTagParse($input).'" -->';
}
/*urlParse *UNIMPLEMENTED*:
*Parse the <url-desc> tags and output 
*the html code in comments for parsing later in the action field
*Used to grab a direct URL from the wiki tags. 
*Params:
*$input: text inclosed by the wiki tags
*$args: any arguments passed to the tags, ignored here.
*$parser: pointer to the parser class that holds all 
*the tag/extension modifiers (usually $wgOut)
*/
function urlParse( $input, $args, $parser ) {
    return '<!-- rss-url text="'.$input.'" -->';
}
$glob_match=array();
/*feedParse :
*Surrounds the RSS feed in enclosing comment fields for easy parsing.
*Also adds a bit of html code for supported browsers to redirect to 
*the RSS feed from the main page.
*Params:
*$input: text inclosed by the wiki tags
*$args: any arguments passed to the tags, like title="", desc="" for 
*the site.  All others are added but ignored in the RSS feed action.
*$parser: pointer to the parser class that holds all 
*the tag/extension modifiers (usually $wgOut)
*/
function feedParse( $input, $args, $parser ) {
    global $wgServer, $wgScript, $wgTitle;
    $str=$parser->recursiveTagParse($input); //Get html code of page
    //vv-- Make-up a header that includes the redirect feature.
    $header='<link rel="alternate" type="application/rss+xml" title="News Feed" href="'.$wgTitle->getLocalUrl( 'action=bullet_feed' ).'" /><!-- FEED_START -->'.'<!-- ';
    foreach($args as $name=>$value){	//Add the arguments as comments to the code.
    	$header.=$name.'="'.$value.'" ';
    }
    $header.='-->';
    return $header.$str.'<!-- FEED_END -->';	//Return the fully parsed html code where the tags were before.
}
/*feedAction:
*Core code for outputting the actual RSS xml file on the fly.
*Called on by http://mysite.ext/index.php?title=my_page&action=bullet_feed
*Params:
*$action: action and arguments sent via address. (arguments ignored).
*$article: Entire article object to be parsed.
*/
function feedAction($action,$article){
	global $wgOut,$wgScriptPath,$wgServer;
	if(!($action=='bullet_feed')){ return true;}
	//Parse the article into html and strip out the table of contents and edit sections
	$content = $wgOut->parse($article->getContent()."\n__NOEDITSECTION__ __NOTOC__");
	//Get the page address for use as a default link.
	preg_match('/^http:\/\/(.*?)\//s',$article->getTitle()->getFullUrl(),$site);
	$site='http://'.$site[1].$wgScriptPath;
	//Cut out all the extra stuff and grab the RSS arguments within the html code.
	preg_match_all('/<!--\\s*FEED_START\\s*-->(.*?)<!--\\s*FEED_END\\s*-->/s',$content,$matches);
	preg_match('/<!--(?:.*?)title=\"(.*?)\"/s',$matches[1][0],$Feedtitle);
	preg_match('/<!--(?:.*?)desc=\"(.*?)\"/s',$matches[1][0],$Feeddesc);
	foreach($matches[1] as $feedKey=>$feed){
		//Make a new standard RSSFeed object to use to output the xml
		$feedStream = new RSSFeed($Feedtitle[1],$Feeddesc[1], $site);
		$feedStream->outHeader();
		//Split the code into news items.
		$titles=preg_split('/<li>(.*?)/s',$feed);
		foreach($titles as $titlekey=>$title){
			if(preg_match('/[A-Za-z1-9]+/',strip_tags($title))==0) continue;
			$link=$article->getTitle()->getFullUrl();
			//If there's a link in the title, use that as the RSS link,
			//otherwise, use the default site link.
			preg_match('/<!--(?:.*?)rss-desc text=\"(.*?)\" -->/s',$title,$desc);
			$title=str_replace($desc,'',$title);
			if(preg_match('/href=\"(.*?)\"/',$title)>0){
				preg_match('/href="(.*?)"/',htmlspecialchars_decode($title),$temp);
				$link=$temp[1];
				if(preg_match('/^\/(.*?)/',$link)){
					$link=$wgServer.$link;
				}
			}
			//Grab the description of the item.
			$i=new FeedItem(strip_tags($title),$desc[1],$link.strip_tags($title),$Date=wfTimestampNow()-$titlekey);
			$feedStream->outItem($i);
		}
		$feedStream->outFooter();
		die();//<--Not sure if needed
	}
	return false;//<--Don't let wiki parse this article further.
}