Extension:Bullet Feed

From MediaWiki.org

Jump to: navigation, search
Manual on MediaWiki Extensions
List of MediaWiki Extensions
Bullet Feed

Release status: beta

Implementation Tag, Page action
Description Allows a minimalistic bullet-style news page thrown into a simple RSS feed.
Author(s) Cory Perry (cperry Talk)
Version 1.2b (2/5/08)
MediaWiki Tested on 1.9.1 and 1.11.1
Download Extension:Bullet Feed#Code
Example
Bullet Feed Example
Bullet Feed Example
Hooks used

UnknownAction

Contents

[edit] What can this extension do?

Adds an action to pages that allows a bulleted list to be displayed as an RSS feed for easy updating and formatting on, for example, a main page. Links are not required for items, and if no link is given, the default link is http://yoursite.com/index.php?title=my_page#MY%20ITEM%20TITLE substituting where needed. This project was initially intended for a minimalistic view of the news page that could be posted on the main page without needing to reformat the page. Such example main pages would be http://hdwiki.nmu.edu.

[edit] Usage

<bullet_feed title=TITLE desc=DESC>
*Item1 [link]                    <!-- Link Optional -->
<rss-desc>ITEM DESCRIPTION</rss-desc> <!-- Optional -->
</bullet_feed>

Enclose the bulleted-list in the tags <bullet-feed> and </bullet-feed>, with the descriptions listed in between the tags <rss-desc> and </rss-desc>. This extension automatically puts an alternative link in the source of the page, so the orange RSS icon shown in Firefox or Internet Explorer 7 will revert to the RSS link, or you can access it directly via: http://yoursite.com/index.php?title=my_page&action=bullet-feed.

[edit] Example

<bullet_feed title="My News Feed" desc="A description of my news feed and what it does.">
* Item with [http://mysite.com link]
* Item without link
* Item with a description
<rss-desc>This is an example of an item with a description.</rss-desc>
* ''Item with other formatting options''
</bullet_feed>

[edit] Download instructions

Please cut and paste the code found below and place it in $IP/extensions/bullet_feed.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

[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 (cperry@nmu.edu).  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");
$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="'.$wgServer.$wgScript.'?title='.$wgTitle->getDBKey().'&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.
}
?>

[edit] Thanks

  • Christopher Finke for helping me out with some issues with his RSS Ticker, an add-on for firefox
  • The immense contributors here on the MediaWiki extensions, your code helped understand the wiki much better to accomplish the goal.

[edit] See also

Personal tools