https://www.mediawiki.org/wiki/Special:Code/MediaWiki/103372

Revision: 103372
Author:   gregchiasson
Date:     2011-11-16 20:29:30 +0000 (Wed, 16 Nov 2011)
Log Message:
-----------
Enable rollup tables for ratings in AFT5. Ratings still aren't being passed 
from the JS to the PHP, but once that's worked out this should be functional.

Modified Paths:
--------------
    trunk/extensions/ArticleFeedbackv5/ArticleFeedbackv5.hooks.php
    trunk/extensions/ArticleFeedbackv5/api/ApiArticleFeedbackv5.php
    trunk/extensions/ArticleFeedbackv5/api/ApiArticleFeedbackv5Utils.php

Modified: trunk/extensions/ArticleFeedbackv5/ArticleFeedbackv5.hooks.php
===================================================================
--- trunk/extensions/ArticleFeedbackv5/ArticleFeedbackv5.hooks.php      
2011-11-16 20:05:35 UTC (rev 103371)
+++ trunk/extensions/ArticleFeedbackv5/ArticleFeedbackv5.hooks.php      
2011-11-16 20:29:30 UTC (rev 103372)
@@ -187,13 +187,15 @@
                                $fields = 
ApiArticleFeedbackv5Utils::getFields();
                                $prefix = 'articlefeedbackv5-bucket5-';
                                foreach( $fields as $field ) {
-                                       $resources['messages'][] = $prefix . 
$field->afi_name . '-label';
-                                       $resources['messages'][] = $prefix . 
$field->afi_name . '-tip';
-                                       $resources['messages'][] = $prefix . 
$field->afi_name . '-tooltip-1';
-                                       $resources['messages'][] = $prefix . 
$field->afi_name . '-tooltip-2';
-                                       $resources['messages'][] = $prefix . 
$field->afi_name . '-tooltip-3';
-                                       $resources['messages'][] = $prefix . 
$field->afi_name . '-tooltip-4';
-                                       $resources['messages'][] = $prefix . 
$field->afi_name . '-tooltip-5';
+                                       if($fielf->afi_data_type == 'rating') {
+                                               $resources['messages'][] = 
$prefix . $field->afi_name . '-label';
+                                               $resources['messages'][] = 
$prefix . $field->afi_name . '-tip';
+                                               $resources['messages'][] = 
$prefix . $field->afi_name . '-tooltip-1';
+                                               $resources['messages'][] = 
$prefix . $field->afi_name . '-tooltip-2';
+                                               $resources['messages'][] = 
$prefix . $field->afi_name . '-tooltip-3';
+                                               $resources['messages'][] = 
$prefix . $field->afi_name . '-tooltip-4';
+                                               $resources['messages'][] = 
$prefix . $field->afi_name . '-tooltip-5';
+                                       }
                                }
                        }
                        $resourceLoader->register(

Modified: trunk/extensions/ArticleFeedbackv5/api/ApiArticleFeedbackv5.php
===================================================================
--- trunk/extensions/ArticleFeedbackv5/api/ApiArticleFeedbackv5.php     
2011-11-16 20:05:35 UTC (rev 103371)
+++ trunk/extensions/ArticleFeedbackv5/api/ApiArticleFeedbackv5.php     
2011-11-16 20:29:30 UTC (rev 103372)
@@ -16,12 +16,11 @@
                $token = ApiArticleFeedbackv5Utils::getAnonToken( $params );
 
                // Is feedback enabled on this page check?
-               if ( !ApiArticleFeedbackv5Utils::isFeedbackEnabled( $params ) ) 
{
-                       $this->dieUsage( 'ArticleFeedback is not enabled on 
this page', 'invalidpage' );
-               }
+#              if ( !ApiArticleFeedbackv5Utils::isFeedbackEnabled( $params ) ) 
{
+#                      $this->dieUsage( 'ArticleFeedback is not enabled on 
this page', 'invalidpage' );
+#              }
 
                $feedbackId   = $this->getFeedbackId($params);
-error_log("feedback id is $feedbackId");
                $dbr          = wfGetDB( DB_SLAVE );
                $keys         = array();
                foreach($params as $key => $unused) { $keys[] = $key; }
@@ -37,17 +36,13 @@
                );
 
                foreach($answers as $answer) {
-                       $type = $answer->afi_data_type;
-                       # TODO: validation
-                       # rating: int between 1 and 5 (inclusive)
-                       # boolean: 1 or 0
-                       # option:  option exists
-                       # text:    none (maybe xss encode)
-                       if($params[$answer->afi_name]) {
+                       $type  = $answer->afi_data_type;
+                       $value = $params[$answer->afi_name];
+                       if($value && $this->validateParam($value, $type)) {
                                $user_answers[] = array(
                                        'aa_feedback_id'    => $feedbackId,
                                        'aa_field_id'       => $answer->afi_id,
-                                       "aa_response_$type" => 
$params[$answer->afi_name]
+                                       "aa_response_$type" => $value
                                );
                        }
                }
@@ -78,31 +73,131 @@
                );
        }
 
-       public function updateRollupTables($page, $revision) {
-               $this->updateRatingRollup($page, $revision);
-               $this->updateSelectRollup($page, $revision);
-       }
+       protected function validateParams($value, $type) {
+               # rating: int between 1 and 5 (inclusive)
+               # boolean: 1 or 0
+               # option:  option exists
+               # text:    none (maybe xss encode)
+               switch($type) {
+                       case 'rating':
+                               if(preg_match('/^(1|2|3|4|5)$/', $value)) {
+                                       return 1;
+                               }
+                               break;
+                       case 'boolean':
+                               if(preg_match('/^(1|0)$/', $value)) {
+                                       return 1;
+                               }
+                               break;
+                       case 'option':
+                               # TODO: check for option existance.
+                       case 'text':
+                               return 1;
+                               break;
+                       default:
+                               return 0;
+                               break;
+               }
 
-       public function updateRatingRollup($page, $rev) {
-               $this->__updateRollup($page, $rev, 'ratings', 'page');
-               $this->__updateRollup($page, $rev, 'ratings', 'revision');
+               return 0;
        }
 
-       public function updateSelectRollup($page, $rev) {
-               $this->__updateRollup($page, $rev, 'select', 'page');
-               $this->__updateRollup($page, $rev, 'select', 'revision');
+       public function updateRollupTables($page, $revision) {
+#              foreach( array( 'rating', 'boolean', 'select' ) as $type ) {
+#              foreach( array( 'rating', 'boolean' ) as $type ) {
+               foreach( array( 'rating' ) as $type ) {
+                       $this->updateRollup( $page, $revision, $type );
+               }
        }
 
-       # page and rev and page and revision ids
-       # type is either ratings or select, the two rollups we have
-       # scope is either page or revision
-       private function __updateRollup($page, $rev, $type, $scope) {
+       # TODO: This breaks on the select/option_id type.
+       private function updateRollup($pageId, $revId, $type) {
+               global $wgArticleFeedbackv5RatingLifetime;
+               $dbr   = wfGetDB( DB_SLAVE );
+               $dbw   = wfGetDB( DB_MASTER );
+               $limit = ApiArticleFeedbackv5Utils::getRevisionLimit($pageId);
+
                # sanity check
-               if($type != 'ratings' && $type != 'select') { return 0; }
-               if($scope != 'page' && $scope != 'revision') { return 0; }
+               if( $type != 'rating' && $type != 'select' 
+                && $type != 'boolean' ) { return 0; }
 
-               # TODO
-               $table = 'aft_article_'.$rev.'_feedback_'.$type.'_rollup';
+               $rows = $dbr->select(
+                       array( 'aft_article_answer', 'aft_article_feedback', 
+                        'aft_article_field' ),
+                       array( 'aa_field_id', 
+                        "SUM(aa_response_$type) AS earned", 
+                        'COUNT(*) AS submits'),
+                       array(
+                               'afi_data_type'  => $type,
+                               'af_page_id'     => $pageId,
+                               'aa_feedback_id = af_id',
+                               'afi_id = aa_field_id',
+                               "af_revision_id >= $limit"
+                       ),
+                       __METHOD__,
+                       array( 'GROUP BY' =>  'aa_field_id' )
+               );
+
+               if( $type == 'select' ) {
+                       $page_prefix = 'afsr_';
+                       $rev_prefix  = 'arfsr_';
+               } else {
+                       $page_prefix = 'arr_';
+                       $rev_prefix  = 'afrr_';
+               }
+               $page_data  = array();
+               $rev_data   = array();
+               $rev_table  = 'aft_article_revision_feedback_'
+                .( $type == 'select' ? 'select' : 'ratings' ).'_rollup'; 
+               $page_table = 'aft_article_feedback_'
+                .( $type == 'select' ? 'select' : 'ratings' ).'_rollup'; 
+
+               foreach($rows as $row) {
+                       if($type == 'rating') {
+                               $points = $row->submits * 5;
+                       } else {
+                               $points = $row->submits;
+                       }
+
+                       if(!array_key_exists($row->aa_field_id, $page_data)) {
+                               $page_data[$row->aa_field_id] = array(
+                                       $page_prefix.'page_id' => $pageId,
+                                       $page_prefix.'total'   => 0,
+                                       $page_prefix.'count'   => 0,
+                                       $page_prefix.($type == 'select' ? 
'option' : 'rating').'_id' => $row->aa_field_id
+                               );
+                       }
+
+                       $rev_data[] = array(
+                               $rev_prefix.'page_id'     => $pageId,
+                               $rev_prefix.'revision_id' => $revId,
+                               $rev_prefix.'total'       => $row->earned,
+                               $rev_prefix.'count'       => $points,
+                               $rev_prefix.($type == 'select' ? 'option' : 
'rating').'_id' => $row->aa_field_id
+                       );
+                       $page_data[$row->aa_field_id][$page_prefix.'total'] += 
$row->earned;
+                       $page_data[$row->aa_field_id][$page_prefix.'count'] += 
$points;
+               }
+
+               # Hack becuse you can't use array keys or insert barfs.
+               $tmp = array();
+               foreach($page_data as $p) {
+                       $tmp[] = $p;
+               }
+               $page_data = $tmp;
+
+               $dbw->begin();
+               $dbw->delete( $rev_table, array(
+                       $rev_prefix.'page_id'     => $pageId,
+                       $rev_prefix.'revision_id' => $revId,
+               ) );
+               $dbw->insert( $rev_table,  $rev_data );
+               $dbw->delete( $page_table, array(
+                       $page_prefix.'page_id'     => $pageId,
+               ) );
+               $dbw->insert( $page_table, $page_data );
+
+               $dbw->commit();
        }
 
        public function getFeedbackId($params) {

Modified: trunk/extensions/ArticleFeedbackv5/api/ApiArticleFeedbackv5Utils.php
===================================================================
--- trunk/extensions/ArticleFeedbackv5/api/ApiArticleFeedbackv5Utils.php        
2011-11-16 20:05:35 UTC (rev 103371)
+++ trunk/extensions/ArticleFeedbackv5/api/ApiArticleFeedbackv5Utils.php        
2011-11-16 20:29:30 UTC (rev 103372)
@@ -38,6 +38,23 @@
                return 1;
        }
 
+       public static function getRevisionLimit( $pageId ) {
+               global $wgArticleFeedbackv5RatingLifetime;
+               $dbr      = wfGetDB( DB_SLAVE );
+               $revision = $dbr->selectField(
+                       'revision', 'rev_id',
+                       array( 'rev_page' => $pageId ),
+                       __METHOD__,
+                       array(
+                               'ORDER BY' => 'rev_id DESC',
+                               'LIMIT'    => 1,
+                               'OFFSET'   => 
$wgArticleFeedbackv5RatingLifetime - 1
+                       )
+               );
+
+               return $revision ? intval($revision) : 0;
+       }
+
        public static function getRevisionId($pageId) {
                $dbr   = wfGetDB( DB_SLAVE );
                $revId = $dbr->selectField(


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to