| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
m |
| 20 |
|
| 21 |
|
| 22 |
namespace MediaWiki\Cache; |
| 23 |
|
| 24 |
use MediaWiki\Context\RequestContext; |
| 25 |
use MediaWiki\Linker\LinkTarget; |
| 26 |
use MediaWiki\MediaWikiServices; |
| 27 |
use MediaWiki\Title\NamespaceInfo; |
| 28 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 29 |
use MediaWiki\User\UserIdentity; |
| 30 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class GenderCache { |
| 43 |
|
| 44 |
protected $cache = []; |
| 45 |
|
| 46 |
protected $default = null; |
| 47 |
|
| 48 |
protected $misses = 0; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
protected $missLimit = 1000; |
| 54 |
|
| 55 |
private NamespaceInfo $nsInfo; |
| 56 |
private ?IConnectionProvider $dbProvider; |
| 57 |
private UserOptionsLookup $userOptionsLookup; |
| 58 |
|
| 59 |
public function __construct( |
| 60 |
?NamespaceInfo $nsInfo = null, |
| 61 |
?IConnectionProvider $dbProvider = null, |
| 62 |
?UserOptionsLookup $userOptionsLookup = null |
| 63 |
) { |
| 64 |
$this->nsInfo = $nsInfo ?? MediaWikiServices::getInstance()->getNamespaceInfo(); |
| 65 |
$this->dbProvider = $dbProvider; |
| 66 |
$this->userOptionsLookup = $userOptionsLookup ?? MediaWikiServices::getInstance()->getUserOptionsLookup(); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
protected function getDefault() { |
| 75 |
$this->default ??= $this->userOptionsLookup->getDefaultOption( 'gender' ); |
| 76 |
return $this->default; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function getGenderOf( $username, $caller = '' ) { |
| 87 |
if ( $username instanceof UserIdentity ) { |
| 88 |
$username = $username->getName(); |
| 89 |
} |
| 90 |
|
| 91 |
$username = self::normalizeUsername( $username ); |
| 92 |
if ( !isset( $this->cache[$username] ) ) { |
| 93 |
if ( $this->misses < $this->missLimit || |
| 94 |
RequestContext::getMain()->getUser()->getName() === $username |
| 95 |
) { |
| 96 |
$this->misses++; |
| 97 |
$this->doQuery( $username, $caller ); |
| 98 |
} |
| 99 |
if ( $this->misses === $this->missLimit ) { |
| 100 |
|
| 101 |
$this->misses++; |
| 102 |
wfDebug( __METHOD__ . ': too many misses, returning default onwards' ); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
return $this->cache[$username] ?? $this->getDefault(); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public function doLinkBatch( array $data, $caller = '' ) { |
| 116 |
$users = []; |
| 117 |
foreach ( $data as $ns => $pagenames ) { |
| 118 |
if ( $this->nsInfo->hasGenderDistinction( $ns ) ) { |
| 119 |
$users += $pagenames; |
| 120 |
} |
| 121 |
} |
| 122 |
$this->doQuery( array_keys( $users ), $caller ); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public function doTitlesArray( $titles, $caller = '' ) { |
| 133 |
$users = []; |
| 134 |
foreach ( $titles as $titleObj ) { |
| 135 |
if ( $this->nsInfo->hasGenderDistinction( $titleObj->getNamespace() ) ) { |
| 136 |
$users[] = $titleObj->getText(); |
| 137 |
} |
| 138 |
} |
| 139 |
$this->doQuery( $users, $caller ); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public function doQuery( $users, $caller = '' ) { |
| 149 |
$default = $this->getDefault(); |
| 150 |
|
| 151 |
$usersToFetch = []; |
| 152 |
foreach ( (array)$users as $value ) { |
| 153 |
$name = self::normalizeUsername( $value ); |
| 154 |
if ( !isset( $this->cache[$name] ) ) { |
| 155 |
|
| 156 |
$this->cache[$name] = $default; |
| 157 |
|
| 158 |
$usersToFetch[] = $name; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
if ( !$usersToFetch || !$this->dbProvider ) { |
| 164 |
return; |
| 165 |
} |
| 166 |
|
| 167 |
$caller = __METHOD__ . ( $caller ? "/$caller" : '' ); |
| 168 |
|
| 169 |
$res = $queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder() |
| 170 |
->select( [ 'user_name', 'up_value' ] ) |
| 171 |
->from( 'user' ) |
| 172 |
->leftJoin( 'user_properties', null, [ 'user_id = up_user', 'up_property' => 'gender' ] ) |
| 173 |
->where( [ 'user_name' => $usersToFetch ] ) |
| 174 |
->caller( $caller ) |
| 175 |
->fetchResultSet(); |
| 176 |
|
| 177 |
foreach ( $res as $row ) { |
| 178 |
$this->cache[$row->user_name] = $row->up_value ?: $default; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
private static function normalizeUsername( $username ) { |
| 183 |
|
| 184 |
$indexSlash = strpos( $username, '/' ); |
| 185 |
if ( $indexSlash !== false ) { |
| 186 |
$username = substr( $username, 0, $indexSlash ); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
return strtr( $username, '_', ' ' ); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
class_alias( GenderCache::class, 'GenderCache' ); |