forked from turnermm/news
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.php
executable file
·182 lines (159 loc) · 5.01 KB
/
helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
*/
if(!defined('DOKU_INC')) die();
if(!defined('DOKU_META')) define('DOKU_META',DOKU_INC.'data/meta/newsfeed/');
class helper_plugin_news extends Dokuwiki_Plugin {
var $wasUpdated = false;
var $header;
var $sub_feed = "";
var $has_permission = -1;
function getMethods(){
$result = array();
$result[] = array(
'name' => 'pageUpdated',
'desc' => 'Check if news for $ID is updated',
'params' => array(),
'return' => array('result' => 'bool')
);
$result[] = array(
'name' => 'setUpdate',
'desc' => 'set boolean to signal page was updated',
'params' => array(),
'return' => array()
);
$result[] = array(
'name' => 'saveFeedData',
'desc' => 'Save feed data for page',
'params' => array(),
'return' => array()
);
return $result;
}
/***
*/
function set_permission($perm) {
$this->has_permission = $perm;
}
function has_permission() {
return $this->has_permission;
}
function pageUpdated(){
return $this->wasUpdated;
}
function setUpdate($ar = null){
if($ar) {
$this->header = $ar[0];
$this->sub_feed = trim($ar[1]);
}
$this->wasUpdated = true;
}
function setSubFeed($ns) {
$this->sub_feed = trim($ns);
}
function getMetaDirectory() {
if(preg_match("/notset/i", $this->sub_feed)) {
$this->sub_feed ='/';
return "newsfeed:";
}
return 'newsfeed:' . $this->sub_feed . ':';
}
function getMetaFN($file,$ext) {
$ns = $this->getMetaDirectory();
$file = $ns . $file;
return metaFN($file, $ext);
}
function saveFeedData($id=null) {
if(!$id) return;
if(!$this->header) {
$md_5 = $this->_parse_items($id);
}
elseif($this->header) {
$md_5 = $this->_parse_headers($id) ;
}
$metafile = $this->getMetaFN('pagedata', '.ser');
$ar = $this->_readFile($metafile, true);
if(!$md_5) {
if(isset($ar[$md_5])) {
unset($ar[$md_5]);
$this->_writeFile($metafile,$ar,true);
}
return;
}
$file_path = wikiFN($id);
$tm = filemtime ($file_path);
// update page db
$result = array();
$result['id'] = $id;
$result['url'] = DOKU_URL . 'doku.php?id=' . $id;
$result['time'] = $tm;
$result['gmtime'] = gmdate('r',$tm);
if(empty($ar[$md_5]['create_time'])) {
$result['create_time'] = gmdate('r',$tm);
}
else $result['create_time'] = $ar[$md_5]['create_time'];
$result['header'] = $this->header;
$ar[$md_5] = $result;
$this->_writeFile($metafile,$ar,true);
}
function _parse_items($id) {
// get page contents
$data = file_get_contents(wikiFN($id));
$n = preg_match_all("#<news(.*?)>.*?(?=</news>)#ms", $data, $matches);
if($n == 0) return false;
$data = $matches[0];
$titles = $matches[1];
$feed_data = array();
for($i=0; $i<count($data); $i++) {
list($news,$title) = explode(':',$titles[$i]);
if(isset($title)) {
$title = trim($title);
}
if(isset($title) && $title) {
$feed_data[$i]['title'] = $title;
}
else $feed_data[$i]['title'] = "News Item";
$data[$i] = preg_replace("#^\s*<news(.*?)>#","",$data[$i]);
$feed_data[$i]['item'] = $this->render($data[$i]);
$feed_data[$i]['name'] = 'rss_' . ($i+1);
}
$md_5 = md5($id);
$metafile = $this->getMetaFN($md_5, '.gz');
$this->_writeFile($metafile,$feed_data,true);
return $md_5;
}
function _parse_headers($id) {
$data = file_get_contents(wikiFN($id));
$segs = $this->header;
$ar = preg_split("/(={" . $segs . ",}.*?={" . $segs . ",})\s*\n/",$data, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
if(count($ar) == 0) return false;
array_shift($ar); //removes ~~NEWSFEED.*?~~ and any text above first header
$feed_data = array();
for($i=0, $j=0; $i<count($ar); $i+=2, $j++) {
$feed_data[$j]['title'] = str_replace('=',"",$ar[$i]);
$feed_data[$j]['item'] = $this->render($ar[$i+1]);
$check=false;
$feed_data[$j]['name'] = sectionID($ar[$i],$check);
}
$md_5 = md5($id);
$metafile = $this->getMetaFN($md_5, '.gz');
$this->_writeFile($metafile,$feed_data,true);
return $md_5;
}
function _readFile($file, $ser=false) {
$ret = io_readFile($file,$ser);
if($ser) {
if(!$ret) return array();
return unserialize($ret);
}
return $ret;
}
function _writeFile($file,$data,$ser) {
if($ser) {
$data = serialize($data);
}
io_saveFile($file,$data);
}
}