2011-02-12 20:40:40 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2012-05-08 12:51:21 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
2011-02-12 20:40:40 +00:00
|
|
|
* @author Niklas Laxström
|
2012-05-08 12:51:21 +00:00
|
|
|
*/
|
2019-06-05 21:16:45 +00:00
|
|
|
|
2024-02-08 19:09:50 +00:00
|
|
|
namespace MediaWiki\Cache;
|
|
|
|
|
|
2024-02-08 14:56:54 +00:00
|
|
|
use MediaWiki\Context\RequestContext;
|
2020-04-01 20:51:09 +00:00
|
|
|
use MediaWiki\Linker\LinkTarget;
|
2016-05-07 12:15:44 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2023-09-18 14:17:28 +00:00
|
|
|
use MediaWiki\Title\NamespaceInfo;
|
2023-11-29 10:21:43 +00:00
|
|
|
use MediaWiki\User\Options\UserOptionsLookup;
|
2019-08-19 21:11:50 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
2023-09-15 17:07:11 +00:00
|
|
|
use Wikimedia\Rdbms\IConnectionProvider;
|
2012-05-08 12:51:21 +00:00
|
|
|
|
|
|
|
|
/**
|
2023-09-26 04:46:30 +00:00
|
|
|
* Look up "gender" user preference.
|
|
|
|
|
*
|
|
|
|
|
* This primarily used in MediaWiki\Title\MediaWikiTitleCodec for title formatting
|
|
|
|
|
* of pages in gendered namespace aliases, and in CoreParserFunctions for the
|
|
|
|
|
* `{{gender:}}` parser function.
|
2012-05-08 12:51:21 +00:00
|
|
|
*
|
2011-02-12 20:40:40 +00:00
|
|
|
* @since 1.18
|
language: Add missing `@ingroup`, subgroup "Languages" and ungroup files
== Ungroup file blocks
Remove `@ingroup` from `@file` blocks and keep only the class block.
This matches similar changes previously applied to API, Skins, Profile,
and ResourceLoader.
This helps make the API documentation easier to navigate.
E.g. Modules -> Language in the sidebar of
<https://doc.wikimedia.org/mediawiki-core/master/php/> as well as
<https://doc.wikimedia.org/mediawiki-core/master/php/group__Language.html>
These are currently cluttered with tons of duplicate entries for files
and classes both. We only need to group files that aren't also
documented as a class (e.g. message files, entry points, other scripts
or files that we mainly consider a data file). This has the helpful
side-effect that we don't encourage duplication of the class
description (or worse, place useful docs only in the file block), and
makes the class files consistently start with a mentally ignorable
block. Basically, unless there's something other than a class, don't
describe or group the file itself.
== Missing group
Various classes in this subtree were missing the `Language` group,
or were using different group from before T225756.
== Subgroup
For ease of navigation, move Converter subclasses to a group called
"Languages", which for documentation purposes is a subgroup of
"Language". The next commit does the same for Messages* files,
and Language subclasses (done separately for ease of review).
Change-Id: I301f471f86ba2dee924fece29a16dc3c20b5bebe
2022-06-22 22:37:31 +00:00
|
|
|
* @ingroup Cache
|
2011-02-12 20:40:40 +00:00
|
|
|
*/
|
|
|
|
|
class GenderCache {
|
2024-09-10 18:57:59 +00:00
|
|
|
/** @var string[] */
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $cache = [];
|
2024-09-10 18:57:59 +00:00
|
|
|
/** @var string|null */
|
2023-09-26 04:46:30 +00:00
|
|
|
protected $default = null;
|
2024-09-10 18:57:59 +00:00
|
|
|
/** @var int */
|
2011-02-12 20:40:40 +00:00
|
|
|
protected $misses = 0;
|
2024-09-10 18:57:59 +00:00
|
|
|
/**
|
|
|
|
|
* @internal Exposed for MediaWiki core unit tests.
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
2011-02-12 20:40:40 +00:00
|
|
|
protected $missLimit = 1000;
|
2011-05-26 20:26:51 +00:00
|
|
|
|
2023-09-26 04:46:30 +00:00
|
|
|
private NamespaceInfo $nsInfo;
|
|
|
|
|
private ?IConnectionProvider $dbProvider;
|
|
|
|
|
private UserOptionsLookup $userOptionsLookup;
|
2020-06-02 17:27:10 +00:00
|
|
|
|
|
|
|
|
public function __construct(
|
2024-10-16 18:58:33 +00:00
|
|
|
?NamespaceInfo $nsInfo = null,
|
|
|
|
|
?IConnectionProvider $dbProvider = null,
|
|
|
|
|
?UserOptionsLookup $userOptionsLookup = null
|
2020-06-02 17:27:10 +00:00
|
|
|
) {
|
2018-08-05 10:05:44 +00:00
|
|
|
$this->nsInfo = $nsInfo ?? MediaWikiServices::getInstance()->getNamespaceInfo();
|
2023-09-15 17:07:11 +00:00
|
|
|
$this->dbProvider = $dbProvider;
|
2020-06-02 17:27:10 +00:00
|
|
|
$this->userOptionsLookup = $userOptionsLookup ?? MediaWikiServices::getInstance()->getUserOptionsLookup();
|
2018-08-05 10:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
2011-02-12 20:40:40 +00:00
|
|
|
/**
|
2023-09-26 04:46:30 +00:00
|
|
|
* Get the default gender option on this wiki.
|
|
|
|
|
*
|
2014-04-18 23:19:46 +00:00
|
|
|
* @return string
|
2011-02-12 20:40:40 +00:00
|
|
|
*/
|
|
|
|
|
protected function getDefault() {
|
2022-12-16 23:48:27 +00:00
|
|
|
$this->default ??= $this->userOptionsLookup->getDefaultOption( 'gender' );
|
2011-02-12 20:40:40 +00:00
|
|
|
return $this->default;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-09-26 04:46:30 +00:00
|
|
|
* Get the gender option for given username.
|
|
|
|
|
*
|
2019-08-19 21:11:50 +00:00
|
|
|
* @param string|UserIdentity $username
|
2023-09-26 04:46:30 +00:00
|
|
|
* @param string|null $caller Calling method for database profiling
|
2014-04-18 23:19:46 +00:00
|
|
|
* @return string
|
2011-02-12 20:40:40 +00:00
|
|
|
*/
|
|
|
|
|
public function getGenderOf( $username, $caller = '' ) {
|
2019-08-19 21:11:50 +00:00
|
|
|
if ( $username instanceof UserIdentity ) {
|
2012-04-07 20:45:31 +00:00
|
|
|
$username = $username->getName();
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-21 08:32:09 +00:00
|
|
|
$username = self::normalizeUsername( $username );
|
2011-02-12 20:40:40 +00:00
|
|
|
if ( !isset( $this->cache[$username] ) ) {
|
2023-09-26 04:46:30 +00:00
|
|
|
if ( $this->misses < $this->missLimit ||
|
|
|
|
|
RequestContext::getMain()->getUser()->getName() === $username
|
2020-06-08 02:02:33 +00:00
|
|
|
) {
|
2011-02-12 20:40:40 +00:00
|
|
|
$this->misses++;
|
2012-04-21 08:32:09 +00:00
|
|
|
$this->doQuery( $username, $caller );
|
2011-02-12 20:40:40 +00:00
|
|
|
}
|
2023-09-26 04:46:30 +00:00
|
|
|
if ( $this->misses === $this->missLimit ) {
|
|
|
|
|
// Log only once and don't bother incrementing beyond limit+1
|
|
|
|
|
$this->misses++;
|
|
|
|
|
wfDebug( __METHOD__ . ': too many misses, returning default onwards' );
|
|
|
|
|
}
|
2011-02-12 20:40:40 +00:00
|
|
|
}
|
|
|
|
|
|
2017-10-06 22:17:58 +00:00
|
|
|
return $this->cache[$username] ?? $this->getDefault();
|
2011-02-12 20:40:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Wrapper for doQuery that processes raw LinkBatch data.
|
2011-05-26 20:26:51 +00:00
|
|
|
*
|
2023-07-03 13:58:50 +00:00
|
|
|
* @param array<int,array<string,mixed>> $data
|
|
|
|
|
* @param string|null $caller
|
2011-02-12 20:40:40 +00:00
|
|
|
*/
|
2023-07-03 13:58:50 +00:00
|
|
|
public function doLinkBatch( array $data, $caller = '' ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$users = [];
|
2011-02-12 20:40:40 +00:00
|
|
|
foreach ( $data as $ns => $pagenames ) {
|
2023-07-03 13:58:50 +00:00
|
|
|
if ( $this->nsInfo->hasGenderDistinction( $ns ) ) {
|
|
|
|
|
$users += $pagenames;
|
2011-02-12 20:40:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$this->doQuery( array_keys( $users ), $caller );
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-16 14:46:22 +00:00
|
|
|
/**
|
2020-04-01 20:51:09 +00:00
|
|
|
* Wrapper for doQuery that processes a title array.
|
2012-05-16 14:46:22 +00:00
|
|
|
*
|
|
|
|
|
* @since 1.20
|
2020-04-01 20:51:09 +00:00
|
|
|
* @param LinkTarget[] $titles
|
2023-09-26 04:46:30 +00:00
|
|
|
* @param string|null $caller Calling method for database profiling
|
2012-05-16 14:46:22 +00:00
|
|
|
*/
|
|
|
|
|
public function doTitlesArray( $titles, $caller = '' ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$users = [];
|
2020-04-01 20:51:09 +00:00
|
|
|
foreach ( $titles as $titleObj ) {
|
2023-06-15 13:14:40 +00:00
|
|
|
if ( $this->nsInfo->hasGenderDistinction( $titleObj->getNamespace() ) ) {
|
|
|
|
|
$users[] = $titleObj->getText();
|
2012-05-16 14:46:22 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$this->doQuery( $users, $caller );
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-12 20:40:40 +00:00
|
|
|
/**
|
2023-09-26 04:46:30 +00:00
|
|
|
* Preload gender option for multiple user names.
|
|
|
|
|
*
|
2019-08-19 21:11:50 +00:00
|
|
|
* @param string[]|string $users Usernames
|
2023-09-26 04:46:30 +00:00
|
|
|
* @param string|null $caller Calling method for database profiling
|
2011-02-12 20:40:40 +00:00
|
|
|
*/
|
|
|
|
|
public function doQuery( $users, $caller = '' ) {
|
2011-09-19 09:04:39 +00:00
|
|
|
$default = $this->getDefault();
|
2011-02-12 20:40:40 +00:00
|
|
|
|
2023-09-26 04:46:30 +00:00
|
|
|
$usersToFetch = [];
|
2013-08-31 16:36:02 +00:00
|
|
|
foreach ( (array)$users as $value ) {
|
2012-04-21 08:32:09 +00:00
|
|
|
$name = self::normalizeUsername( $value );
|
|
|
|
|
if ( !isset( $this->cache[$name] ) ) {
|
2023-09-26 04:46:30 +00:00
|
|
|
// This may be overwritten below by a fetched value
|
2011-09-19 09:04:39 +00:00
|
|
|
$this->cache[$name] = $default;
|
2023-09-26 04:46:30 +00:00
|
|
|
// T267054: We don't need to fetch data for invalid usernames, but filtering breaks DI
|
|
|
|
|
$usersToFetch[] = $name;
|
2011-09-19 09:04:39 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-26 04:46:30 +00:00
|
|
|
// Skip query when database is unavailable (e.g. via the installer)
|
|
|
|
|
if ( !$usersToFetch || !$this->dbProvider ) {
|
2011-10-26 03:44:47 +00:00
|
|
|
return;
|
2011-02-12 20:40:40 +00:00
|
|
|
}
|
|
|
|
|
|
2023-09-26 04:46:30 +00:00
|
|
|
$caller = __METHOD__ . ( $caller ? "/$caller" : '' );
|
2019-06-05 21:16:45 +00:00
|
|
|
|
2023-09-26 04:46:30 +00:00
|
|
|
$res = $queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder()
|
2023-08-24 10:55:34 +00:00
|
|
|
->select( [ 'user_name', 'up_value' ] )
|
|
|
|
|
->from( 'user' )
|
|
|
|
|
->leftJoin( 'user_properties', null, [ 'user_id = up_user', 'up_property' => 'gender' ] )
|
2023-09-26 04:46:30 +00:00
|
|
|
->where( [ 'user_name' => $usersToFetch ] )
|
|
|
|
|
->caller( $caller )
|
|
|
|
|
->fetchResultSet();
|
2011-02-12 20:40:40 +00:00
|
|
|
|
|
|
|
|
foreach ( $res as $row ) {
|
2018-06-11 09:16:48 +00:00
|
|
|
$this->cache[$row->user_name] = $row->up_value ?: $default;
|
2011-02-12 20:40:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-21 08:32:09 +00:00
|
|
|
private static function normalizeUsername( $username ) {
|
|
|
|
|
// Strip off subpages
|
|
|
|
|
$indexSlash = strpos( $username, '/' );
|
|
|
|
|
if ( $indexSlash !== false ) {
|
|
|
|
|
$username = substr( $username, 0, $indexSlash );
|
|
|
|
|
}
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2012-04-21 08:32:09 +00:00
|
|
|
// normalize underscore/spaces
|
|
|
|
|
return strtr( $username, '_', ' ' );
|
|
|
|
|
}
|
2011-02-12 20:40:40 +00:00
|
|
|
}
|
2024-02-08 19:09:50 +00:00
|
|
|
|
2024-03-07 21:56:58 +00:00
|
|
|
/** @deprecated class alias since 1.42 */
|
2024-02-08 19:09:50 +00:00
|
|
|
class_alias( GenderCache::class, 'GenderCache' );
|