-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleFeed.php
More file actions
170 lines (138 loc) · 4.97 KB
/
SimpleFeed.php
File metadata and controls
170 lines (138 loc) · 4.97 KB
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
<?php
/*
* SimpleFeed MediaWiki extension
*
* Copyright (C) 2007-2008 Jonny Lamb <jonnylamb@jonnylamb.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*/
// Check to make sure we're actually in MediaWiki.
if (!defined('MEDIAWIKI'))
{
echo 'This file is part of MediaWiki. It is not a valid entry point.';
exit(1);
}
// Path to SimplePie cache folder (excluding leader slash).
$wgSimpleFeed_Path = dirname( __FILE__ );
$wgSimpleFeed_Cache = $wgSimpleFeed_Path . '/cache/';
if ( ! @include($wgSimpleFeed_Path.'/simplepie.inc') )
{
define('SIMPLEPIE_NOT_FOUND', true);
}
$wgExtensionFunctions[] = 'wfSimpleFeed';
$wgExtensionCredits['parserhook'][] = array(
'name' => 'SimpleFeed',
'descriptionmsg' => 'simplefeed-desc',
'author' => array(
'Jonny Lamb',
'Dennis Roczek'),
'url' => 'http://www.mediawiki.org/wiki/Extension:SimpleFeed',
'license-name' => 'GPLv2+',
'version' => '1.0.23'
);
$wgMessagesDirs['SimpleFeed'] = __DIR__ . '/i18n';
function wfSimpleFeed()
{
global $wgParser;
$wgParser->setHook('feed', 'parseFeed');
}
function parseFeed($input, $args, $parser)
{
global $wgSimpleFeed_Cache;
// Disable page caching.
$parser->disableCache();
// Check to see whether SimplePie was actually included.
if (defined('SIMPLEPIE_NOT_FOUND'))
{
return 'simplefeed-pienotfound';
}
// Must have a feed URL and a template to go by outputting items.
if (!isset($args['url']) or !isset($input))
{
return 0;
}
$feed = new SimplePie();
$feed->set_cache_location($wgSimpleFeed_Cache);
$feed->set_feed_url($args['url']);
// Get the feed information!
$feed->init();
$feed->handle_content_type();
// Either use default date format (j F Y), or the $date(string) argument.
// The date argument should conform to PHP's date function, nicely documented
// at http://php.net/date.
$date = (isset($args['date'])) ? $args['date'] : 'j F Y';
$limit = (isset($args['limit'])) ? $args['limit'] : '100';
$output = '';
// Use the $entries(int) argument to determine how many entries to show.
// Defaults to 5, and 0 is unlimited.
if (isset($args['entries']))
{
$max = ($args['entries'] == 0) ? $feed->get_item_quantity() : $feed->get_item_quantity($args['entries']);
}
else
{
$max = $feed->get_item_quantity(5);
}
// sorting descending is default
$fOffset = 0;
$fMult = 1;
if (isset($args['sort']) and $args['sort'] == 'asc')
{
$fOffset = $max - 1; $fMult = -1;
}
// Loop through each item.
for ($i = 0; $i < $max; $i++)
{
$item = $feed->get_item($i * $fMult + $fOffset);
$itemwikitext = $input;
// {PERMALINK} -> Link to the URL of the post.
$itemwikitext = str_replace('{PERMALINK}', $item->get_permalink(), $itemwikitext);
// {DATE} -> The posting date of the post, formatted in the aforementioned way.
$itemwikitext = str_replace('{DATE}', $item->get_date($date), $itemwikitext);
// {DESCRIPTION} -> The actual post (or post description if there's a tear).
$itemwikitext = str_replace('{DESCRIPTION}', substr($item->get_description(), 0, $limit).'...', $itemwikitext);
// If $type="planet" is used, the author is got from the post title.
// e.g. title = "Joe Bloggs: I love Mediawiki"
// This will make: {AUTHOR} -> "Joe Bloggs"
// {TITLE} -> "I love Mediawiki"
// If this is not set however, the title and author are received the usual way.
if ((isset($args['type'])) && ($args['type'] == 'planet'))
{
$title = preg_replace('/(.*): (.*)/sU', '\\2', $item->get_title());
preg_match('/(.+?): (.+)/sU', $item->get_title(), $matches);
$author = $matches[1];
}
else
{
$title = $item->get_title();
// Often the author is hard to recieve. Maybe it's not a very important
// thing to output into RSS...?
$itemauthor = $item->get_author();
$author = ($itemauthor != null) ? $itemauthor->get_name() : '';
}
// {TITLE} -> Title of the post.
$itemwikitext = str_replace('{TITLE}', $title, $itemwikitext);
// {AUTHOR} -> Author of the post.
$itemwikitext = str_replace('{AUTHOR}', $author, $itemwikitext);
// Add to the overall output the post just done.
$output .= $itemwikitext;
}
// Parse the text into HTML between the <feed>[...]</feed> tags, with arguments replaced.
$parserObject = $parser->parse($output, $parser->mTitle, $parser->mOptions, false, false);
// Output formatted text.
return $parserObject->getText();
}
?>