forked from typecho-fans/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Widget.php
184 lines (162 loc) · 6.82 KB
/
Widget.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
183
184
<?php
class JustFeed_Widget extends Widget_Archive
{
/**
* 请到 Widget_Archive 中看以下私有变量的作用
* 由于父类中是私有变量不能在子类中直接使用,只能间接获取
*/
private $_feed;
private $_description;
private $_archiveTitle;
private $_feedType;
private $_currentFeedUrl;
private $_feedContentType;
/**
* 构造函数,初始化组件
*
* @access public
* @param mixed $request request对象
* @param mixed $response response对象
* @param mixed $params 参数列表
* @return void
*/
public function __construct($request, $response, $params = NULL)
{
parent::__construct($request, $response, $params);
$this->_feed = $this->getFeed();
$this->_feedType = $this->getFeedType();
/** 判断聚合类型 */
switch (true) {
case 0 === strpos($this->request->feed, '/rss/') || '/rss' == $this->request->feed:
/** 如果是RSS1标准 */
$this->_currentFeedUrl = $this->options->feedRssUrl;
$this->_feedContentType = 'application/rdf+xml';
break;
case 0 === strpos($this->request->feed, '/atom/') || '/atom' == $this->request->feed:
/** 如果是ATOM标准 */
$this->_currentFeedUrl = $this->options->feedAtomUrl;
$this->_feedContentType = 'application/atom+xml';
break;
default:
$this->_currentFeedUrl = $this->options->feedUrl;
$this->_feedContentType = 'application/rss+xml';
break;
}
}
/**
* 输出 feed
*/
public function feed()
{
$this->_description = $this->getDescription();
$this->_archiveTitle = $this->getArchiveTitle();
// 获取系统设置
$options = Helper::options();
// 获取 JustFeed 设置
$settings = Helper::options()->plugin('JustFeed');
//$related_post_num = is_numeric($settings->cfg_related_post_num) ? intval($settings->cfg_related_post_num) : 5;
$search = array('{sitetitle}','{siteurl}','{author}','{authorurl}','{permalink}','{date}','{time}','{commentsnumber}');
$this->_feed->setSubTitle($this->_description);
$this->_feed->setFeedUrl($this->_currentFeedUrl);
$this->_feed->setBaseUrl(('/' == $this->request->feed || 0 == strlen($this->request->feed)
|| '/comments' == $this->request->feed || '/comments/' == $this->request->feed) ?
$this->options->siteUrl : Typecho_Common::url($this->request->feed, $this->options->index));
$this->_feed->setFeedUrl($this->request->makeUriByRequest());
if ($this->is('single') || 'comments' == $this->parameter->type) {
$this->_feed->setTitle(_t('%s 的评论',
$this->options->title . ($this->_archiveTitle ? ' - ' . implode(' - ', $this->_archiveTitle) : NULL)));
if ('comments' == $this->parameter->type) {
$comments = $this->widget('Widget_Comments_Recent', 'pageSize=10');
} else {
$comments = $this->widget('Widget_Comments_Recent', 'pageSize=10&parentId=' . $this->cid);
}
while ($comments->next()) {
$d = getdate($comments->created);
$replace = array(
$options->title,
$options->siteUrl,
$this->author->screenName,
$this->author->url,
$comments->permalink,
$d['year'].'/'.$d['mon'].'/'.$d['mday'],
$d['hours'].':'.$d['minutes'].':'.$d['seconds'],
$this->commentsNum
);
$copyright = str_replace($search, $replace, $settings->cfg_copyright);
$suffix = $this->pluginHandle()->trigger($plugged)->commentFeedItem($this->_feedType, $comments);
if (!$plugged) {
$suffix = NULL;
}
$this->_feed->addItem(array(
'title' => $comments->author,
'content' => $comments->content . $copyright,
'date' => $comments->created,
'link' => $comments->permalink,
'author' => (object) array(
'screenName' => $comments->author,
'url' => $comments->url,
'mail' => $comments->mail
),
'excerpt' => strip_tags($comments->content),
'suffix' => $suffix
));
}
} else {
$this->_feed->setTitle($this->options->title . ($this->_archiveTitle ? ' - ' . implode(' - ', $this->_archiveTitle) : NULL));
$feedUrl = '';
if (Typecho_Feed::RSS2 == $this->_feedType) {
$feedUrl = $this->feedUrl;
} else if (Typecho_Feed::RSS1 == $this->_feedType) {
$feedUrl = $this->feedRssUrl;
} else if (Typecho_Feed::ATOM1 == $this->_feedType) {
$feedUrl = $this->feedAtomUrl;
}
while ($this->next()) {
//set_time_limit(60);
// 获取文字内容的时间
$d = getdate($this->created);
$replace = array(
$options->title,
$options->siteUrl,
$this->author->screenName,
$this->author->url,
$this->permalink,
$d['year'].'/'.$d['mon'].'/'.$d['mday'],
$d['hours'].':'.$d['minutes'].':'.$d['seconds'],
$this->commentsNum
);
$copyright = str_replace($search, $replace, $settings->cfg_copyright);
// 相关日志
/* $related_post_html = '';
if ($settings->cfg_related_post) {
$relatedPosts = $this->related($related_post_num);
if ($relatedPosts->have()) {
$related_post_html = '<h4>相关日志</h4><ul>';
while ($relatedPosts->next()) {
$related_post_html .= '<li><a href="' . $relatedPosts->permalink . '" title="' . $relatedPosts->title . '">' . $relatedPosts->title . '</a></li>';
}
$related_post_html .= '</ul>';
}
} */
$suffix = $this->pluginHandle()->trigger($plugged)->feedItem($this->_feedType, $this);
if (!$plugged) {
$suffix = NULL;
}
$this->_feed->addItem(array(
'title' => $this->title,
'content' => $this->options->feedFullText ? $this->content.$related_post_html.$copyright : (false !== strpos($this->text, '<!--more-->') ?
$this->excerpt . "<p class=\"more\"><a href=\"{$this->permalink}\" title=\"{$this->title}\">[...]</a></p>" . $related_post_html.$copyright : $this->content.$related_post_html.$copyright),
'date' => $this->created,
'link' => $this->permalink,
'author' => $this->author,
'excerpt' => $this->description,
'comments' => $this->commentsNum,
'commentsFeedUrl' => $feedUrl,
'suffix' => $suffix
));
}
}
$this->response->setContentType($this->_feedContentType);
echo $this->_feed->__toString();
}
}