Extension:RSS feed for recent changes

From MediaWiki.org
Jump to: navigation, search
Note: If you just want an rss feed for Recent Changes, that is already available without any extensions. For example: http://mediawiki.org/w/index.php?title=Special:RecentChanges&feed=rss
MediaWiki extensions manual - list
Crystal Clear action run.png
RSS feeds for the results of a search term

Release status: beta

Description The extension allows you to get the RSS feeds for the results of a search term.
Author(s) Sushant Mehta
MediaWiki 1.11
License No license specified
Download No link

Check usage (experimental)

Usage: http://example.test/w/index.php?title=Special:SearchFeed&term=foo&limit=100

Contents

[edit] Installation

  1. Place SearchFeed.i18n.php ,SearchFeed.php and SearchFeed_body.php extension files in the extensions/SearchFeed directory.
  2. Save the file in the extensions directory of mediawiki folder.
  3. Add to LocalSettings.php the line
    require_once ("extensions/SearchFeed/SearchFeed.php");
    

The extension needs the following three files.

[edit] SearchFeed.i18n.php

<?php
$allMessages = array(
        'en' => array(
                'searchfeed' => 'SearchFeed',
        ),
        'et' => array(
                'searchfeed'=>  'Muutuste voog',
        )
);
?>

[edit] SearchFeed.php

<?php
# Not a valid entry point, skip unless MEDIAWIKI is defined
if (!defined('MEDIAWIKI')) {
                exit( 1 );
}

$wgAutoloadClasses['SearchFeed'] = dirname(__FILE__) . '/SearchFeed_body.php';
$wgSpecialPages['SearchFeed'] = 'SearchFeed';
$wgHooks['LoadAllMessages'][] = 'wfSearchLoadMessages';
#'SearchFeed::loadMessages';
#$wgHooks['LangugeGetSpecialPageAliases'][] = 'SearchFeedLocalizedPageName';

function wfSearchLoadMessages() {
        (SearchFeed::loadMessages());
        return true;
}

?>

[edit] SearchFeed_body.php

<?php
class SearchFeed extends SpecialPage
{
        function SearchFeed() {
                SpecialPage::SpecialPage("SearchFeed");
                self::loadMessages(); 

        }

        function execute( $par ) {
                global $wgRequest, $wgOut, $wgParser, $wgUser;
                                global $wgFeedClasses, $wgTitle, $wgSitename, $wgContLang, $wgContLanguageCode;

                                $feedFormat = $wgRequest->getVal( 'feed', 'rss' );

                                if( !isset( $wgFeedClasses[$feedFormat] ) ) {
                                        wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
                                        return false;
                                }

                                $feedTitle = $wgSitename . ' - ' . ' [' . $wgContLanguageCode . ']';
                                $feed = new $wgFeedClasses[$feedFormat](
                                        $feedTitle,
                                        htmlspecialchars( '' ),
                                        $wgTitle->getFullUrl() );

                $term = $wgRequest->getText('term','');
                $limit = $wgRequest->getText('limit','50');

                                $feed->outHeader();

                                $offset = 0;
                                $namespaces = array_keys(SearchEngine::searchableNamespaces());
                                $popts = new ParserOptions();
                                $popts->setEditSection(false);
                                $popts->setAllowSpecialInclusion(false);
//                              $popts->setInterwikiMagic(true);
                                $popts->setAllowExternalImages(true);
                                $popts->setTidy(true);

                        $search = SearchEngine::create();
                                $search->setLimitOffset( $limit, $offset );
                                $search->setNamespaces( $namespaces );
                                $search->showRedirects = true; //$this->searchRedirects

                                $textMatches = $search->searchText( $term );

                                while($result = $textMatches->next()) {
                                        $title = $result->getTitle();

                                        $revision = Revision::newFromTitle( $title );
                                        $wikitext = $revision->getRawText();

                                        $lines = explode( "\n", $wikitext );

                                        $contextlines = $wgUser->getOption( 'contextlines',  5 );
                                        $contextchars = $wgUser->getOption( 'contextchars', 120 );

                                        $max = intval( $contextchars ) + 1;
                                        $pat1 = "/(.*)($term)(.{0,$max})/i";

                                        $lineno = 0;

                                        $extract = '';
                                        foreach ( $lines as $line ) {
                                                if ( 0 == $contextlines ) {
                                                        break;
                                                }
                                                ++$lineno;
                                                $m = array();
                                                if ( ! preg_match( $pat1, $line, $m ) ) {
                                                        continue;
                                                }
                                                --$contextlines;
                                                $pre = $wgContLang->truncate( $m[1], -$contextchars, '...' );

                                                if ( count( $m ) < 3 ) {
                                                        $post = '';
                                                } else {
                                                        $post = $wgContLang->truncate( $m[3], $contextchars, '...' );
                                                }

                                                $found = $m[2];

                                                $line = htmlspecialchars( $pre . $found . $post );
//                                              $pat2 = '/(' . $terms . ")/i";
//                                              $line = preg_replace( $pat2, "\\1", $line );

                                                if(!empty($line))
                                                        $extract .= "\n{$line}\n";
                                        }

//                                      $lc = SearchEngine::legalSearchChars() . '&#;';
//                                      $extract = preg_replace( "/[^{$lc}]+/", " ", $extract );
//                                      $extract = preg_replace( "/''[']*/", " ", $extract );

                                        $parserOutput = $wgParser->parse( $extract, $title, $popts,
                                                1, true, $revision->getId());
                                        $text = $parserOutput->getText();

                                        $text = strip_tags($text, "<br><p>");
//                                      $text = preg_replace( "/\<img.*?\>/i" ,' ', $text);
//                                      $text = preg_replace( "/\<a \>(.*?)\<\/a\>/i" ,"\\1", $text);

                                        $talkpage = $title->getTalkPage();
                                        $item = new FeedItem(
                                                $title->getPrefixedText(),
                                                $text,
                                                $title->getFullURL(),
                                                $revision->getTimestamp(),
                                                $revision->getRawUserText(),
                                                $talkpage->getFullURL()
                                                );
                                        $feed->outItem( $item );
                                }

                                $feed->outFooter();

                                exit;
        }

        function loadMessages() {
                static $messagesLoaded = false;
                global $wgMessageCache;
                if ( $messagesLoaded ) return;
                $messagesLoaded = true;

                require( dirname( __FILE__ ) . '/SearchFeed.i18n.php' );
                #echo $allMessages; prints>> Array
                foreach ( $allMessages as $lang => $langMessages ) {

                    $wgMessageCache->addMessages( $langMessages,$lang);

                }
                return;
        }
}
?>
Personal tools
Namespaces
Variants
Actions
Site
Support
Download
Development
Communication
Print/export
Toolbox