http://www.mediawiki.org/wiki/Special:Code/MediaWiki/84535

Revision: 84535
Author:   tparscal
Date:     2011-03-22 17:18:19 +0000 (Tue, 22 Mar 2011)
Log Message:
-----------
Step 2 of 2 for implementing expirations.
* Added revision column to page table which is part of the primary key
* Modified ApiQueryArticleFeedback to sum up counts and totals for all valid 
revisions
* Modified ApiArticleFeedback to automatically subtract totals and counts from 
previous revision's tally

Modified Paths:
--------------
    trunk/extensions/ArticleFeedback/ArticleFeedback.hooks.php
    trunk/extensions/ArticleFeedback/api/ApiArticleFeedback.php
    trunk/extensions/ArticleFeedback/api/ApiQueryArticleFeedback.php
    trunk/extensions/ArticleFeedback/sql/ArticleFeedback.sql

Added Paths:
-----------
    trunk/extensions/ArticleFeedback/sql/AddPageRevisionColumn.sql

Modified: trunk/extensions/ArticleFeedback/ArticleFeedback.hooks.php
===================================================================
--- trunk/extensions/ArticleFeedback/ArticleFeedback.hooks.php  2011-03-22 
17:18:15 UTC (rev 84534)
+++ trunk/extensions/ArticleFeedback/ArticleFeedback.hooks.php  2011-03-22 
17:18:19 UTC (rev 84535)
@@ -137,6 +137,15 @@
                                        true
                                ) );
                        }
+                       if ( !$db->fieldExists( 'article_feedback_pages', 
'aap_revision', __METHOD__ ) ) {
+                               $updater->addExtensionUpdate( array(
+                                       'addField',
+                                       'article_feedback_pages',
+                                       'aap_revision',
+                                       $dir . '/sql/AddPageRevisionColumn.sql',
+                                       true
+                               ) );
+                       }
                        $updater->addExtensionUpdate( array(
                                'addTable',
                                'article_feedback_properties',
@@ -156,7 +165,7 @@
                }
                return true;
        }
-       
+
        /**
         * ParserTestTables hook
         */

Modified: trunk/extensions/ArticleFeedback/api/ApiArticleFeedback.php
===================================================================
--- trunk/extensions/ArticleFeedback/api/ApiArticleFeedback.php 2011-03-22 
17:18:15 UTC (rev 84534)
+++ trunk/extensions/ArticleFeedback/api/ApiArticleFeedback.php 2011-03-22 
17:18:19 UTC (rev 84535)
@@ -45,7 +45,8 @@
                $lastRatings = array();
 
                foreach ( $res as $row ) {
-                       $lastRatings[$row->aa_rating_id] = 
$row->aa_rating_value;
+                       $lastRatings[$row->aa_rating_id]['value'] = 
$row->aa_rating_value;
+                       $lastRatings[$row->aa_rating_id]['revision'] = 
$row->aa_revision;
                }
 
                $pageId = $params['pageid'];
@@ -54,7 +55,8 @@
                foreach( $wgArticleFeedbackRatings as $rating ) {
                        $lastRating = false;
                        if ( isset( $lastRatings[$rating] ) ) {
-                               $lastRating = intval( $lastRatings[$rating] );
+                               $lastRating = intval( 
$lastRatings[$rating]['value'] );
+                               $lastRevision = intval( 
$lastRatings[$rating]['revision'] );
                        }
 
                        $thisRating = false;
@@ -62,7 +64,7 @@
                                $thisRating = intval( $params["r{$rating}"] );
                        }
 
