/**
     * 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 most popular blog posts from the database and then cache them
     * in memcached for 15 minutes.
     * The definition of 'popular' is very arbitrary at the moment.
     *
     * Fork of the original getPopularPosts() method, the only thing changed
     * here is the HTML output which was toned down and count changed from 25
     * to 10.
     *
     * @return String: HTML
     */
    public function getPopularPostsForRightSide()
    {
        global $wgMemc;
        // Try cache first
        $key = wfMemcKey('blog', 'popular', 'ten');
        $data = $wgMemc->get($key);
        if ($data != '') {
            wfDebugLog('BlogPage', 'Got popular posts in ArticlesHome from cache');
            $popularBlogPosts = $data;
        } else {
            wfDebugLog('BlogPage', 'Got popular posts in ArticlesHome from DB');
            $dbr = wfGetDB(DB_SLAVE);
            $commentsTable = $dbr->tableName('Comments');
            $voteTable = $dbr->tableName('Vote');
            // Code sporked from Rob Church's NewestPages extension
            $res = $dbr->select(array('page', 'Comments', 'Vote'), array('DISTINCT page_id', 'page_namespace', 'page_title', 'page_is_redirect'), array('page_namespace' => NS_BLOG, 'page_is_redirect' => 0, 'page_id = Comment_Page_ID', 'page_id = vote_page_id', "((SELECT COUNT(*) FROM {$voteTable} WHERE vote_page_id = page_id) >= 5 OR\n\t\t\t\t\t(SELECT COUNT(*) FROM {$commentsTable} WHERE Comment_Page_ID = page_id) >= 5)"), __METHOD__, array('ORDER BY' => 'page_id DESC', 'LIMIT' => 10), array('Comments' => array('INNER JOIN', 'page_id = Comment_Page_ID'), 'Vote' => array('INNER JOIN', 'page_id = vote_page_id')));
            $popularBlogPosts = array();
            foreach ($res as $row) {
                $popularBlogPosts[] = array('title' => $row->page_title, 'ns' => $row->page_namespace, 'id' => $row->page_id);
            }
            // Cache in memcached for 15 minutes
            $wgMemc->set($key, $popularBlogPosts, 60 * 15);
        }
        $output = '<div class="listpages-container">';
        if (empty($popularBlogPosts)) {
            $output .= wfMsg('ah-no-results');
        } else {
            foreach ($popularBlogPosts as $popularBlogPost) {
                $titleObj = Title::makeTitle(NS_BLOG, $popularBlogPost['title']);
                $votes = BlogPage::getVotesForPage($popularBlogPost['id']);
                $output .= '<div class="listpages-item">';
                $output .= '<div class="listpages-votebox">' . "\n";
                $output .= '<div class="listpages-votebox-number">' . $votes . "</div>\n";
                $output .= '<div class="listpages-votebox-text">' . wfMsgExt('blog-author-votes', 'parsemag', $votes) . "</div>\n";
                // .listpages-votebox-text
                $output .= '</div>' . "\n";
                // .listpages-votebox
                $output .= '<a href="' . $titleObj->escapeFullURL() . '">' . $titleObj->getText() . '</a>
					</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;
    }