1
0
Fork 0
mirror of https://bitbucket.org/vertlach/iusethis.git synced 2024-05-25 20:38:20 +03:00

fix code for MW 1.40

This commit is contained in:
Alexander Yakovlev 2023-08-06 11:32:35 +06:00
parent 1af57d1518
commit bb45bde09c
2 changed files with 30 additions and 16 deletions

View file

@ -1,4 +1,7 @@
<?php <?php
use MediaWiki\MediaWikiServices;
use MediaWiki\Logger\LoggerFactory;
/** /**
* Vote class - class for handling vote-related functions (counting * Vote class - class for handling vote-related functions (counting
* the average score of a given page, inserting/updating/removing a vote etc.) * the average score of a given page, inserting/updating/removing a vote etc.)
@ -31,17 +34,19 @@ class IUT {
* @return int Amount of votes * @return int Amount of votes
*/ */
function count() { function count() {
global $wgMemc; $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
$logger = LoggerFactory::getInstance( 'VoteNY' );
$key = wfMemcKey( 'iusethis', 'count', $this->PageID ); $key = 'iusethis_count_'.$this->PageID;
$data = $wgMemc->get( $key ); $data = $cache->get( $key );
// Try cache // Try cache
if ( $data ) { 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; $vote_count = $data;
} else { } else {
$dbr = wfGetDB( DB_SLAVE ); $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
$dbr = $lb->getConnection( DB_PRIMARY );
$vote_count = 0; $vote_count = 0;
$res = $dbr->select( $res = $dbr->select(
'IUseThis', 'IUseThis',
@ -49,11 +54,11 @@ class IUT {
array( 'vote_page_id' => $this->PageID ), array( 'vote_page_id' => $this->PageID ),
__METHOD__ __METHOD__
); );
$row = $dbr->fetchObject( $res ); $row = $res->fetchRow();
if ( $row ) { if ( $row ) {
$vote_count = $row->votecount; $vote_count = $row->votecount;
} }
$wgMemc->set( $key, $vote_count ); $cache->set( $key, $vote_count );
} }
return $vote_count; return $vote_count;
@ -63,11 +68,12 @@ class IUT {
* Clear caches - memcached, parser cache and Squid cache * Clear caches - memcached, parser cache and Squid cache
*/ */
function clearCache() { function clearCache() {
global $wgUser, $wgMemc; $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
global $wgUser;
// Kill internal cache // Kill internal cache
$wgMemc->delete( wfMemcKey( 'iusethis', 'count', $this->PageID ) ); $cache->delete( 'iusethis_count_'.$this->PageID );
$wgMemc->delete( wfMemcKey( 'iusethis', 'avg', $this->PageID ) ); $cache->delete( 'iusethis_avg_'.$this->PageID );
// Purge squid // Purge squid
$pageTitle = Title::newFromID( $this->PageID ); $pageTitle = Title::newFromID( $this->PageID );
@ -79,7 +85,7 @@ class IUT {
$article = new Article( $pageTitle, /* oldid */0 ); $article = new Article( $pageTitle, /* oldid */0 );
$parserCache = ParserCache::singleton(); $parserCache = ParserCache::singleton();
$parserKey = $parserCache->getKey( $article, $wgUser ); $parserKey = $parserCache->getKey( $article, $wgUser );
$wgMemc->delete( $parserKey ); $cache->delete( $parserKey );
} }
} }
@ -87,7 +93,8 @@ class IUT {
* Delete the user's vote from the database, purges normal caches. * Delete the user's vote from the database, purges normal caches.
*/ */
function delete() { function delete() {
$dbw = wfGetDB( DB_MASTER ); $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
$dbw = $lb->getConnection( DB_PRIMARY );
$dbw->delete( $dbw->delete(
'IUseThis', 'IUseThis',
array( array(
@ -105,7 +112,8 @@ class IUT {
*/ */
function insert() { function insert() {
global $wgRequest; global $wgRequest;
$dbw = wfGetDB( DB_MASTER ); $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
$dbw = $lb->getConnection( DB_PRIMARY );
wfSuppressWarnings(); // E_STRICT whining wfSuppressWarnings(); // E_STRICT whining
$voteDate = date( 'Y-m-d H:i:s' ); $voteDate = date( 'Y-m-d H:i:s' );
wfRestoreWarnings(); wfRestoreWarnings();
@ -133,7 +141,8 @@ class IUT {
* value of 'vote_value' column from Vote DB table * value of 'vote_value' column from Vote DB table
*/ */
function UserAlreadyVoted() { function UserAlreadyVoted() {
$dbr = wfGetDB( DB_SLAVE ); $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
$dbr = $lb->getConnection( DB_PRIMARY );
$s = $dbr->selectRow( $s = $dbr->selectRow(
'IUseThis', 'IUseThis',
'COUNT(*) as count', 'COUNT(*) as count',
@ -167,6 +176,8 @@ class IUT {
$output = "<div id='iutbox' class=\"noprint iut-box{$make_vote_box_clickable}\">"; $output = "<div id='iutbox' class=\"noprint iut-box{$make_vote_box_clickable}\">";
$output .= '<span id="iutcount" class="count">' . $this->count() . ' ' . wfMessage('people-count')->parse() . ' </span>'; $output .= '<span id="iutcount" class="count">' . $this->count() . ' ' . wfMessage('people-count')->parse() . ' </span>';
$output .= '<span id="iutaction" class="iut-action">'; $output .= '<span id="iutaction" class="iut-action">';
$services = MediaWikiServices::getInstance();
if ( !$wgUser->isAllowed( 'iusethis' ) ) { if ( !$wgUser->isAllowed( 'iusethis' ) ) {
// @todo FIXME: this is horrible. If we don't have enough // @todo FIXME: this is horrible. If we don't have enough
@ -177,7 +188,8 @@ class IUT {
htmlspecialchars( $login->getFullURL() ) . '" rel="nofollow">' . htmlspecialchars( $login->getFullURL() ) . '" rel="nofollow">' .
wfMessage('iusethis-link')->parse() . '</a>'; wfMessage('iusethis-link')->parse() . '</a>';
} else { } else {
if ( !wfReadOnly() ) { $readonly = $services->getReadOnlyMode()->isReadOnly();
if ( $readonly ) {
if ( $voted === false ) { if ( $voted === false ) {
$output .= '<a href="javascript:void(0);" class="iut-vote-link">' . $output .= '<a href="javascript:void(0);" class="iut-vote-link">' .
wfMessage('iusethis-link')->parse() . '</a>'; wfMessage('iusethis-link')->parse() . '</a>';

View file

@ -1,4 +1,6 @@
<?php <?php
use MediaWiki\MediaWikiServices;
/** /**
* All hooked functions used by IUseThis extension. * All hooked functions used by IUseThis extension.
* *
@ -32,7 +34,7 @@ class IUTHooks {
// Disable parser cache (sadly we have to do this, because the caching is // Disable parser cache (sadly we have to do this, because the caching is
// messing stuff up; we want to show an up-to-date rating instead of old // messing stuff up; we want to show an up-to-date rating instead of old
// or totally wrong rating, i.e. another page's rating...) // or totally wrong rating, i.e. another page's rating...)
$parser->disableCache(); $parser->getOutput()->updateCacheExpiry(0);
// Add CSS & JS // Add CSS & JS
// In order for us to do this *here* instead of having to do this in // In order for us to do this *here* instead of having to do this in