-                       $this->insertPageRating( $pageId, $rating, ( 
$thisRating - $lastRating ),
+                       $this->insertPageRating( $pageId, $revisionId, 
$lastRevision, $rating, ( $thisRating - $lastRating ),
                                        $thisRating, $lastRating
                        );
 
@@ -79,32 +81,15 @@
         * Inserts (or Updates, where appropriate) the aggregate page rating
         * 
         * @param $pageId Integer: Page Id
+        * @param $revisionId Integer: Revision Id
         * @param $ratingId Integer: Rating Id
         * @param $updateAddition Integer: Difference between user's last 
rating (if applicable)
         * @param $thisRating Integer|Boolean: Value of the Rating
         * @param $lastRating Integer|Boolean: Value of the last Rating
         */
-       private function insertPageRating( $pageId, $ratingId, $updateAddition, 
$thisRating, $lastRating ) {
+       private function insertPageRating( $pageId, $revisionId, $lastRevision, 
$ratingId, $updateAddition, $thisRating, $lastRating ) {
                $dbw = wfGetDB( DB_MASTER );
 
-               // 0 == No change in rating count
-               // 1 == No rating last time (or new rating), and now there is
-               // -1 == Rating last time, but abstained this time
-               $countChange = 0;
-               if ( $lastRating === false || $lastRating === 0 ) {
-                       if ( $thisRating === 0 ) {
-                               $countChange = 0;
-                       } else {
-                               $countChange = 1;
-                       }
-               } else { // Last rating was > 0
-                       if ( $thisRating === 0 ) {
-                               $countChange = -1;
-                       } else {
-                               $countChange = 0;
-                       }
-               }
-
                $dbw->insert(
                        'article_feedback_pages',
                         array(
@@ -112,23 +97,73 @@
                                'aap_total' => 0,
                                'aap_count' => 0,
                                'aap_rating_id' => $ratingId,
+                               'aap_revision' => $revisionId,
                        ),
                        __METHOD__,
                         array( 'IGNORE' )
                );
 
-               $dbw->update(
-                       'article_feedback_pages',
-                       array(
-                               'aap_total = aap_total + ' . $updateAddition,
-                               'aap_count = aap_count + ' . $countChange,
-                       ),
-                       array(
-                               'aap_page_id' => $pageId,
-                               'aap_rating_id' => $ratingId,
-                       ),
-                       __METHOD__
-               );
+               if ( $dbw->affectedRows() ) {
+                       if ( $lastRating ) {
+                               $dbw->update(
+                                       'article_feedback_pages',
+                                       array(
+                                               'aap_total = aap_total - ' . 
intval( $lastRating ),
+                                               'aap_count = aap_count - 1',
+                                       ),
+                                       array(
+                                               'aap_page_id' => $pageId,
+                                               'aap_rating_id' => $ratingId,
+                                               'aap_revision' => $lastRevision
+                                       ),
+                                       __METHOD__
+                               );
+                       }
+                       $dbw->update(
+                               'article_feedback_pages',
+                               array(
+                                       'aap_total' => $thisRating,
+                                       'aap_count' => 1,
+                               ),
+                               array(
+                                       'aap_page_id' => $pageId,
+                                       'aap_rating_id' => $ratingId,
+                                       'aap_revision' => $revisionId,
+                               ),
+                               __METHOD__
+                       );
+               } else {
+                       // 0 == No change in rating count
+                       // 1 == No rating last time (or new rating), and now 
there is
+                       // -1 == Rating last time, but abstained this time
+                       $countChange = 0;
+                       if ( $lastRating === false || $lastRating === 0 ) {
+                               if ( $thisRating === 0 ) {
+                                       $countChange = 0;
+                               } else {
+                                       $countChange = 1;
+                               }
+                       } else { // Last rating was > 0
+                               if ( $thisRating === 0 ) {
+                                       $countChange = -1;
+                               } else {
+                                       $countChange = 0;
+                               }
+                       }
+                       $dbw->update(
+                               'article_feedback_pages',
+                               array(
+                                       'aap_total = aap_total + ' . 
$updateAddition,
+                                       'aap_count = aap_count + ' . 
$countChange,
+                               ),
+                               array(
+                                       'aap_page_id' => $pageId,
+                                       'aap_rating_id' => $ratingId,
+                                       'aap_revision' => $revisionId,
+                               ),
+                               __METHOD__
+                       );
+               }
        }
 
        /**
@@ -153,12 +188,12 @@
                                'aa_page_id' => $pageId,
                                'aa_user_id' => $user->getId(),
                                'aa_user_text' => $user->getName(),
+                               'aa_user_anon_token' => $token,
                                'aa_revision' => $revisionId,
                                'aa_timestamp' => $timestamp,
                                'aa_rating_id' => $ratingId,
                                'aa_rating_value' => $ratingValue,
                                'aa_design_bucket' => $bucket,
-                               'aa_user_anon_token' => $token
                        ),
                        __METHOD__,
                         array( 'IGNORE' )
@@ -176,7 +211,7 @@
                                        'aa_user_text' => $user->getName(),
                                        'aa_revision' => $revisionId,
                                        'aa_rating_id' => $ratingId,
-                                       'aa_user_anon_token' => $token
+                                       'aa_user_anon_token' => $token,
                                ),
                                __METHOD__
                        );

Modified: trunk/extensions/ArticleFeedback/api/ApiQueryArticleFeedback.php
===================================================================
--- trunk/extensions/ArticleFeedback/api/ApiQueryArticleFeedback.php    
2011-03-22 17:18:15 UTC (rev 84534)
+++ trunk/extensions/ArticleFeedback/api/ApiQueryArticleFeedback.php    
2011-03-22 17:18:19 UTC (rev 84535)
@@ -10,9 +10,13 @@
 
                $result = $this->getResult();
 
+               $revisionLimit = self::getRevisionLimit( $params['pageid'] );
+
                $this->addTables( array( 'article_feedback_pages', 
'article_feedback_ratings' ) );
 
-               $this->addFields( array( 'aap_page_id', 'aap_total', 
'aap_count', 'aap_rating_id', 'aar_rating' ) );
+               $this->addFields( array(
+                       'aap_page_id', 'SUM(aap_total) as aap_total', 
'SUM(aap_count) as aap_count', 'aap_rating_id', 'aar_rating'
+               ) );
 
                $this->addJoinConds( array(
                                'article_feedback_ratings' => array( 'LEFT 
JOIN', array(
@@ -23,7 +27,11 @@
                ) );
 
                $this->addWhereFld( 'aap_page_id', $params['pageid'] );
+               $this->addWhereFld( 'aap_revision > ' . $revisionLimit );
 
+               $this->addOption( 'GROUP BY', 'aap_rating_id' );
+               $this->addOption( 'LIMIT', count( $wgArticleFeedbackRatings ) );
+
                if ( $params['userrating'] ) {
                        global $wgUser;
 
@@ -43,7 +51,6 @@
                                } elseif ( strlen( $params['anontoken'] ) != 32 
) {
                                        $this->dieUsage( 'The anontoken is not 
32 characters', 'invalidtoken' );
                                }
-
                                $leftJoinCondsAF['aa_user_anon_token'] = 
$params['anontoken'];
                        } else {
                                $leftJoinCondsAF['aa_user_anon_token'] = '';
@@ -61,8 +68,6 @@
                        $this->addOption( 'ORDER BY', 'aa_revision DESC' );
                }
 
-               $this->addOption( 'LIMIT', count( $wgArticleFeedbackRatings ) );
-
                $res = $this->select( __METHOD__ );
 
                $ratings = array();
@@ -106,13 +111,12 @@
                // Ratings can only be expired if the user has rated before
                $ratings[$params['pageid']]['status'] = 'current';
                if ( $params['userrating'] && $userRatedArticle ) {
-                       $dbr = wfGetDb( DB_SLAVE );
-
                        global $wgArticleFeedbackRatingLifetime;
 
-                       $res = $dbr->select(
+                       $dbr = wfGetDb( DB_SLAVE );
+                       $updates = $dbr->selectField(
                                'revision',
-                               'rev_id',
+                               'COUNT(rev_id) as revisions',
                                array(
                                        'rev_page' => $params['pageid'],
                                        'rev_id > ' . $ratings[$pageId]['revid']
@@ -120,13 +124,12 @@
                                __METHOD__,
                                array ( 'LIMIT', $wgArticleFeedbackStaleCount + 
1 )
                        );
-
-                       if ( $res && $dbr->numRows( $res ) > 
$wgArticleFeedbackRatingLifetime ) {
+                       if ( $updates > $wgArticleFeedbackRatingLifetime ) {
                                // Expired status
                                $ratings[$params['pageid']]['status'] = 
'expired';
                        }
                }
-               
+
                foreach ( $ratings as $rat ) {
                        $result->setIndexedTagName( $rat['ratings'], 'r' );
                        $result->addValue( array( 'query', 
$this->getModuleName() ), null, $rat );
@@ -135,6 +138,33 @@
                $result->setIndexedTagName_internal( array( 'query', 
$this->getModuleName() ), 'aa' );
        }
 
+       /**
+        * Get the revision number of the oldest revision still being counted 
in totals.
+        * 
+        * @param $pageId Integer: ID of page to check revisions for
+        * @return Integer: Oldest valid revision number or 0 of all revisions 
are valid
+        */
+       protected static function getRevisionLimit( $pageId ) {
+               global $wgArticleFeedbackRatingLifetime;
+
+               $dbr = wfGetDb( DB_SLAVE );
+               $revision = $dbr->selectField(
+                       'revision',
+                       'rev_id',
+                       array( 'rev_page' => $pageId ),
+                       __METHOD__,
+                       array(
+                               'ORDER BY' => 'rev_id DESC',
+                               'LIMIT' => 1,
+                               'OFFSET' => $wgArticleFeedbackRatingLifetime - 1
+                       )
+               );
+               if ( $revision ) {
+                       return intval( $revision );
+               }
+               return 0;
+       }
+
        public function getAllowedParams() {
                return array(
                        'pageid' => array(

Added: trunk/extensions/ArticleFeedback/sql/AddPageRevisionColumn.sql
===================================================================
--- trunk/extensions/ArticleFeedback/sql/AddPageRevisionColumn.sql              
                (rev 0)
+++ trunk/extensions/ArticleFeedback/sql/AddPageRevisionColumn.sql      
2011-03-22 17:18:19 UTC (rev 84535)
@@ -0,0 +1,4 @@
+ALTER TABLE /*_*/article_feedback_pages
+       ADD aap_revision integer unsigned NOT NULL,
+       DROP PRIMARY KEY,
+       ADD PRIMARY KEY (aap_page_id, aap_rating_id, aap_revision);
\ No newline at end of file

Modified: trunk/extensions/ArticleFeedback/sql/ArticleFeedback.sql
===================================================================
--- trunk/extensions/ArticleFeedback/sql/ArticleFeedback.sql    2011-03-22 
17:18:15 UTC (rev 84534)
+++ trunk/extensions/ArticleFeedback/sql/ArticleFeedback.sql    2011-03-22 
17:18:19 UTC (rev 84535)
@@ -34,12 +34,15 @@
   -- 1 vote per user per revision
   PRIMARY KEY (aa_revision, aa_user_text, aa_rating_id, aa_user_anon_token)
 ) /*$wgDBTableOptions*/;
-CREATE INDEX /*i*/aa_user_page_revision ON /*_*/article_feedback (aa_user_id, 
aa_page_id, aa_revision);
+CREATE INDEX /*i*/aa_user_page_revision ON /*_*/article_feedback
+       (aa_user_id, aa_page_id, aa_revision);
 
 -- Aggregate rating table for a page
 CREATE TABLE IF NOT EXISTS /*_*/article_feedback_pages (
   -- Foreign key to page.page_id
   aap_page_id integer unsigned NOT NULL,
+  -- Revision that totals are relevant to
+  aap_revision integer unsigned NOT NULL,
   -- Foreign key to article_feedback_ratings.aar_rating
   aap_rating_id integer unsigned NOT NULL,
   -- Sum (total) of all the ratings for this article revision
@@ -47,7 +50,7 @@
   -- Number of ratings
   aap_count integer unsigned NOT NULL,
   -- One rating row per page
-  PRIMARY KEY (aap_page_id, aap_rating_id)
+  PRIMARY KEY (aap_page_id, aap_rating_id, aap_revision)
 ) /*$wgDBTableOptions*/;
 
 -- Properties table for meta information
@@ -57,7 +60,6 @@
   afp_revision integer unsigned NOT NULL,
   afp_user_text varbinary(255) NOT NULL,
   afp_user_anon_token varbinary(32) NOT NULL DEFAULT '',
-
   -- Key/value pairs
   afp_key varbinary(255) NOT NULL,
   -- Integer value
@@ -65,4 +67,5 @@
   -- Text value
   afp_value_text varbinary(255) DEFAULT '' NOT NULL
 ) /*$wgDBTableOptions*/;
-CREATE UNIQUE INDEX /*i*/afp_rating_key ON /*_*/article_feedback_properties 
(afp_revision, afp_user_text, afp_user_anon_token, afp_key);
+CREATE UNIQUE INDEX /*i*/afp_rating_key ON /*_*/article_feedback_properties
+       (afp_revision, afp_user_text, afp_user_anon_token, afp_key);


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

Reply via email to