Extension:RssExport

From MediaWiki.org

Jump to: navigation, search

         

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

Release status: beta

Implementation  Tag
Description This extension enables you to add rss export from your mediawiki site
Author(s)  Nikolai Gishkevich (black zorro)
Last Version  1.4
MediaWiki  Tested on 1.9.0
License No license specified
Download Узнай больше об плагине rss для mediawiki

check usage (experimental)

Contents

[edit] What can this extension do?

This extension enables you to add rss export from your mediawiki site. Your can select rss format (rss/atom). Rules for expire news items.

[edit] Usage

in your wiki page insert next tag
<rss-set>
  <rss-item>
   <date>2007.2.13 12:8:00</date>
   <title>Title Of News # 1</title>
   <body>Body of news # 1 </body>
  </rss-item>
  <rss-item>
   <date>2005.12.3 17:28:00</date>
   <title>Title Of News # 2</title>
   <body>Body of news # 2 </body>
  </rss-item>
</rss-set>

next, create new page (for example named RSS) and add to them this code:

@{rss kind="rss" count="10" encoding="utf-8" title="Hello Vasyan"}
* count = there your select count of news items on rss page
* kind = (rss|atom) = format of rss feed
* encoding = output encoding
* title = news feed title

last step, add this tag in your template file (skin file):

`skin file` is a `mediawiki/skins/MonoBook.(php|tpl)`
		  <link rel="alternate" type="application/rss+xml" title="Export Feed Articles" href="/mediawiki/index.php/Rss?go" />

Attention: adapt the href-URL relative to your server document root: newer versions need: href="/__Path_2_Wiki__/index.php5/Rss?go"


[edit] Installation

copy 2 files (rss_main.php and rss_setup.php into your extension dir)

[edit] Parameters

[edit] Changes to LocalSettings.php

require_once("$IP/extensions/rss/rss_setup.php");

[edit] Code

File rss_setup.php
 <?php
/*
'OutputPageBeforeHTML': a page has been processed by the parser and
the resulting HTML is about to be displayed.
$parserOutput: the parserOutput (object) that corresponds to the page
$text: the text that will be displayed, in HTML (string)
*/
 
include_once ('rss_main.php');
 
 
/*
$wgExtensionFunctions[] = "makeRSSTag_Setup";
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'makeRSSTag',
        'url' => 'http://black-zorro.jino-net.ru',
        'author' => 'black zorro',
        'description' => 'RSS ATOM feeds support',
);
 
function makeRSSTag_Setup() {
    global $wgParser;
    $wgParser->setHook( "rss-set", "makeRSSTag" );
}
 
*/
 
$wgHooks['OutputPageBeforeHTML'][] = 'makeRSSTag';
 
if (! function_exists('win2utf'))    {
	function win2utf($str)
	{
		$utf = "";
		for($i = 0; $i < strlen($str); $i++)
		{
			$donotrecode = false;
			$c = ord(substr($str, $i, 1));
			if ($c == 0xA8) $res = 0xD081;
			elseif ($c == 0xB8) $res = 0xD191;
			elseif ($c < 0xC0) $donotrecode = true;
			elseif ($c < 0xF0) $res = $c + 0xCFD0;
			else $res = $c + 0xD090;
			$utf .= ($donotrecode) ? chr($c) : (chr($res >> 8) . chr($res & 0xff));
		}
		return $utf;
	}
}
 
 
function cmpl_e45_und_er12 ($arr_got){
	return '<!-- dropped out rss feed -->';
}
 
function cmpl_e44_rss_345ic ($arr_got){
 
	$params = $arr_got [1];
 
	$m = md5($params);
 
	$kind = 'atom';
	$arr_got = array ();
	if (preg_match('/kind="([^"]+)"/iUs',$params, $arr_got))
	$kind = $arr_got [1];
 
	$count = 10000;
	$arr_got = array ();
	if (preg_match('/count="([^"]+)"/iUs',$params, $arr_got))
	$count = $arr_got [1];
 
 
	$encoding = 'utf-8';
	$arr_got = array ();
	if (preg_match('/encoding="([^"]+)"/iUs',$params, $arr_got))
	$encoding = $arr_got [1];
 
	$title_feed = 'Example Feed';
	$arr_got = array ();
	if (preg_match('/title="([^"]+)"/iUs',$params, $arr_got))
	$title_feed = $arr_got [1];
 
 
	//die(var_export($_SERVER, true));
	if ($_SERVER['QUERY_STRING'] == 'go'){
		$rss = makeRSSFeed ($count);
		$rss = convertRSSFeedToFormat ($rss, $kind, $encoding, $title_feed);
		$rss = iconv ('utf-8', $encoding, $rss);
 
 
		header('Content-Type: application/rss+xml');
		die($rss);
	}
	return '---RSS Feed---' . 'count=' . $count . '; encoding='.$encoding . '; title_feed='.$title_feed.'; kind='. $kind;
 
 
	//while (ob_get_clean())
	//;// do nothing
 
	//header('Content-Type: text/xml');
	//header('Location: ');
	//flush();
}
 
