Update code for MW 1.40

This commit is contained in:
Alexander Yakovlev 2023-08-06 11:24:45 +06:00
parent 26a1a64270
commit d903805e69
3 changed files with 44 additions and 32 deletions

View file

@ -1,5 +1,6 @@
<?php
use MediaWiki\MediaWikiServices;
use MediaWiki\Logger\LoggerFactory;
/**
* A special page to display the highest rated pages on the wiki.
@ -63,7 +64,8 @@ class SpecialTopRatings extends IncludableSpecialPage {
$ratings = array();
$output = '';
$dbr = wfGetDB( DB_SLAVE );
$lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
$dbr = $lb->getConnection( DB_PRIMARY );
$tables = $where = $joinConds = array();
$whatToSelect = array( 'DISTINCT vote_page_id', 'SUM(vote_value) AS vote_value_sum' );
@ -171,24 +173,25 @@ class SpecialTopRatings extends IncludableSpecialPage {
* @return Integer: average vote for the given page (ID)
*/
public static function getAverageRatingForPage( $pageId ) {
global $wgMemc;
$key = wfMemcKey( 'vote', 'avg', $pageId );
$data = $wgMemc->get( $key );
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
$logger = LoggerFactory::getInstance( 'VoteNY' );
$key = 'vote_avg_'.$pageId;
$data = $cache->get( $key );
$voteAvg = 0;
if ( $data ) {
wfDebug( "Loading vote avg for page {$pageId} from cache (TopRatings)\n" );
$logger->debug( "Loading vote avg for page {$pageId} from cache (TopRatings)\n" );
$voteAvg = $data;
} else {
$dbr = wfGetDB( DB_SLAVE );
$lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
$dbr = $lb->getConnection( DB_PRIMARY );
$voteAvg = (int)$dbr->selectField(
'Vote',
'AVG(vote_value) AS VoteAvg',
array( 'vote_page_id' => $pageId ),
__METHOD__
);
$wgMemc->set( $key, $voteAvg );
$cache->set( $key, $voteAvg );
}
return $voteAvg;

View file

@ -1,6 +1,7 @@
<?php
use MediaWiki\MediaWikiServices;
use MediaWiki\Logger\LoggerFactory;
/**
* Vote class - class for handling vote-related functions (counting
@ -35,15 +36,17 @@ class Vote {
*/
function count() {
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
$logger = LoggerFactory::getInstance( 'VoteNY' );
$key = 'vote_count_'.$this->PageID;
$data = $cache->get( $key );
// Try cache
if ( $data ) {
wfDebug( "Loading vote count for page {$this->PageID} from cache\n" );
$logger->debug( "Loading vote count for page {$this->PageID} from cache\n" );
$vote_count = $data;
} else {
$dbr = wfGetDB( DB_SLAVE );
$lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
$dbr = $lb->getConnection( DB_PRIMARY );
$vote_count = 0;
$res = $dbr->select(
'Vote',
@ -51,7 +54,7 @@ class Vote {
array( 'vote_page_id' => $this->PageID ),
__METHOD__
);
$row = $dbr->fetchObject( $res );
$row = $res->fetchRow();
if ( $row ) {
$vote_count = $row->votecount;
}
@ -68,22 +71,24 @@ class Vote {
*/
function getAverageVote() {
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
$logger = LoggerFactory::getInstance( 'VoteNY' );
$key = 'vote_avg_'.$this->PageID;
$data = $cache->get( $key );
$voteAvg = 0;
if ( $data ) {
wfDebug( "Loading vote avg for page {$this->PageID} from cache\n" );
$logger->debug( "Loading vote avg for page {$this->PageID} from cache\n" );
$voteAvg = $data;
} else {
$dbr = wfGetDB( DB_SLAVE );
$lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
$dbr = $lb->getConnection( DB_PRIMARY );
$res = $dbr->select(
'Vote',
'AVG(vote_value) AS voteavg',
array( 'vote_page_id' => $this->PageID ),
__METHOD__
);
$row = $dbr->fetchObject( $res );
$row = $res->fetchRow();
if ( $row ) {
$voteAvg = $row->voteavg;
}
@ -184,7 +189,8 @@ class Vote {
* value of 'vote_value' column from Vote DB table
*/
function UserAlreadyVoted() {
$dbr = wfGetDB( DB_SLAVE );
$lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
$dbr = $lb->getConnection( DB_PRIMARY );
$s = $dbr->selectRow(
'Vote',
array( 'vote_value' ),

View file

@ -1,4 +1,7 @@
<?php
use MediaWiki\MediaWikiServices;
use MediaWiki\Logger\LoggerFactory;
/**
* All hooked functions used by VoteNY extension.
*
@ -95,32 +98,30 @@ class VoteHooks {
* @return bool
*/
public static function assignValueToMagicWord( &$parser, &$cache, &$magicWordId, &$ret ) {
global $wgMemc;
if ( $magicWordId == 'NUMBEROFVOTES' ) {
$key = wfMemcKey( 'vote', 'magic-word' );
$data = $wgMemc->get( $key );
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
$logger = LoggerFactory::getInstance( 'VoteNY' );
if ( $magicWordId == 'NUMBEROFVOTES' ) {
$key = 'vote_magic_word';
$data = $cache->get( $key );
if ( $data != '' ) {
// We have it in cache? Oh goody, let's just use the cached value!
wfDebugLog(
'VoteNY',
'Got the amount of votes from memcached'
);
$logger->debug('Got the amount of votes from memcached');
// return value
$ret = $data;
} else {
// Not cached → have to fetch it from the database
$dbr = wfGetDB( DB_SLAVE );
$lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
$dbr = $lb->getConnection( DB_PRIMARY );
$voteCount = (int)$dbr->selectField(
'Vote',
'COUNT(*) AS count',
array(),
__METHOD__
);
wfDebugLog( 'VoteNY', 'Got the amount of votes from DB' );
$logger->debug('Got the amount of votes from DB');
// Store the count in cache...
// (86400 = seconds in a day)
$wgMemc->set( $key, $voteCount, 86400 );
$cache->set( $key, $voteCount, 86400 );
// ...and return the value to the user
$ret = $voteCount;
}
@ -138,17 +139,19 @@ class VoteHooks {
* @return int Number of votes for the given page
*/
public static function getNumberOfVotesPage( Title $title ) {
global $wgMemc;
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
$logger = LoggerFactory::getInstance( 'VoteNY' );
$id = $title->getArticleID();
$key = wfMemcKey( 'vote', 'magic-word-page', $id );
$data = $wgMemc->get( $key );
$key = 'vote_magic-word-page_'.$id;
$data = $cache->get( $key );
if ( $data ) {
return $data;
} else {
$dbr = wfGetDB( DB_SLAVE );
$lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
$dbr = $lb->getConnection( DB_PRIMARY );
$voteCount = (int)$dbr->selectField(
'Vote',
@ -157,7 +160,7 @@ class VoteHooks {
__METHOD__
);
$wgMemc->set( $key, $voteCount, 3600 );
$cache->set( $key, $voteCount, 3600 );
return $voteCount;
}