1<?php
2
3
4class feedData {
5    var $meta_data;  // array of meta data entries from meta/newsfeed:pagedata.ser
6    var $rss_data;   // array of data items, titles, anchor names from current file
7
8	var $feedDataBaseNames;  // array of md5 basenames of files holding feed data
9	var $currentMD5BaseName;
10	var $currentDataArray;
11	var $currentMetaArray;
12	var $newsFeedDate;
13	var $helper;
14    function __construct($subfeed) {
15		global $newsChannelTitle;
16		global $newsChannelDescription;
17		global $newsChannelTtl;
18
19        $this->helper = plugin_load('helper', 'news');
20         $this->helper->setSubFeed($subfeed) ;
21
22        $metafile = $this->helper->getMetaFN('pagedata', '.ser');
23		$this->meta_data = $this->_readFile($metafile, true);
24        uasort($this->meta_data,function($a,$b) {
25            $ta = strtotime($a['create_time']);
26            $tb = strtotime($b['create_time']);
27            if ($ta==$tb) return 0;
28            return ($ta>$tb)?-1:1;
29        });
30
31		$this->get_md5_array();
32
33        $metafile = $this->helper->getMetaFN('timestamp','.meta') ;
34
35        $this->newsFeedDate	= $this->_readFile($metafile);
36
37	}
38
39	function get_md5_array() {
40	     $this->feedDataBaseNames = array();
41		 $ar = array_keys($this->meta_data);
42	      foreach($ar as $md5) {
43              $file = $this->helper->getMetaFN($md5, '.gz');
44			  if(@file_exists($file)) {
45			     $this->feedDataBaseNames[] = $md5;
46			  }
47		  }
48	}
49
50	function _readFile($file, $ser=false) {
51	    $ret = io_readFile($file,$ser);
52		if($ser) {
53		  if(!$ret) return array();
54		  return unserialize($ret);
55		  }
56		return $ret;
57
58	}
59
60
61    function description() {
62            $this->currentDataArray['item'] =
63                        preg_replace('#(href|src)\s*=\s*([\'\"])/.*?/#ms', "$1=$2" . $this->news_feed_url(), $this->currentDataArray['item']);
64
65                        return $this->currentDataArray['item'];
66    }
67
68
69    function rss_id() {
70		return $this->currentDataArray['name'];
71    }
72    function title() {
73		return $this->currentDataArray['title'];
74
75    }
76
77   function id() {
78		return $this->currentMetaArray['id'];
79    }
80
81    function url() {
82		return $this->currentMetaArray['url'];
83	}
84
85    function timestamp() {
86		return $this->currentMetaArray['time'];
87	}
88
89    function date($which='gm') {
90		if($which == 'gm') {
91            if($this->helper->getConf('createtime') && isset($this->currentMetaArray['create_time'])) {
92                 return $this->currentMetaArray['create_time'];
93            }
94              else {
95                 return $this->currentMetaArray['gmtime'];
96              }
97        }
98
99		return date('r',$this->timestamp());
100    }
101
102      function news_feed_url() {
103
104           list($server,$rest) = explode('?', $this->url());
105           if(!$server) $server = DOKU_URL;
106
107		   $server = str_replace('doku.php',"",$server);
108		   if(preg_match("#http://([^/]+)/([^/]+)/*$#", $server)) {
109					return $server;
110			}
111
112	     if(preg_match("#(?!:/)/([^/]+)/[^/]+/$#", $server)) {
113				return preg_replace("#/[^/]+/*$#", "/",$server);
114		   }
115
116		    return $server;
117
118        }
119
120	function news_feed_date($which='gm') {
121	    if($which == 'gm') return gmdate('r',$this->newsFeedDate);
122	    return date('r',$this->newsFeedDate);
123	}
124
125	function md5_id() {
126	   return $this->currentMD5BaseName;
127	}
128
129    function _dataFN() {
130		$md5 = array_shift($this->feedDataBaseNames);
131		if(!$md5) return false;
132		$this->currentMD5BaseName = $md5;
133		return  $this->helper->getMetaFN($md5, '.gz');
134   }
135
136   function testDataElements() {
137
138		$file = $this->_dataFN();
139		$ar = $this->_readFile($file, true);
140        for($i=0;$i<count($ar);$i++) {
141			echo "Name: " . $ar[$i]['name'] ."\n";
142			echo "Title: " . $ar[$i]['title'] ."\n";
143			//echo "Item: " . $ar[$i]['item'] ."\n\n";
144        }
145
146   }
147
148   function next_data_file() {
149        $file = $this->_dataFN();
150		if(!$file) {
151		    $this->currentMetaArray = array();
152			return;
153		}
154   		$this->rss_data = $this->_readFile($file, true);
155		$this->currentMetaArray = $this->meta_data[$this->currentMD5BaseName];
156	}
157
158
159   function feed_data() {
160        if(is_array($this->currentDataArray)) {
161			   $this->currentDataArray = array_shift($this->rss_data);
162		}
163
164       if(!$this->currentDataArray) {
165	           $this->next_data_file();
166			   $this->currentDataArray = array_shift($this->rss_data);
167    	 }
168
169
170		if(!$this->currentDataArray) return false;
171		return true;
172
173    }
174
175	function channel_title() {
176		global $newsChannelTitle;
177		if(!$newsChannelTitle) return 'DokuWiki News Feed';
178		return $newsChannelTitle;
179	}
180
181	function channel_description() {
182		global $newsChannelDescription;
183		if(!$newsChannelDescription) return  'DokuWiki News Feed';
184		return $newsChannelDescription;
185	}
186
187	function channel_ttl() {
188		global $newsChannelTtl;
189		if(!$newsChannelTtl) return $this->ttl;
190		return $newsChannelTtl;
191	}
192}
193
194?>
195