<?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&offset=' . ($GLOBALS['_IN']['offset']['int'] + $pageSize);
}
if($GLOBALS['_IN']['offset']['int'] > 0){
$GLOBALS['_T']['blog']['prevUrl'] = 'index.php?p=blog&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&a=archive&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&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&a=show&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;
}
?>