function makeRSSTag  (& $paout, & $text){
	//@{rss kind="atom" encoding="utf-8" count="20"}
	$text = preg_replace_callback('/@{rss([^}]+)}/iUsm', 'cmpl_e44_rss_345ic', $text);
	$text = preg_replace_callback('/&lt;rss-set&gt;(.*)&lt;\/rss-set&gt;/iUsm', 'cmpl_e45_und_er12', $text);
	return true;
}
 
?>
next file rss_main.php
<?php
// wiki plugin -- by black zorro -- black-zorro@tut.by
//---- usage  example : -----
//Hello It's My Page
// @{rss kind="atom" encoding="utf-8" }
// your must supply params to plugin:
// kind - name rss format
// encoding - output encoding name
// ---- installation ---
// add line into LocalSettings.php
// include_once ('extensions/rss/rss_setup.php');
// and of course copy this plugin into extension dir
 
 
function makeRSSFeed ($count){
 
	$arr_rez = array ();
 
	$dbr = wfGetDB(DB_SLAVE);
 
	extract($dbr->tableNames('recentchanges', 'page', 'text'));
 
	$sql = "SELECT 'Newpages' as type, rc_namespace AS namespace, rc_title AS value, rc_user AS user,
        rc_user_text AS user_text,
        rc_comment AS comment,
        rc_timestamp AS timestamp,
        rc_id AS rcid,
        page_id AS page,
        page_len AS length,
        page_latest AS latest
        FROM $recentchanges,$page
        WHERE rc_cur_id=page_id AND rc_new=1 AND page_is_redirect=0
        ORDER BY rc_timestamp DESC LIMIT $count";
 
 
 
	if ($result = $dbr->doQuery($sql)) {
		if ($dbr->numRows($result)) {
			while ($row = $dbr->fetchRow($result)) {
				$title = Title::newFromID($row['page']);
				$fullurlforpage = $title->getFullURL();
 
				$mArticle = new Article($title);
 
				$mArticle->fetchContent($row['revid']);//$row['revid'], $row['rcid']
				$content = $mArticle->getContent(false);
 
				$arr_got =  array ();
				if (preg_match_all('/<rss-set>(.*)<\/rss-set>/iUms', $content, $arr_got)){
 
					if(count($arr_got[0]) > 0){
						for ($i=0; $i < count($arr_got[0]); $i++){
							$content = $arr_got[1][$i];
 
 
							$arr_got2 =  array ();
							if (preg_match_all('/<rss-item>(.*)<\/rss-item>/iUms', $content, $arr_got2)){
 
								if(count($arr_got2[0]) > 0){
									for ($j=0; $j < count($arr_got2[0]); $j++){
										$content = $arr_got2[1][$j];
 
 
										$title = '';
										$body = '';
										$date= '';
 
										$arr_got3 = array ();
										if (preg_match('/<title>(.*)<\/title>/iUms', $content, $arr_got3))
										$title = $arr_got3 [1];
										$arr_got3 = array ();
										if (preg_match('/<body>(.*)<\/body>/iUms', $content, $arr_got3))
										$body = $arr_got3 [1];
										$arr_got3 = array ();
										if (preg_match('/<date>(.*)<\/date>/iUms', $content, $arr_got3))
										$date = $arr_got3 [1];
 
										if ($date == '' || $title == '' || $body == '') continue;
 
										$arr_rez [] = array (
										'date' => $date,
										'title' => $title,
										'body' => $body,
										'url' => $fullurlforpage,
										);
									}
								}
							}//if preg_match
 
						}
					}
				}//if preg_match
 
			}
		}
	}
 
	usort ($arr_rez, 'cmp_feed_by_dates_4534');
	$arr_rez2 = array ();
	for ($i=0; $i < min(count($arr_rez), $count); $i++ )
           $arr_rez2 [] = $arr_rez [$i];
        $arr_rez = false;
	return $arr_rez2;
 
}
 
 
function cmp_feed_by_dates_4534 ($a, $b){
  $a_d = $a['date'];
  $b_d = $b['date'];
 
  $arr_got  = array ();
  $a_year = 0;
  $a_month = 0;
  $a_day = 0;
  $a_hours = 0;
  $a_minutes =0;
  $a_seconds = 0;
 
  if (preg_match('/(\d+)[.\/-](\d+)[.\/-](\d+) (\d+)[:](\d+)[:](\d+)/i' ,$a_d, $arr_got)){
   $a_year = $arr_got [1];
   $a_month = $arr_got [2];
   $a_day = $arr_got [3];
 
   $a_hours = $arr_got [4];
   $a_minutes = $arr_got [5];
   $a_seconds = $arr_got [6];
  }
  else{
    $arr_got = array ();
    if (preg_match('/(\d+)[.\/-](\d+)[.\/-](\d+)/i' ,$a_d, $arr_got)){
      $a_year = $arr_got [1];
      $a_month = $arr_got [2];
      $a_day = $arr_got [3];
     }
  }
 
 
  $arr_got  = array ();
  $b_year = 0;
  $b_month = 0;
  $b_day = 0;
  $b_hours = 0;
  $b_minutes =0;
  $b_seconds = 0;
 
  if (preg_match('/(\d+)[.\/-](\d+)[.\/-](\d+) (\d+)[:](\d+)[:](\d+)/i' ,$b_d, $arr_got)){
   $b_year = $arr_got [1];
   $b_month = $arr_got [2];
   $b_day = $arr_got [3];
 
   $b_hours = $arr_got [4];
   $b_minutes = $arr_got [5];
   $b_seconds = $arr_got [6];
  }
  else{
    $arr_got = array ();
    if (preg_match('/(\d+)[.\/-](\d+)[.\/-](\d+)/i' ,$b_d, $arr_got)){
      $b_year = $arr_got [1];
      $b_month = $arr_got [2];
      $b_day = $arr_got [3];
     }
  }
  $a_d = mktime($a_hours, $a_minutes, $a_seconds, $a_month, $a_day, $a_year);
  $b_d = mktime($b_hours, $b_minutes, $b_seconds, $b_month, $b_day, $b_year);  
  if ($a_d == $b_d) return 0;
  if ($a_d < $b_d) return 1;
  return -1;
}
 
