From 5daeba21b7c9dd6f88b3cd45e1f5061f40610531 Mon Sep 17 00:00:00 2001 From: saper Date: Sun, 1 Dec 2013 00:19:47 +0100 Subject: [PATCH] PostgreSQL support for VoteNY Use integer value for votes to count averages. Initial contributor: Sebastian Fiedler Bug: 51441 Change-Id: I9c09b84d3652449d3328586332c636a7a676273a --- Vote.js | 2 +- VoteClass.php | 12 +++++++----- VoteHooks.php | 12 ++++++++---- vote.sql => vote.mysql | 0 vote.postgres | 21 +++++++++++++++++++++ 5 files changed, 37 insertions(+), 10 deletions(-) rename vote.sql => vote.mysql (100%) create mode 100644 vote.postgres diff --git a/Vote.js b/Vote.js index f1ca0bb..021e398 100644 --- a/Vote.js +++ b/Vote.js @@ -165,4 +165,4 @@ jQuery( document ).ready( function() { jQuery( this ).data( 'vote-id' ) ); } ); -} ); \ No newline at end of file +} ); diff --git a/VoteClass.php b/VoteClass.php index 60e4e7f..250c7c3 100644 --- a/VoteClass.php +++ b/VoteClass.php @@ -45,13 +45,13 @@ class Vote { $vote_count = 0; $res = $dbr->select( 'Vote', - 'COUNT(*) AS VoteCount', + 'COUNT(*) AS votecount', array( 'vote_page_id' => $this->PageID ), __METHOD__ ); $row = $dbr->fetchObject( $res ); if( $row ) { - $vote_count = $row->VoteCount; + $vote_count = $row->votecount; } $wgMemc->set( $key, $vote_count ); } @@ -76,13 +76,13 @@ class Vote { $dbr = wfGetDB( DB_SLAVE ); $res = $dbr->select( 'Vote', - 'AVG(vote_value) AS VoteAvg', + 'AVG(vote_value) AS voteavg', array( 'vote_page_id' => $this->PageID ), __METHOD__ ); $row = $dbr->fetchObject( $res ); if( $row ) { - $voteAvg = $row->VoteAvg; + $voteAvg = $row->voteavg; } $wgMemc->set( $key, $voteAvg ); } @@ -120,6 +120,7 @@ class Vote { */ function delete() { $dbw = wfGetDB( DB_MASTER ); + $dbw->begin(); $dbw->delete( 'Vote', array( @@ -149,6 +150,7 @@ class Vote { $voteDate = date( 'Y-m-d H:i:s' ); wfRestoreWarnings(); if( $this->UserAlreadyVoted() == false ) { + $dbw->begin(); $dbw->insert( 'Vote', array( @@ -276,7 +278,7 @@ class VoteStars extends Vote { $output .= '
'; $output .= $this->displayStars( $id, $display_stars_rating, $voted ); $count = $this->count(); - if( $count ) { + if( isset( $count ) ) { $output .= ' (' . wfMsgExt( 'voteny-votes', 'parsemag', $count ) . ')'; } diff --git a/VoteHooks.php b/VoteHooks.php index eb69ae6..f028b57 100644 --- a/VoteHooks.php +++ b/VoteHooks.php @@ -149,9 +149,13 @@ class VoteHooks { * @return Boolean: true */ public static function addTable( $updater ) { - $dir = dirname( __FILE__ ); - $file = "$dir/vote.sql"; - $updater->addExtensionUpdate( array( 'addTable', 'Vote', $file, true ) ); + $dbt = $updater->getDB()->getType(); + $file = dirname( __FILE__ ) . "/vote.$dbt"; + if ( file_exists( $file ) ) { + $updater->addExtensionUpdate( array( 'addTable', 'Vote', $file, true ) ); + } else { + throw new MWException("VoteNY does not support $dbt."); + } return true; } -} \ No newline at end of file +} diff --git a/vote.sql b/vote.mysql similarity index 100% rename from vote.sql rename to vote.mysql diff --git a/vote.postgres b/vote.postgres new file mode 100644 index 0000000..81f397f --- /dev/null +++ b/vote.postgres @@ -0,0 +1,21 @@ +CREATE TABLE "Vote" ( + -- Internal ID to identify between different vote tags on different pages + vote_id SERIAL NOT NULL PRIMARY KEY, + -- Username (if any) of the person who voted + username varchar(255) NOT NULL default '0', + -- User ID of the person who voted + vote_user_id integer NOT NULL default '0', + -- ID of the page where the vote tag is in + vote_page_id integer NOT NULL default '0', + -- Value of the vote (ranging from 1 to 5) + vote_value integer NOT NULL, + -- Timestamp when the vote was cast + vote_date timestamp without time zone NOT NULL, + -- IP address of the user who voted + vote_ip varchar(45) NOT NULL default '' +) /*$wgDBTableOptions*/; + +CREATE INDEX vote_page_id_index ON "Vote" (vote_page_id); +CREATE INDEX valueidx ON "Vote" (vote_value); +CREATE INDEX usernameidx ON "Vote" (username); +CREATE INDEX vote_date ON "Vote" (vote_date);