Menu

[r63]: / modules / blog.inc  Maximize  Restore  History

Download this file

222 lines (210 with data), 9.0 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
* blog.inc
* Publish articles to a blog widget and display articles with a blog-style layout.
* @copyright Copyright(c) 2002-2010 Bjorn Winberg
* @author Bjorn Winberg <cms@anomalye.net>
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License version 2
* @package AnomalyWiki
*/
system_loadSettings('blog');
$GLOBALS['_T']['system']['modules']['blog'] = array(
'name' => system_loadStr('BLOG_MODULE'),
'version' => '0.6.0',
);
$GLOBALS['_C']['dbKeys']['BLOG_LIST'] = array('blog_list');
/**
* Standard display function.
*/
function blog_controller(){
if($GLOBALS['_IN']['a']['enc'] == 'rss'){
$GLOBALS['_T']['system']['outputType'] = $GLOBALS['_C']['SYSTEM_OUTPUT_XML'];
return blog_generateRss();
}
elseif($GLOBALS['_IN']['a']['enc'] == 'archive'){
system_readParamInt('offset', 0, $GLOBALS['_S']['system']['MAX_ENTRIES'], 0);
$listDbKey = system_dbKey('BLOG_LIST');
$blogList = system_dbRead($listDbKey);
$pageSize = $GLOBALS['_S']['blog']['BLOG_ENTRIES_PER_PAGE'];
if($blogList !== false){
$reverseList = system_arraySlice(array_reverse($blogList, true), $GLOBALS['_IN']['offset']['int'], $pageSize);
$GLOBALS['_T']['blog']['parsedList'] = blog_generateBlogList($reverseList);
system_triggerWarnings($GLOBALS['_T']['blog']['parsedList']['warnings']);
$count = $GLOBALS['_T']['blog']['itemCount'] = count($blogList);
if($pageSize > 0 && $GLOBALS['_IN']['offset']['int'] + $pageSize < $count){
$GLOBALS['_T']['blog']['nextUrl'] = 'index.php?p=blog&amp;offset=' . ($GLOBALS['_IN']['offset']['int'] + $pageSize);
}
if($GLOBALS['_IN']['offset']['int'] > 0){
$GLOBALS['_T']['blog']['prevUrl'] = 'index.php?p=blog&amp;offset=' . ($GLOBALS['_IN']['offset']['int'] - $pageSize);
}
}
return system_applyTemplate('blog_archive.inc');
}
else{
$parseItem = blog_generateBlogItem($_SESSION['currentItem']);
return $parseItem['parsedText'];
}
}
/**
* Callback function that handles parser commands that belongs to this module.
* @param array &$aParseItem Parse item that will hold the results.
* @param string &$aTag The full tag (command+params) being handled.
* @see system_parseRootTag()
*/
function blog_handleParseTag(&$aParseItem, &$aTag){
$tagBits = explode(rawurlencode($GLOBALS['_S']['system']['COMMAND_PARAMETER_SEPARATOR']), $aTag);
if($tagBits[1] == rawurlencode('blog') && isset($tagBits[2])){
$action = $tagBits[2];
if($action == 'list'){
$listDbKey = system_dbKey('BLOG_LIST');
$blogList = system_dbRead($listDbKey);
if($blogList !== false){
$reverseList = system_arraySlice(array_reverse($blogList, true), 0, $GLOBALS['_S']['blog']['BLOG_ENTRIES_PER_PAGE']);
$parsedList = blog_generateBlogList($reverseList);
$aParseItem['warnings'] = array_merge($aParseItem['warnings'], $parsedList['warnings']);
$aParseItem['parsedText'] .= $parsedList['parsedText'];
}
}
elseif($action == 'archive'){
$listDbKey = system_dbKey('BLOG_LIST');
$blogList = system_dbRead($listDbKey);
$count = $blogList !== false? count($blogList): 0;
$page = isset($tagBits[3]) && is_numeric($tagBits[3])? $tagBits[3]: 0;
$title = isset($tagBits[4])? system_getSafeStr($tagBits[4]): system_loadStr('BLOG_ARCHIVE_LINK');
$offset = $page * $GLOBALS['_S']['blog']['BLOG_ENTRIES_PER_PAGE'];
if($count > $offset){
$aParseItem['parsedText'] .= '<a href="index.php?p=blog&amp;a=archive&amp;offset=' . $offset . '">' . $title . '</a>';
}
}
elseif($action == 'item'){
if(isset($tagBits[3])){
$title = isset($tagBits[4])? system_getSafeStr($tagBits[4]): system_loadStr('BLOG_ITEM_LINK');
$aParseItem['parsedText'] .= '<a href="index.php?p=blog&amp;item=' . $tagBits[3] . '">' . $title . '</a>';
}
}
elseif($action == 'publish'){
// Do nothing
}
}
else{
$aParseItem['warnings'][] = system_loadStr('SYSTEM_PARSE_INVALID_VARIABLE', array(system_getSafeStr($tagBits[1])));
}
}
/**
* Generate a list parsed articles ready for output.
* @param array $aList A list of article names to be parsed.
* @return string A parsed list of articles (hmtl).
*/
function blog_generateBlogList($aList){
$retParseItem['parsedText'] = '';
$retParseItem['sourceText'] = '';
$retParseItem['itemName'] = '';
$retParseItem['warnings'] = array();
foreach($aList as $itemName => $item){
$parseItem = blog_generateBlogItem($itemName);
$retParseItem['warnings'] = array_merge($retParseItem['warnings'], $parseItem['warnings']);
$retParseItem['parsedText'] .= $parseItem['parsedText'];
}
return $retParseItem;
}
/**
*
*/
function blog_generateBlogItem($aItemName){
system_loadModule('article');
$retParseItem = array();
$retParseItem['itemName'] = '';
$varString = rawurlencode(system_loadStr('BLOG_LISTITEM_TEMPLATE'));
$retParseItem['sourceText'] = system_insertVars($varString, array($aItemName));
system_parseItem($retParseItem, $GLOBALS['_S']['article']['ALLOWED_TAGS'], $GLOBALS['_S']['article']['ALLOWED_COMMANDS'], false);
return $retParseItem;
}
/**
* General callback function.
* @param array &$aItem The orginator item of the action. The exact item type depends on the action.
* @param array &$aData Additional data. Exact type depends on the action.
* @param string $aAction The action that has been reported.
* @see system_parseDiffNotify()
*/
function blog_handleEvent(&$aItem, &$aData, $aAction){
if(isset($aItem['module']) && $aItem['module'] == 'article'){
if($aAction === 'system_parseTagRemoved'){
blog_diffTagRemoved($aItem, $aData);
}
elseif($aAction === 'system_parseTagAdded'){
blog_diffTagAdded($aItem, $aData);
}
elseif($aAction === 'system_parseTagRetained'){
// Do nothing
}
}
}
/**
* Handle the notification when a tag is added to a parse item
* by updating the observers.
* @param array &$aListenerItem The parse item that just added a tag.
* @param array &$aTag The full tag being added.
*/
function blog_diffTagAdded(&$aListenerItem, &$aTag){
$tagBits = explode(rawurlencode($GLOBALS['_S']['system']['COMMAND_PARAMETER_SEPARATOR']), $aTag);
if($tagBits[1] == rawurlencode('blog') && isset($tagBits[2]) && $tagBits[2] == 'publish'){
$listDbKey = system_dbKey('BLOG_LIST');
$blogList = system_dbRead($listDbKey);
if(!isset($blogList[$aListenerItem['itemName']])){
$blogList[$aListenerItem['itemName']]['time'] = time();
system_dbWrite($listDbKey, $blogList);
}
}
}
/**
* Handle the notification when a tag is removed from a parse item.
* by updating the observers.
* @param array &$aListenerItem The parse item that just removed a tag.
* @param array &$aTag The full tag being removed.
*/
function blog_diffTagRemoved(&$aListenerItem, &$aTag){
$tagBits = explode(rawurlencode($GLOBALS['_S']['system']['COMMAND_PARAMETER_SEPARATOR']), $aTag);
if($tagBits[1] == rawurlencode('blog') && isset($tagBits[2]) && $tagBits[2] == 'publish'){
$listDbKey = system_dbKey('BLOG_LIST');
$blogList = system_dbRead($listDbKey);
if(isset($blogList[$aListenerItem['itemName']])){
unset($blogList[$aListenerItem['itemName']]);
system_dbWrite($listDbKey, $blogList);
}
}
}
/**
*
*/
function blog_generateRss(){
system_loadModule('article');
$listDbKey = system_dbKey('BLOG_LIST');
$blogList = system_dbRead($listDbKey);
$scriptDir = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
$xml = '<?xml version="1.0"?>' . "\n";
$xml .= '<rss version="2.0">' . "\n";
$xml .= '<channel>' . "\n";
$xml .= '<title>' . system_loadStr('BLOG_RSS_TITLE') . '</title>' . "\n";
$xml .= '<link>' . $scriptDir . '</link>' . "\n";
$xml .= '<description>' . system_loadStr('BLOG_RSS_DESCRIPTION') . '</description>' . "\n";
if($blogList !== false){
$reverseList = system_arraySlice(array_reverse($blogList, true), 0, $GLOBALS['_S']['blog']['BLOG_ENTRIES_PER_PAGE']);
foreach($reverseList as $itemName => $item){
$contentDbKey = system_dbKey('ARTICLE_ITEM', array($itemName));
$contentItem = system_dbRead($contentDbKey);
system_parseItem($contentItem, $GLOBALS['_S']['article']['ALLOWED_TAGS'], $GLOBALS['_S']['article']['ALLOWED_COMMANDS'], false);
$url = $scriptDir . '?p=blog&amp;a=show&amp;item=' . $itemName;
$title = isset($contentItem['info']['article']['title'])? $contentItem['info']['article']['title']: $itemName;
$description = isset($contentItem['info']['article']['description'])? $contentItem['info']['article']['description']: $contentItem['parsedText'];
$xml .= '<item>' . "\n";
$xml .= '<title>' . system_getSafeStr($title) . '</title>' . "\n";
$xml .= '<description>' . system_getSafeStr($description) . '</description>' . "\n";
$xml .= '<link>' . $url . '</link>' . "\n";
$xml .= '</item>' . "\n";
}
}
$xml .= '</channel>' . "\n";
$xml .= '</rss>' . "\n";
return $xml;
}
?>