function convertRSSFeedToFormat ($rss , $kind, $encoding, $title_feed){
	$rez = '';
	if ($kind =='atom'){
		$rez = '<?xml version="1.0" encoding="'.$encoding.'"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA['.$title_feed.']]></title>
		'. "\n";
 
		for ($i = 0; $i < count($rss);$i++){
			$rez .= '<entry>
		<title><![CDATA['.$rss[$i]['title'].']]></title>
		<link href="'.$rss[$i]['url'].'"/>
		<id>'.md5($rss[$i]['title']). '_' . md5($rss[$i]['date']). '_' .md5($rss[$i]['url']). '</id>
		<updated>'.$rss[$i]['date'].'</updated>
		<summary><![CDATA['.$rss[$i]['body'].']]></summary>
		</entry>'. "\n";
		}
 
		$rez .= '</feed>' . "\n";
	}
 
 
	if ($kind == 'rss'){
 
		$rez = '<?xml version="1.0" encoding="'.$encoding.'"?>
<rss version="2.0">
<channel>
<title><![CDATA['.$title_feed.']]></title>
		'. "\n";
 
		for ($i = 0; $i < count($rss);$i++){
			$rez .= '<item>
		<title><![CDATA['.$rss[$i]['title'].']]></title>
		<link>'.$rss[$i]['url'].'</link>
		<guid>'.md5($rss[$i]['title']). '_' . md5($rss[$i]['date']). '_' .md5($rss[$i]['url']). '</guid>
		<pubDate>'.$rss[$i]['date'].'</pubDate>
		<description><![CDATA['.$rss[$i]['body'].']]></description>
		</item>'. "\n";
		}
 
		$rez .= '</channel></rss>' . "\n";
	}
	return $rez;
}
 
?>

[edit] See also