public function build($runData) { $pl = $runData->getParameterList(); $site = $runData->getTemp("site"); $categoryIds = $pl->getParameterValue("category"); $limit = $pl->getParameterValue("limit"); $offset = $pl->getParameterValue("offset"); if ($limit == null) { $limit = 20; } if ($categoryIds === null) { throw new ProcessException(_('No forum category has been specified. Please use attribute category="id" where id is the index number of the category.'), "no_category"); } if (strlen($categoryIds) > 90) { throw new ProcessException(_("Category string too long."), "max_categories"); } $cats = preg_split('/[,;] ?/', $categoryIds); $ccat = new Criteria(); $categories = array(); if (count($cats) > 20) { throw new ProcessException(_("Maximum number of categories exceeded."), "max_categories"); } foreach ($cats as $categoryId) { if ($categoryId === null || !is_numeric($categoryId)) { throw new ProcessException(_('Problem parsing attribute "category".'), "no_category"); } $category = DB_ForumCategoryPeer::instance()->selectByPrimaryKey($categoryId); if ($category == null) { throw new ProcessException(_('Requested forum category does not exist.'), "no_category"); } if ($category->getSiteId() !== $site->getSiteId()) { $fSite = DB_SitePeer::instance()->selectByPrimaryKey($category->getSiteId()); if ($fSite->getPrivate()) { throw new ProcessException(_('The requested category belongs to a private site.'), "no_category"); } } $category->setTemp("group", $category->getForumGroup()); $categories[$category->getCategoryId()] = $category; $ccat->addOr("category_id", $category->getCategoryId()); } $c = new Criteria(); $c->addCriteriaAnd($ccat); $c->addOrderDescending("thread_id"); $c->setLimit($limit, $offset); $threads = DB_ForumThreadPeer::instance()->select($c); $format = $pl->getParameterValue("module_body"); if ($format == null || $format == '') { $format = "" . "+ %%linked_title%%\n\n" . _("by") . " %%author%% %%date|%O ago (%e %b %Y, %H:%M %Z)%%\n\n" . "%%content%%\n\n%%comments%% | " . _("category") . ": %%category%%"; } // process the format and create the message template $wt = new WikiTransformation(); $wt->setMode("feed"); $template = $wt->processSource($format); $template = preg_replace('/<p\\s*>\\s*(%%((?:short)|(?:description)|(?:summary)|(?:content)|(?:long)|(?:body)|(?:text))%%)\\s*<\\/\\s*p>/smi', "<div>\\1</div>", $template); $items = array(); foreach ($threads as $thread) { $post = $thread->getFirstPost(); if (!$post) { continue; } $b = $template; $b = str_ireplace("%%title%%", htmlspecialchars($thread->getTitle()), $b); $b = preg_replace("/%%((linked_title)|(title_linked))%%/i", preg_quote_replacement('<a href="/forum/t-' . $thread->getThreadId() . '/' . $thread->getUnixifiedTitle() . '">' . htmlspecialchars($thread->getTitle()) . '</a>'), $b); $b = str_ireplace("%%author%%", WDRenderUtils::renderUser($thread->getUserOrString(), array("image" => true)), $b); $dateString = '<span class="odate">' . $thread->getDateStarted()->getTimestamp() . '|%e %b %Y, %H:%M %Z|agohover</span>'; $b = str_ireplace('%%date%%', $dateString, $b); $b = preg_replace('/%%date\\|(.*?)%%/i', '<span class="odate">' . preg_quote_replacement($thread->getDateStarted()->getTimestamp()) . '|\\1</span>', $b); $b = str_ireplace("%%comments%%", '<a href="/forum/t-' . $thread->getThreadId() . '/' . $thread->getUnixifiedTitle() . '">' . _('Comments') . ': ' . ($thread->getNumberPosts() - 1) . '</a>', $b); $b = str_ireplace("%%link%%", '/forum/t-' . $thread->getThreadId() . '/' . $thread->getUnixifiedTitle(), $b); $category = $categories[$thread->getCategoryId()]; $b = str_ireplace("%%category%%", '<a href="/forum/c-' . $category->getCategoryId() . '/' . $category->getUnixifiedName() . '">' . htmlspecialchars($category->getTemp("group")->getName() . " / " . $category->getName()) . '</a>', $b); $b = preg_replace("/%%((description)|(short)|(summary))%%/i", preg_quote_replacement(htmlspecialchars($thread->getDescription())), $b); $b = preg_replace("/%%((body)|(text)|(long)|(content))%%/i", preg_quote_replacement($post->getText()), $b); $items[] = $b; } $runData->contextAdd("items", $items); // post a feed??? $flabel = WDStringUtils::toUnixName($pl->getParameterValue("feed")); $page = $runData->getTemp("page"); if ($flabel && $page) { $ftitle = trim($pl->getParameterValue("feedTitle")); if ($ftitle == '') { $ftitle = $site->getName() . " feed"; } $fdescription = $pl->getParameterValue("feedDescription"); $fcats = trim($categoryIds); $parmhash = crc32($ftitle . " " . $fcats); // first check the memcache!!! to avoid db connection. // get the feed object $c = new Criteria(); $c->add("page_id", $page->getPageId()); $c->add("label", $flabel); $feed = DB_FrontForumFeedPeer::instance()->selectOne($c); if ($feed == null) { // create the feed $feed = new DB_FrontForumFeed(); $feed->setLabel($flabel); $feed->setTitle($ftitle); $feed->setCategories($fcats); $feed->setPageId($page->getPageId()); $feed->setDescription($fdescription); $feed->setSiteId($site->getSiteId()); $feed->save(); } else { // check hash if ($feed->getParmhash() != $parmhash) { $feed->setTitle($ftitle); $feed->setCategories($fcats); $feed->setDescription($fdescription); $feed->save(); } } // and the feed url is: $feedUrl = "/feed/front/" . $page->getUnixName() . "/" . $flabel . ".xml"; $this->vars['feedUrl'] = $feedUrl; $this->vars['feedTitle'] = $ftitle; $this->vars['feedLabel'] = $flabel; // put a link into text $runData->contextAdd("feedUri", $feedUrl); } }
public function build($runData) { $site = $runData->getTemp("site"); $pl = $runData->getParameterList(); $pageName = $pl->getParameterValue("page"); $label = $pl->getParameterValue("label"); // get the feed object $page = DB_PagePeer::instance()->selectByName($site->getSiteId(), $pageName); if (!$page) { throw new ProcessException(_("No such page."), "no_page"); } $c = new Criteria(); $c->add("page_id", $page->getPageId()); $c->add("label", $label); $feed = DB_FrontForumFeedPeer::instance()->selectOne($c); $categoryIds = $feed->getCategories(); $cats = preg_split('/[,;] ?/', $categoryIds); $ccat = new Criteria(); $categories = array(); if (count($cats) > 20) { throw new ProcessException(_("Maximum number of categories exceeded."), "max_categories"); } // get page $page = DB_PagePeer::instance()->selectByPrimaryKey($feed->getPageId()); if (!$page) { throw new ProcessException(_("Page can not be found."), "no_page"); } foreach ($cats as $categoryId) { if ($categoryId === null || !is_numeric($categoryId)) { throw new ProcessException(_('Problem parsing attribute "category".'), "no_category"); } $category = DB_ForumCategoryPeer::instance()->selectByPrimaryKey($categoryId); if ($category == null || $category->getSiteId() !== $site->getSiteId()) { throw new ProcessException(_("Requested forum category does not exist."), "no_category"); } $categories[$category->getCategoryId()] = $category; $ccat->addOr("category_id", $category->getCategoryId()); } $c = new Criteria(); $c->addCriteriaAnd($ccat); $c->addOrderDescending("thread_id"); $c->setLimit(30); $threads = DB_ForumThreadPeer::instance()->select($c); $channel['title'] = $feed->getTitle(); $channel['link'] = "http://" . $site->getDomain() . "/" . $page->getUnixName(); if ($feed->getDescription()) { $channel['description'] = $feed->getDescription(); } $items = array(); foreach ($threads as $thread) { $item = array(); $item['title'] = $thread->getTitle(); $item['link'] = "http://" . $site->getDomain() . "/forum/t-" . $thread->getThreadId() . '/' . $thread->getUnixifiedTitle(); $item['guid'] = $item['link']; $item['date'] = date('r', $thread->getDateStarted()->getTimestamp()); $item['category'] = $thread->getCategory()->getName(); //replace relative links with absolute links! $post = $thread->getFirstPost(); if (!$post) { continue; } $content = $post->getText(); $content = preg_replace(';(<.*?)(src|href)="/([^"]+)"([^>]*>);si', '\\1\\2="http://' . $site->getDomain() . '/\\3"\\4', $content); $content = preg_replace(';<script\\s+[^>]+>.*?</script>;is', '', $content); $content = preg_replace(';(<[^>]*\\s+)on[a-z]+="[^"]+"([^>]*>);si', '\\1 \\2', $content); if ($thread->getDescription()) { $item['description'] = $thread->getDescription(); } $item['content'] = $content; if ($post->getUserId() > 0) { $item['authorUserId'] = $post->getUserId(); $user = $post->getUser(); $item['author'] = $user->getNickName(); } else { $item['author'] = $post->getUserString(); } $items[] = $item; } $runData->contextAdd("channel", $channel); $runData->contextAdd("items", $items); }