RatingExtension/Source Code
From PeacockWiki
Revision as of 01:55, 30 July 2006; Trevorp (Talk | contribs)
(diff) ←Older revision | Current revision | Newer revision→ (diff)
(diff) ←Older revision | Current revision | Newer revision→ (diff)
<?php #If html request requests file, serve file if(isset($_GET['file'])) doFile(); #If html request contains rate information, process it if(isset($_GET['rate'])) { #Init #---------------- require_once( 'includes/Setup.php' ); require_once( "includes/Wiki.php" ); $mediaWiki = new MediaWiki(); $action = $wgRequest->getVal( 'action', 'view' ); $title = $wgRequest->getVal( 'title' ); $wgTitle = $mediaWiki->checkInitialQueries( $title,$action,$wgOut, $wgRequest, $wgContLang ); #Record Rating #---------------- $sql = "CREATE TABLE IF NOT EXISTS `ratings` (\n". " `page_oldid` int(8) unsigned NOT NULL auto_increment,\n". " `user_id` int(11) NOT NULL,\n". " `page_rating` int(8) unsigned NOT NULL,\n". " PRIMARY KEY (`page_oldid`, `user_id`)\n". ") ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;"; $sql = "REPLACE INTO `ratings` (`page_oldid`, `user_id`, `page_rating`) VALUES (".$_GET['oldid'].", '".($wgUser->getID()?$wgUser->getID():$wgUser->getName())."', ".$_GET['rate'].")"; $dbr =& wfGetDB( DB_WRITE ); $res=wfQuery($sql, DB_WRITE, ""); #Return to refering page and exit #---------------- $wgTitle->invalidateCache(); header( 'Location: '.$_SERVER["HTTP_REFERER"] ) ; die(); } #get oldid of current page function getOldID($article) { $oldid=$article->getOldIDFromRequest(); if($oldid!=0) return $oldid; $dbr =& wfGetDB( DB_SLAVE ); $sql = "SELECT MAX(`rc_this_oldid`) AS `rc_this_oldid` FROM recentchanges WHERE rc_cur_id=".$article->getID().";"; $res=wfQuery($sql, DB_WRITE, ""); $row=$dbr->fetchObject( $res ); if($row->rc_this_oldid) return $row->rc_this_oldid; return null; } #run sql code function runQuery($sql) { $dbr =& wfGetDB( DB_SLAVE ); $res=wfQuery($sql, DB_SLAVE, ""); return $dbr->fetchObject( $res ); } #perform sql query to fetch rating for a given oldid function getRatingData($oldid) { $sql="SELECT COUNT(*) AS `count`, AVG(`page_rating`) AS `rating` FROM ratings WHERE `page_oldid`=".$oldid." GROUP BY `page_oldid`;"; return runQuery($sql); } #fetch/calculate rating from database function getRating($oldid) { global $wgTitle; $countlimit=3; $ratingdata=getRatingData($oldid); $finalrating='?'; $currentcount=number_format($ratingdata->count, 0); if($rating->count<$countlimit) { $count=$ratingdata->count; $rating=$count*$ratingdata->rating; # echo "not enough data ($rating/$count)\n"; while($oldid=$wgTitle->getPreviousRevisionID($oldid)) { # echo "trying previous ratings\n"; $ratingdata=getRatingData($oldid); if($count+$ratingdata->count<$countlimit) { $count+=$ratingdata->count; $rating+=$ratingdata->count*$ratingdata->rating; # echo "still not enough data ($rating/$count)\n"; } else { $rating+=($countlimit-$count)*$ratingdata->rating; $count=$countlimit; # echo "enough data ($rating/$count)\n"; $finalrating=$rating/$count; $oldid=false; } } } else $finalrating=$rating/$count; $finalrating=($finalrating-1)*1.25; $ratingarray=array('display'=>(is_numeric($finalrating)?number_format($finalrating, 2):$finalrating)." ($currentcount ratings)", 'count'=>$currentcount, 'rating'=>$finalrating); return $ratingarray; } #Generate html code to insert into page function getRatingHTML($rating, $oldid) { global $wgTitle, $wgScriptPath; $countlimit=3; $html=''; for($x=0;$x<=4;$x++) { $html.='<a href="'.$wgTitle->getFullURL('oldid='.$oldid.'&rate='.($x+1)).'">'. '<img src="'.$wgScriptPath.'/extensions/rating/star'; if($rating['rating']>=$x+1) $html.='4'.($rating['count']<$countlimit?'b':''); elseif($rating['rating']>$x+0.75) $html.='3'.($rating['count']<$countlimit?'b':''); elseif($rating['rating']>$x+0.5) $html.='2'.($rating['count']<$countlimit?'b':''); elseif($rating['rating']>$x+0.25) $html.='1'.($rating['count']<$countlimit?'b':''); else $html.='0'; $html.='.png" align=bottom/></a>'."\n"; } $html.=' '.$rating['display'].''; $html=($rating['count']<$countlimit?'<b>'.$html.'</b>':$html); return $html; } #Insert rating to top of page function InsertRating($parserOutput, $text) { global $wgArticle; $oldid=getOldID($wgArticle); if($oldid) $text='<div id="ratingsection">'.getRatingHTML(getRating($oldid), $oldid).'</div>'.$text; } #set up extension info if(isset($wgScriptPath)) { $wgExtensionCredits["parserhook"][]=array( 'name' => 'Rating Extension', 'version' => '0.1', 'url' => 'http://wiki.peacocktech.com/wiki/RatingExtension', 'author' => '[http://about.peacocktech.com/trevorp/ Trevor Peacock]', 'description' => 'Adds rating mechanism' ); $wgHooks['OutputPageBeforeHTML'][] = 'InsertRating'; $wgHooks['SkinTemplateSetupPageCss'][] = 'RatingCss'; } #Add CSS into skin for rating function RatingCss(&$css) { global $wgScriptPath; $css = "/*<![CDATA[*/". " @import \"$wgScriptPath/?file=rating.css\"; ". "/*]]>*/"; return true; } #Return file if requested function doFile() { switch ($_GET['file']) { case "rating.css": header("Content-type: text/css"); ?> #ratingsection { float: right; margin-top: -3.7em; padding: 3px; } #ratingsection b { // border: 1px solid black; color: red; padding: 4px; } <?php die(); } } ?>