/** * Show the new special page * * @param $limit Integer: show this many entries (LIMIT for SQL) */ public function execute($limit) { global $wgMemc, $wgOut, $wgScriptPath; $wgOut->setPageTitle(wfMsg('ah-new-articles')); if (empty($limit)) { $limit = 25; } elseif (!empty($limit)) { $limit = intval($limit); } // Add some CSS for ListPages // @todo FIXME: this should be loaded when including the special page, // too, but if ( $this->including() ) does nothing, prolly because of // the parser cache $wgOut->addModules('ext.blogPage.articlesHome'); $imgPath = $wgScriptPath . '/extensions/BlogPage/images/'; $output = '<div class="left-articles">'; if (!$this->including()) { $output .= '<h2>' . wfMsg('ah-new-articles') . '</h2>'; } // Try cache first $key = wfMemcKey('blog', 'new', 'twentyfive'); $data = $wgMemc->get($key); if ($data != '') { wfDebugLog('BlogPage', 'Got new articles in ArticleLists from cache'); $newBlogPosts = $data; } else { wfDebugLog('BlogPage', 'Got new articles in ArticleLists from DB'); $dbr = wfGetDB(DB_SLAVE); // Code sporked from Rob Church's NewestPages extension // You rock, dude! $res = $dbr->select('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' => $limit)); $newBlogPosts = array(); foreach ($res as $row) { $newBlogPosts[] = array('title' => $row->page_title, 'ns' => $row->page_namespace, 'id' => $row->page_id); } // Cache in memcached for 15 minutes $wgMemc->set($key, $newBlogPosts, 60 * 15); } $output .= '<div class="listpages-container">' . "\n"; if (empty($newBlogPosts)) { $output .= wfMsg('ah-no-results'); } else { foreach ($newBlogPosts as $newBlogPost) { $titleObj = Title::makeTitle(NS_BLOG, $newBlogPost['title']); $output .= "\t\t\t\t" . '<div class="listpages-item">'; $pageImage = BlogPage::getPageImage($newBlogPost['id']); if ($pageImage) { // Load MediaWiki image object to get thumbnail tag $img = wfFindFile($pageImage); $imgTag = ''; if (is_object($img)) { $thumb = $img->transform(array('width' => 65, 'height' => 0)); $imgTag = $thumb->toHtml(); } $output .= "<div class=\"listpages-image\">{$imgTag}</div>\n"; } $output .= '<a href="' . $titleObj->escapeFullURL() . '">' . $titleObj->getText() . "</a>\n\t\t\t\t\t\t<div class=\"listpages-blurb\">\n" . BlogPage::getBlurb($newBlogPost['title'], $newBlogPost['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($newBlogPost['id'])); $output .= " <img src=\"{$imgPath}comment.gif\" alt=\"\" border=\"0\" /> " . wfMsgExt('blog-author-comments', 'parsemag', BlogPage::getCommentsForPage($newBlogPost['id'])) . '</div><!-- . listpages-stats --> </div><!-- .listpages-item --> <div class="cleared"></div>' . "\n"; } } $output .= '</div>' . "\n"; // .listpages-container $output .= '</div>' . "\n"; // .left-articles $wgOut->addHTML($output); }
/** * 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; }
function getAuthorArticles($author_index) { global $wgOut, $wgBlogPageDisplay, $wgMemc; if ($wgBlogPageDisplay['author_articles'] == false) { return ''; } $user_name = $this->authors[$author_index]['user_name']; $user_id = $this->authors[$author_index]['user_id']; $blogCat = wfMsgForContent('blog-category'); $archiveLink = Title::makeTitle(NS_CATEGORY, wfMsg('blog-by-user-category', $blogCat) . " {$user_name}"); $articles = array(); // Try cache first $key = wfMemcKey('blog', 'author', 'articles', $user_id); $data = $wgMemc->get($key); if ($data != '') { wfDebugLog('BlogPage', "Got blog author articles for user {$user_name} from cache"); $articles = $data; } else { wfDebugLog('BlogPage', "Got blog author articles for user {$user_name} from DB"); $dbr = wfGetDB(DB_SLAVE); $categoryTitle = Title::newFromText(wfMsg('blog-by-user-category', $blogCat) . " {$user_name}"); $res = $dbr->select(array('page', 'categorylinks'), array('DISTINCT(page_id) AS page_id', 'page_title'), array('cl_to' => array($categoryTitle->getDBkey()), 'page_namespace' => NS_BLOG), __METHOD__, array('ORDER BY' => 'page_id DESC', 'LIMIT' => 4), array('categorylinks' => array('INNER JOIN', 'cl_from = page_id'))); $array_count = 0; foreach ($res as $row) { if ($row->page_id != $this->getId() && $array_count < 3) { $articles[] = array('page_title' => $row->page_title, 'page_id' => $row->page_id); $array_count++; } } // Cache for half an hour $wgMemc->set($key, $articles, 60 * 30); } $output = ''; if (count($articles) > 0) { $css_fix = ''; if (count($this->getVotersList()) == 0 && count($this->getEditorsList()) == 0) { $css_fix = ' more-container-fix'; } $output .= "<div class=\"more-container{$css_fix}\">\n\t\t\t<h3>" . wfMsg('blog-author-more-by', $user_name) . '</h3>'; $x = 1; foreach ($articles as $article) { $articleTitle = Title::makeTitle(NS_BLOG, $article['page_title']); $output .= '<div class="author-article-item"> <a href="' . $articleTitle->escapeFullURL() . "\">{$articleTitle->getText()}</a>\n\t\t\t\t\t<div class=\"author-item-small\">" . wfMsgExt('blog-author-votes', 'parsemag', BlogPage::getVotesForPage($article['page_id'])) . ', ' . wfMsgExt('blog-author-comments', 'parsemag', BlogPage::getCommentsForPage($article['page_id'])) . '</div> </div>'; $x++; } $output .= '<div class="author-archive-link"> <a href="' . $archiveLink->escapeFullURL() . '">' . wfMsg('blog-view-archive-link') . '</a> </div> </div>'; } return $output; }
/** * Show a list of this user's blog articles in their user profile page. * * @param $userProfile Object: instance of UserProfilePage * @return Boolean: true */ public static function getArticles($userProfile) { global $wgUserProfileDisplay, $wgMemc, $wgOut; if (!$wgUserProfileDisplay['articles']) { return ''; } $user_name = $userProfile->user_name; $output = ''; // Try cache first $key = wfMemcKey('user', 'profile', 'articles', $userProfile->user_id); $data = $wgMemc->get($key); $articles = array(); if ($data != '') { wfDebugLog('BlogPage', "Got UserProfile articles for user {$user_name} from cache\n"); $articles = $data; } else { wfDebugLog('BlogPage', "Got UserProfile articles for user {$user_name} from DB\n"); $categoryTitle = Title::newFromText(wfMsgForContent('blog-by-user-category', wfMsgForContent('blog-category')) . " {$user_name}"); $dbr = wfGetDB(DB_SLAVE); /** * I changed the original query a bit, since it wasn't returning * what it should've. * I added the DISTINCT to prevent one page being listed five times * and added the page_namespace to the WHERE clause to get only * blog pages and the cl_from = page_id to the WHERE clause so that * the cl_to stuff actually, y'know, works :) */ $res = $dbr->select(array('page', 'categorylinks'), array('DISTINCT page_id', 'page_title', 'page_namespace'), array('cl_from = page_id', 'cl_to' => array($categoryTitle->getDBkey()), 'page_namespace' => NS_BLOG), __METHOD__, array('ORDER BY' => 'page_id DESC', 'LIMIT' => 5)); foreach ($res as $row) { $articles[] = array('page_title' => $row->page_title, 'page_namespace' => $row->page_namespace, 'page_id' => $row->page_id); } $wgMemc->set($key, $articles, 60); } // Load opinion count via user stats; $stats = new UserStats($userProfile->user_id, $user_name); $stats_data = $stats->getUserStats(); $articleCount = $stats_data['opinions_created']; $articleLink = Title::makeTitle(NS_CATEGORY, wfMsgForContent('blog-by-user-category', wfMsgForContent('blog-category')) . " {$user_name}"); if (count($articles) > 0) { $output .= '<div class="user-section-heading"> <div class="user-section-title">' . wfMsg('blog-user-articles-title') . '</div> <div class="user-section-actions"> <div class="action-right">'; if ($articleCount > 5) { $output .= '<a href="' . $articleLink->escapeFullURL() . '" rel="nofollow">' . wfMsg('user-view-all') . '</a>'; } $output .= '</div> <div class="action-left">' . wfMsgExt('user-count-separator', 'parsemag', count($articles), $articleCount) . '</div> <div class="cleared"></div> </div> </div> <div class="cleared"></div> <div class="user-articles-container">'; $x = 1; foreach ($articles as $article) { $articleTitle = Title::makeTitle($article['page_namespace'], $article['page_title']); $voteCount = BlogPage::getVotesForPage($article['page_id']); $commentCount = BlogPage::getCommentsForPage($article['page_id']); if ($x == 1) { $divClass = 'article-item-top'; } else { $divClass = 'article-item'; } $output .= '<div class="' . $divClass . "\">\n\t\t\t\t\t<div class=\"number-of-votes\">\n\t\t\t\t\t\t<div class=\"vote-number\">{$voteCount}</div>\n\t\t\t\t\t\t<div class=\"vote-text\">" . wfMsgExt('blog-user-articles-votes', 'parsemag', $voteCount) . '</div> </div> <div class="article-title"> <a href="' . $articleTitle->escapeFullURL() . "\">{$articleTitle->getText()}</a>\n\t\t\t\t\t\t<span class=\"item-small\">" . wfMsgExt('blog-user-article-comment', 'parsemag', $commentCount) . '</span> </div> <div class="cleared"></div> </div>'; $x++; } $output .= '</div>'; } $wgOut->addHTML($output); return true; }