1<?php 2/** 3 * RSS Syntax Plugin: display feeds in your wiki. 4 * 5 * @author Algorys 6 */ 7 8if (!defined('DOKU_INC')) die(); 9 10class syntax_plugin_rss extends DokuWiki_Syntax_Plugin { 11 12 public function getType() { 13 return 'container'; 14 } 15 16 /** 17 * @return string Paragraph type 18 **/ 19 20 public function getPType() { 21 return 'normal'; 22 } 23 24 // Keep syntax inside plugin 25 function getAllowedTypes() { 26 return array('container', 'baseonly', 'substition','protected','disabled','formatting','paragraphs'); 27 } 28 29 public function getSort() { 30 return 198; 31 } 32 33 function connectTo($mode) { 34 $this->Lexer->addSpecialPattern('<rss[^>]*>(?=.*)', $mode,'plugin_rss'); 35 } 36 37 38/** 39* Do the regexp 40**/ 41 function handle($match, $state, $pos, Doku_Handler $handler) { 42 switch($state){ 43 case DOKU_LEXER_SPECIAL : 44 case DOKU_LEXER_ENTER : 45 $data = array( 46 'state'=>$state, 47 'feed'=> "", 48 ); 49 // Looking for id 50 preg_match("/feed *= *(['\"])(.*?)\\1/", $match, $feed); 51 if( count($feed) != 0 ) { 52 $data['feed'] = $feed[2]; 53 } 54 return $data; 55 case DOKU_LEXER_UNMATCHED : 56 return array('state'=>$state, 'text'=>$match); 57 default: 58 return array('state'=>$state, 'bytepos_end' => $pos + strlen($match)); 59 } 60 } 61 62 function _check_rss($data) { 63 $feed = $data['feed']; 64 try { 65 if(!@$fluxrss=simplexml_load_file($feed)) { 66 throw new Exception('Flux invalide'); 67 } 68 if(empty($fluxrss->channel->title) || empty($fluxrss->channel->description) || empty($fluxrss->channel->item->title)) { 69 throw new Exception('Invalid Feed !'); 70 } 71 //$renderer->doc .= '<p>Flux RSS trouvé !</p>'; 72 } catch(Exception $e){ 73 echo $e->getMessage(); 74 } 75 return $fluxrss; 76 } 77 78 function _render_rss($renderer, $data){ 79 $fluxrss = $this->_check_rss($data); 80 if($fluxrss) { 81 $renderer->doc .= '<img src="lib/plugins/rss/images/rss.png" alt="rss" class="rss">'; 82 $renderer->doc .= '<h3>'.(string)$fluxrss->channel->title.'</h3>'; 83 $renderer->doc .= '<p>'.(string)$fluxrss->channel->description.'</p>'; 84 $i = 0; 85 $nb_to_display = 5; 86 $renderer->doc .= '<ul>'; 87 foreach($fluxrss->channel->item as $item) { 88 $renderer->doc .= '<li>'; 89 $renderer->doc .= '<a href="'.(string)$item->link.'">'.(string)$item->title.'</a>'; 90 $renderer->doc .= '<i> ✎ '.$this->getLang('rss.publish').' '.(string)date('G\hi, d/m/Y', strtotime($item->pubDate)).'</i>'; 91 $renderer->doc .= '</li>'; 92 if(++$i>=$nb_to_display) 93 break; 94 } 95 }else { 96 $renderer->doc .= '<p>'.$this->getLang('rss.empty').' </p>'; 97 } 98 } 99 100 // Dokuwiki Renderer 101 function render($mode, Doku_Renderer $renderer, $data) { 102 if($mode != 'xhtml') return false; 103 104 if($data['error']) { 105 $renderer->doc .= $data['text']; 106 return true; 107 } 108 $renderer->info['cache'] = false; 109 switch($data['state']) { 110 case DOKU_LEXER_SPECIAL : 111 case DOKU_LEXER_ENTER : 112 $this->_render_rss($renderer, $data); 113 break; 114 case DOKU_LEXER_UNMATCHED : 115 $renderer->doc .= $renderer->_xmlEntities($data['text']); 116 break; 117 } 118 return true; 119 } 120 121} 122