* @author Niklas Laxström
* @ingroup Cache
*/
+use MediaWiki\MediaWikiServices;
/**
* Caches user genders when needed to use correct namespace aliases.
* @since 1.18
*/
class GenderCache {
- protected $cache = array();
+ protected $cache = [];
protected $default;
protected $misses = 0;
protected $missLimit = 1000;
/**
+ * @deprecated in 1.28 see MediaWikiServices::getInstance()->getGenderCache()
* @return GenderCache
*/
public static function singleton() {
- static $that = null;
- if ( $that === null ) {
- $that = new self();
- }
-
- return $that;
- }
-
- protected function __construct() {
+ return MediaWikiServices::getInstance()->getGenderCache();
}
/**
* @param string $caller
*/
public function doLinkBatch( $data, $caller = '' ) {
- $users = array();
+ $users = [];
foreach ( $data as $ns => $pagenames ) {
if ( !MWNamespace::hasGenderDistinction( $ns ) ) {
continue;
* @param string $caller The calling method
*/
public function doTitlesArray( $titles, $caller = '' ) {
- $users = array();
+ $users = [];
foreach ( $titles as $title ) {
$titleObj = is_string( $title ) ? Title::newFromText( $title ) : $title;
if ( !$titleObj ) {
public function doQuery( $users, $caller = '' ) {
$default = $this->getDefault();
- $usersToCheck = array();
+ $usersToCheck = [];
foreach ( (array)$users as $value ) {
$name = self::normalizeUsername( $value );
// Skip users whose gender setting we already know
return;
}
- $dbr = wfGetDB( DB_SLAVE );
- $table = array( 'user', 'user_properties' );
- $fields = array( 'user_name', 'up_value' );
- $conds = array( 'user_name' => $usersToCheck );
- $joins = array( 'user_properties' =>
- array( 'LEFT JOIN', array( 'user_id = up_user', 'up_property' => 'gender' ) ) );
+ $dbr = wfGetDB( DB_REPLICA );
+ $table = [ 'user', 'user_properties' ];
+ $fields = [ 'user_name', 'up_value' ];
+ $conds = [ 'user_name' => $usersToCheck ];
+ $joins = [ 'user_properties' =>
+ [ 'LEFT JOIN', [ 'user_id = up_user', 'up_property' => 'gender' ] ] ];
$comment = __METHOD__;
if ( strval( $caller ) !== '' ) {
$comment .= "/$caller";
}
- $res = $dbr->select( $table, $fields, $conds, $comment, array(), $joins );
+ $res = $dbr->select( $table, $fields, $conds, $comment, [], $joins );
foreach ( $res as $row ) {
$this->cache[$row->user_name] = $row->up_value ? $row->up_value : $default;