User:Bill Flanagan/Extension/wikiHow/RateArticle

Description
A basic tool that uses Javascript and Ajax to allow users to rate pages and to view these ratings.

Installation
To install (this works on Mediawiki 1.6.7), download and untar this file in your based Mediawiki directory. This will install these files:

extensions/RateArticle.php skins/common/images/rating/ skins/common/images/rating/check-nohover.gif skins/common/images/rating/check-hover.gif skins/common/images/rating/check-10.gif skins/common/images/rating/check-0.gif skins/common/images/rating/check-1.gif skins/common/images/rating/check-2.gif skins/common/images/rating/check-3.gif skins/common/images/rating/check-4.gif skins/common/images/rating/check-5.gif skins/common/images/rating/check-6.gif skins/common/images/rating/check-7.gif skins/common/images/rating/check-8.gif skins/common/images/rating/check-9.gif

Unfortunately there's no hook in this version of Mediawiki that's called after a article's content is displayed, so you have to change the function 'html' in includes/SkinTemplate.php to:

function html( $str ) { echo $this->data[$str]; if ($str == 'bodytext') { wfRunHooks( 'AfterArticleDisplayed'); }   }

Alternatively, I think you could just call wfRateArticleForm from your skin directly. ( Yes, both are crappy hacks. )

Also, you have to create a database table to store the page rating information. Run the following SQL:

CREATE TABLE `rating` ( `rat_id` int(8) unsigned NOT NULL auto_increment,  `rat_page` int(8) unsigned NOT NULL default '0',  `rat_user` int(5) unsigned NOT NULL default '0',  `rat_user_text` varchar(255) NOT NULL default '',  `rat_timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP,  `rat_rating` tinyint(1) unsigned NOT NULL default '0',  PRIMARY KEY  (`rat_page`,`rat_id`),  UNIQUE KEY `rat_id` (`rat_id`),  KEY `rat_timestamp` (`rat_timestamp`) ) ENGINE=InnoDB

(note) If you are using prefixes for your database tables, name the ratings table $prefix_rating instead (eg. I use mw as prefix, so i had to name it mw_rating)

Spent some time to find this "error" =)

Also, edit in wfRateArticleForm function the line $res = $dbr->query("select avg(rat_rating) as R from rating where rat_page=$page_id;"); to $res = $dbr->query("select avg(rat_rating) as R from mw_rating where rat_page=$page_id;"); (this is an example of the mw prefix.

And, in the function wfSpecialListRatings make according changes to the $res = $dbr->query("select rat_page, avg(rat_rating) as R, count(*) as C from rating group by rat_page order by R DESC limit 50;");

Of course add this line to your LocalSettings.php:

require_once("$IP/extensions/RateArticle.php");

Configuration
If you don't want the check mark images to show the current rating and don't want the Special:ListRatings to display the best rated pages (privacy issues, just curious about using the data internally or waiting until enough data is gathered to display the ratings...), set $wgShowRatings = false; in RateArticle.php.

TODO

 * Add ability to view more than the top 50 rated articles.
 * A few options for smarter rating calculation (e.g. weight averages based on time, or on number of edits of an article...). Should be easy to do after some data is collected.