id = $pageID; $this->setContext( $context ); $this->title = Title::newFromID( $pageID ); } /** * Gets the total amount of comments on this page * * @return int */ function countTotal() { $dbr = wfGetDB( DB_SLAVE ); $count = 0; $s = $dbr->selectRow( 'Comments', array( 'COUNT(*) AS CommentCount' ), array( 'Comment_Page_ID' => $this->id ), __METHOD__ ); if ( $s !== false ) { $count = $s->CommentCount; } return $count; } /** * Gets the ID number of the latest comment for the current page. * * @return int */ function getLatestCommentID() { $latestCommentID = 0; $dbr = wfGetDB( DB_SLAVE ); $s = $dbr->selectRow( 'Comments', array( 'CommentID' ), array( 'Comment_Page_ID' => $this->id ), __METHOD__, array( 'ORDER BY' => 'Comment_Date DESC', 'LIMIT' => 1 ) ); if ( $s !== false ) { $latestCommentID = $s->CommentID; } return $latestCommentID; } /** * Set voting either totally off, or disallow "thumbs down" or disallow * "thumbs up". * * @param string $voting 'OFF', 'PLUS' or 'MINUS' (will be strtoupper()ed) */ function setVoting( $voting ) { $this->voting = $voting; $voting = strtoupper( $voting ); if ( $voting == 'OFF' ) { $this->allowMinus = false; $this->allowPlus = false; } if ( $voting == 'PLUS' ) { $this->allowMinus = false; } if ( $voting == 'MINUS' ) { $this->allowPlus = false; } } /** * Fetches all comments, called by display(). * * @return array Array containing every possible bit of information about * a comment, including score, timestamp and more */ public function getComments() { $dbr = wfGetDB( DB_SLAVE ); // Defaults (for non-social wikis) $tables = array( 'Comments', 'vote1' => 'Comments_Vote', 'vote2' => 'Comments_Vote', ); $fields = array( 'Comment_Username', 'Comment_IP', 'Comment_Text', 'Comment_Date', 'Comment_Date AS timestamp', 'Comment_user_id', 'CommentID', 'Comment_Parent_ID', 'vote1.Comment_Vote_Score AS current_vote', 'SUM(vote2.Comment_Vote_Score) AS comment_score' ); $joinConds = array( // For current user's vote 'vote1' => array( 'LEFT JOIN', array( 'vote1.Comment_Vote_ID = CommentID', 'vote1.Comment_Vote_Username' => $this->getUser()->getName() ) ), // For total vote count 'vote2' => array( 'LEFT JOIN', 'vote2.Comment_Vote_ID = CommentID' ) ); $params = array( 'GROUP BY' => 'CommentID' ); // If SocialProfile is installed, query the user_stats table too. if ( class_exists( 'UserProfile' ) && $dbr->tableExists( 'user_stats' ) ) { $tables[] = 'user_stats'; $fields[] = 'stats_total_points'; $joinConds['Comments'] = array( 'LEFT JOIN', 'Comment_user_id = stats_user_id' ); } // Perform the query $res = $dbr->select( $tables, $fields, array( 'Comment_Page_ID' => $this->id ), __METHOD__, $params, $joinConds ); $comments = array(); foreach ( $res as $row ) { if ( $row->Comment_Parent_ID == 0 ) { $thread = $row->CommentID; } else { $thread = $row->Comment_Parent_ID; } $data = array( 'Comment_Username' => $row->Comment_Username, 'Comment_IP' => $row->Comment_IP, 'Comment_Text' => $row->Comment_Text, 'Comment_Date' => $row->Comment_Date, 'Comment_user_id' => $row->Comment_user_id, 'Comment_user_points' => ( isset( $row->stats_total_points ) ? number_format( $row->stats_total_points ) : 0 ), 'CommentID' => $row->CommentID, 'Comment_Parent_ID' => $row->Comment_Parent_ID, 'thread' => $thread, 'timestamp' => wfTimestamp( TS_UNIX, $row->timestamp ), 'current_vote' => ( isset( $row->current_vote ) ? $row->current_vote : false ), 'total_vote' => ( isset( $row->comment_score ) ? $row->comment_score : 0 ), ); $comments[] = new Comment( $this, $this->getContext(), $data ); } $commentThreads = array(); foreach ( $comments as $comment ) { if ( $comment->parentID == 0 ) { $commentThreads[$comment->id] = array( $comment ); } else { $commentThreads[$comment->parentID][] = $comment; } } return $commentThreads; } /** * @return int The page we are currently paged to * not used for any API calls */ function getCurrentPagerPage() { if ( $this->currentPagerPage == 0 ) { $this->currentPagerPage = $this->getRequest()->getInt( $this->pageQuery, 1 ); if ( $this->currentPagerPage < 1 ) { $this->currentPagerPage = 1; } } return $this->currentPagerPage; } /** * Display pager for the current page. * * @param int $pagerCurrent Page we are currently paged to * @param int $pagesCount The maximum page number * * @return string: the links for paging through pages of comments */ function displayPager( $pagerCurrent, $pagesCount ) { // Middle is used to "center" pages around the current page. $pager_middle = ceil( $this->pagerLimit / 2 ); // first is the first page listed by this pager piece (re quantity) $pagerFirst = $pagerCurrent - $pager_middle + 1; // last is the last page listed by this pager piece (re quantity) $pagerLast = $pagerCurrent + $this->pagerLimit - $pager_middle; // Prepare for generation loop. $i = $pagerFirst; if ( $pagerLast > $pagesCount ) { // Adjust "center" if at end of query. $i = $i + ( $pagesCount - $pagerLast ); $pagerLast = $pagesCount; } if ( $i <= 0 ) { // Adjust "center" if at start of query. $pagerLast = $pagerLast + ( 1 - $i ); $i = 1; } $output = ''; if ( $pagesCount > 1 ) { $output .= '