/** * Reset the cache used in idFromName(). For use in tests. */ public static function resetIdByNameCache() { self::$idCacheByName = array(); }
/** * Get database id given a user name * @param $name \string Username * @return \types{\int,\null} The corresponding user's ID, or null if user is nonexistent */ static function idFromName($name) { $nt = Title::makeTitleSafe(NS_USER, $name); if (is_null($nt)) { # Illegal name return null; } if (isset(self::$idCacheByName[$name])) { return self::$idCacheByName[$name]; } $dbr = wfGetDB(DB_SLAVE); $s = $dbr->selectRow('user', array('user_id'), array('user_name' => $nt->getText()), __METHOD__); if ($s === false) { $result = null; } else { $result = $s->user_id; } self::$idCacheByName[$name] = $result; if (count(self::$idCacheByName) > 1000) { self::$idCacheByName = array(); } return $result; }