/** * Get the 25 newest blog posts from the database and then cache them in * memcached for 15 minutes. * * @return String: HTML */ public function getNewestPosts() { global $wgMemc, $wgScriptPath; // Try cache first $key = wfMemcKey('blog', 'newest', 'twentyfive'); $data = $wgMemc->get($key); if ($data != '') { wfDebugLog('BlogPage', 'Got newest posts in ArticlesHome from cache'); $newestBlogPosts = $data; } else { wfDebugLog('BlogPage', 'Got newest posts in ArticlesHome from DB'); $dbr = wfGetDB(DB_SLAVE); // Code sporked from Rob Church's NewestPages extension $res = $dbr->select(array('page'), array('page_namespace', 'page_title', 'page_is_redirect', 'page_id'), array('page_namespace' => NS_BLOG, 'page_is_redirect' => 0), __METHOD__, array('ORDER BY' => 'page_id DESC', 'LIMIT' => 25)); $newestBlogPosts = array(); foreach ($res as $row) { $newestBlogPosts[] = array('title' => $row->page_title, 'ns' => $row->page_namespace, 'id' => $row->page_id); } // Cache in memcached for 15 minutes $wgMemc->set($key, $newestBlogPosts, 60 * 15); } $imgPath = $wgScriptPath . '/extensions/BlogPage/images/'; $output = '<div class="listpages-container">'; if (empty($newestBlogPosts)) { $output .= wfMsg('ah-no-results'); } else { foreach ($newestBlogPosts as $newestBlogPost) { $titleObj = Title::makeTitle(NS_BLOG, $newestBlogPost['title']); $output .= '<div class="listpages-item">'; $pageImage = BlogPage::getPageImage($newestBlogPost['id']); if ($pageImage) { // Load MediaWiki image object to get thumbnail tag $img = wfFindFile($pageImage); $imgTag = ''; if (is_object($img)) { $thumb = $img->getThumbnail(65, 0, true); $imgTag = $thumb->toHtml(); } $output .= "<div class=\"listpages-image\">{$imgTag}</div>\n"; } $output .= '<a href="' . $titleObj->escapeFullURL() . '">' . $titleObj->getText() . '</a> <div class="listpages-date">'; $output .= '(' . wfMsg('blog-created-ago', BlogPage::getTimeAgo(strtotime(BlogPage::getCreateDate($newestBlogPost['id'])))) . ')'; $output .= "</div>\n\t\t\t\t<div class=\"listpages-blurb\">\n" . BlogPage::getBlurb($newestBlogPost['title'], $newestBlogPost['ns'], 300) . '</div><!-- .listpages-blurb --> <div class="listpages-stats">' . "\n"; $output .= "<img src=\"{$imgPath}voteIcon.gif\" alt=\"\" border=\"0\" /> " . wfMsgExt('blog-author-votes', 'parsemag', BlogPage::getVotesForPage($newestBlogPost['id'])); $output .= " <img src=\"{$imgPath}comment.gif\" alt=\"\" border=\"0\" /> " . wfMsgExt('blog-author-comments', 'parsemag', BlogPage::getCommentsForPage($newestBlogPost['id'])) . '</div><!-- . listpages-stats --> </div><!-- .listpages-item --> <div class="cleared"></div>' . "\n"; } } $output .= '</div>' . "\n"; // .listpages-container return $output; }